「tcpdump」- 转储网络流量

常用选项

-i interface
–interface=interface
指定监听端口。
如果未指定,tcpdump 会在系统接口列表中搜索最小编号的、已启动接口(但不包括 lo),这可能是“eth0”。
在 Linux 2.2+ 上,可以使用“any”来捕获所有接口的数据包。注意,“any”捕获不会在混杂模式下进行。
如果支持使用-D 选项,那么由-D 选项打印的接口编号可以用作interface参数,如果系统上的接口没有使用该数字作为接口名称。

-n
不转化地址,显示数据类型的 IP 地址、端口号。

常用选项:
tcpdump -i eth0 // 指定过滤金额口,any 表示全部接口
tcpdump -nn // 不解析网络地址和端口号
tcpdump -c5 // 限制抓取网络包的个数
tcpdump -A // 以 ASCII 显示网络包内容
tcpdump -w file.pcap // 将抓包内容写入文件中
tcpdump -e // 输出链路层头部信息

网络协议位于 RFC 文档,或者阅读 《TCP/IP 详解》 第一卷的 TCP/IP 协议族。

常用表达式:
host、src host、dst host / 主机过滤 / tcpdump -nn host “ip address”
net、src net、dst net / 网络过滤 / tcpdump -nn net “192.168.0.0”
port、portrange、src port、dst port / 端口过滤 / tcpdump -nn dst port “80”
ip、ip6、arp、tcp、udp、icmp / 协议过滤 / tcpdump -nn tcp
and、or、not / 逻辑表达式 / tcpdump -nn icmp or udp
tcp[tcpflags] / 特定状态的 TCP 包 / tcpdump -nn “tcp[tcpflags] & tcp-syn != 0”

网络包的详细信息取决于协议,不同协议展示的格式也不同。所以更详细的使用方法,需要查询 tcpdump 的 man 手册

常用命令

打印 HTTP 请求

Human readable format for http headers with tcpdump
Use TCPDUMP to Monitor HTTP Traffic
Can I use tcpdump to get HTTP requests, response header and response body?

命令语法格式

