wireshark文件pcap的格式

来源:互联网 发布:office2016激活工具mac 编辑:程序博客网 时间:2024/04/30 13:54

pcap格式文件组成

pcap文件由Global Header、Packet Header及Packet Data组成,其中Packet Data即为需要根据网络协议解释的数据。

Global Header的定义
typedef struct pcap_hdr_s {        guint32 magic_number;   /* magic number */        guint16 version_major;  /* major version number */        guint16 version_minor;  /* minor version number */        gint32  thiszone;       /* GMT to local correction */        guint32 sigfigs;        /* accuracy of timestamps */        guint32 snaplen;        /* max length of captured packets, in octets */        guint32 network;        /* data link type */} pcap_hdr_t;
通过python解析一个pcap文件,得到的前6个words数据:a1b2c3d4、00040002、00000000、00000000、0000ffff、00000065 主要关心最后一个word对应协议格式DLT的数据,0x65 101对应表示后面数据类型为IP包格式(DLT可以参考https://www.tcpdump.org/linktypes.html)。

Packet Header的定义

typedef struct pcaprec_hdr_s {        guint32 ts_sec;         /* timestamp seconds */        guint32 ts_usec;        /* timestamp microseconds */        guint32 incl_len;       /* number of octets of packet saved in file */        guint32 orig_len;       /* actual length of packet */} pcaprec_hdr_t;

其中incl_len小于等于orig_len,orig_len为抓取的时候的真实数据包长度,incl_len为存储的数据包长度(不等情况的使用,还未考虑过)。pcap文件得到一个PacketHeader示例值000000b5、0ad26fc4、00000033、00000033,示例中的incl_len和orig_len是相等的为0x33=51个字节长度。

数据和wireshark解析示例


注意示例中的文本数据和wireshark解析数据存在大小端不一致的映射关系,所以33000045在解析时是按照45 00 00 33顺序进行的。
参考
pcap文件格式:https://wiki.wireshark.org/Development/LibpcapFileFormat
DLT:https://www.tcpdump.org/linktypes.html
wireshark源代码地址:https://github.com/wireshark/wireshark