ToB企服应用市场:ToB评测及商务社交产业平台

标题: Tcpdump使用示例 [打印本页]

作者: 美丽的神话    时间: 2023-12-31 09:54
标题: Tcpdump使用示例
这里收集了一些实用的 tcpdump 使用示例,使用它们可提升您的网络故障排除和安全测试能力。
熟练掌握下面的 tcpdump 使用示例,可以帮助我们更好的了解自己的网络。
了解 tcpdump 是一项基本技能,不仅对于系统管理员、网络工程师或安全专业人员,
对于自己部署玩的一些服务器来说,也会派上用场。
基础知识

常用参数

下面的命令是使用 tcpdump 时常见的参数。
  1. $ sudo tcpdump -i eth0 -nn -s0 -v port 80
复制代码
如果只有一个网络适配器,不用指定也行。
这不仅对于查看 IP/端口号很方便,而且在抓包大量数据时也很方便,因为名称解析会减慢抓包速度。
如果您想抓包所有流量,或者从网络流量中提取二进制文件/文件,则需要此选项。
显示 ASCII 文本

-A参数使得输出中包含抓包的 ascii 字符串。
这样便于结合 grep 或其他命令解析输出。
另一个可以同时显示十六进制输出和 ascii 的参数是 -X。
  1. $ sudo tcpdump -A -s0 port 80
复制代码
根据协议抓包

比如,过滤 UDP 流量,可以指定udp,也可以指定使用协议17,这两个命令效果一样。
TCP 对应的协议是 6。
  1. $ sudo tcpdump -i eth0 udp
  2. $ sudo tcpdump -i eth0 proto 17
复制代码
根据 IP 抓包

使用 host 过滤器将同时抓包前往(目标)和来自()IP 地址的流量。
  1. $ sudo tcpdump -i eth0 host 10.10.1.1
复制代码
或者使用 src 或 dst 仅抓包单向流量。
  1. $ sudo tcpdump -i eth0 src 10.10.1.20
  2. $ sudo tcpdump -i eth0 dst 10.10.1.20
复制代码
抓包内容写入文件

将抓包文件写入磁盘,这样就可以用其它工具,比如 Wireshark 来分析。
  1. $ sudo tcpdump -i eth0 -s0 -w test.pcap
复制代码
行缓冲模式

指定缓冲模式,比如行缓冲(-l)或数据包缓冲(-C),可以让 tcpdump 的输出立即发送到管道命令,在故障排除时立即做出响应。
  1. $ sudo tcpdump -i eth0 -s0 -l port 80 | grep 'Server:'
复制代码
不指定缓冲模式,有可能会得不到预期的结果。
组合过滤器

在上面的示例中,可以使用使用下面的逻辑符号来组合不同的过滤器。
  1. and or &&
  2. or or ||
  3. not or !
复制代码
使用示例

Tcpdump命令参数很多,常常有多种方法可以实现同样的结果。
使用哪种方法取决于所需的输出以及线路上的流量。比如,在繁忙的千兆位链路上进行抓包可能会迫使您使用特定的低级数据包过滤器。
下面的示例中,将列举一些以最简单(因此最快)的方式获得结果的方法。
提取 HTTP 用户代理

从 HTTP 请求标头中提取 HTTP 用户代理。
  1. $ sudo tcpdump -nn -A -s1500 -l | grep "User-Agent:"
复制代码
通过使用 egrep 和多个匹配规则,可以从请求中获取用户代理和主机(或任何其他标头)。
  1. $ sudo tcpdump -nn -A -s1500 -l | egrep -i 'User-Agent:|Host:'
复制代码
仅捕获 HTTP GET 和 POST 数据包

仅指定与 GET 匹配的数据包。
  1. $ sudo tcpdump -s 0 -A -vv 'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420'
复制代码
只选择 POST 请求。
  1. $ sudo tcpdump -s 0 -A -vv 'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x504f5354'
复制代码
注意,使用此过滤器抓包的数据中可能不包含 POST 数据,因为POST 请求很可能会被拆分为多个 TCP 数据包。
上面的表达式中的十六进制是与 GET 和 POST 请求中的 ascii 对应的。
提取 HTTP 请求的 URL

从流量中解析主机和 HTTP 请求位置。
如果服务不在80 端口,则需要指定端口。
  1. $ sudo tcpdump -s 0 -v -n -l | egrep -i "POST /|GET /|Host:"
  2. tcpdump: listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  3.         POST /wp-login.php HTTP/1.1
  4.         Host: dev.example.com
  5.         GET /wp-login.php HTTP/1.1
  6.         Host: dev.example.com
  7.         GET /favicon.ico HTTP/1.1
  8.         Host: dev.example.com
  9.         GET / HTTP/1.1
  10.         Host: dev.example.com
