Tcpdump使用示例

这里收集了一些实用的 tcpdump 使用示例,使用它们可提升您的网络故障排除和安全测试能力。

熟练掌握下面的 tcpdump 使用示例,可以帮助我们更好的了解自己的网络。

了解 tcpdump 是一项基本技能,不仅对于系统管理员、网络工程师或安全专业人员,

对于自己部署玩的一些服务器来说,也会派上用场。

基础知识

常用参数

下面的命令是使用 tcpdump 时常见的参数。

bash 复制代码
$ sudo tcpdump -i eth0 -nn -s0 -v port 80
  • -i :进行抓包的接口,通常是以太网卡或无线适配器,但也可能是 vlan 或其它东西。

如果只有一个网络适配器,不用指定也行。

  • -nn :单个 (n) 不会解析主机名。两个 (nn) 不会解析主机名或端口。

这不仅对于查看 IP/端口号很方便,而且在抓包大量数据时也很方便,因为名称解析会减慢抓包速度。

  • -s0:抓包大小。 -s0 会将大小设置为无限制 。

如果您想抓包所有流量,或者从网络流量中提取二进制文件/文件,则需要此选项。

  • -v:详细,使用 (-v) 或 (-vv) 会增加输出中显示更详细信息,通常会显示更多协议特定的信息。
  • port 80 :端口过滤器,这里设置的是抓包端口 80 上的流量。

显示 ASCII 文本

-A参数使得输出中包含抓包的 ascii 字符串。

这样便于结合 grep 或其他命令解析输出。

另一个可以同时显示十六进制输出和 ascii 的参数是 -X

bash 复制代码
$ sudo tcpdump -A -s0 port 80

根据协议抓包

比如,过滤 UDP 流量,可以指定udp,也可以指定使用协议17,这两个命令效果一样。
TCP 对应的协议是 6。

bash 复制代码
$ sudo tcpdump -i eth0 udp
$ sudo tcpdump -i eth0 proto 17

根据 IP 抓包

使用 host 过滤器将同时抓包前往(目标 )和来自()IP 地址的流量。

bash 复制代码
$ sudo tcpdump -i eth0 host 10.10.1.1

或者使用 srcdst 仅抓包单向流量。

bash 复制代码
$ sudo tcpdump -i eth0 src 10.10.1.20
$ sudo tcpdump -i eth0 dst 10.10.1.20

抓包内容写入文件

将抓包文件写入磁盘,这样就可以用其它工具,比如 Wireshark 来分析。

bash 复制代码
$ sudo tcpdump -i eth0 -s0 -w test.pcap

行缓冲模式

指定缓冲模式,比如行缓冲(-l)或数据包缓冲(-C),可以让 tcpdump 的输出立即发送到管道命令,在故障排除时立即做出响应。

bash 复制代码
$ sudo tcpdump -i eth0 -s0 -l port 80 | grep 'Server:'

不指定缓冲模式,有可能会得不到预期的结果。

组合过滤器

在上面的示例中,可以使用使用下面的逻辑符号来组合不同的过滤器。

bash 复制代码
and or &&
or or ||
not or !

使用示例

Tcpdump命令参数很多,常常有多种方法可以实现同样的结果。

使用哪种方法取决于所需的输出以及线路上的流量。比如,在繁忙的千兆位链路上进行抓包可能会迫使您使用特定的低级数据包过滤器。

下面的示例中,将列举一些以最简单(因此最快)的方式获得结果的方法。

提取 HTTP 用户代理

从 HTTP 请求标头中提取 HTTP 用户代理。

bash 复制代码
$ sudo tcpdump -nn -A -s1500 -l | grep "User-Agent:"

通过使用 egrep 和多个匹配规则,可以从请求中获取用户代理和主机(或任何其他标头)。

bash 复制代码
$ sudo tcpdump -nn -A -s1500 -l | egrep -i 'User-Agent:|Host:'

仅捕获 HTTP GET 和 POST 数据包

仅指定与 GET 匹配的数据包。

bash 复制代码
$ sudo tcpdump -s 0 -A -vv 'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420'

只选择 POST 请求。

bash 复制代码
$ sudo tcpdump -s 0 -A -vv 'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x504f5354'

注意 ,使用此过滤器抓包的数据中可能不包含 POST 数据 ,因为POST 请求很可能会被拆分为多个 TCP 数据包。

