学习黑客TTryHackMe — Tcpdump基础 | 网络安全入门(THM)

TryHackMe --- Tcpdump基础 | 网络安全入门(THM)

大家好!

TryHackMe刚刚宣布了
全新的网络安全101

学习路径,而且这次有大量赠品!这篇文章可能会帮到你,但为了便于理解,我把总结写得很简短。祝大家享受黑客技术探索之旅!

引言

学习网络协议时面临的主要挑战在于,我们没有机会看到协议 "对话" 的发生。所有的技术复杂性都隐藏在友好且优雅的用户界面背后。你在本地网络上访问资源时,从未看到过ARP查询。同样,在查阅网络书籍或检查网络流量捕获之前,你可能多年来一直在使用互联网服务,却从未见过一次三次握手。最好的学习辅助方法是捕获网络流量,并仔细研究各种协议;这有助于我们更好地理解网络的工作原理。

本章节介绍一些使用Tcpdump的基本命令行参数。Tcpdump工具及其libpcap库是用C和C++编写的,于20世纪80年代末或90年代初发布,适用于类Unix系统。因此,它们非常稳定,速度也很理想。libpcap库是当今各种其他网络工具的基础。此外,它还被移植到MS Windows系统,名为winpcap

学习目标

本实验环境旨在为你提供使用tcpdump所需的基础知识。具体而言,你将学习如何:

  • 捕获数据包并将其保存到文件中
  • 对捕获的数据包设置过滤器
  • 控制捕获的数据包的显示方式

基本数据包捕获

Tcpdump是一个用于捕获网络数据包的工具。捕获数据包的一些基本命令:

  • tcpdump -i INTERFACE: 在指定的网络接口上捕获数据包(例如,-i eth0 用于以太网,或 -i any 用于所有接口)。
  • tcpdump -w FILE: 将捕获的数据包保存到文件中,通常文件扩展名为 .pcap extension, for later analysis.
  • tcpdump -r FILE:从保存的文件中读取并显示数据包。
  • tcpdump -c COUNT: 将捕获限制为特定数量的数据包。
  • tcpdump -n / -nn: 避免将IP地址和端口解析为域名和协议名,从而使输出更快且更易于解读。

例子:

复制代码
user@TryHackMe$ sudo tcpdump -i ens5 -c 5 -n  
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode  
listening on ens5, link-type EN10MB (Ethernet), capture size 262144 bytes  
08:55:18.989213 IP 10.10.117.2.22 > 10.11.81.126.53378: Flags \[P.\], seq 2888580014:2888580210, ack 771262362, win 922, options \[nop,nop,TS val 3216251159 ecr 33295823\], length 196  
08:55:18.989446 IP 10.10.117.2.22 > 10.11.81.126.53378: Flags \[P.\], seq 196:424, ack 1, win 922, options \[nop,nop,TS val 3216251159 ecr 33295823\], length 228  
08:55:18.989576 IP 10.10.117.2.22 > 10.11.81.126.53378: Flags \[P.\], seq 424:620, ack 1, win 922, options \[nop,nop,TS val 3216251159 ecr 33295823\], length 196  
08:55:18.989839 IP 10.10.117.2.22 > 10.11.81.126.53378: Flags \[P.\], seq 620:816, ack 1, win 922, options \[nop,nop,TS val 3216251159 ecr 33295823\], length 196  
08:55:18.989958 IP 10.10.117.2.22 > 10.11.81.126.53378: Flags \[P.\], seq 816:1012, ack 1, win 922, options \[nop,nop,TS val 3216251159 ecr 33295823\], length 196  
5 packets captured  
6 packets received by filter  
0 packets dropped by kernel

考虑以下示例:

  • tcpdump -i eth0 -c 50 -v 通过监听有线以太网接口eth0捕获并显示50个数据包,并详细显示这些数据包。
  • tcpdump -i wlo1 -w data.pcap 通过监听wlo1接口(即无线网卡接口)捕获数据包,并将数据包写入data.pcap文件。它会持续运行,直到用户按下CTRL - C中断捕获。
  • tcpdump -i any -nn 在所有接口上捕获数据包,并在屏幕上显示,不进行域名或协议解析。

Answer the questions below

What option can you add to your command to display addresses only in numeric format?

Answer: -n

过滤表达式

