想玩明白网络抓包王者技能——Tcpdump,看完这篇直接封神!

想玩明白网络抓包王者技能——Tcpdump,看完这篇直接封神!号主 老杨丨 11 年资深网络工程师 更多网工提升干货 请关注公众号 网络工程师俱乐部上午好 我的网工朋友 在日常网络排障中 如果你想要 秒杀 问题 抓包几乎是必备技能 tcpdump 则是耳熟能详的抓包神奇

欢迎大家来到IT世界,在知识的湖畔探索吧!

号主:老杨丨11年资深网络工程师,更多网工提升干货,请关注公众号:网络工程师俱乐部

上午好,我的网工朋友。

在日常网络排障中,如果你想要“秒杀”问题,抓包几乎是必备技能。

tcpdump ,则是耳熟能详的抓包神奇。

今天收集了一些实用的 tcpdump 使用示例,使用它们可提升你的网络故障排除和安全测试能力。
熟练掌握下面的 tcpdump 使用示例,可以帮助你更好的了解自己的网络。

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

今日文章阅读福利:《 tcpdump离线安装》

需要tcpdump离线安装的朋友,私信我,发送暗号“离线”(建议复制一下直接发哈),限时获取资源哈。

01 基础知识

01常用参数

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

$ sudo tcpdump -i eth0 -nn -s0 -v port 80 

欢迎大家来到IT世界,在知识的湖畔探索吧!

  • -i:进行抓包的接口,通常是以太网卡或无线适配器,但也可能是 vlan 或其它东西。
  • 如果只有一个网络适配器,不用指定也行。
  • -nn :单个 (n) 不会解析主机名。两个 (nn) 不会解析主机名或端口。
  • 这不仅对于查看 IP/端口号很方便,而且在抓包大量数据时也很方便,因为名称解析会减慢抓包速度。
  • -s0:抓包大小。 -s0 会将大小设置为无限制 。
  • 如果您想抓包所有流量,或者从网络流量中提取二进制文件/文件,则需要此选项。
  • -v:详细,使用 (-v) 或 (-vv) 会增加输出中显示更详细信息,通常会显示更多协议特定的信息。
  • port 80 :端口过滤器,这里设置的是抓包端口 80 上的流量。

02显示 ASCII 文本

-A参数使得输出中包含抓包的 ascii 字符串。
这样便于结合 grep 或其他命令解析输出。
另一个可以同时显示十六进制输出和 ascii 的参数是 -X。

欢迎大家来到IT世界,在知识的湖畔探索吧!$ sudo tcpdump -A -s0 port 80 

03根据协议抓包

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

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

04根据 IP 抓包

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

欢迎大家来到IT世界,在知识的湖畔探索吧!$ sudo tcpdump -i eth0 host 10.10.1.1 

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

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

05抓包内容写入文件

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

欢迎大家来到IT世界,在知识的湖畔探索吧!$ sudo tcpdump -i eth0 -s0 -w test.pcap 

06行缓冲模式

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

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

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

07组合过滤器

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

欢迎大家来到IT世界,在知识的湖畔探索吧!and or && or or || not or ! 

02 使用示例

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

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

01提取 HTTP 用户代理

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

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

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

欢迎大家来到IT世界,在知识的湖畔探索吧!$ sudo tcpdump -nn -A -s1500 -l | egrep -i 'User-Agent:|Host:' 

02仅捕获 HTTP GET 和 POST 数据包

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

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

只选择 POST 请求。

欢迎大家来到IT世界,在知识的湖畔探索吧!$ sudo tcpdump -s 0 -A -vv 'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x504f5354' 

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

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

03提取 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  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

04在 POST 请求中提取 HTTP 密码

欢迎大家来到IT世界,在知识的湖畔探索吧!$ 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  bytes 11:25:54. IP 10.10.1.30.39224 > 10.10.1.125.80: Flags [P.], seq :, ack , win 704, options [nop,nop,TS val  ecr ], 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 

05从服务器和客户端抓包 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  bytes Host: dev.example.com Cookie: wordpress_86be02xxxxxxxxxxxxxxxxxxxc43=admin%7C152xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxfb3e15c744fdd6; _ga=GA1.2.; _gid=GA1.2.9426; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_86be53fc43=admin%7Ctesttesttesttestab7a61e; wp-settings-time-1= 

06抓包所有 ICMP数据包

欢迎大家来到IT世界,在知识的湖畔探索吧!$ 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  bytes 11:34:21. IP 10.10.1.217 > 10.10.1.30: ICMP echo request, id 27948, seq 1, length 64 11:34:21. IP 10.10.1.30 > 10.10.1.217: ICMP echo reply, id 27948, seq 1, length 64 11:34:27. IP 10.10.1.159 > 10.10.1.1: ICMP 10.10.1.189 udp port 59619 unreachable, length 115 

07非 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  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 

08抓包 SMTP/POP3 电子邮件

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

