Linux常用命令-tcpdump

命令

tcpdump

描述

dump traffic on a network
网络抓包工具

用法

1
2
3
4
5
6
7
8
tcpdump [-aAbdDefhHIJKlLnNOpqStuUvxX#] [ -B size ] [ -c count ]
[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
[ -i interface ] [ -j tstamptype ] [ -M secret ] [ --number ]
[ -Q|-P in|out|inout ]
[ -r file ] [ -s snaplen ] [ --time-stamp-precision precision ]
[ --immediate-mode ] [ -T type ] [ --version ] [ -V file ]
[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]
[ -Z user ] [ expression ]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Options:
-A 以ASCII码显示每个数据包,便于捕获web网页
-b 以ASDOT符号而不是ASPLAIN符号显示BGP数据包中的AS号
-B,--buffer-size= 指定缓冲区大小,单位KB
-c 指定捕获的数据包的数量
-C 指定保存的抓包文件的大小,单位MB,配合-w使用,用于分割文件,按指定大小自动分割并增加数字后缀
-d 将解码后的数据包以可读形式显示
-dd 显示为C语言格式
-ddd 显示为十进制数格式
-D,--list-interfaces 显示系统上可用的网络接口列表
-e 显示数据包中的数据链路层头部信息
-f 显示外部IPv4地址时显示为数字格式而非主机名
-F 使用指定文件作为过滤器表达式的输入,忽略命令行中提供的其他表达式
-H 尝试检测802.11s mesh网络头部信息
-i,--interface= 指定监听的网络接口
-l 对标准输出进行行缓冲,例如同时查看抓包内容并追加到data文件中,命令tcpdump -l | tee data
-L,--list-data-link-types 显示指定接口的已知数据链路类型
-m 通过指定文件加载SMI或MIB模块信息
-n 显示IP地址而非域名
-nn 显示端口号而非协议名称
-N 显示主机名而非完整主机名FQDN
-#,--number 在行首显示数据包序号
-p,--no-promiscuous-mode 不要将接口设置为promiscuous混杂模式
-Q|-P direction,--direction=direction 选择应捕获数据包的发送/接收方向。可选参数in,out,inout.并非在所有平台上都可用
-q 简化显示,快速输出
-r 从文件中读取数据包信息,文件必须是通过-w保存或者是通过其他工具保存的pcap或pcap-ng数据包文件
-S,--absolute-tcp-sequence-numbers 显示绝对TCP序列号,而不是相对TCP序列号
-s,--snapshot-length=snaplen Snarf snaplen bytes of data from each packet rather than the default of 262144 bytes
设置tcpdump的数据包抓取长度,设置为0则tcpdump自动选择合适的长度来抓取数据包,推荐0,避免产生数据丢失
-t 不显示时间戳
-tt 显示时间戳为秒数,即seconds since January 1, 1970, 00:00:00, UTC
-ttt 显示当前行和上一行之间的时间增量,单位微秒micro-second
-tttt 显示时间格式为时分秒加日期
-ttttt 显示当前行和第一行之间的时间增量,单位微秒micro-second
-u 显示未加密的NFS句柄
-U,--packet-buffered 配合-w使用,实时写入数据包到文件
-v,-vv,-vvv 显示详细信息,逐级增加
-V 从文件中读取文件列表,如需加载多个数据包文件
-w 将数据包内容保存到文件,可通过-r读取
-W 限制文件数量,配合-C使用,超过数量后会循环覆盖之前的文件
-x,-xx 以十六进制显示数据包中的数据信息
-X,-XX 以ASCII码显示数据包中的数据信息

expression表达式,可通过命令man pcap-filter查看
$ man pcap-filter
type 指定类型,可选参数host(默认), net, port, portrange
dir 指定传输方向,可选参数src, dst, src or dst(默认), src and dst.无线设备可选参数ra, ta, addr1, addr2, addr3, addr4
proto 指定协议,可选参数ether, fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp, udp,如果没有指定协议则默认为all
and, or, not, \(\) 关系表达式,\(\)表示优先运算符 && = and, || = or, ! = not
>, <, >=, <=, =, != 关系表达式

表达式示例
host 192.168.31.11
port 53 = (tcp or udp) port 53 #注意没有指定协议则默认为all
portrange 6000-6008
src or dst port 8080
tcp port 21
udp portrange 7000-7009
dst host 192.168.31.12
net 172.16.10.0/24
dst net 192.168.31 = dst net 192.168.31.0 #网络简写
dst net 172.16 = dst net 172.16.0.0
dst net 10 = dst net 10.0.0.0
host 192.168.31.11 and not port ftp and not port ftp-data
tcp dst port ftp or ftp-data or domain = tcp dst port ftp or tcp dst port ftp-data or tcp dst port domain #简化写法
ip proto icmp
ip multicast
host host1 and \( host2 or host3 \)

注意

需要安装tcpdump软件包
官网https://www.tcpdump.org/index.html
推荐在目标主机上用tcpdump工具抓包保存,然后复制包文件到本地使用wireshark等图形化工具打开查看

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
$ tcpdump --version
tcpdump version 4.9.2
libpcap version 1.5.3
OpenSSL 1.0.2k-fips 26 Jan 2017

# 获取eth0接口的tcp80或443的数据包
$ tcpdump -i eth0 tcp port 80 or 443
# 新开窗口执行
$ curl -I http://www.baidu.com

# -c 100 抓取100个包
$ tcpdump -i eth0 tcp port 443 -c 100
# -D 显示可用接口
$ tcpdump -D
1.eth0
2.docker0
3.nflog (Linux netfilter log (NFLOG) interface)
4.nfqueue (Linux netfilter queue (NFQUEUE) interface)
5.any (Pseudo-device that captures on all interfaces)
6.lo [Loopback]

# 将抓包显示的内容保存到cap文件中,注意此为文本文件而非数据包文件,可以直接cat查看,但不可通过-r读取
$ tcpdump -i eth0 -f -l tcp port 80 | tee cap
$ file cap
cap: ASCII text
$ more cap
# 保存抓取的数据包到文件中,-w保存的文件可以通过-r读取
$ tcpdump -i eth0 -f -l tcp port 80 -w data.cap
$ file data.cap
data.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 262144)
$ tcpdump -r data.cap

# -nn 显示IP和端口号,而非主机名和协议名称.
$ tcpdump -r data.cap -nn
# -q 简化显示 -# 行首显示序列号
$ tcpdump -r data.cap -nn -# -q
reading from file data.cap, link-type EN10MB (Ethernet)
1 18:26:20.550939 IP 192.168.31.11.23902 > 192.168.31.80.80: tcp 0
2 18:26:20.551640 IP 192.168.31.80.80 > 192.168.31.11.23902: tcp 0
3 18:26:20.551750 IP 192.168.31.11.23902 > 192.168.31.80.80: tcp 0
4 18:26:20.552072 IP 192.168.31.11.23902 > 192.168.31.80.80: tcp 75
# -Q out 抓取出去的流量
$ tcpdump -i eth0 tcp port 80 -nn -# -Q out
# -s 0 设置截取数据包的长度为0,表示按数据包的长度来截取数据,避免产生数据丢失
$ tcpdump -i eth0 tcp port 443 -nn -s 0 -c 100
# -tttt 显示时间
$ tcpdump -r data.cap -tttt
reading from file data.cap, link-type EN10MB (Ethernet)
2022-01-16 18:26:20.550939 IP centos7.23902 > 192.168.31.80.http: Flags [S], seq 641484940, win 29200, options [mss 1460,sackOK,TS val 35876125 ecr 0,nop,wscale 9], length 0
2022-01-16 18:26:20.551640 IP 192.168.31.80.http > centos7.23902: Flags [S.], seq 1734186162, ack 641484941, win 28960, options [mss 1460,sackOK,TS val 1873151467 ecr 35876125,nop,wscale 9], length 0


$ tcpdump -i eth0 host 192.168.80.12
$ tcpdump host 192.168.80.12 and 192.168.80.13
$ tcpdump ip host ! 192.168.80.12
$ tcpdump src host 192.168.80.12
$ tcpdump dst host 192.168.80.13
$ tcpdump host 192.168.31.12 and tcp port 80 or 443
$ tcpdump -i eth0 icmp
$ tcpdump -i eth0 broadcast
$ tcpdump -i eth0 multicast
$ tcpdump tcp -i eth0 -s 0 -c 100 and dst port 80 or 443 and src 192.168.31.12 -w data.cap