Sliceaemc
Sliceaemc

Go dev

构建tcpdump/wireshark pcap文件

 pcap文件格式是bpf保存原始数据包的格式,很多软件都在使用,比如tcpdump、wireshark等等,了解pcap格式可以加深对原始数据包的了解,自己也可以手工构造任意的数据包进行测试。


 

pcap文件的格式为:

  文件头    24字节

  数据包头 + 数据包  数据包头为16字节,后面紧跟数据包

  数据包头 + 数据包  ……

 

pcap.h里定义了文件头的格式


struct pcap_file_header {

bpf_u_int32 magic;

u_short version_major;

u_short version_minor;

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_*) */

};

看一下各字段的含义:

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

常用链路类型:

      0           BSD loopback devices, except for later 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


=======================================================================================

|    magic    |major  | minor |   thiszone  |   sigfigs   |   snaplen   |  linktype  

| d4 c3 b2 a1 | 02 00 | 04 00 | 00 00 00 00 | 00 00 00 00 | ff ff 00 00 | 01 00 00 00

=======================================================================================

 

 

数据包头的格式


struct pcap_pkthdr {

struct timeval ts; /* time stamp */

bpf_u_int32 caplen; /* length of portion present */

bpf_u_int32 len; /* length this packet (off wire) */

};

struct timeval {

long tv_sec; /* seconds (XXX should be time_t) */

suseconds_t tv_usec; /* and microseconds */

};

ts:    8字节 抓包时间 4字节表示秒数,4字节表示微秒数

caplen:4字节 保存下来的包长度(最多是snaplen,比如68字节)

len:   4字节 数据包的真实长度,如果文件中保存的不是完整数据包,可能比caplen大

————————————————

版权声明:本文为CSDN博主「功名半纸」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/force_eagle/article/details/6681802

CC BY-NC-ND 2.0 版权声明

喜欢我的文章吗?
别忘了给点支持与赞赏,让我知道创作的路上有你陪伴。

加载中…

发布评论