欢迎大家来到IT世界,在知识的湖畔探索吧!$ sudo tcpdump -nn -l port 25 | grep -i 'MAIL FROM\|RCPT TO' 

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

$ 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. IP test33.ntp > 199.30.140.74.ntp: NTPv4, Client, length 48 21:02:19. IP 216.239.35.0.ntp > test33.ntp: NTPv4, Server, length 48 21:02:20. IP test33.ntp > 216.239.35.0.ntp: NTPv4, Client, length 48 21:02:20. IP 216.239.35.0.ntp > test33.ntp: NTPv4, Server, length 48 

10抓包 SNMP 的查询和响应

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

模拟SNMP扫描:

欢迎大家来到IT世界,在知识的湖畔探索吧!$ 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  bytes 23:39:13. 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. 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" 

11抓包 FTP 的凭证和命令

欢迎大家来到IT世界,在知识的湖畔探索吧!$ sudo tcpdump -nn -v port ftp or ftp-data 

12滚动抓包文件

针对大流量或长时间抓包时,自动创建固定大小的新文件会很有帮助,一般使用参数 -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 

13抓包 IPv6 流量

使用 ip6 过滤器捕获 IPv6 流量。
可以使用 proto 6 或 proto 17 指定了 TCP或UDP 协议。

欢迎大家来到IT世界,在知识的湖畔探索吧!tcpdump -nn ip6 proto 6 

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

tcpdump -nr ipv6-test.pcap ip6 proto 17 

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

欢迎大家来到IT世界,在知识的湖畔探索吧!$ tcpdump -nn 21:46:19. IP 10.10.1.10.60460 > 10.10.1.199.5432: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.10.35470 > 10.10.1.199.513: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.10.44244 > 10.10.1.199.389: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.199.389 > 10.10.1.10.44244: Flags [R.], seq 0, ack , win 0, length 0 21:46:19. IP 10.10.1.10.35172 > 10.10.1.199.1433: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.10.33022 > 10.10.1.199.49153: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.10.55130 > 10.10.1.199.49154: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.199.49154 > 10.10.1.10.55130: Flags [R.], seq 0, ack , win 0, length 0 21:46:19. IP 10.10.1.10.33460 > 10.10.1.199.49152: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.199.49152 > 10.10.1.10.33460: Flags [R.], seq 0, ack , win 0, length 0 21:46:19. IP 10.10.1.10.34940 > 10.10.1.199.1029: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.199.1029 > 10.10.1.10.34940: Flags [R.], seq 0, ack , win 0, length 0 21:46:19. IP 10.10.1.10.45648 > 10.10.1.199.5060: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.10.49028 > 10.10.1.199.2000: Flags [S], seq , win 29200, options [mss 1460,sackOK,TS val  ecr 0,nop,wscale 7], length 0 21:46:19. IP 10.10.1.199.2000 > 10.10.1.10.49028: Flags [R.], seq 0, ack , win 0, length 0 

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

在 Nmap 机器上模拟NSE脚本:

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

在目标机器上抓包:

欢迎大家来到IT世界,在知识的湖畔探索吧!$ 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 

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

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

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

17抓包 DNS 请求和响应

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

欢迎大家来到IT世界,在知识的湖畔探索吧!$ 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  bytes 14:19:06. 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) 

18抓包 HTTP 数据包

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

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

19在 tcpdump 中抓包,在 Wireshark 中查看

一般方法是通过tcpdump抓包之后保存成文件,再将文件拷贝到Wireshark中查看。
不过,除此之外,还可以通过 SSH 连接将抓包的内容实时提供给 Wireshark。
不要忘记 not port 22 ,加上这个就不会捕获 SSH 流量了。

欢迎大家来到IT世界,在知识的湖畔探索吧!$ ssh root@remotesystem 'tcpdump -s0 -c 1000 -nn -w - not port 22' | wireshark -k -i - 

20按数据包数量排名主机

列出一段时间内或数据包数量最多的通话者。
使用简单的命令行字段提取来获取 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  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 

21抓包 所有明文密码

下面的示例中,重点关注标准纯文本协议,并选择 grep 处理任何与用户或密码相关的内容。
通过 grep 的 -B5 选项,只获取前 5 行(可以提供有关的密码的上下文、主机名、IP 地址、系统)。

欢迎大家来到IT世界,在知识的湖畔探索吧!$ 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 ' 

22DHCP 示例

监视 DHCP 请求和回复, DHCP 请求在端口 67 上显示,回复在端口 68 上显示。
使用参数 -v 可以查看协议选项和其他详细信息。

$ sudo tcpdump -v -n port 67 or 68 tcpdump: listening on enp7s0, link-type EN10MB (Ethernet), capture size  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 0x 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 0x 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 0x 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 

整理:老杨丨11年资深网络工程师,更多网工提升干货,请关注公众号:网络工程师俱乐部

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/122002.html

(0)
上一篇 1天前
下一篇 1天前

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信