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_int32thiszone;     /* gmt to local correction */
        bpf_u_int32sigfigs;    /* accuracy of timestamps */
        bpf_u_int32snaplen;    /* 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 laterOpenBSD

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 0000 00 |

 

数据包头的格式
struct pcap_pkthdr {
        struct timevalts;      /* time stamp */
        bpf_u_int32caplen;     /* length of portion present */
        bpf_u_int32 len;       /* length this packet (off wire) */
};
struct timeval {
       long           tv_sec;         /* seconds (XXX shouldbe time_t) */
        suseconds_t    tv_usec;        /* and microseconds */
};
 ts:    8字节 抓包时间 4字节表示秒数,4字节表示微秒数
 caplen:4字节 保存下来的包长度(最多是snaplen,比如68字节)
 len:   4字节 数据包的真实长度,如果文件中保存的不是完整数据包,可能比caplen大

说明:

1、时间戳,包括:

秒计时:32位,一个UNIX格式的精确到秒时间值,用来记录数据包抓获的时间,记录方式是记录从格林尼治时间的197011 00:00:00 到抓包时经过的秒数;

毫秒计时:32位,抓取数据包时的毫秒值。

a time stamp,consisting of:

a UNIX-formattime-in-seconds when the packet was captured, i.e. the number of seconds sinceJanuary 1,1970, 00:00:00 GMT (that GMT, *NOT* local time!);  

the number ofmicroseconds since that second when the packet was captured;

 

2、数据包长度:32,标识所抓获的数据包保存在pcap文件中的实际长度,以字节为单位。

a 32-bit valuegiving the number of bytes of packet data that were captured;

 

3、数据包实际长度:所抓获的数据包的真实长度,如果文件中保存不是完整的数据包,那么这个值可能要比前面的数据包长度的值大。

a 32-bit valuegiving the actual length of the packet, in bytes (which may be greater than theprevious number, if you are not saving the entire packet).

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值