要根据主机、端口或协议等条件过滤数据包:*
主机过滤tcpdump host HOSTNAME 捕获涉及特定主机的数据包。使用 src host HOSTdst host HOST 仅过滤来自某个源或发往某个目的地的数据包。

复制代码
user@TryHackMe$ sudo tcpdump host example.com -w http.pcap  
tcpdump: verbose output suppressed, use -v\[v\]... for full protocol decode  
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes  
16:49:02.482295 IP 192.168.139.132.49480 \> 93.184.215.14.http: Flags \[S\], seq 3330895816, win 32120, options \[mss 1460,sackOK,TS val 621343956 ecr 0,nop,wscale 7\], length 0  
16:49:02.635087 IP 93.184.215.14.http \> 192.168.139.132.49480: Flags \[S.\], seq 2231582859, ack 3330895817, win 64240, options \[mss 1460\], length 0  
16:49:02.635125 IP 192.168.139.132.49480 \> 93.184.215.14.http: Flags \[.\], ack 1, win 32120, length 0  
16:49:02.635491 IP 192.168.139.132.49480 \> 93.184.215.14.http: Flags \[P.\], seq 1:131, ack 1, win 32120, length 130: HTTP: GET / HTTP/1.1  
16:49:02.635580 IP 93.184.215.14.http \> 192.168.139.132.49480: Flags \[.\], ack 131, win 64240, length 0  
\[...\]  
^C  
13 packets captured  
25 packets received by filter  
0 packets dropped by kernel
  • 端口过滤 : tcpdump port PORT_NUMBER 在特定端口捕获数据包。使用 src port(源端口)或 dst port(目的端口)进行更精细的过滤。

    user@TryHackMe$ sudo tcpdump -i ens5 port 53 -n

    sudo\] password for strategos: tcpdump: verbose output suppressed, use -v\[v\]... for full protocol decode listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes 17:26:33.591670 IP 192.168.139.132.47902 \> 192.168.139.2.53: 47108+ A? example.org. (29) 17:26:33.591717 IP 192.168.139.132.47902 \> 192.168.139.2.53: 5+ AAAA? example.org. (29) 17:26:33.593324 IP 192.168.139.2.53 \> 192.168.139.132.47902: 47108 1/0/0 A 93.184.215.14 (45) 17:26:33.593325 IP 192.168.139.2.53 \> 192.168.139.132.47902: 5 1/0/0 AAAA 2606:2800:21f:cb07:6820:80da:af6b:8b2c (57) \[...

    ^C
    12 packets captured
    12 packets received by filter
    0 packets dropped by kernel

  • 协议过滤 : tcpdump PROTOCOL 通过协议(如 tcpudpicmp 等)过滤数据包。

    user@TryHackMe$ sudo tcpdump -i ens5 icmp -n
    tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
    listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
    18:11:00.624681 IP 192.168.139.132 > 93.184.215.14: ICMP echo request, id 47038, seq 1, length 64
    18:11:00.781482 IP 93.184.215.14 > 192.168.139.132: ICMP echo reply, id 47038, seq 1, length 64
    18:11:04.168792 IP 192.168.139.2 > 192.168.139.132: ICMP time exceeded in-transit, length 68
    18:11:04.168815 IP 192.168.139.2 > 192.168.139.132: ICMP time exceeded in-transit, length 68

    ...

    18:11:14.857188 IP 93.184.215.14 > 192.168.139.132: ICMP 93.184.215.14 udp port 33495 unreachable, length 68
    ^C
    52 packets captured
    52 packets received by filter
    0 packets dropped by kernel

逻辑运算符 in Tcpdump:

  • and (tcpdump host 1.1.1.1 and tcp): 捕获同时满足两个条件的数据包。
  • or (tcpdump udp or icmp): 捕获满足任一条件的数据包。
  • not (tcpdump not tcp): 捕获除符合该条件之外的所有数据包。

考虑以下示例:

  • tcpdump -i any tcp port 22 监听所有网络接口,并捕获去往或来自 端口22tcp 数据包,即SSH流量。
  • tcpdump -i wlo1 udp port 123 监听无线网卡,并过滤发往 端口123(网络时间协议 (NTP))的 udp 流量。
  • tcpdump -i eth0 host example.com and tcp port 443 -w https.pcap 命令将监听有线以太网接口 eth0,并过滤与 example.com 使用 tcp 协议且通过 端口443 进行交互的流量。换句话说,此命令正在过滤与 example.com 相关的HTTPS流量。

对于此任务中的问题,我们将从 traffic.pcap 文件中读取捕获的数据包。如前所述,我们使用 -r FILE 从数据包捕获文件中读取。为了测试这一点,尝试执行 tcpdump -r traffic.pcap -c 5 -n;它应该显示文件中的前五个数据包,且不会查询IP地址。

请记住,你可以通过将输出通过 wc 命令进行管道传输来统计行数。在下面的终端中,我们可以看到有910个数据包的源IP地址设置为 192.168.124.1。请注意,我们添加 -n 选项是为了避免在尝试解析IP地址时出现不必要的延迟。在下面的示例中,我们没有使用 sudo,因为从数据包捕获文件读取不需要 root 权限。

复制代码
user@TryHackMe$ tcpdump -r traffic.pcap src host 192.168.124.1 -n | wc  
reading from file traffic.pcap, link-type EN10MB (Ethernet)  
    910   17415  140616

Answer the questions below

How many packets in traffic.pcap use the ICMP protocol?

Answer: 26

复制代码
user@ip-10-10-218-40:~$ sudo tcpdump -r traffic.pcap icmp -n | wc  
reading from file traffic.pcap, link-type EN10MB (Ethernet)  
     26     358    2722

What is the IP address of the host that asked for the MAC address of 192.168.124.137?

Answer: 192.168.124.148

复制代码
user@ip-10-10-218-40:~$ sudo tcpdump -r traffic.pcap arp and host 192.168.124.137  
reading from file traffic.pcap, link-type EN10MB (Ethernet)  
07:18:29.940761 ARP, Request who-has ip-192-168-124-137.eu-west-1.compute.internal tell ip-192-168-124-148.eu-west-1.compute.internal, length 28  
07:18:29.940776 ARP, Reply ip-192-168-124-137.eu-west-1.compute.internal is-at 52:54:00:23:60:2b (oui Unknown), length 28

What hostname (subdomain) appears in the first DNS query?

Answer: mirrors.rockylinux.org

复制代码
user@ip-10-10-218-40:~$ sudo tcpdump -r traffic.pcap port 53 -A  
reading from file traffic.pcap, link-type EN10MB (Ethernet)  
07:18:24.058626 IP ip-192-168-124-137.eu-west-1.compute.internal.33672 > ip-192-168-124-1.eu-west-1.compute.internal.domain: 39913+ A? mirrors.rockylinux.org. (40)  
E..D..@.@.'...|...|....5.0z..............mirrors  
rockylinux.org.....

进阶过滤

进阶过滤使用条件来满足更特定的需求:

  • 数据包长度 : 使用 greater LENGTH(长度大于) 或 less LENGTH(长度小于)来捕获长度大于或小于指定长度的数据包。
  • 二元操作符: 可以使用协议报头中的二元操作符来定义过滤器。示例:
  • tcp[tcpflags] == tcp-syn: 捕获仅设置了SYN标志的数据包。
  • tcp[tcpflags] & tcp-ack != 0: 捕获至少设置了ACK标志的数据包。

在继续之前,值得探讨一下二元运算。二元运算作用于比特位,即0和1。一个运算接受一个或两个比特位,并返回一个比特位。让我们更深入地解释一下,并考虑以下三种二元运算:&|!

&(与)接受两个比特位,除非两个输入均为1,否则返回0,如下表所示。

|(或)接受两个比特位,除非两个输入均为0,否则返回1。如下表所示。

!(非)对一个比特位取反;输入1返回0,输入0返回1,如下表所示。

报头字节

本节的目的是能够根据报头字节的内容过滤数据包。考虑以下协议:ARP、以太网、ICMP、IP、TCP和UDP。这些只是我们研究过的一些网络协议。我们如何告诉Tcpdump根据协议报头字节的内容过滤数据包呢?(我们不会深入探讨每个协议的报头细节,因为这超出了本章节的范围;相反,我们将专注于TCP标志。)

使用pcap过滤器,Tcpdump允许你使用以下语法proto[expr:size]来引用报头中任意字节的内容,其中:

  • proto指的是协议。例如,arpethericmpipip6tcpudp分别指地址解析协议(ARP)、以太网、网际控制报文协议(ICMP)、互联网协议版本4(IPv4)、互联网协议版本6(IPv6)、传输控制协议(TCP)和用户数据报协议(UDP)。
  • expr表示字节偏移量,其中0 指第一个字节。
  • size表示我们关注的字节数,可以是1、2或4 。它是可选的,默认值为1。

为了更好地理解这一点,请参考pcap过滤器手册页中的以下两个示例(如果觉得难懂也不用担心):

  • ether[0] & 1 != 0 会取以太网头部的第一个字节与十进制数字1(即二进制的 0000 0001),并进行 &(按位与二进制运算)。如果结果不等于数字0(即二进制的 0000 0000),则返回真。此过滤器的作用是显示发送到多播地址的数据包。多播以太网地址是一种特殊地址,用于标识一组旨在接收相同数据的设备。
  • ip[0] & 0xf != 5 取IP头部的第一个字节,并将其与十六进制数F(即二进制的 0000 1111)进行比较。如果结果不等于(十进制)数字5(即二进制的 0000 0101),则返回真。此过滤器的作用是捕获所有带有选项的IP数据包。

如果你觉得上述两个例子很复杂,也不用担心。我们给出这些例子是为了让你了解利用这个能实现什么;不过,要完成这项任务,并不需要完全理解上述例子。相反,我们将专注于根据设置的TCP标志过滤TCP数据包。

你可以使用

"tcp[tcpflags]"

来引用TCP标志字段。以下TCP标志可用于比较:

  • tcp-syn TCP SYN (Synchronize)
  • tcp-ack TCP ACK (Acknowledge)
  • tcp-fin TCP FIN (Finish)
  • tcp-rst TCP RST (Reset)
  • tcp-push TCP Push

基于上述内容,我们可以编写:

  • tcpdump "tcp[tcpflags] == tcp-syn" 用于捕获仅设置了SYN(同步)标志,而所有其他标志均未设置的TCP数据包。
  • tcpdump "tcp[tcpflags] & tcp-syn != 0" 用于捕获至少设置了SYN(同步)标志的TCP数据包。
  • tcpdump "tcp[tcpflags] & (tcp-syn|tcp-ack) != 0" 用于捕获至少 设置了SYN(同步)ACK(确认)标志的TCP数据包。

你可以根据自己的需求编写自定义过滤器。

Answer the questions below

How many packets have only the TCP Reset (RST) flag set?

Answer: 57

复制代码
user@ip-10-10-218-40:~$ sudo tcpdump -r traffic.pcap 'tcp\[tcpflags\] == tcp-rst' | wc -l  
reading from file traffic.pcap, link-type EN10MB (Ethernet)  
57

What is the IP address of the host that sent packets larger than 15000 bytes?

Answer: 185.117.80.53

复制代码
user@ip-10-10-218-40:~$ sudo tcpdump -r traffic.pcap 'greater 15000' -n  
reading from file traffic.pcap, link-type EN10MB (Ethernet)  
07:18:24.967023 IP 185.117.80.53.80 \> 192.168.124.137.60518: Flags \[.\], seq 2140876081:2140896901, ack 741991605, win 235, options \[nop,nop,TS val 2226566282 ecr 3054280184\], length 20820: H  
TTP  
07:18:25.778012 IP 185.117.80.53.80 \> 192.168.124.137.60518: Flags \[.\], seq 1293616:1308884, ack 1, win 235, options \[nop,nop,TS val 2226567095 ecr 3054280994\], length 15268: HTTP  
07:18:25.861724 IP 185.117.80.53.80 \> 192.168.124.137.60518: Flags \[.\], seq 1378284:1397716, ack 1, win 235, options \[nop,nop,TS val 2226567176 ecr 3054281078\], length 19432: HTTP  
07:18:26.457422 IP 185.117.80.53.80 \> 192.168.124.137.60518: Flags \[.\], seq 2356824:2373480, ack 1, win 235, options \[nop,nop,TS val 2226567777 ecr 3054281682\], length 16656: HTTP  
07:18:26.746414 IP 185.117.80.53.80 \> 192.168.124.137.60492: Flags \[.\], seq 964376218:964395650, ack 1034282473, win 235, options \[nop,nop,TS val 2226568063 ecr 3054281963\], length 19432: HT  
TP  
07:18:26.978560 IP 185.117.80.53.80 \> 192.168.124.137.60502: Flags \[.\], seq 3786023752:3786039020, ack 3169565691, win 235, options \[nop,nop,TS val 2226568298 ecr 3054282201\], length 15268:   
HTTP  
07:18:27.195761 IP 185.117.80.53.80 \> 192.168.124.137.60492: Flags \[.\], seq 570468:589900, ack 1, win 235, options \[nop,nop,TS val 2226568513 ecr 3054282418\], length 19432: HTTP  
07:18:27.391916 IP 185.117.80.53.80 \> 192.168.124.137.60492: Flags \[.\], seq 831412:848068, ack 1, win 235, options \[nop,nop,TS val 2226568707 ecr 3054282611\], length 16656: HTTP

显示数据包

定制Tcpdump显示捕获数据包的方式,可以使分析更加简单明了:

  • tcpdump -q: 快速输出简要信息。
  • tcpdump -e: 显示MAC地址。
  • tcpdump -A: 以ASCII格式显示数据包内容。
  • tcpdump -xx: 以十六进制格式显示数据包数据。
  • tcpdump -X: 以十六进制和ASCII格式显示。

例子:

复制代码
user@TryHackMe$ tcpdump -r TwoPackets.pcap -X  
reading from file TwoPackets.pcap, link-type EN10MB (Ethernet), snapshot length 262144  
18:59:59.979771 IP 104.18.12.149.https > g5000.45248: Flags \[P.\], seq 2695955324:2695955349, ack 2856007037, win 16, options \[nop,nop,TS val 412758285 ecr 3959057198\], length 25  
        0x0000:  4500 004d fbd8 4000 3506 d229 6812 0c95  E..M..@.5..)h...  
        0x0010:  c0a8 4259 01bb b0c0 a0b1 037c aa3b 357d  ..BY.......|.;5}  
        0x0020:  8018 0010 f905 0000 0101 080a 189a 310d  ..............1.  
        0x0030:  ebfa 6b2e 1703 0300 146a 8f33 1832 e6a2  ..k......j.3.2..  
        0x0040:  fb99 eb26 3961 dad4 1611 152d 4c         ...&9a.....-L  
