PCAP(packet capture)格式
文章目录
PCAP(packet capture)格式定义pcap文件的格式文件头24 Bytes 各字段的含义:常用链路类型 实际pcap文件的文件头Packet Data 综述wireshark添加 lua plugins解析pcap脚本存放位置sudo 打开wireshark查看pcap数据时间戳解析 参考文章定义
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 各字段的含义:
常用链路类型
实际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
通过在线时间戳转换工具转换即可得到真实时间:-8-19 18:25:25,时区以格林尼治时间为标准的,所以东八区时间减去8小时,真实时间为-8-19 10:25:25