PCAP(packet capture)格式
文章目录
定义
The Packet Capture library provides a high level interface to packet capture systems. All packets on the network, even those destined for other hosts, are accessible through this mechanism. It also supports saving captured packets to a
savefile
, and reading packets from asavefile
.
pcap是一种常用的存储数据报文的文件格式,里面的数据按照特定格式存储的,通常就是链路层的数据帧,可以存储Ethernet帧、IPv4结构数据、IPv6结构数据、TCP格式数据、UDP格式数据等。
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_*) */
};
文件头24 Bytes 各字段的含义:
header field | size | explain |
---|---|---|
Magic | 4B | pcap文件标识。标记文件开始,并用来识别文件和字节顺序。值可以是 0xa1b2c3d4 或者 0x4dc3b2a1,如果是 0xa1b2c3d4 表示是大端模式,按照原来的顺序一个字节一个字节的读,如果是 0x4dc3b2a1 表示小端模式,下面的字节要交换顺序。现在电脑大部分是小端模式 |
Major | 2B | 主版本号 #define PCAP_VERSION_MAJOR 2,一般为0x0200 |
Minor | 2B | 次版本号 #define PCAP_VERSION_MINOR 4,一般为0x0400 |
ThisZone | 4B | 当地的标准事件,如果用的是 GMT 则全零,一般全零 |
SigFigs | 4B | 时间戳的精度,一般为全零 |
SnapLen | 4B | 抓包最大长度。如果要抓全,设为0x0000ffff(65535), |
tcpdump | 68B | -s 0就是设置这个参数,缺省为68字节 |
LinkType | 4B | 链路类型。一般都是1:ethernet,即以太网常用的 LinkType(链路类型) |
常用链路类型
类型 | 含义 |
---|---|
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 |
实际pcap文件的文件头
| 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 |
红色方框为Pcap Header,红色红线为Packet Header
Pcap Header的Magic:d4 c3 b2 a1,表示是小端模式,后面的字节从后往前读 a1b2c3d4 小端模式
Pcap Header的Major:02 00,计算机读的应该是00 02。
最大存储长度SnapLen:ff ff 00 00 ,正常就是00 00 ff ff,所以是2的16次方减一,是65535个字节。
LinkType:01 00 00 00 ,实际是00 00 00 01,是以太网类型。
Packet Header: 时间戳秒数:61DE82DE,换算成十进制为1641972446,微妙数为0000399C,即14748,合并起来时间戳为1641972446.14748;
后面两个数000004E4,即caplen为1252字节
查看每一帧报文数据,与文件头中caplen大小一致。
Packet Data
Packet 是链路层的数据帧,长度就是 Packet Header 中定义的 Caplen 值,所以每个 Packet Header 后面都跟着 Caplen 长的 Packet Data。也就是说 pcap 文件并没有规定捕获的数据帧之间有什么间隔字符串。Packet 数据帧部分的格式就是标准的网络协议格式了。
具体每个报文结构可查看参考文章中《pcap/cap 文件格式》
综述
-
pcap文件头:数据链路层14 Bytes 数据包头+20 Bytes IP数据包头+20 Bytes TCP数据包头或者UDP数据包头;
-
目的MAC(6 Bytes)+源MAC(6 Bytes)+type(2 Bytes,0800,IP)+协议版本及头长度(0×45,1 Bytes)+区分服务(1 Bytes)
+总长度(2 Bytes)+唯一标示(2 Bytes)+标志与偏移量(2 Bytes)+TTL(1 Bytes)+协议(1 Bytes,TCP|UDP)+校验和(2 Bytes)
+源IP地址(4 Bytes)+目的IP地址(4 Bytes)+源端口(2 Bytes)+目的端口(2 Bytes)+序列号(4 Bytes)+确认号(4 Bytes)+头长度(1 Bytes)+ack标志(1 Bytes)+窗口大小(2 Bytes)+校验和(2 Bytes)+紧急数据偏移量(2 Bytes)
wireshark添加 lua plugins解析pcap
如果已知Packet Data数据结构,可以编写好解析的lua脚本,加载到wireshark中,从而可以在打开pcap文件的同时,直接解读数据内容。
脚本存放位置
/usr/lib/x86_64-linux-gnu/wireshark/plugins
sudo 打开wireshark
qiancj@qianchengjun:~$ sudo wireshark
查看 Help
--> About Wireshark
在Plugins
选项卡中可以看到加载的 lua 脚本
查看pcap数据
加载任意pcap文件
可以看到wireshark data数据中数据包的头被解析出来了
时间戳解析
时间秒数为00 00 62 ff 65 15
, 小数部分为后面的4字节00 02 da 3f
秒数所有字节转换为十进制就是1660904725
,小数部分所有字节转换为十进制就是186943
通过在线时间戳转换工具转换即可得到真实时间:2022-8-19 18:25:25,时区以格林尼治时间为标准的,所以东八区时间减去8小时,真实时间为2022-8-19 10:25:25
参考文章
- pcap文件的格式:http://blog.chinaunix.net/uid-30280182-id-5208298.html
- pcap 文件格式–转载: http://blog.chinaunix.net/uid-29119135-id-3853961.html
- pcap/cap 文件格式: https://zhuanlan.zhihu.com/p/394946101
- pcap库中每个函数的具体使用方法,可参考PCAP(3PCAP) MAN PAGE : https://www.tcpdump.org/manpages/pcap.3pcap.html
- pcap解析工具及录包工具wireshark的使用,以及相关editcap、mergecap、randpkt、text2pcap的命令使用参考WireShark官网: https://wiki.wireshark.org/Tools