上面的表达式中的十六进制是与 GETPOST 请求中的 ascii 对应的。

提取 HTTP 请求的 URL

从流量中解析主机和 HTTP 请求位置。

如果服务不在80 端口,则需要指定端口。

bash 复制代码
$ sudo tcpdump -s 0 -v -n -l | egrep -i "POST /|GET /|Host:"

tcpdump: listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
	POST /wp-login.php HTTP/1.1
	Host: dev.example.com
	GET /wp-login.php HTTP/1.1
	Host: dev.example.com
	GET /favicon.ico HTTP/1.1
	Host: dev.example.com
	GET / HTTP/1.1
	Host: dev.example.com

在 POST 请求中提取 HTTP 密码

bash 复制代码
$ sudo tcpdump -s 0 -A -n -l | egrep -i "POST /|pwd=|passwd=|password=|Host:"

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
11:25:54.799014 IP 10.10.1.30.39224 > 10.10.1.125.80: Flags [P.], seq 1458768667:1458770008, ack 2440130792, win 704, options [nop,nop,TS val 461552632 ecr 208900561], length 1341: HTTP: POST /wp-login.php HTTP/1.1
.....s..POST /wp-login.php HTTP/1.1
Host: dev.example.com
.....s..log=admin&pwd=notmypassword&wp-submit=Log+In&redirect_to=http%3A%2F%2Fdev.example.com%2Fwp-admin%2F&testcookie=1

通过搜索 Set-Cookie(来自服务器)和 Cookie(来自客户端)来抓包 cookie

bash 复制代码
$ sudo tcpdump -nn -A -s0 -l | egrep -i 'Set-Cookie|Host:|Cookie:'

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlp58s0, link-type EN10MB (Ethernet), capture size 262144 bytes
Host: dev.example.com
Cookie: wordpress_86be02xxxxxxxxxxxxxxxxxxxc43=admin%7C152xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxfb3e15c744fdd6; _ga=GA1.2.21343434343421934; _gid=GA1.2.927343434349426; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_86be654654645645645654645653fc43=admin%7C15275102testtesttesttestab7a61e; wp-settings-time-1=1527337439

抓包所有 ICMP 数据包

bash 复制代码
$ sudo tcpdump -n icmp

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
11:34:21.590380 IP 10.10.1.217 > 10.10.1.30: ICMP echo request, id 27948, seq 1, length 64
11:34:21.590434 IP 10.10.1.30 > 10.10.1.217: ICMP echo reply, id 27948, seq 1, length 64
11:34:27.680307 IP 10.10.1.159 > 10.10.1.1: ICMP 10.10.1.189 udp port 59619 unreachable, length 115

非 ECHO/REPLY 的 ICMP 数据包

icmp 类型进行过滤,以选择非标准 ping 包的 icmp 包。

bash 复制代码
$ sudo tcpdump 'icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply'

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
11:37:04.041037 IP 10.10.1.189 > 10.10.1.20: ICMP 10.10.1.189 udp port 36078 unreachable, length 156

抓包 SMTP/POP3 电子邮件

可以提取电子邮件正文其他数据 ,下面的例子中仅解析电子邮件收件人

bash 复制代码
$ sudo tcpdump -nn -l port 25 | grep -i 'MAIL FROM\|RCPT TO'

NTP 的查询和响应的故障排除

bash 复制代码
$ sudo tcpdump dst port 123

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
21:02:19.112502 IP test33.ntp > 199.30.140.74.ntp: NTPv4, Client, length 48
21:02:19.113888 IP 216.239.35.0.ntp > test33.ntp: NTPv4, Server, length 48
21:02:20.150347 IP test33.ntp > 216.239.35.0.ntp: NTPv4, Client, length 48
21:02:20.150991 IP 216.239.35.0.ntp > test33.ntp: NTPv4, Server, length 48

抓包 SNMP 的查询和响应

使用 onesixtyone 快速 SNMP 协议扫描器,然后在本地网络上测试 SNMP 服务并捕获 GetRequestGetResponse

模拟SNMP扫描:

bash 复制代码
$ onesixtyone 10.10.1.10 public

