tcpdump是一个强大的网络数据包抓包分析工具,用于抓取数据包和分析网络流量。并以可阅读的格式展示,包括源和目标地址、端口、协议类型等信息。
tcpdump还有很多过滤选项,可以帮助我们筛选有用的信息,精准筛选特定流量,便于网络故障判断、网络性能分析、安全审计等等。
tcpdump支持多种协议和格式,服务器管理员、网络工程师、安全专家都经常会用到这个工具。
tcpdump是一个跨平台的工具,可以支持Linux、bsd、macos等不同的操作系统,Windows下也可以用windump。今天我们基于Ubuntu进行分享:
TCP dump在大多数Linux系统中都有默认安装,无需额外安装。包括Ubuntu系统。
Ubuntu执行tcpdump报错
error:tcpdump: socket:address family not supported by protocol
错误通常是因为尝试在不支持的地址族上运行 tcpdump 导致的。在某些情况下,可能是因为使用了不支持的选项或参数。
这个错误可能有几个常见原因:
tcpdump -D
尝试以下操作:
depmod -a
modprobe af_packet
执行如果失败,可以通过如下命令查看是否存在:
find /lib/modules -name "af_packet*"
ps:以上方法,我再Windows的wls子系统中执行时失败了的。提示找不到目录。
如果问题仍然存在,可能需要更详细地检查 tcpdump 的用法、支持的选项和系统文档,或者提供更多的上下文信息来帮助进一步解决。寻求tcpdump帮助指令:man tcpdump
sudo tcpdump -D
列出当前Ubuntu服务器的所有接口。
mirror@Ubuntu22:~$ sudo tcpdump -D
[sudo] password for mirror:
1.ens160 [Up, Running, Connected]
2.ens192 [Up, Running, Connected]
3.any (Pseudo-device that captures on all interfaces) [Up, Running]
4.lo [Up, Running, Loopback]
5.bluetooth-monitor (Bluetooth Linux Monitor) [Wireless]
6.nflog (Linux netfilter log (NFLOG) interface) [none]
7.nfqueue (Linux netfilter queue (NFQUEUE) interface) [none]
8.dbus-system (D-Bus system bus) [none]
9.dbus-session (D-Bus session bus) [none]
sudo tcpdump -i ens160
从以下输出结果,已经可以正常抓取数据包信息。因为是实际环境的,所以筛检了部分信息。
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on ens160, link-type EN10MB (Ethernet), snapshot length 262144 bytes
03:42:17.246688 IP 192.168.1.8.42463 > public1.114dns.com.domain: 41988+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:17.246854 IP 192.168.1.8.58001 > public1.114dns.com.domain: 25469+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:17.246905 IP 192.168.1.8.58686 > public1.114dns.com.domain: Flags [S], seq 3628915760, win 64240, options [mss 1460,sackOK,TS val 380598131 ecr 0,nop,wscale 7,tfo cookiereq,nop,nop], length 0
03:42:17.288016 IP public1.114dns.com.domain > 192.168.1.8.58686: Flags [S.], seq 1011366664, ack 3628915761, win 64240, options [mss 1460,sackOK,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,tfo cookiereq,nop,nop], length 0
03:42:17.288084 IP 192.168.1.8.58686 > public1.114dns.com.domain: Flags [.], ack 1, win 64240, length 0
03:42:17.288122 IP 192.168.1.8.58686 > public1.114dns.com.domain: Flags [P.], seq 1:46, ack 1, win 64240, length 45 17322+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:17.533736 IP 192.168.1.8.58686 > public1.114dns.com.domain: Flags [P.], seq 1:46, ack 1, win 64240, length 45 17322+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:17.781715 IP 192.168.1.8.58686 > public1.114dns.com.domain: Flags [P.], seq 1:46, ack 1, win 64240, length 45 17322+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:18.273741 IP 192.168.1.8.58686 > public1.114dns.com.domain: Flags [P.], seq 1:46, ack 1, win 64240, length 45 17322+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:19.265742 IP 192.168.1.8.58686 > public1.114dns.com.domain: Flags [P.], seq 1:46, ack 1, win 64240, length 45 17322+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:21.249711 IP 192.168.1.8.58686 > public1.114dns.com.domain: Flags [P.], seq 1:46, ack 1, win 64240, length 45 17322+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:37.263316 IP 192.168.1.8.33584 > public1.114dns.com.domain: 49580+ PTR? 114.114.114.114.in-addr.arpa. (46)
03:42:37.263436 IP 192.168.1.8.47765 > public1.114dns.com.domain: 39262+ PTR? 111.1.30.172.in-addr.arpa. (43)
03:42:37.263483 IP 192.168.1.8.42463 > public1.114dns.com.domain: 41988+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:37.263498 IP 192.168.1.8.58001 > public1.114dns.com.domain: 25469+ PTR? 69.35.10.10.in-addr.arpa. (43)
03:42:37.263512 IP 192.168.1.8.45521 > public1.114dns.com.domain: 42802+ PTR? 228.1.30.172.in-addr.arpa. (43)
listening on ens160, link-type EN10MB (Ethernet), snapshot length 262144 bytes
这句输出的信息表示当前 tcpdump 正在监听名为 ens160 的网络接口,该接口的链路类型为以太网(Ethernet),并设置了捕获的快照长度为 262144 字节。
sudo tcpdump -i ens160 -n
mirror@Ubuntu22:~$ sudo tcpdump -i ens160 -n
[sudo] password for mirror:
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on ens160, link-type EN10MB (Ethernet), snapshot length 262144 bytes
05:10:15.243705 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 3209440688:3209440928, ack 2284055446, win 501, length 240
05:10:15.287791 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 0, win 513, length 0
05:10:15.318211 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 240:560, ack 1, win 501, length 320
05:10:15.373988 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 240, win 512, length 0
05:10:15.422191 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 560:848, ack 1, win 501, length 288
05:10:15.463074 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 560, win 511, length 0
05:10:15.526180 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 848:1136, ack 1, win 501, length 288
05:10:15.532448 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 848, win 509, length 0
05:10:15.630126 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 1136:1440, ack 1, win 501, length 304
05:10:15.635049 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 1136, win 508, length 0
05:10:15.686352 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 1440, win 507, length 0
05:10:15.733967 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 1440:1840, ack 1, win 501, length 400
05:10:15.792947 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 1840, win 513, length 0
05:10:15.838213 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 1840:2144, ack 1, win 501, length 304
05:10:15.896544 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 2144, win 512, length 0
05:10:15.941949 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 2144:2448, ack 1, win 501, length 304
05:10:15.993089 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 2448, win 510, length 0
05:10:16.046087 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 2448:2752, ack 1, win 501, length 304
05:10:16.110191 IP 10.10.35.69.56256 > 192.168.1.8.22: Flags [.], ack 2752, win 509, length 0
05:10:16.149927 IP 192.168.1.8.22 > 10.10.35.69.56256: Flags [P.], seq 2752:3056, ack 1, win 501, length 304
sudo tcpdump -i ens160 port 80 -n -c 20
mirror@Ubuntu22:~$ sudo tcpdump -i ens160 port 80 -n -c 20
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on ens160, link-type EN10MB (Ethernet), snapshot length 262144 bytes
05:18:01.319622 IP 10.10.35.69.58740 > 192.168.1.228.80: Flags [S], seq 988678533, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
05:18:01.319648 IP 192.168.1.228.80 > 10.10.35.69.58740: Flags [S.], seq 776352123, ack 988678534, win 64240, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
05:18:01.324080 IP 10.10.35.69.58740 > 192.168.1.228.80: Flags [.], ack 1, win 256, length 0
05:18:01.488661 IP 10.10.35.69.58741 > 192.168.1.228.80: Flags [S], seq 2538381446, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
05:18:01.488701 IP 192.168.1.228.80 > 10.10.35.69.58741: Flags [S.], seq 3999755288, ack 2538381447, win 64240, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
05:18:01.492415 IP 10.10.35.69.58740 > 192.168.1.228.80: Flags [P.], seq 1:499, ack 1, win 256, length 498: HTTP: GET /simple/0/ HTTP/1.1
05:18:01.492440 IP 192.168.1.228.80 > 10.10.35.69.58740: Flags [.], ack 499, win 501, length 0
05:18:01.494088 IP 10.10.35.69.58741 > 192.168.1.228.80: Flags [.], ack 1, win 256, length 0
05:18:01.498164 IP 192.168.1.228.80 > 10.10.35.69.58740: Flags [P.], seq 1:650, ack 499, win 501, length 649: HTTP: HTTP/1.1 200 OK
05:18:01.554996 IP 10.10.35.69.58740 > 192.168.1.228.80: Flags [.], ack 650, win 254, length 0
05:18:06.504170 IP 192.168.1.228.80 > 10.10.35.69.58740: Flags [F.], seq 650, ack 499, win 501, length 0
05:18:06.508091 IP 10.10.35.69.58740 > 192.168.1.228.80: Flags [.], ack 651, win 254, length 0
05:18:14.517204 IP 10.10.35.69.58740 > 192.168.1.228.80: Flags [F.], seq 499, ack 651, win 254, length 0
05:18:14.517229 IP 192.168.1.228.80 > 10.10.35.69.58740: Flags [.], ack 500, win 501, length 0
05:18:14.519102 IP 10.10.35.69.58752 > 192.168.1.228.80: Flags [S], seq 777512590, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
05:18:14.519126 IP 192.168.1.228.80 > 10.10.35.69.58752: Flags [S.], seq 2039516034, ack 777512591, win 64240, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
05:18:14.540753 IP 10.10.35.69.58752 > 192.168.1.228.80: Flags [.], ack 1, win 256, length 0
05:18:19.362131 IP 10.10.35.69.58741 > 192.168.1.228.80: Flags [P.], seq 1:501, ack 1, win 256, length 500: HTTP: GET /simple/0-0/ HTTP/1.1
05:18:19.362184 IP 192.168.1.228.80 > 10.10.35.69.58741: Flags [.], ack 501, win 501, length 0
05:18:19.605373 IP 192.168.1.228.80 > 10.10.35.69.58741: Flags [P.], seq 1:513, ack 501, win 501, length 512: HTTP: HTTP/1.1 200 OK
20 packets captured
20 packets received by filter
0 packets dropped by kernel
mirror@ Ubuntu22:~$
sudo tcpdump -i ens160 host 10.10.35.69 -n -c 20
如果要抓取指定的主机的指定端口,则用and 来连接host和port。
sudo tcpdump -i ens160 host 10.10.35.69 and port 80 -n -c 20
sudo tcpdump -i ens160 net 10.10.0.0/16 and port 80 -n -c 20
sudo tcpdump -i ens160 src 10.10.35.69 and port 80 -n -c 20
通过dst参数指定目的IP地址:
sudo tcpdump -i ens160 dst 10.10.35.69 and port 80 -n -c 20
如果我们抓包的数量和时间很长,可以通过-w参数将抓包保存下来。方法参考如下:
sudo tcpdump -i ens160 src 10.10.35.69 and port 80 -n -c 20 -w packet.pcap
mirror@Ubuntu22:~$ sudo tcpdump -i ens160 src 10.10.35.69 and port 80 -n -c 20 -w packet.pcap
tcpdump: listening on ens160, link-type EN10MB (Ethernet), snapshot length 262144 bytes
20 packets captured
274 packets received by filter
0 packets dropped by kernel
mirror@Ubuntu22:~$ ls
bandersnatch mirrored-files packet.pcap route.sh
mirror@Ubuntu22:~$
以上方法可以帮助大家完成基本的抓包技能了。如果还需要更高阶的抓包方法,可以使用man tcpdump寻求帮助。得空我也再写一篇高阶的抓包方法。