pcap文件格式分析

转自:http://www.cnblogs.com/therock/admin/EditArticles.aspx?opt=1

pcap文件格式是常用的数据报存储格式,包括wireshark在内的主流抓包软件都可以生成这种格式的数据包
下面对这种格式的文件简单分析一下: 
 
pcap文件的格式为:   文件头    24字节   数据报头 + 数据报  数据包头为16字节,后面紧跟数据报   数据报头 + 数据报  ......
 
pcap.h里定义了文件头的格式 struct pcap_file_header {         bpf_u_int32 magic;         u_short version_major;         u_short version_minor;         bpf_int32 thiszone;             bpf_u_int32 sigfigs;            bpf_u_int32 snaplen;            bpf_u_int32 linktype;   };
看一下各字段的含义:  magic:   4字节 pcap文件标识 目前为“d4 c3 b2 a1”  major:   2字节主版本号     #define PCAP_VERSION_MAJOR 2  minor:   2字节次版本号     #define PCAP_VERSION_MINOR 4  thiszone:4字节时区修正     并未使用,目前全为0  sigfigs: 4字节精确时间戳   并未使用,目前全为0  snaplen: 4字节抓包最大长度如果要抓全,设为0x0000ffff(65535),           tcpdump -s 0就是设置这个参数,缺省为68字节  linktype:4字节链路类型    一般都是1:ethernet
 
 
 
数据报头的格式 struct pcap_pkthdr {         struct timeval ts;              bpf_u_int32 caplen;             bpf_u_int32 len;        }; struct timeval {         long            tv_sec;                 suseconds_t     tv_usec;        };  ts:    8字节 抓包时间 4字节表示秒数,4字节表示微秒数  caplen:4字节 保存下来的包长度(最多是snaplen,比如68字节)  len:   4字节数据报的真实长度,如果文件中保存的不是完整数据包,可能比caplen大
 
posted @ 2013-07-18 14:03  therockthe  阅读(269)  评论(0)    收藏  举报