Scanning 1 hosts, 1 communities
10.10.1.10 [public] Linux test33 4.15.0-20-generic #21-Ubuntu SMP Tue Apr 24 06:16:15 UTC 2018 x86_64

抓包SNMP查询和扫描:

bash 复制代码
$ sudo tcpdump -n -s0  port 161 and udp

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlp58s0, link-type EN10MB (Ethernet), capture size 262144 bytes
23:39:13.725522 IP 10.10.1.159.36826 > 10.10.1.20.161:  GetRequest(28)  .1.3.6.1.2.1.1.1.0
23:39:13.728789 IP 10.10.1.20.161 > 10.10.1.159.36826:  GetResponse(109)  .1.3.6.1.2.1.1.1.0="Linux testmachine 4.15.0-20-generic #21-Ubuntu SMP Tue Apr 24 06:16:15 UTC 2018 x86_64"

抓包 FTP 的凭证和命令

bash 复制代码
$ sudo tcpdump -nn -v port ftp or ftp-data

滚动抓包文件

针对大流量或长时间抓包时,自动创建固定大小的新文件会很有帮助,一般使用参数 -W -G -C 来完成。

下面的示例中,文件 capture-(hour).pcap 将每 (-G) 3600 秒(1 小时)创建一次,这些文件将在第二天被覆盖。

因此,最终应该得到 capture-{1-24}.pcap,如果小时为 15,则新文件为 (/tmp/capture-15.pcap)。

bash 复制代码
$ tcpdump  -w /tmp/capture-%H.pcap -G 3600 -C 200

抓包 IPv6 流量

使用 ip6 过滤器捕获 IPv6 流量。

可以使用 proto 6proto 17 指定了 TCPUDP 协议。

bash 复制代码
tcpdump -nn ip6 proto 6

从先前保存的抓包文件中读取UDPIPv6 流量。

bash 复制代码
tcpdump -nr ipv6-test.pcap ip6 proto 17

检测网络流量中的端口扫描

bash 复制代码
$ tcpdump -nn

21:46:19.693601 IP 10.10.1.10.60460 > 10.10.1.199.5432: Flags [S], seq 116466344, win 29200, options [mss 1460,sackOK,TS val 3547090332 ecr 0,nop,wscale 7], length 0
21:46:19.693626 IP 10.10.1.10.35470 > 10.10.1.199.513: Flags [S], seq 3400074709, win 29200, options [mss 1460,sackOK,TS val 3547090332 ecr 0,nop,wscale 7], length 0
21:46:19.693762 IP 10.10.1.10.44244 > 10.10.1.199.389: Flags [S], seq 2214070267, win 29200, options [mss 1460,sackOK,TS val 3547090333 ecr 0,nop,wscale 7], length 0
21:46:19.693772 IP 10.10.1.199.389 > 10.10.1.10.44244: Flags [R.], seq 0, ack 2214070268, win 0, length 0
21:46:19.693783 IP 10.10.1.10.35172 > 10.10.1.199.1433: Flags [S], seq 2358257571, win 29200, options [mss 1460,sackOK,TS val 3547090333 ecr 0,nop,wscale 7], length 0
21:46:19.693826 IP 10.10.1.10.33022 > 10.10.1.199.49153: Flags [S], seq 2406028551, win 29200, options [mss 1460,sackOK,TS val 3547090333 ecr 0,nop,wscale 7], length 0
21:46:19.695567 IP 10.10.1.10.55130 > 10.10.1.199.49154: Flags [S], seq 3230403372, win 29200, options [mss 1460,sackOK,TS val 3547090334 ecr 0,nop,wscale 7], length 0
21:46:19.695590 IP 10.10.1.199.49154 > 10.10.1.10.55130: Flags [R.], seq 0, ack 3230403373, win 0, length 0
21:46:19.695608 IP 10.10.1.10.33460 > 10.10.1.199.49152: Flags [S], seq 3289070068, win 29200, options [mss 1460,sackOK,TS val 3547090335 ecr 0,nop,wscale 7], length 0
21:46:19.695622 IP 10.10.1.199.49152 > 10.10.1.10.33460: Flags [R.], seq 0, ack 3289070069, win 0, length 0
21:46:19.695637 IP 10.10.1.10.34940 > 10.10.1.199.1029: Flags [S], seq 140319147, win 29200, options [mss 1460,sackOK,TS val 3547090335 ecr 0,nop,wscale 7], length 0
21:46:19.695650 IP 10.10.1.199.1029 > 10.10.1.10.34940: Flags [R.], seq 0, ack 140319148, win 0, length 0
21:46:19.695664 IP 10.10.1.10.45648 > 10.10.1.199.5060: Flags [S], seq 2203629201, win 29200, options [mss 1460,sackOK,TS val 3547090335 ecr 0,nop,wscale 7], length 0
21:46:19.695775 IP 10.10.1.10.49028 > 10.10.1.199.2000: Flags [S], seq 635990431, win 29200, options [mss 1460,sackOK,TS val 3547090335 ecr 0,nop,wscale 7], length 0
21:46:19.695790 IP 10.10.1.199.2000 > 10.10.1.10.49028: Flags [R.], seq 0, ack 635990432, win 0, length 0

