ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:4.13MB ,
资源ID:29136706      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/29136706.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(pcap文件格式及文件解析共14页.docx)为本站会员(b****1)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

pcap文件格式及文件解析共14页.docx

1、pcap文件格式及文件解析共14页第一(dy)部分:PCAP包文件(wnjin)格式(g shi)一 基本(jbn)格式: 文件头 数据包头数据报数据包头数据报.二、文件头: 文件头结构体sturct pcap_file_header DWORD magic; DWORDversion_major; DWORDversion_minor; DWORDthiszone; DWORDsigfigs; DWORDsnaplen; DWORDlinktype;说明:1、标识位:32位的,这个标识位的值是16进制的 0xa1b2c3d4。a 32-bit magic number ,The magic

2、number has the value hex a1b2c3d4.2、主版本号:16位, 默认值为0x2。a 16-bit major version number,The major version number should have the value 2.3、副版本号:16位,默认值为0x04。a 16-bit minor version number,The minor version number should have the value 4.4、区域时间:32位,实际上该值并未使用,因此可以将该位设置为0。a 32-bit time zone offset field tha

3、t actually not used, so you can (and probably should) just make it 0;5、精确时间戳:32位,实际上该值并未使用,因此可以将该值设置为0。a 32-bit time stamp accuracy field tha not actually used,so you can (and probably should) just make it 0;6、数据包最大长度:32位,该值设置所抓获的数据包的最大长度,如果所有数据包都要抓获,将该值设置为65535;例如:想获取数据包的前64字节,可将该值设置为64。a 32-bit sn

4、apshot length" field;The snapshot length field should be the maximum number of bytes perpacket that will be captured. If the entire packet is captured, make it 65535; if you only capture, for example, the first 64 bytes of the packet, make it 64.7、链路层类型:32位, 数据包的链路层包头决定了链路层的类型。a 32-bit link lay

5、er type field.The link-layer type depends on the type of link-layer header that thepackets in the capture file have:以下是数据值与链路层类型的对应表0 BSD loopback devices, except for later OpenBSD1 Ethernet, and Linux loopback devices 以太网类型,大多数的数据包为这种类型。6 802.5 Token Ring7 ARCnet8 SLIP9 PPP10 FDDI100 LLC/SNAP-encap

6、sulated ATM101 raw IP, with no link102 BSD/OS SLIP103 BSD/OS PPP104 Cisco HDLC105 802.11108 later OpenBSD loopback devices (with the AF_value in network byte order)113 special Linux cooked capture114 LocalTalk三 packet数据(shj)包头:struct pcap_pkthdrstruct tim ts; DWORD caplen; DWORD len;struct timDWORD

7、GMTtime;DWORD microTime说明(shumng):1、时间(shjin)戳,包括:秒计时:32位,一个UNIX格式的精确到秒时间值,用来记录数据包抓获的时间,记录方式(fngsh)是记录从格林尼治时间的1970年1月1日 00:00:00 到抓包时经过的秒数;微秒计时:32位, 抓取数据包时的微秒值。a time stamp, consisting of:a UNIX-format time-in-seconds when the packet was captured, i.e. the number of seconds since January 1,1970, 00:

8、00:00 GMT (that GMT, *NOT* local time!);the number of microseconds since that second when the packet was captured;2、数据包长度:32位 ,标识所抓获的数据包保存在pcap文件中的实际长度,以字节为单位。a 32-bit value giving the number of bytes of packet data that were captured;3、数据包实际长度: 所抓获的数据包的真实长度,如果文件中保存不是完整的数据包,那么这个值可能要比前面的数据包长度的值大。a 32

9、-bit value giving the actual length of the packet, in bytes (which may be greater than the previous number, if you are not saving the entire packet).四:packet数据(shj): 即Packet(通常就是(jish)链路层的数据帧)具体内容,长度就是Caplen,这个长度的后面,就是当前PCAP文件中存放的下一个Packet数据包,也就是说:PCAP文件里面并没有规定捕获的Packet数据包之间有什么间隔字符串,下一组数据在文件中的起始位置。我