复制代码
在 POST 请求中提取 HTTP 密码
  1. $ sudo tcpdump -s 0 -A -n -l | egrep -i "POST /|pwd=|passwd=|password=|Host:"
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  4. 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
  5. .....s..POST /wp-login.php HTTP/1.1
  6. Host: dev.example.com
  7. .....s..log=admin&pwd=notmypassword&wp-submit=Log+In&redirect_to=http%3A%2F%2Fdev.example.com%2Fwp-admin%2F&testcookie=1
复制代码
从服务器和客户端抓包 Cookie

通过搜索 Set-Cookie(来自服务器)和 Cookie(来自客户端)来抓包 cookie。
  1. $ sudo tcpdump -nn -A -s0 -l | egrep -i 'Set-Cookie|Host:|Cookie:'
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on wlp58s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  4. Host: dev.example.com
  5. 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 数据包
  1. $ sudo tcpdump -n icmp
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  4. 11:34:21.590380 IP 10.10.1.217 > 10.10.1.30: ICMP echo request, id 27948, seq 1, length 64
  5. 11:34:21.590434 IP 10.10.1.30 > 10.10.1.217: ICMP echo reply, id 27948, seq 1, length 64
  6. 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 包。
  1. $ sudo tcpdump 'icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply'
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  4. 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 电子邮件

可以提取电子邮件正文其他数据,下面的例子中仅解析电子邮件收件人
  1. $ sudo tcpdump -nn -l port 25 | grep -i 'MAIL FROM\|RCPT TO'
复制代码
NTP 的查询和响应的故障排除
  1. $ sudo tcpdump dst port 123
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
  4. 21:02:19.112502 IP test33.ntp > 199.30.140.74.ntp: NTPv4, Client, length 48
  5. 21:02:19.113888 IP 216.239.35.0.ntp > test33.ntp: NTPv4, Server, length 48
  6. 21:02:20.150347 IP test33.ntp > 216.239.35.0.ntp: NTPv4, Client, length 48
  7. 21:02:20.150991 IP 216.239.35.0.ntp > test33.ntp: NTPv4, Server, length 48
复制代码
抓包 SNMP 的查询和响应

使用 onesixtyone 快速 SNMP 协议扫描器,然后在本地网络上测试 SNMP 服务并捕获 GetRequest 和 GetResponse 。
模拟SNMP扫描:
  1. $ onesixtyone 10.10.1.10 public
  2. Scanning 1 hosts, 1 communities
  3. 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查询和扫描:
  1. $ sudo tcpdump -n -s0  port 161 and udp
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on wlp58s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  4. 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
  5. 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 的凭证和命令
  1. $ 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)。
  1. $ tcpdump  -w /tmp/capture-%H.pcap -G 3600 -C 200
复制代码
抓包 IPv6 流量

使用 ip6 过滤器捕获 IPv6 流量。
可以使用 proto 6 或 proto 17 指定了 TCP或UDP 协议。
  1. tcpdump -nn ip6 proto 6
复制代码
从先前保存的抓包文件中读取UDP 的 IPv6 流量。
  1. tcpdump -nr ipv6-test.pcap ip6 proto 17
复制代码
检测网络流量中的端口扫描
  1. $ tcpdump -nn
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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脚本:
  1. $ nmap -p 80 --script=http-enum.nse targetip
复制代码
在目标机器上抓包:
  1. $ tcpdump -nn port 80 | grep "GET /"
  2. GET /w3perl/ HTTP/1.1
  3. GET /w-agora/ HTTP/1.1
  4. GET /way-board/ HTTP/1.1
  5. GET /web800fo/ HTTP/1.1
  6. GET /webaccess/ HTTP/1.1
  7. GET /webadmin/ HTTP/1.1
  8. GET /webAdmin/ HTTP/1.1
复制代码
抓包非本地主机上的开始和结束的数据包

通过选择 tcp-syn 和 tcp-fin 数据包,可以显示每个已建立的 TCP 会话,其中包含时间戳,但不包含数据。
  1. $ tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net localnet'
复制代码
抓包 DNS 请求和响应