18:59:59.980574 IP g5000.45248 > 104.18.12.149.https: Flags \[P.\], seq 1:30, ack 25, win 2175, options \[nop,nop,TS val 3959057384 ecr 412758285\], length 29  
        0x0000:  4500 0051 6ca8 4000 4006 5656 c0a8 4259  E..Ql.@.@.VV..BY  
        0x0010:  6812 0c95 b0c0 01bb aa3b 357d a0b1 0395  h........;5}....  
        0x0020:  8018 087f 17e0 0000 0101 080a ebfa 6be8  ..............k.  
        0x0030:  189a 310d 1703 0300 18f4 31fa 798d 2656  ..1.......1.y.&V  
        0x0040:  433c 2389 5f4a 24c2 fa7a 1496 8444 238e  C<#.\_J$..z...D#.  
        0x0050:  60

Answer the questions below

What is the MAC address of the host that sent an ARP request?

Answer: 52:54:00:7c:d3:5b

复制代码
user@ip-10-10-218-40:~$ sudo tcpdump -r traffic.pcap arp -e  
reading from file traffic.pcap, link-type EN10MB (Ethernet)  
07:18:29.940761 52:54:00:7c:d3:5b (oui Unknown) > Broadcast, ethertype ARP (0x0806), length 42: Request who-has ip-192-168-124-137.eu-west-1.compute.internal tell ip-192-168-124-148.eu-west-  
1.compute.internal, length 28  
07:18:29.940776 52:54:00:23:60:2b (oui Unknown) > 52:54:00:7c:d3:5b (oui Unknown), ethertype ARP (0x0806), length 42: Reply ip-192-168-124-137.eu-west-1.compute.internal is-at 52:54:00:23:60  
:2b (oui Unknown), length 28
相关推荐
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码5 天前
嵌入式学习路线
学习
毛小茛5 天前
计算机系统概论——校验码
学习
babe小鑫5 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms5 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下5 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。5 天前
2026.2.25监控学习
学习
im_AMBER5 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J5 天前
从“Hello World“ 开始 C++
c语言·c++·学习