Wireshark TS | 如何按起始和终止时间拆分pcap文件

233 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

问题背景

来自于 CSDN 的一个问题 Tcpdump按起始和终止时间段拆分pcap文件的指令,简述就是题主想用 tcpdump 来拆分固定时间段的文件,譬如拆分 Monday.pcap 中的 1:00pm-1:30pm 的数据包到 1.pcap 中。

问题分析

其实在 Wireshark 数据包分析中根据起始和终止时间来过滤分析,是个很常见的需求,尤其是需要精准定位故障时间的时候。

但对于问题中的需求,特别是指定需要 tcpdump 指令,个人理解相对于 Wireshark 却并不是很好实现(😂 学艺不精),总结几个方法如下:

  1. tcpdump + awk 方式;
  2. Wireshark 方式(GUI);
  3. Tshark 方式(CLI);
  4. editcap 方式。

问题示例

在 linux 上简单抓取了 10 个数据包,时间从 17:51:19.30860417:51:19.601371 如下:

[root@7ace test]# tcpdump -r test.pcapng -n
reading from file test.pcapng, link-type EN10MB (Ethernet)
17:51:19.308604 IP 10.0.1.142.ssh > 10.0.25.12.58320: Flags [P.], seq 1816507392:1816507540, ack 1285932546, win 251, length 148
17:51:19.347977 ARP, Request who-has 10.0.1.14 tell 10.0.1.20, length 46
17:51:19.358489 IP 10.0.25.12.58320 > 10.0.1.142.ssh: Flags [.], ack 148, win 2049, length 0
17:51:19.519085 ARP, Request who-has 10.0.3.46 tell 10.0.1.20, length 46
17:51:19.568699 ARP, Request who-has 10.0.1.131 tell 10.0.1.20, length 46
17:51:19.601066 IP 10.0.1.20 > 10.0.1.142: ICMP echo request, id 26625, seq 22363, length 64
17:51:19.601107 IP 10.0.1.142 > 10.0.1.20: ICMP echo reply, id 26625, seq 22363, length 64
17:51:19.601263 IP 10.0.1.20 > 10.0.1.142: ICMP echo request, id 26625, seq 22365, length 64
17:51:19.601272 IP 10.0.1.142 > 10.0.1.20: ICMP echo reply, id 26625, seq 22365, length 64
17:51:19.601371 IP 10.0.1.20 > 10.0.1.142: ICMP echo request, id 26625, seq 22366, length 64
[root@7ace test]# 
  1. tcpdump + awk 方式
[root@7ace test]# tcpdump -r test.pcapng -n | awk '{if($1>="17:51:19.50" && $1<"17:51:19.6012") print $0}'
reading from file test.pcapng, link-type EN10MB (Ethernet)
17:51:19.519085 ARP, Request who-has 10.0.3.46 tell 10.0.1.20, length 46
17:51:19.568699 ARP, Request who-has 10.0.1.131 tell 10.0.1.20, length 46
17:51:19.601066 IP 10.0.1.20 > 10.0.1.142: ICMP echo request, id 26625, seq 22363, length 64
17:51:19.601107 IP 10.0.1.142 > 10.0.1.20: ICMP echo reply, id 26625, seq 22363, length 64
[root@7ace test]# 

通过 awk 处理过滤了指定时间段的 4 个数据包,但是唯一的问题是结果并非 pcapng 格式文件。

  1. Wireshark 方式
(frame.time >= "Nov 14, 2021 17:51:19.50" ) && (frame.time <= "Nov 14, 2021 17:51:19.6012")

之后保存应用显式过滤后的数据包为 pcapng 格式文件。

  1. Tshark 方式
$ tshark -r test.pcapng -t a -Y "(frame.time >= "Nov 14, 2021 17:51:19.50") && (frame.time <= "Nov 14, 2021 17:51:19.6012")"
    4 17:51:19.519085 0.000000000 VMware_44:ba:e2 Broadcast    ARP  60  Who has 10.0.3.46? Tell 10.0.1.20
    5 17:51:19.568699 0.049614000 VMware_44:ba:e2 Broadcast    ARP  60  Who has 10.0.1.131? Tell 10.0.1.20
    6 17:51:19.601066 0.032367000    10.0.1.20 10.0.1.142   ICMP  98 64 Echo (ping) request  id=0x6801, seq=22363/23383, ttl=64
    7 17:51:19.601107 0.000041000   10.0.1.142 10.0.1.20    ICMP  98 64 Echo (ping) reply    id=0x6801, seq=22363/23383, ttl=64 (request in 6)
        
        
$ tshark -r test.pcapng -t a -Y "(frame.time >= "Nov 14, 2021 17:51:19.50") && (frame.time <= "Nov 14, 2021 17:51:19.6012")" -w 1.pcapng


$ tshark -t a -r 1.pcapng
    1 17:51:19.519085 0.000000000 VMware_44:ba:e2 Broadcast    ARP  60  Who has 10.0.3.46? Tell 10.0.1.20
    2 17:51:19.568699 0.049614000 VMware_44:ba:e2 Broadcast    ARP  60  Who has 10.0.1.131? Tell 10.0.1.20
    3 17:51:19.601066 0.032367000    10.0.1.20 10.0.1.142   ICMP  98 64 Echo (ping) request  id=0x6801, seq=22363/23383, ttl=64
    4 17:51:19.601107 0.000041000   10.0.1.142 10.0.1.20    ICMP  98 64 Echo (ping) reply    id=0x6801, seq=22363/23383, ttl=64 (request in 3)
  1. editcap 方式
$ editcap -A "2021-11-14 17:51:19.50" -B "2021-11-14 17:51:19.6012" test.pcapng 1.pcapng


$ tshark -t a -r 1.pcapng
    1 17:51:19.519085 0.000000000 VMware_44:ba:e2 Broadcast    ARP  60  Who has 10.0.3.46? Tell 10.0.1.20
    2 17:51:19.568699 0.049614000 VMware_44:ba:e2 Broadcast    ARP  60  Who has 10.0.1.131? Tell 10.0.1.20
    3 17:51:19.601066 0.032367000    10.0.1.20 10.0.1.142   ICMP  98 64 Echo (ping) request  id=0x6801, seq=22363/23383, ttl=64
    4 17:51:19.601107 0.000041000   10.0.1.142 10.0.1.20    ICMP  98 64 Echo (ping) reply    id=0x6801, seq=22363/23383, ttl=64 (request in 3)

问题总结

对于 tcpdump 或者 wireshark ,根据不同的场景和需求,可灵活选择使用。引申出来的 tcpdump 和 wireshark 的几点不同,总结如下:

  1. tcpdump 和 wireshark 都是网络抓包分析工具,前者主用于 linux,后者主用于 windows;(注:虽然各自都有不同操作系统的版本)
  2. tcpdump 过滤主要是捕获过滤,而 wireshark 过滤会分有捕获过滤和显示过滤,其中显示过滤应用在数据包分析上,语法非常丰富;
  3. tcpdump 在 linux 中配合 grep、sed、awk 之类的工具使用,会异常强大;
  4. wireshark 相对 tcpdump ,在分析和统计等功能更为全面,同时命令行版本的 tshark、wireshark 一样可达到很好的分析效果。