10、们需要靠第一个Packet包确定。最后,Packet数据部分的格式其实就是标准的网路协议格式了可以任何网络教材(jioci)上找得到。五:举例(j l)分析图中最开始的绿色部分(b fen)就是24 Bytes的Pcap Header,接下来红色(hngs)的16 Bytes是第一个消息的Pcap Header。后面(hu mian)的红色的16 Bytes是第二个消息(xio xi)的Pcap Header。两块蓝色的部分分别是两个消息从链路层开始的完整内容。在网络上实际传输的数据包在数据链路层上每一个Packet开始都会有7个用于同步的字节和一个用于标识该Packet开始的字节,最后还会有

11、四个CRC校验字节;而PCAP文件中会把前8个字节和最后4个校验自己去掉,因为这些信息对于协议分析是没有用的。用Wireshark打开一个PCAP数据包,每条消息的所有field会被解析出来并会按照协议层次折叠起来。第一层显示的是FrameXXX,这一级别没有对应某层具体的协议,而是对本条消息的一个概括性总结,描述了一些有用的概括性信息,比如从里面我们可以看到本条消息各种协议的层次关系,展开其它协议层之后对应的是该协议的各个域,如下图所示:第二(d r)部分:PCAP文件解析1、pcap解析(ji x)工具 XplicoXplico 是一个(y )从 pcap 文件中解析出IP流量数据的工具,

12、可解析每个邮箱(yuxing) (POP, IMAP, 和 SMTP 协议), 所有 HTTP 内容, VoIP calls (SIP) 等等2、C语言实现(shxin)PCAP文件分析实现(shxin)步骤:1)用Wireshark软件(run jin)抓包得到test.pcap文件(wnjin)2)程序:分析pcap文件头 - 分析pcap_pkt头 - 分析帧头 - 分析ip头 - 分析tcp头 - 分析http信息#include#include#include#include#include#define BUFSIZE 10240#define STRSIZE 1024typede

13、f long bpf_int32;typedef unsigned long bpf_u_int32;typedef unsigned short u_short;typedef unsigned long u_int32;typedef unsigned short u_int16;typedef unsigned char u_int8;/pacp文件头结构体struct pcap_file_headerbpf_u_int32 magic; /* 0xa1b2c3d4 */u_short version_major; /* magjor Version 2 */u_short versio

14、n_minor; /* magjor Version 4 */bpf_int32 thiszone; /* gmt to local correction */bpf_u_int32 sigfigs; /* accuracy of timestamps */bpf_u_int32 snaplen; /* max length saved portion of each pkt */bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */;/时间(shjin)戳struct time_vallong tv_sec; /* seconds 含义

15、(hny)同 time_t 对象(duxing)的值 */long tv_usec; /* and microseconds */;/pcap数据(shj)包头结构体struct pcap_pkthdrstruct time_val ts; /* time stamp */bpf_u_int32 caplen; /* length of portion present */bpf_u_int32 len; /* length this packet (off wire) */;/数据帧头typedef struct FramHeader_t /Pcap捕获的数据帧头u_int8 DstMAC6

16、; /目的MAC地址u_int8 SrcMAC6; /源MAC地址u_short FrameType; /帧类型 FramHeader_t;/IP数据报头typedef struct IPHeader_t /IP数据报头u_int8 Ver_HLen; /版本+报头长度u_int8 TOS; /服务类型u_int16 TotalLen; /总长度u_int16 ID; /标识u_int16 Flag_Segment; /标志+片偏移u_int8 TTL; /生存周期u_int8 Protocol; /协议类型u_int16 Checksum; /头部校验和u_int32 SrcIP; /源IP

17、地址u_int32 DstIP; /目的IP地址 IPHeader_t;/TCP数据报头typedef struct TCPHeader_t /TCP数据报头u_int16 SrcPort; /源端口u_int16 DstPort; /目的端口u_int32 SeqNO; /序号u_int32 AckNO; /确认号u_int8 HeaderLen; /数据报头(botu)的长度(4 bit) + 保留(boli)(4 bit)u_int8 Flags; /标识(biozh)TCP不同(b tn)的控制消息u_int16 Window; /窗口大小u_int16 Checksum; /校验和u

18、_int16 UrgentPointer; /紧急指针TCPHeader_t;/void match_http(FILE *fp, char *head_str, char *tail_str, char *buf, int total_len); /查找 http 信息函数/int main()struct pcap_file_header *file_header;struct pcap_pkthdr *ptk_header;IPHeader_t *ip_header;TCPHeader_t *tcp_header;FILE *fp, *output;int pkt_offset, i=0

