tcpdump 是一个命令行应用程序,可让你捕获和分析通过系统的网络流量。它通常用于帮助解决网络问题以及安全工具。
tcpdump 是一个强大且多功能的工具,包括许多选项和过滤器,可用于各种情况。由于它是一个命令行工具,因此非常适合在没有 GUI 的远程服务器或设备中运行,以收集可以稍后分析的数据。它也可以在后台启动或使用 cron 等工具作为预定作业启动。
Tcpdump 包含在多个 Linux 发行版中,因此你可能已经安装了它。使用以下命令检查你的系统上是否安装了 tcpdump:
$ which tcpdump
/usr/sbin/tcpdump
如果未安装 tcpdump,你可以安装它,但使用你的发行版的包管理器。例如,在 CentOS 或 Red Hat Enterprise Linux 上,如下所示:
$ sudo dnf install -y tcpdump
Tcpdump 需要
libpcap
,这是一个用于网络数据包捕获的库。如果未安装,它将自动添加为依赖项。你已准备好开始捕获一些数据包。
要捕获数据包以进行故障排除或分析,tcpdump 需要提升权限,因此在以下示例中,大多数命令都以
sudo
.首先,使用命令
tcpdump --list-interfaces
(或
-D
简称)查看哪些接口可用于捕获:
$ sudo tcpdump -D
eth0
virbr0
eth1
any (Pseudo-device that captures on all interfaces)
lo [Loopback]
在上面的示例中,你可以看到我的机器中可用的所有接口。特殊接口
any
允许在任何活动接口中进行捕获。让我们用它来开始捕获一些数据包。通过运行以下命令捕获任何接口中的所有数据包:
$ sudo tcpdump --interface any
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
09:56:293641 IP rhellocaldomain.ssh > 1156322: Flags [P.], seq 3770820720:3770820916, ack 3503648727, win 309, options [nop,nop,TS val 76577898 ecr 510770929], length 196
09:56:293794 IP 1156322 > rhellocaldomain.ssh: Flags [.], ack 196, win 391, options [nop,nop,TS val 510771017 ecr 76577898], length 0
09:56:295058 IP rhel59883 > gateway.domain: 2486\+ PTR? 11in-addr.arpa. (43)
09:56:310225 IP gateway.domain > rhel59883: 2486 NXDomain* 0/1/0 (102)
09:56:312482 IP rhel49685 > gateway.domain: 34242\+ PTR? 11in-addr.arpa. (44)
09:56:322425 IP gateway.domain > rhel49685: 34242 NXDomain* 0/1/0 (103)
09:56:323164 IP rhel56631 > gateway.domain: 29904\+ PTR? 111in-addr.arpa. (44)
09:56:323342 IP rhellocaldomain.ssh > 1156322: Flags [P.], seq 196:584, ack 1, win 309, options [nop,nop,TS val 76577928 ecr 510771017], length 388
09:56:323563 IP 1156322 > rhellocaldomain.ssh: Flags [.], ack 584, win 411, options [nop,nop,TS val 510771047 ecr 76577928], length 0
09:56:335569 IP gateway.domain > rhel56631: 29904 NXDomain* 0/1/0 (103)
09:56:336429 IP rhel44007 > gateway.domain: 61677\+ PTR? 111in-addr.arpa. (45)
09:56:336655 IP gateway.domain > rhel44007: 61677* 1/0/0 PTR rhel (65)
09:56:337177 IP rhellocaldomain.ssh > 1156322: Flags [P.], seq 584:1644, ack 1, win 309, options [nop,nop,TS val 76577942 ecr 510771047], length 1060
---- SKIPPING LONG OUTPUT -----
09:56:342939 IP 1156322 > rhellocaldomain.ssh: Flags [.], ack 1752016, win 1444, options [nop,nop,TS val 510772067 ecr 76578948], length 0
^C
9003 packets captured
9010 packets received by filter
7 packets dropped by kernel
$
Tcpdump 继续捕获数据包,直到它收到中断信号。你可以按 中断捕获
Ctrl+C
。正如你在此示例中看到的,tcpdump
捕获了 9,000 多个数据包。在这种情况下,由于我使用 连接到该服务器ssh
,tcpdump 捕获了所有这些数据包。要限制捕获和停止的数据包数量tcpdump
,请使用-c
(用于计数
)选项:
$ sudo tcpdump -i any -c 5
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
11:21:242740 IP rhellocaldomain.ssh > 1156322: Flags [P.], seq 3772575680:3772575876, ack 3503651743, win 309, options [nop,nop,TS val 81689848 ecr 515883153], length 196
11:21:242906 IP 1156322 > rhellocaldomain.ssh: Flags [.], ack 196, win 1443, options [nop,nop,TS val 515883235 ecr 81689848], length 0
11:21:244442 IP rhel43634 > gateway.domain: 57680\+ PTR? 11in-addr.arpa. (43)
11:21:244829 IP gateway.domain > rhel43634: 57680 NXDomain 0/0/0 (43)
11:21:247048 IP rhel33696 > gateway.domain: 37429\+ PTR? 11in-addr.arpa. (44)
5 packets captured
12 packets received by filter
0 packets dropped by kernel
$
在这种情况下,
tcpdump
捕获五个数据包后自动停止捕获。这在不同的情况下很有用 - 例如,如果你正在排除连接故障并且捕获一些初始数据包就足够了。当我们应用过滤器来捕获特定数据包时,这更加有用默认情况下,tcpdump 将 IP 地址和端口解析为名称,如前面的示例所示。在排除网络问题时,使用 IP 地址和端口号通常更容易,通过使用选项
-n
和端口解析禁用名称解析-nn
$ sudo tcpdump -i any -c5 -nn
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
23:56:292206 IP 1122 > 1135110: Flags [P.], seq 166198580:166198776, ack 2414541257, win 309, options [nop,nop,TS val 615664 ecr 540031155], length 196
23:56:292357 IP 1135110 > 1122: Flags [.], ack 196, win 1377, options [nop,nop,TS val 540031229 ecr 615664], length 0
23:56:292570 IP 1122 > 1135110: Flags [P.], seq 196:568, ack 1, win 309, options [nop,nop,TS val 615664 ecr 540031229], length 372
23:56:292655 IP 1135110 > 1122: Flags [.], ack 568, win 1400, options [nop,nop,TS val 540031229 ecr 615664], length 0
23:56:292752 IP 1122 > 1135110: Flags [P.], seq 568:908, ack 1, win 309, options [nop,nop,TS val 615664 ecr 540031229], length 340
5 packets captured
6 packets received by filter
0 packets dropped by kernel
如上所示,捕获输出现在显示 IP 地址和端口号。这还可以防止 tcpdump 发出 DNS 查询,这有助于在解决网络问题的同时降低网络流量。
既然你能够捕获网络数据包,让我们探索一下这个输出的含义。
Tcpdump 能够捕获和解码许多不同的协议,例如 TCP、UDP、ICMP 等等。虽然我们不能在这里涵盖所有这些,但为了帮助你入门,让我们探索 TCP 数据包。你可以在 tcpdump 的手册页中找到有关不同协议格式的更多详细信息。tcpdump 捕获的典型 TCP 数据包如下所示:
08:41:729687 IP 1122 > 1141916: Flags [P.], seq 196:568, ack 1, win 309, options [nop,nop,TS val 117964079 ecr 816509256], length 372
这些字段可能会因发送的数据包类型而异,但这是通用格式。
第一个字段
08:41:13.729687,
表示根据本地时钟接收到的数据包的时间戳。接下来,
IP
代表网络层协议——在本例中,IPv4
. 对于IPv6
数据包,该值为IP6
。下一个字段
192.168.64.28.22
是源 IP 地址和端口。后面是目标 IP 地址和端口,用 表示192.168.64.1.41916
。在源和目标之后,你可以找到 TCP Flags
Flags [P.]
。此字段的典型值包括:
Value | Flag Type | Description |
S | SYN | Connection Start |
F | FIN | Connection Finish |
P | PUSH | Data push |
R | RST | Connection reset |
. | ACK | Acknowledgment |
该字段也可以是这些值的组合,例如
[S.]
对于SYN-ACK
数据包。接下来是数据包中包含的数据的序列号。对于捕获的第一个数据包,这是一个绝对数字。后续数据包使用相对编号以使其更易于跟踪。在这个例子中,序列是
seq 196:568,
这意味着这个数据包包含这个流的字节 196 到 568。后面跟着确认号:
ack 1
。在这种情况下,它是 1,因为这是发送数据的一侧。对于接收数据的一侧,该字段表示此流上的下一个预期字节(数据)。例如,此流中下一个数据包的 Ack 编号将为 568。下一个字段是窗口大小
win 309
,它表示接收缓冲区中可用的字节数,后面是 TCP 选项,例如 MSS(最大段大小)或窗口比例。有关 TCP 协议选项的详细信息,请参阅传输控制协议 (TCP) 参数。最后,我们有数据包长度 ,
length 372
它表示有效载荷数据的长度(以字节为单位)。长度是序列号中最后一个字节和第一个字节之间的差值。现在让我们学习如何过滤数据包以缩小结果范围并更轻松地解决特定问题。
如上所述,tcpdump 可以捕获过多的数据包,其中一些甚至与你要进行故障排除的问题无关。例如,如果你正在对 Web 服务器的连接问题进行故障排除,那么你对SSH流量不感兴趣,因此从输出中删除 SSH数据包可以更轻松地解决实际问题。
tcpdump 最强大的功能之一是它能够使用各种参数过滤捕获的数据包,例如源和目标 IP 地址、端口、协议等。让我们看一些最常见的。
根据协议过滤数据包,在命令行中指定协议。例如,仅使用以下命令捕获 ICMP 数据包:
$ sudo tcpdump -i any -c5 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
在不同的终端中,尝试 ping 另一台机器:
$ ping rumenz.com
PING rumenz.com (2132) 56(84) bytes of data.
64 bytes from ec2-54-204-39-1compute-amazonaws.com (2132): icmp_seq=1 ttl=47 time=6 ms
回到 tcpdump 捕获,注意 tcpdump 只捕获并显示与 ICMP 相关的数据包。在这种情况下,tcpdump 不显示解析名称时生成的名称解析数据包
rumenz.com
:
09:34:136766 IP rhel75 > ec2-54-204-39-1compute-amazonaws.com: ICMP echo request, id 20361, seq 1, length 64
09:34:176402 IP ec2-54-204-39-1compute-amazonaws.com > rhel75: ICMP echo reply, id 20361, seq 1, length 64
09:34:140230 IP rhel75 > ec2-54-204-39-1compute-amazonaws.com: ICMP echo request, id 20361, seq 2, length 64
09:34:180020 IP ec2-54-204-39-1compute-amazonaws.com > rhel75: ICMP echo reply, id 20361, seq 2, length 64
09:34:141777 IP rhel75 > ec2-54-204-39-1compute-amazonaws.com: ICMP echo request, id 20361, seq 3, length 64
5 packets captured
5 packets received by filter
0 packets dropped by kernel
使用
host
过滤器将捕获限制为仅与特定主机相关的数据包:
$ sudo tcpdump -i any -c5 -nn host 2132
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
09:54:042023 IP 11139326 > 2180: Flags [S], seq 1375157070, win 29200, options [mss 1460,sackOK,TS val 122350391 ecr 0,nop,wscale 7], length 0
09:54:088127 IP 2180 > 11139326: Flags [S.], seq 1935542841, ack 1375157071, win 28960, options [mss 1460,sackOK,TS val 522713542 ecr 122350391,nop,wscale 9], length 0
09:54:088204 IP 11139326 > 2180: Flags [.], ack 1, win 229, options [nop,nop,TS val 122350437 ecr 522713542], length 0
09:54:088734 IP 11139326 > 2180: Flags [P.], seq 1:113, ack 1, win 229, options [nop,nop,TS val 122350438 ecr 522713542], length 112: HTTP: GET / HTTP/1
09:54:129733 IP 2180 > 11139326: Flags [.], ack 113, win 57, options [nop,nop,TS val 522713552 ecr 122350438], length 0
5 packets captured
5 packets received by filter
0 packets dropped by kernel
在此示例中,tcpdump 仅捕获并显示进出主机的数据包
54.204.39.132
。
要根据所需的服务或端口过滤数据包,请使用
port
过滤器。例如,使用以下命令捕获与 Web (HTTP) 服务相关的数据包:
$ sudo tcpdump -i any -c5 -nn port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
09:58:790548 IP 11139330 > 2180: Flags [S], seq 1745665159, win 29200, options [mss 1460,sackOK,TS val 122599140 ecr 0,nop,wscale 7], length 0
09:58:834026 IP 2180 > 11139330: Flags [S.], seq 4063583040, ack 1745665160, win 28960, options [mss 1460,sackOK,TS val 522775728 ecr 122599140,nop,wscale 9], length 0
09:58:834093 IP 11139330 > 2180: Flags [.], ack 1, win 229, options [nop,nop,TS val 122599183 ecr 522775728], length 0
09:58:834588 IP 11139330 > 2180: Flags [P.], seq 1:113, ack 1, win 229, options [nop,nop,TS val 122599184 ecr 522775728], length 112: HTTP: GET / HTTP/1
09:58:878445 IP 2180 > 11139330: Flags [.], ack 113, win 57, options [nop,nop,TS val 522775739 ecr 122599184], length 0
5 packets captured
5 packets received by filter
0 packets dropped by kernel
你还可以根据源或目标 IP 地址或主机名过滤数据包。例如,从主机捕获数据包
192.168.122.98
:
$ sudo tcpdump -i any -c5 -nn src 11198
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
10:02:220824 IP 11139436 > 11153: 59332\+ A? rumenz.com. (32)
10:02:220862 IP 11139436 > 11153: 20749\+ AAAA? rumenz.com. (32)
10:02:364062 IP 11139334 > 2180: Flags [S], seq 1108640533, win 29200, options [mss 1460,sackOK,TS val 122825713 ecr 0,nop,wscale 7], length 0
10:02:409229 IP 11139334 > 2180: Flags [.], ack 669337581, win 229, options [nop,nop,TS val 122825758 ecr 522832372], length 0
10:02:409667 IP 11139334 > 2180: Flags [P.], seq 0:112, ack 1, win 229, options [nop,nop,TS val 122825759 ecr 522832372], length 112: HTTP: GET / HTTP/1
5 packets captured
5 packets received by filter
0 packets dropped by kernel
请注意,tcpdumps
192.168.122.98
为名称解析(端口 53)和 HTTP(端口 80)等多项服务捕获了具有源 IP 地址的数据包。响应数据包不显示,因为它们的源 IP 不同。相反,你可以使用
dst
过滤器按目标 IP/主机名过滤:
$ sudo tcpdump -i any -c5 -nn dst 11198
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
10:05:572931 IP 11153 > 11147049: 2248 1/0/0 A 2132 (48)
10:05:572944 IP 11153 > 11147049: 33770 0/0/0 (32)
10:05:621833 IP 2180 > 11139338: Flags [S.], seq 3474204576, ack 3256851264, win 28960, options [mss 1460,sackOK,TS val 522874425 ecr 122993922,nop,wscale 9], length 0
10:05:667767 IP 2180 > 11139338: Flags [.], ack 113, win 57, options [nop,nop,TS val 522874436 ecr 122993972], length 0
10:05:672221 IP 2180 > 11139338: Flags [P.], seq 1:643, ack 113, win 57, options [nop,nop,TS val 522874437 ecr 122993972], length 642: HTTP: HTTP/1 302 Found
5 packets captured
5 packets received by filter
0 packets dropped by kernel
你还可以使用逻辑运算符组合过滤器
and
并or
创建更复杂的表达式。例如,要过滤来自源 IP 地址192.168.122.98
和服务 HTTP 的数据包,请使用以下命令:
$ sudo tcpdump -i any -c5 -nn src 11198 and port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
10:08:472696 IP 11139342 > 2180: Flags [S], seq 2712685325, win 29200, options [mss 1460,sackOK,TS val 123170822 ecr 0,nop,wscale 7], length 0
10:08:516118 IP 11139342 > 2180: Flags [.], ack 268723504, win 229, options [nop,nop,TS val 123170865 ecr 522918648], length 0
10:08:516583 IP 11139342 > 2180: Flags [P.], seq 0:112, ack 1, win 229, options [nop,nop,TS val 123170866 ecr 522918648], length 112: HTTP: GET / HTTP/1
10:08:567044 IP 11139342 > 2180: Flags [.], ack 643, win 239, options [nop,nop,TS val 123170916 ecr 522918661], length 0
10:08:788153 IP 11139342 > 2180: Flags [F.], seq 112, ack 643, win 239, options [nop,nop,TS val 123171137 ecr 522918661], length 0
5 packets captured
5 packets received by filter
0 packets dropped by kernel
你可以通过使用括号对过滤器进行分组来创建更复杂的表达式。在这种情况下,用引号将整个过滤器表达式括起来,以防止 shell 将它们与 shell 表达式混淆:
$ sudo tcpdump -i any -c5 -nn "port 80 and (src 11198 or src 2132)"
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
10:10:602214 IP 11139346 > 2180: Flags [S], seq 871108679, win 29200, options [mss 1460,sackOK,TS val 123327951 ecr 0,nop,wscale 7], length 0
10:10:650651 IP 2180 > 11139346: Flags [S.], seq 854753193, ack 871108680, win 28960, options [mss 1460,sackOK,TS val 522957932 ecr 123327951,nop,wscale 9], length 0
10:10:650708 IP 11139346 > 2180: Flags [.], ack 1, win 229, options [nop,nop,TS val 123328000 ecr 522957932], length 0
10:10:651097 IP 11139346 > 2180: Flags [P.], seq 1:113, ack 1, win 229, options [nop,nop,TS val 123328000 ecr 522957932], length 112: HTTP: GET / HTTP/1
10:10:692900 IP 2180 > 11139346: Flags [.], ack 113, win 57, options [nop,nop,TS val 522957942 ecr 123328000], length 0
5 packets captured
5 packets received by filter
0 packets dropped by kernel
在此示例中,我们仅针对HTTP服务(端口 80)和源IP地址
192.168.122.98
或54.204.39.132
. 这是检查同一流的两端的快速方法。
在前面的示例中,我们只检查数据包的标头以获取源、目的地、端口等信息。有时这就是我们解决网络连接问题所需的全部内容。然而,有时我们需要检查数据包的内容以确保我们发送的消息包含我们需要的内容或我们收到了预期的响应。要查看数据包内容,tcpdump 提供了两个附加标志:
-X
以十六进制和 ASCII-A
打印内容或以ASCII打印内容。例如,像这样检查Web请求的HTTP内容:
$ sudo tcpdump -i any -c10 -nn -A port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
13:02:871803 IP 11139366 > 2180: Flags [S], seq 2546602048, win 29200, options [mss 1460,sackOK,TS val 133625221 ecr 0,nop,wscale 7], length 0
E..<..@.@.....zb'....P...@......r............
............................
13:02:910734 IP 2180 > 11139366: Flags [S.], seq 1877348646, ack 2546602049, win 28960, options [mss 1460,sackOK,TS val 525532247 ecr 133625221,nop,wscale 9], length 0
E..<..@./..a'...zb.P..o..&...A..q a..........
.R.W....... ................
13:02:910832 IP 11139366 > 2180: Flags [.], ack 1, win 229, options [nop,nop,TS val 133625260 ecr 525532247], length 0
E...@.@.....zb'....P...Ao..'...........
.....R.W................
13:02:911808 IP 11139366 > 2180: Flags [P.], seq 1:113, ack 1, win 229, options [nop,nop,TS val 133625261 ecr 525532247], length 112: HTTP: GET / HTTP/1
E.....@.@...zb'....P...Ao..'...........
.....R.WGET / HTTP/1
User-Agent: Wget/14 (linux-gnu)
Accept: */*
Host: rumenz.com
Connection: Keep-Alive
................
13:02:951199 IP 2180 > 11139366: Flags [.], ack 113, win 57, options [nop,nop,TS val 525532257 ecr 133625261], length 0
E..F@./.."'...zb.P..o..'...........
.R.a....................
13:02:955030 IP 2180 > 11139366: Flags [P.], seq 1:643, ack 113, win 57, options [nop,nop,TS val 525532258 ecr 133625261], length 642: HTTP: HTTP/1 302 Found
E....G@./...'...zb.P..o..'.............
.R.b....HTTP/1 302 Found
Server: nginx
Date: Sun, 23 Sep 2018 17:02:14 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 207
X-Content-Type-Options: nosniff
Location: https://rumenz.com/
Cache-Control: max-age=1209600
Expires: Sun, 07 Oct 2018 17:02:14 GMT
X-Request-ID: v-6baa3acc-bf52-11e8-9195-22000ab8cf2d
X-Varnish: 632951979
Age: 0
Via: 1 varnish (Varnish/2)
X-Cache: MISS
Connection: keep-alive
<!DOCTYPE HTML>
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://rumenz.com/">here</a>.</p>
</body></html>
................
13:02:955083 IP 11139366 > 2180: Flags [.], ack 643, win 239, options [nop,nop,TS val 133625304 ecr 525532258], length 0
E...@.@.....zb'....P....o..............
.....R.b................
13:02:195524 IP 11139366 > 2180: Flags [F.], seq 113, ack 643, win 239, options [nop,nop,TS val 133625545 ecr 525532258], length 0
E...@.@.....zb'....P....o..............
.....R.b................
13:02:236592 IP 2180 > 11139366: Flags [F.], seq 643, ack 114, win 57, options [nop,nop,TS val 525532329 ecr 133625545], length 0
E..H@./.. '...zb.P..o..........I.....
.R......................
13:02:236656 IP 11139366 > 2180: Flags [.], ack 644, win 239, options [nop,nop,TS val 133625586 ecr 525532329], length 0
E...@.@.....zb'....P....o..............
.....R..................
10 packets captured
10 packets received by filter
0 packets dropped by kernel
这有助于解决API调用的问题,假设调用使用的是纯HTTP。然而对于加密连接不太有用。
tcpdump 提供的另一个有用的功能是能够将捕获保存到文件中,以便你稍后分析结果。例如,这允许你在夜间以批处理模式捕获数据包,并在早上验证结果。当有太多数据包需要分析时,它也有帮助,因为实时捕获可能发生得太快。
要将数据包保存到文件而不是在屏幕上显示它们,请使用选项
-w
(用于write
):
$ sudo tcpdump -i any -c10 -nn -w webserver.pcap port 80
[sudo] password for ricardo:
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
10 packets captured
10 packets received by filter
0 packets dropped by kernel
此命令将输出保存在名为
webserver.pcap
. 该.pcap
扩展名代表抓包,是这种文件格式的惯例。如本例所示,屏幕上不会显示任何内容,并且根据选项在捕获 10 个数据包后完成捕获
-c10
。如果你需要一些反馈以确保数据包被捕获,请使用选项-v
。Tcpdump 以二进制格式创建文件,因此你不能简单地使用文本编辑器打开它。要读取文件的内容,请使用
-r
(read
) 选项执行 tcpdump :
$ tcpdump -nn -r webserver.pcap
reading from file webserver.pcap, link-type LINUX_SLL (Linux cooked)
13:36:679494 IP 11139378 > 2180: Flags [S], seq 3709732619, win 29200, options [mss 1460,sackOK,TS val 135708029 ecr 0,nop,wscale 7], length 0
13:36:718932 IP 2180 > 11139378: Flags [S.], seq 1999298316, ack 3709732620, win 28960, options [mss 1460,sackOK,TS val 526052949 ecr 135708029,nop,wscale 9], length 0
13:36:719005 IP 11139378 > 2180: Flags [.], ack 1, win 229, options [nop,nop,TS val 135708068 ecr 526052949], length 0
13:36:719186 IP 11139378 > 2180: Flags [P.], seq 1:113, ack 1, win 229, options [nop,nop,TS val 135708068 ecr 526052949], length 112: HTTP: GET / HTTP/1
13:36:756979 IP 2180 > 11139378: Flags [.], ack 113, win 57, options [nop,nop,TS val 526052959 ecr 135708068], length 0
13:36:760122 IP 2180 > 11139378: Flags [P.], seq 1:643, ack 113, win 57, options [nop,nop,TS val 526052959 ecr 135708068], length 642: HTTP: HTTP/1 302 Found
13:36:760182 IP 11139378 > 2180: Flags [.], ack 643, win 239, options [nop,nop,TS val 135708109 ecr 526052959], length 0
13:36:977602 IP 11139378 > 2180: Flags [F.], seq 113, ack 643, win 239, options [nop,nop,TS val 135708327 ecr 526052959], length 0
13:36:022089 IP 2180 > 11139378: Flags [F.], seq 643, ack 114, win 57, options [nop,nop,TS val 526053025 ecr 135708327], length 0
13:36:022132 IP 11139378 > 2180: Flags [.], ack 644, win 239, options [nop,nop,TS val 135708371 ecr 526053025], length 0
$
由于不再直接从网络接口捕获数据包,因此不需要读取文件。
你还可以使用我们讨论过的任何过滤器来过滤文件中的内容,就像处理实时数据一样。例如,
54.204.39.132
通过执行以下命令检查来自源 IP 地址的捕获文件中的数据包:
$ tcpdump -nn -r webserver.pcap src 2132
reading from file webserver.pcap, link-type LINUX_SLL (Linux cooked)
13:36:718932 IP 2180 > 11139378: Flags [S.], seq 1999298316, ack 3709732620, win 28960, options [mss 1460,sackOK,TS val 526052949 ecr 135708029,nop,wscale 9], length 0
13:36:756979 IP 2180 > 11139378: Flags [.], ack 113, win 57, options [nop,nop,TS val 526052959 ecr 135708068], length 0
13:36:760122 IP 2180 > 11139378: Flags [P.], seq 1:643, ack 113, win 57, options [nop,nop,TS val 526052959 ecr 135708068], length 642: HTTP: HTTP/1 302 Found
13:36:022089 IP 2180 > 11139378: Flags [F.], seq 643, ack 114, win 57, options [nop,nop,TS val 526053025 ecr 135708327], length 0