PCAP Capture File Format 2023

介绍

在19世纪80年代后期,Van Jacobson, Steve McCanne 和其他人在位于Lawrence Berkeley National Laboratory的Network Research Group中开发了tcpdump——用来捕获和分析网络流。读取网络流,然后通过libpcap写入到文件。

常规文件结构

一个pcap文件由一个文件头组成,后续跟着0填充或者多个数据包,每个数据包也有一个头。

读写pcap文件不需要进行大小端转换,效率更高。

文件头

文件头的格式如下,左边数字是字节偏移:

   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 0 |                          Magic Number                         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 4 |          Major Version        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 6 |          Minor Version        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 8 |                           Reserved1                           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12 |                           Reserved2                           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16 |                            SnapLen                            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20 |               LinkType and additional information             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

文件头的长度是24个字节,每个字段的意思是:

Magic Number (32 bits)

一个无符号的魔术数,值可以是0xA1B2C3D4或者0xA1B23C4D

  • 0xA1B2C3D4——数据包中的时间戳是秒和微妙

  • 0xA1B23C4D——数据包中的时间戳是秒和纳秒

这个数据可以用来区分pcap包是大端还是小端系统生成的。

Major Version (16 bits)

无符号整数,指定当前格式的主版本号。当前版本号是2。如果后续规则做了修改,可能会变化这个版本号。不同版本号之间理论上可能无法兼容,解析文件的时候需要检查版本号进行确认。

Minor Version (16 bits)

无符号整数,指定当前格式的副版本号。当前版本号是4。与主版本号一样,不同版本号之间理论上可能无法兼容,解析的时候需要确认。

Reserved1 (32 bits)

保留字段1,没有使用,0填充。

Reserved2 (32 bits)

保留字段2,没有使用,0填充。

SnapLen (32 bits)

无符号整数,指定了一个pcap文件中所有数据包的最大字节数。一个数据包,超过这个数的部分将会被丢掉。这个值不能为0,如果没有设定最大限制,那么应该大于或者等于最大的数据包长度。

LinkType and additional information (32 bits)

32位无符号整数。包含当前文件中数据包的链路层类型信息,也可能包含其他信息。

                        1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |FCS len|R|P|     Reserved3     |        Link-layer type        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

这个字段按照当前系统的字节序读写,0是最高位,31是最低位。

16位数据,指明了链路层的类型。具体定义参考[I-D.ietf-opsawg-pcaplinktype]。

Reserved3 (10 bits)

保留字段,没有使用,0填充。

P (1 bit)

指明Frame Check Sequence (FCS)长度字段是否存在

R (1 bit)

保留字段,没有使用,0填充。

FCS len (4 bits)

FCS长度字段,无符号整数。指明附加到每个数据包后面的有关FCS(一个FCS由2个字节——16位组成)的个数。如果P字段未设置,则这个字段无效,FCS长度未知。FCS长度的字段数值为0~15。比如这个值是2,表示有2个16位数据,也就是4个字节。

数据包

   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 0 |                      Timestamp (Seconds)                      |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 4 |            Timestamp (Microseconds or nanoseconds)            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 8 |                    Captured Packet Length                     |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12 |                    Original Packet Length                     |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16 /                                                               /
   /                          Packet Data                          /
   /                  variable length, not padded                  /
   /                                                               /

数据包由一个16位长度的包头组成,后续跟着包的数据。每一个字段的详细解释:

Timestamp (Seconds) and Timestamp (Microseconds or nanoseconds)

时间戳,记录了秒和秒的小数部分——可能是毫秒,也可能是纳秒。

Captured Packet Length (32 bits)

无符号整数。数据的长度(字节数)。SnapLen限制的就是这个值。

Original Packet Length (32 bits)

无符号整数。数据包原始长度。因为SnapLen限制,数据包记录的时候有可能被截断,Captured Packet Length表示记录的字节数,这个表示网络包实际的长度。

Packet Data

原始网络数据,包括链接层的头(link-layer headers)。Captured Packet Length记录的就是这个字段的长度。链接层的头的内容根据LinkType字段定义。

数据包没有4字节对齐。如果不是4字节的倍数,也不会进行填充,所以不能保证数据包保存的位置在4字节边界上。

建议的文件后缀.pcap

虽然解析pcap文件的时候,需要根据开头的魔术字进行判断,但是还是建议使用.pcap后缀标明这是一个pcap文件。.cap文件与pcap文件有很大差别,不能混用,避免产生混淆。

下一代pcap标准(参考I-D.ietf-opsawg-pcapng)与现在的并不兼容,新的魔术字是0x0A0D0D0A。

Media-Type Registry

文件的标示类型为application/pcap

application/pcap

Type name:  application
Subtype name:  pcap
Required parameters:  none
Optional parameters:  none
Encoding considerations:  PCAP files contain network packets
Security considerations:  See Security Considerations, Section
Interoperability considerations:  The format is designed to be broadly interoperable.
Published specification:  THIS RFC.
Applications that use this media type: tcpdump, wireshark, others.
Additional information:
  Magic number(s): 0xA1B2C3D4, and 0xA1B23C4D in both endian orders
  File extension(s):  .pcap
  Macintosh file type code(s):  none
Person & email address to contact for further information: The Tcpdump Group, www.tcpdump.org
Intended usage:  LIMITED
Restrictions on usage:  NONE
Author:  Guy Harris and Michael Richardson
Change controller:  The Tcpdump Group
Provisional registration? (standards tree only):  NO

https://datatracker.ietf.org/doc/draft-ietf-opsawg-pcap/
https://www.ietf.org/archive/id/draft-ietf-opsawg-pcaplinktype-00.html

posted @ 2023-02-20 14:42  秋来叶黄  阅读(159)  评论(0编辑  收藏  举报