19、;int ip_len, http_len, ip_proto;int src_port, dst_port, tcp_flags;char bufBUFSIZE, my_timeSTRSIZE;char src_ipSTRSIZE, dst_ipSTRSIZE;char hostSTRSIZE, uriBUFSIZE;/初始化file_header = (struct pcap_file_header *)malloc(sizeof(struct pcap_file_header);ptk_header = (struct pcap_pkthdr *)malloc(sizeof(struct

20、 pcap_pkthdr);ip_header = (IPHeader_t *)malloc(sizeof(IPHeader_t);tcp_header = (TCPHeader_t *)malloc(sizeof(TCPHeader_t);memset(buf, 0, sizeof(buf);/if(fp = fopen(“test.pcap”,”r”) = NULL)printf(“error: can not open pcap filen”);exit(0);if(output = fopen(“output.txt”,”w+”) = NULL)printf(“error: can n

21、ot open output filen”);exit(0);/开始读数据包pkt_offset = 24; /pcap文件头结构 24个字节while(fseek(fp, pkt_offset, SEEK_SET) = 0) /遍历数据包i+;/pcap_pkt_header 16 byteif(fread(ptk_header, 16, 1, fp) != 1) /读pcap数据(shj)包头结构printf(“nread end of pcap filen”);break;pkt_offset += 16 + ptk_header-caplen; /下一个(y )数据包的偏移值strft

22、ime(my_time, sizeof(my_time), “%Y-%m-%d %T”, localtime(&(ptk_header-ts.tv_sec); /获取(huq)时间/ printf(“%d: %sn”, i, my_time);/数据(shj)帧头 14字节fseek(fp, 14, SEEK_CUR); /忽略数据帧头/IP数据报头 20字节if(fread(ip_header, sizeof(IPHeader_t), 1, fp) != 1)printf(“%d: can not read ip_headern”, i);break;inet_ntop(AF_INET, (

23、void *)&(ip_header-SrcIP), src_ip, 16);inet_ntop(AF_INET, (void *)&(ip_header-DstIP), dst_ip, 16);ip_proto = ip_header-Protocol;ip_len = ip_header-TotalLen; /IP数据报总长度/ printf(“%d: src=%sn”, i, src_ip);if(ip_proto != 006) /判断是否是 TCP 协议continue;/TCP头 20字节if(fread(tcp_header, sizeof(TCPHeader_t), 1, fp

24、) != 1)printf(“%d: can not read ip_headern”, i);break;src_port = ntohs(tcp_header-SrcPort);dst_port = ntohs(tcp_header-DstPort);tcp_flags = tcp_header-Flags;/ printf(“%d: src=%xn”, i, tcp_flags);if(tcp_flags = 018) / (PSH, ACK) 3路握手成功后if(dst_port = 80) / HTTP GET请求http_len = ip_len 40; /http 报文长度mat

25、ch_http(fp, “Host: “, “rn”, host, http_len); /查找 host 值match_http(fp, “GET “, “HTTP”, uri, http_len); /查找(ch zho) uri 值sprintf(buf, “%d: %s src=%s:%d dst=%s:%d %s%srn”, i, my_time, src_ip, src_port, dst_ip, dst_port, host, uri);/printf(“%s”, buf);if(fwrite(buf, strlen(buf), 1, output) != 1)printf(“o

26、utput file can not write”);break; / end whilefclose(fp);fclose(output);return 0;/查找(ch zho) HTTP 信息(xnx)void match_http(FILE *fp, char *head_str, char *tail_str, char *buf, int total_len)int i;int http_offset;int head_len, tail_len, val_len;char head_tmpSTRSIZE, tail_tmpSTRSIZE;/初始化memset(head_tmp,

27、0, sizeof(head_tmp);memset(tail_tmp, 0, sizeof(tail_tmp);head_len = strlen(head_str);tail_len = strlen(tail_str);/查找(ch zho) head_strhttp_offset = ftell(fp); /记录下HTTP报文初始文件偏移while(head_tmp0 = fgetc(fp) != EOF) /逐个字节遍历if(ftell(fp) http_offset) total_len) /遍历完成sprintf(buf, “can not find %s rn”, head_str);exit(0);if(head_tmp0 = *head_str) /匹配到第一个字符for(i=1; i total_len) /遍历完成sprintf(buf, “can not find %s rn”, tail_str);exit(0);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1