pcap文件格式及文件解析

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

u_int16 Flag_Segment; //标志+片偏移 u_int8 TTL; //生存周期 u_int8 Protocol; //协议类型 u_int16 Checksum; //头部校验和 u_int32 SrcIP; //源IP地址 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; //数据报头的长度(4 bit) + 保留(4 bit) u_int8 Flags; //标识TCP不同的控制消息 u_int16 Window; //窗口大小 u_int16 Checksum; //校验和 u_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; int ip_len, http_len, ip_proto; int src_port, dst_port, tcp_flags; char buf[BUFSIZE], my_time[STRSIZE]; char src_ip[STRSIZE], dst_ip[STRSIZE];
五:举例分析
Βιβλιοθήκη Baidu
图中最开始的绿色部分就是24 Bytes的Pcap Header,接下来 红色的16 Bytes是第一个消息的Pcap Header。后面的红色的 16 Bytes是第二个消息的Pcap Header。两块蓝色的部分分别 是两个消息从链路层开始的完整内容。在网络上实际传输的数 据包在数据链路层上每一个Packet开始都会有7个用于同步的 字节和一个用于标识该Packet开始的字节,最后还会有四个 CRC校验字节;而PCAP文件中会把前8个字节和最后4个校验 自己去掉,因为这些信息对于协议分析是没有用的。 用Wireshark打开一个PCAP数据包,每条消息的所有field会 被解析出来并会按照协议层次折叠起来。第一层显示的是 FrameXXX,这一级别没有对应某层具体的协议,而是对本条 消息的一个概括性总结,描述了一些有用的概括性信息,比如 从里面我们可以看到本条消息各种协议的层次关系,展开其它 协议层之后对应的是该协议的各个域,如下图所示:
bpf_u_int32 magic; /* 0xa1b2c3d4 */ u_short version_major; /* magjor Version 2 */ u_short version_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_*) */ }; //时间戳 struct time_val { long tv_sec; /* seconds 含义同 time_t 对象的值 */ long tv_usec; /* and microseconds */ }; //pcap数据包头结构体 struct pcap_pkthdr { struct 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 DstMAC[6]; //目的MAC地址 u_int8 SrcMAC[6]; //源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; //标识
OpenBSD 1 Ethernet, and Linux loopback devices 以太网类 型,大多数的数据包为这种类型。 6 802.5 Token Ring 7 ARCnet 8 SLIP 9 PPP 10 FDDI 100 LLC/SNAP-encapsulated ATM 101 raw IP, with no link 102 BSD/OS SLIP 103 BSD/OS PPP 104 Cisco HDLC 105 802.11 108 later OpenBSD loopback devices (with the AF_value in network byte order) 113 special Linux cooked capture 114 LocalTalk 三 packet数据包头:
bytes (which may be greater than the previous number, if you are not saving the entire packet). 四:packet数据: 即Packet(通常就是链路层的数据帧)具体内容,长度就是 Caplen,这个长度的后面,就是当前PCAP文件中存放的下一 个Packet数据包,也就是说:PCAP文件里面并没有规定捕获 的Packet数据包之间有什么间隔字符串,下一组数据在文件中 的起始位置。我们需要靠第一个Packet包确定。最后, Packet数据部分的格式其实就是标准的网路协议格式了可以任 何网络教材上找得到。
struct pcap_pkthdr { struct tim ts; DWORD caplen;
DWORD }
len;
struct tim { DWORD GMTtime; DWORD microTime } 说明: 1、时间戳,包括: 秒计时:32位,一个UNIX格式的精确到秒时间值,用来记录 数据包抓获的时间,记录方式是记录从格林尼治时间的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: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-bit value giving the actual length of the packet, in
第一部分:PCAP包文件格式
一 基本格式: 文件头 数据包头数据报数据包头数据报...... 二、文件头:
文件头结构体 sturct pcap_file_header { DWORD magic; DWORD version_major; DWORD version_minor; DWORD thiszone; DWORD sigfigs; DWORD snaplen; DWORD linktype; } 说明: 1、标识位:32位的,这个标识位的值是16进制的 0xa1b2c3d4。 a 32-bit magic number ,The magic 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 that 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 snapshot 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 layer type field.The link-layer type depends on the type of link-layer header that the packets in the capture file have: 以下是数据值与链路层类型的对应表 0 BSD loopback devices, except for later
第二部分:PCAP文件解析
1、 pcap解析工具 Xplico Xplico 是一个从 pcap 文件中解析出IP流量数据的工具, 可解析每个邮箱 (POP, IMAP, 和 SMTP 协议), 所有 HTTP 内容, VoIP calls (SIP) 等等
2、 C语言实现PCAP文件分析
实现步骤: 1)用Wireshark软件抓包得到test.pcap文件 2)程序:分析pcap文件头 -> 分析pcap_pkt头 -> 分析帧头 -> 分 析ip头 -> 分析tcp头 -> 分析http信息 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<netinet/in.h> #include<time.h> #define BUFSIZE 10240 #define STRSIZE 1024 typedef 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_header {
相关文档
最新文档