显示 Nmap NSE 脚本测试的示例过滤器

Nmap 机器上模拟NSE脚本:

bash 复制代码
$ nmap -p 80 --script=http-enum.nse targetip

在目标机器上抓包:

bash 复制代码
$ tcpdump -nn port 80 | grep "GET /"

GET /w3perl/ HTTP/1.1
GET /w-agora/ HTTP/1.1
GET /way-board/ HTTP/1.1
GET /web800fo/ HTTP/1.1
GET /webaccess/ HTTP/1.1
GET /webadmin/ HTTP/1.1
GET /webAdmin/ HTTP/1.1

抓包非本地主机上的开始和结束的数据包

通过选择 tcp-syntcp-fin 数据包,可以显示每个已建立的 TCP 会话,其中包含时间戳,但不包含数据。

bash 复制代码
$ tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net localnet'

抓包 DNS 请求和响应

比如下面的示例中可以看到对 Google 公共 DNS 的出站 DNS 请求和 A 记录(IP 地址)响应。

bash 复制代码
$ sudo tcpdump -i wlp58s0 -s0 port 53

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlp58s0, link-type EN10MB (Ethernet), capture size 262144 bytes
14:19:06.879799 IP test.53852 > google-public-dns-a.google.com.domain: 26977+ [1au] A? play.google.com. (44)
14:19:07.022618 IP google-public-dns-a.google.com.domain > test.53852: 26977 1/0/1 A 216.58.203.110 (60)

抓包 HTTP 数据包

仅抓包端口 80 上的 HTTP 流量,避免抓包 TCP 会话 (SYN / FIN / ACK)。

bash 复制代码
$ tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'

在 tcpdump 中抓包,在 Wireshark 中查看

一般方法是通过tcpdump抓包之后保存成文件,再将文件拷贝到Wireshark中查看。

不过,除此之外,还可以通过 SSH 连接将抓包的内容实时提供给 Wireshark

不要忘记 not port 22 ,加上这个就不会捕获 SSH 流量了。

bash 复制代码
$ ssh root@remotesystem 'tcpdump -s0 -c 1000 -nn -w - not port 22' | wireshark -k -i -

按数据包数量排名主机

列出一段时间内或数据包数量最多的通话者。

使用简单的命令行字段提取来获取 IP 地址 ,对出现的次数进行排序和计数。

用于排序和计数的流量与计数参数 -c 相关。

bash 复制代码
$ sudo tcpdump -nnn -t -c 200 | cut -f 1,2,3,4 -d '.' | sort | uniq -c | sort -nr | head -n 20

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
200 packets captured
261 packets received by filter
0 packets dropped by kernel
    108 IP 10.10.211.181
     91 IP 10.10.1.30
      1 IP 10.10.1.50

抓包 所有明文密码

下面的示例中,重点关注标准纯文本协议,并选择 grep 处理任何与用户或密码相关的内容。

通过 grep-B5 选项,只获取前 5 行(可以提供有关的密码的上下文、主机名、IP 地址、系统)。

bash 复制代码
$ sudo tcpdump port http or port ftp or port smtp or port imap or port pop3 or port telnet -l -A | egrep -i -B5 'pass=|pwd=|log=|login=|user=|username=|pw=|passw=|passwd=|password=|pass:|user:|username:|password:|login:|pass |user '

DHCP 示例

监视 DHCP 请求和回复, DHCP 请求在端口 67 上显示,回复在端口 68 上显示。

