这里收集了一些实用的 tcpdump 运用示例,运用它们可提高您的网络毛病排除和安全测验才能。
熟练掌握下面的 tcpdump 运用示例,能够协助我们更好的了解自己的网络。

了解 tcpdump 是一项基本技能,不只关于体系管理员、网络工程师或安全专业人员,
关于自己部署玩的一些服务器来说,也会派上用场。

基础知识

常用参数

下面的指令是运用 tcpdump 经常见的参数。

$ 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

$ sudo tcpdump -A -s0 port 80

根据协议抓包

比方,过滤 UDP 流量,能够指定udp,也能够指定运用协议17,这两个指令效果一样。
TCP 对应的协议是 6。

$ sudo tcpdump -i eth0 udp
$ sudo tcpdump -i eth0 proto 17

根据 IP 抓包

运用 host 过滤器将一起抓包前往(方针)和来自()IP 地址的流量。

$ sudo tcpdump -i eth0 host 10.10.1.1

或许运用 srcdst 仅抓包单向流量。

$ sudo tcpdump -i eth0 src 10.10.1.20
$ sudo tcpdump -i eth0 dst 10.10.1.20

抓包内容写入文件

将抓包文件写入磁盘,这样就能够用其它东西,比方 Wireshark 来剖析。

$ sudo tcpdump -i eth0 -s0 -w test.pcap

行缓冲模式

指定缓冲模式,比方行缓冲(-l)或数据包缓冲(-C),能够让 tcpdump 的输出当即发送到管道指令,在毛病排除时当即做出呼应。

$ sudo tcpdump -i eth0 -s0 -l port 80 | grep 'Server:'

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

组合过滤器

在上面的示例中,能够运用运用下面的逻辑符号来组合不同的过滤器。

and or &&
or or ||
not or !

运用示例

Tcpdump指令参数许多,常常有多种办法能够实现相同的成果。
运用哪种办法取决于所需的输出以及线路上的流量。比方,在繁忙的千兆位链路上进行抓包可能会迫使您运用特定的初级数据包过滤器。

下面的示例中,将列举一些以最简略(因此最快)的办法获得成果的办法。

提取 HTTP 用户署理

从 HTTP 恳求标头中提取 HTTP 用户署理。

$ sudo tcpdump -nn -A -s1500 -l | grep "User-Agent:"

经过运用 egrep 和多个匹配规矩,能够从恳求中获取用户署理和主机(或任何其他标头)。

$ sudo tcpdump -nn -A -s1500 -l | egrep -i 'User-Agent:|Host:'

仅捕获 HTTP GET 和 POST 数据包

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

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

只挑选 POST 恳求。

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

注意,运用此过滤器抓包的数据中可能不包括 POST 数据,由于POST 恳求很可能会被拆分为多个 TCP 数据包。

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

提取 HTTP 恳求的 URL

从流量中解析主机和 HTTP 恳求位置。
假如服务不在80 端口,则需求指定端口。

$ 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 暗码

$ 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

从服务器和客户端抓包 Cookie

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

$ 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 数据包

$ 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 包。

$ 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 电子邮件

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

$ sudo tcpdump -nn -l port 25 | grep -i 'MAIL FROM|RCPT TO'

NTP 的查询和呼应的毛病排除

$ 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扫描:

$ 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查询和扫描:

$ 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 的凭证和指令

$ 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)。

$ tcpdump  -w /tmp/capture-%H.pcap -G 3600 -C 200

抓包 IPv6 流量

运用 ip6 过滤器捕获 IPv6 流量。
能够运用 proto 6proto 17 指定了 TCPUDP 协议。

tcpdump -nn ip6 proto 6

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

tcpdump -nr ipv6-test.pcap ip6 proto 17

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

$ 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脚本:

$ nmap -p 80 --script=http-enum.nse targetip

在方针机器上抓包:

$ 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 会话,其间包括时刻戳,但不包括数据。

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

抓包 DNS 恳求和呼应

比方下面的示例中能够看到对 Google 公共 DNS 的出站 DNS 恳求和 A 记录(IP 地址)呼应。

$ 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)。

$ 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 流量了。

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

按数据包数量排名主机

列出一段时刻内或数据包数量最多的通话者。
运用简略的指令行字段提取来获取 IP 地址,对出现的次数进行排序和计数。
用于排序和计数的流量与计数参数 -c 相关。

$ 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 地址、体系)。

$ 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 能够检查协议选项和其他详细信息。

$ 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…
没有直译,根据自己的理解和实际操作做了一些调整。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。