tcpdump [ -AbdDefhHIJKlLnNOpqStuUvxX# ] [ -B buffer_size ]
		[ -c count ]
		[ -C file_size ] [ -G rotate_seconds ] [ -F file ]
		[ -i interface ] [ -j tstamp_type ] [ -m module ] [ -M secret ]
		[ --number ] [ -Q in|out|inout ]
		[ -r file ] [ -V file ] [ -s snaplen ] [ -T type ] [ -w file ]
		[ -W filecount ]
		[ -E spi@ipaddr algo:secret,...  ]
		[ -y datalinktype ] [ -z postrotate-command ] [ -Z user ]
		[ --time-stamp-precision=tstamp_precision ]
		[ --immediate-mode ] [ --version ]
		[ expression ]

命令描述

Tcpdump prints out a description of the contents of packets on a network interface that match the boolean expression; the description is preceded by a time stamp, printed, by default, as hours, minutes, seconds, and fractions of a second since midnight. It can also be run with the -w flag, which causes it to save the packet data to a file for later analysis, and/or with the -r flag, which causes it to read from a saved packet file rather than to read packets from a network interface. It can also be run with the -V flag, which causes it to read a list of saved packet files. In all cases, only packets that match expression will be processed by tcpdump.

Tcpdump will, if not run with the -c flag, continue capturing packets until it is interrupted by a SIGINT signal (generated, for example, by typing your interrupt character, typically control-C) or a SIGTERM signal (typically generated with the kill(1) command); if run with the -c flag, it will capture packets until it is interrupted by a SIGINT or SIGTERM signal or the specified number of packets have been processed.

When tcpdump finishes capturing packets, it will report counts of:

packets ‘captured’ (this is the number of packets that tcpdump has received and processed);

packets ‘received by filter’ (the meaning of this depends on the OS on which you’re running tcpdump, and possibly on the way the OS was configured – if a filter was specified on the command line, on some OSes it counts packets regardless of whether they were matched by the filter expression and, even if they were matched by the filter expression, regardless of whether tcpdump has read and processed them yet, on other OSes it counts only packets that were matched by the filter expression regardless of whether tcpdump has read and processed them yet, and on other OSes it counts only packets that were matched by the filter expression and were processed by tcpdump);

packets ‘dropped by kernel’ (this is the number of packets that were dropped, due to a lack of buffer space, by the packet capture mechanism in the OS on which tcpdump is running, if the OS reports that information to applications; if not, it will be reported as 0).

On platforms that support the SIGINFO signal, such as most BSDs (including Mac OS X) and Digital/Tru64 UNIX, it will report those counts when it receives a SIGINFO signal (generated, for example, by typing your ‘status’ character, typically control-T, although on some platforms, such as Mac OS X, the ‘status’ character is not set by default, so you must set it with stty(1) in order to use it) and will continue capturing packets. On platforms that do not support the SIGINFO signal, the same can be achieved by using the SIGUSR1 signal.

Reading packets from a network interface may require that you have special privileges; see the pcap (3PCAP) man page for details. Reading a saved packet file doesn’t require special privileges.

命令支持的选项及含义

-c count
在收到 count 个数据包后退出。

-l
使用标准输出行缓冲。如果您想在捕获数据时查看数据,则非常有用。例如tcpdump -l | tee dat或者tcpdump -l > dat & tail -f dat命令。

请注意,在 Windows 上,“行缓冲”表示“不缓冲”,因此如果指定了-l 选项,WinDump 将单独写入每个字符。

-n
不要将地址(即主机地址,端口号等)转换为名称。

-r file
从文件 file 中读取数包。文件是使用-w 选项时创建的,或者是其他可以生成 pcap 或 pcap-ng 文件的工具。如果参数 file 为‘-’,则从标准输入读取。

-S, –absolute-tcp-sequence-numbers
Print absolute, rather than relative, TCP sequence numbers.

-s snaplen, –snapshot-length=snaplen
每个数据包的抽取 snaplen 数据字节,而不是默认的 262144 字节。由于快照有限而被截断的数据包,在输出中用'[| proto]’表示,其中 proto 是发生截断的协议级别的名称。请注意,较大的快照会增加处理数据包所需的时间,并且有效地减少数据包缓冲量。这可能会导致数据包丢失。您应该将 snaplen 限制为捕获您感兴趣的协议信息的最小数字。将 snaplen 设置为 0 会将其设置为默认值 262144,以便向后兼容最新版本的 tcpdump。

-v
在解析和打印时,产生(略多)详细输出。例如,打印 IP 包中的 ttl,id,总长度,选项。同时会启用附加的数据包完整性检查,例如验证 IP 和 ICMP 标头的校验和。

使用选项-w写入文件时,还会每隔 10 秒报告一次捕获的数据包数量。

-vv
在解析和打印时,产生更详细的输出。例如,打印在 NFS 回复数据包中的其他字段,以及完全解码 SMB 数据包。

-vvv
在解析和打印时,产生超级详细的输出。例如,在 telnet 中的 SB…SE 选项会全部打印。使用-X 选项后,Telnet 的选项也以十六进制打印。

-V file
从文件 file 中读取一个文件列表。如果参数 file 为‘-’,则使用标准输入。

-w file
将原始数据包写入文件,而不是解析并打印,之后可以使用-r 选项打印它们。如果参数 file 是‘-’,则使用标准输出。

如果写入文件或管道,则输出将被缓冲。因此对于文件或管道读取的程序,在数据包收到后的某段时间内,可能会看不见数据包。使用选项-U 可以在收到数据包后立即写入。

对于 pcap 文件 MIME 类型 application/vnd.tcpdump.pcap 已在 IANA 注册。与.cap 和.dmp 相比,文件扩展名.pcap 似乎是最常用的。读取捕获文件时,tcpdump 本身不检查扩展名,并且在写入文件时不添加扩展名(它在文件头中使用幻数)。但是,许多操作系统和应用程序都使用扩展名(如果存在),建议添加一个扩展名(例如.pcap)。

有关文件格式的说明,请参阅 pcap-savefile(5)手册。

-x
在解析和打印时,除了打印每个「数据包的头」外,还打印每个「数据包的数据」(减去其链路层的头),以「十六进制」格式。

将打印整个数据包或snaplen字节中较小的一个。

注意,这是整个链路层数据包,因此对于有填充(例如以太网)的链路层,当「较高层数据包」比所需的填充短时,填充字节也会被打印。

-xx
在解析和打印时,除了打印每个「数据包的头」外,还要打印每个「数据包的数据」,包括其「链路层的头」,以「十六进」制表示。

-X
在解析和打印时,除了打印每个「数据包的头」外,还打印每个「数据包的数据」(减去其链路层的头),以「十六进制」和「ASCII」格式。

这对于分析新协议非常方便。

-XX
在解析和打印时,除了打印每个「数据包的头」外,还打印每个「数据包的数据」,包括其「链路层的头」,以「十六进制」和「ASCII」格式。

其他选项

-h, –help
打印 tcpdump 和 libpcap 版本字符串,打印用法消息,然后退出。

–version
打印 tcpdump 和 libpcap 版本字符串,并退出。

命令参数

expression
选择要转储的数据包。如果没有给出expression参数,则网络上的所有数据包都将被转储。否则,只会转储expression为“true”的数据包。

有关表达式语法,请参阅 pcap-filter(7)。

参数expression可以作为单个 Shell 参数传递给 tcpdump,也可以作为多个 Shell 参数传递给它,怎么方便怎么来。通常,如果expression包含 Shell 元字符,例如用于转义协议名称的反斜杠(\),则更容易的做法将其作为单个引用参数传递,而不是转义 Shell 元字符。在解析之前,多个参数与空格连接在一起。

应用

根据协议抓取:

# tcpdump -n -i eth0 not proto \\carp

# tcpdump -i eth0 ip[9] == 89                                                   # OSPF
# tcpdump -i eth0 proto ospf

抓取某个网段(debian – tcpdump ip range):

# tcpdump net 130.190.0.0/17

参考

man 8 tcpdump, Version 4.9.0-2
TCPDump Manual
Protocol filters
Parsing OSPF packets using tcpdump – NIL – Network Information Library
man 8 tcpdump, version 4.9.2