使用参数 -v 可以查看协议选项和其他详细信息。

bash 复制代码
$ sudo tcpdump -v -n port 67 or 68

tcpdump: listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
14:37:50.059662 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
    0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 00:0c:xx:xx:xx:d5, length 300, xid 0xc9779c2a, Flags [none]
	  Client-Ethernet-Address 00:0c:xx:xx:xx:d5
	  Vendor-rfc1048 Extensions
	    Magic Cookie 0x63825363
	    DHCP-Message Option 53, length 1: Request
	    Requested-IP Option 50, length 4: 10.10.1.163
	    Hostname Option 12, length 14: "test-ubuntu"
	    Parameter-Request Option 55, length 16:
	      Subnet-Mask, BR, Time-Zone, Default-Gateway
	      Domain-Name, Domain-Name-Server, Option 119, Hostname
	      Netbios-Name-Server, Netbios-Scope, MTU, Classless-Static-Route
	      NTP, Classless-Static-Route-Microsoft, Static-Route, Option 252
14:37:50.059667 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
    0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from 00:0c:xx:xx:xx:d5, length 300, xid 0xc9779c2a, Flags [none]
	  Client-Ethernet-Address 00:0c:xx:xx:xx:d5
	  Vendor-rfc1048 Extensions
	    Magic Cookie 0x63825363
	    DHCP-Message Option 53, length 1: Request
	    Requested-IP Option 50, length 4: 10.10.1.163
	    Hostname Option 12, length 14: "test-ubuntu"
	    Parameter-Request Option 55, length 16:
	      Subnet-Mask, BR, Time-Zone, Default-Gateway
	      Domain-Name, Domain-Name-Server, Option 119, Hostname
	      Netbios-Name-Server, Netbios-Scope, MTU, Classless-Static-Route
	      NTP, Classless-Static-Route-Microsoft, Static-Route, Option 252
14:37:50.060780 IP (tos 0x0, ttl 64, id 53564, offset 0, flags [none], proto UDP (17), length 339)
    10.10.1.1.67 > 10.10.1.163.68: BOOTP/DHCP, Reply, length 311, xid 0xc9779c2a, Flags [none]
	  Your-IP 10.10.1.163
	  Server-IP 10.10.1.1
	  Client-Ethernet-Address 00:0c:xx:xx:xx:d5
	  Vendor-rfc1048 Extensions
	    Magic Cookie 0x63825363
	    DHCP-Message Option 53, length 1: ACK
	    Server-ID Option 54, length 4: 10.10.1.1
	    Lease-Time Option 51, length 4: 86400
	    RN Option 58, length 4: 43200
	    RB Option 59, length 4: 75600
	    Subnet-Mask Option 1, length 4: 255.255.255.0
	    BR Option 28, length 4: 10.10.1.255
	    Domain-Name-Server Option 6, length 4: 10.10.1.1
	    Hostname Option 12, length 14: "test-ubuntu"
	    T252 Option 252, length 1: 10
	    Default-Gateway Option 3, length 4: 10.10.1.1

附录

本文翻译自:hackertarget.com/tcpdump-exa...

没有直译,根据自己的理解和实际操作做了一些调整。

相关推荐
扫地僧0094 分钟前
【中大厂面试题】腾讯 后端 校招 最新面试题
java·数据结构·后端·算法·面试·排序算法
东方醴歌2 小时前
VMware安装飞牛私有云fnOS并挂载小雅Alist实现异地远程访问
开发语言·后端·golang
她说彩礼65万2 小时前
Asp.NET Core WebApi 创建带鉴权机制的Api
后端·asp.net
bobz9652 小时前
qemu ovs tap down
后端
uhakadotcom2 小时前
简单理解 x402 支付协议
后端·架构·github
shepherd1262 小时前
从零搭建高可用Kafka集群与EFAK监控平台:全流程实战总结
分布式·后端·kafka
uhakadotcom2 小时前
了解Agent2Agent(A2A)协议:实现AI智能体间的无缝通信
后端·面试·github
麓殇⊙2 小时前
springboot--页面的国际化
java·spring boot·后端
java奋斗者2 小时前
新闻推荐系统(springboot+vue+mysql)含万字文档+运行说明文档
spring boot·后端·mysql