比如下面的示例中可以看到对 Google 公共 DNS 的出站 DNS 请求和 A 记录(IP 地址)响应。
  1. $ sudo tcpdump -i wlp58s0 -s0 port 53
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on wlp58s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  4. 14:19:06.879799 IP test.53852 > google-public-dns-a.google.com.domain: 26977+ [1au] A? play.google.com. (44)
  5. 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)。
  1. $ 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 流量了。
  1. $ ssh root@remotesystem 'tcpdump -s0 -c 1000 -nn -w - not port 22' | wireshark -k -i -
复制代码
按数据包数量排名主机

列出一段时间内或数据包数量最多的通话者。
使用简单的命令行字段提取来获取 IP 地址,对出现的次数进行排序和计数。
用于排序和计数的流量与计数参数 -c 相关。
  1. $ sudo tcpdump -nnn -t -c 200 | cut -f 1,2,3,4 -d '.' | sort | uniq -c | sort -nr | head -n 20
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  4. 200 packets captured
  5. 261 packets received by filter
  6. 0 packets dropped by kernel
  7.     108 IP 10.10.211.181
  8.      91 IP 10.10.1.30
  9.       1 IP 10.10.1.50
复制代码
抓包 所有明文密码

下面的示例中,重点关注标准纯文本协议,并选择 grep 处理任何与用户或密码相关的内容。
通过 grep 的 -B5 选项,只获取前 5 行(可以提供有关的密码的上下文、主机名、IP 地址、系统)。
  1. $ 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 可以查看协议选项和其他详细信息。
  1. $ sudo tcpdump -v -n port 67 or 68
  2. tcpdump: listening on enp7s0, link-type EN10MB (Ethernet), capture size 262144 bytes
  3. 14:37:50.059662 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
  4.     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]
  5.           Client-Ethernet-Address 00:0c:xx:xx:xx:d5
  6.           Vendor-rfc1048 Extensions
  7.             Magic Cookie 0x63825363
  8.             DHCP-Message Option 53, length 1: Request
  9.             Requested-IP Option 50, length 4: 10.10.1.163
  10.             Hostname Option 12, length 14: "test-ubuntu"
  11.             Parameter-Request Option 55, length 16:
  12.               Subnet-Mask, BR, Time-Zone, Default-Gateway
  13.               Domain-Name, Domain-Name-Server, Option 119, Hostname
  14.               Netbios-Name-Server, Netbios-Scope, MTU, Classless-Static-Route
  15.               NTP, Classless-Static-Route-Microsoft, Static-Route, Option 252
  16. 14:37:50.059667 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
  17.     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]
  18.           Client-Ethernet-Address 00:0c:xx:xx:xx:d5
  19.           Vendor-rfc1048 Extensions
  20.             Magic Cookie 0x63825363
  21.             DHCP-Message Option 53, length 1: Request
  22.             Requested-IP Option 50, length 4: 10.10.1.163
  23.             Hostname Option 12, length 14: "test-ubuntu"
  24.             Parameter-Request Option 55, length 16:
  25.               Subnet-Mask, BR, Time-Zone, Default-Gateway
  26.               Domain-Name, Domain-Name-Server, Option 119, Hostname
  27.               Netbios-Name-Server, Netbios-Scope, MTU, Classless-Static-Route
  28.               NTP, Classless-Static-Route-Microsoft, Static-Route, Option 252
  29. 14:37:50.060780 IP (tos 0x0, ttl 64, id 53564, offset 0, flags [none], proto UDP (17), length 339)
  30.     10.10.1.1.67 > 10.10.1.163.68: BOOTP/DHCP, Reply, length 311, xid 0xc9779c2a, Flags [none]
  31.           Your-IP 10.10.1.163
  32.           Server-IP 10.10.1.1
  33.           Client-Ethernet-Address 00:0c:xx:xx:xx:d5
  34.           Vendor-rfc1048 Extensions
  35.             Magic Cookie 0x63825363
  36.             DHCP-Message Option 53, length 1: ACK
  37.             Server-ID Option 54, length 4: 10.10.1.1
  38.             Lease-Time Option 51, length 4: 86400
  39.             RN Option 58, length 4: 43200
  40.             RB Option 59, length 4: 75600
  41.             Subnet-Mask Option 1, length 4: 255.255.255.0
  42.             BR Option 28, length 4: 10.10.1.255
  43.             Domain-Name-Server Option 6, length 4: 10.10.1.1
  44.             Hostname Option 12, length 14: "test-ubuntu"
  45.             T252 Option 252, length 1: 10
  46.             Default-Gateway Option 3, length 4: 10.10.1.1
复制代码
附录

本文翻译自:https://hackertarget.com/tcpdump-examples/
没有直译,根据自己的理解和实际操作做了一些调整。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4