tcpdump是linux下的一个抓包工具,类似于windows下的wireshark(linux也有wireshark,同样也有命令版的tshark)。本篇将从命令选项和常用过滤示例两个方面做下介绍。

一、常用参数选项

常用参数选项及说明如下:

 1-i 选项:表示tcpdump监听端口,如果不指定,那么会搜索所有的网络接口
 2~ ]# tcpdump -i ens33            # 在ens33这个网络接口监听
 3~ ]# tcpdump -i any               # linux 2.2以上支持 -i any,可以监听所有端口
 4-n 选项:不要把ip转换为主机名
 5~ ]#tcpdump -i ens33 -n
 6-w -选项,-w为把内容write到某个地方, -表示标准输出  也就是输出到标准输出中,下面是一个超级有用的命令,把包的数据,用字符展示出来。
 7~ ]# tcpdump -i ens33 -w - | strings
 8~ ]# tcpdump -w test.cap                    # 把抓包结果写入test.cap中,方便wireshark分析
 9-C num选项:如果test.cap 超过num大小,则新开一个文件,  -C fileSize , 单位是MB
10~ ]# tcpdump  -C 1  -w test.cap
11-r 选项: 从某个文件读取
12~ ]# tcpdump -n -r test.cap
13-X 选项:以十六进制以及ASCII的形式打印数据内容。
14~ ]# tcpdump -X
15~ ]# tcpdump -x     # 除了打印出header外,还打印packet里面的数据(十六进制的形式)
16~ ]# tcpdump -xx   # 以十六进制的形式打印header, data内容
17- A 选项,把每一个packet都用以ASCII的形式打印出来
18~ ]# tcpdump -i ens33 -A host www.itshouce.com.cn
19-c # 表示收到 # 个packet就退出
20~ ]# tcpdump -i ens33 -A -c 3  172.18.14.55         # 接收三个包就退出
21-D 选项:看目前机器上有哪些网络接口
22~ ]# tcpdump -D
231.ens33
242.ens34
253.lo
26-e 选项:把连接层的头打印出来
27~ ]# tcpdump -i ens33 -e
28-j 选项,修改时间格式
29~ ]# tcpdump -i ens33 -j timestamp type          # 可以修改输出的时间格式,
30~ ]# tcpdump -J
31# 显示支持的时间格式 : List the supported time stamp types for the interface and exit.  If the time stamp  type  cannot  be  set  for  the interface, no time stamp types are listed。
32-l 选项:把stdout bufferd住,当你既想在屏幕上看结果,又想把结果输出到文件中时,比较有用
33~ ]# tcpdump -l
34# 可以配合tee命令,在屏幕上显示dump内容,并把内容输出到dump.log中,如下
35~ ]# tcpdump -l |tee dump.log                               # or下面这种写法
36~ ]# tcpdump -l > dump.log &tail -f dump.log
37-q 选项: 就是quiect output, 静默输出,尽量少的打印一些信息
38~ ]# tcpdump -q
39-S 选项:打印真实的,绝对的tcp seq no
40~ ]# tcpdump -i ens33 -S
41-s 选型:指定包大小,默认抓取包长度是65535,即68个字节
42~ ]# tcpdump                    # capture sieze 65535l
43~ ]# tcpdump -s  256        # 我们设置为256 该参数目的:减少抓包文件的大小
44-t 选项:不要打时间戳,
45~ ]# tcpdump -i ens33 -t
46-tt 选项:打出timstamp,从1970-1-1 以来的秒数,以及微秒数。
47~ ]# tcpdump -i ens33 -tt
48-v 选项:verbose,即打印详细信息。
49~ ]# tcpdump -i ens33 -v      # 打印出详细结果,如ttl值
50~ ]# tcpdump -i ens33 -vv    # 打印出更加详细的结果  如window, checksum等

二、过滤示例

1、简单过滤

 1# 抓取192.168.199.* 网段的arp协议包,arp可以换为tcp,udp等。
 2~ ]# tcpudmp -i eth0 -n arp host 192.168.199
 3# 抓取访问destination 80端口的包
 4~ ]# tcpdump -i eth0 -n dst port 80
 5# 抓取源上端口是80的包
 6~ ]# tcpdump -i eth0 -n src port 80
 7# 抓取源或者目标端口都是80的包
 8~ ]# tcpdump -i eth0 -n port 80
 9# 表示抓取destination prot 在1到80之间的端口的数据
10~ ]# tcpdump -i eth0 -n dst portrange 1-80
11# 抓取端口是20-80的包,不考虑源
12~ ]# tcpdump -i eth0 -n portrange 20-80
13# 抓取destination为www.baidu.com的包
14~ ]# tcpdump -i eth0 dst www.baidu.com
15# 抓取destination为192.168.14.55的包
16~ ]# tcpdump -i eth0 dst 192.168.14.55
17# 抓取destination为192.168.1.[0-255]的包
18~ ]# tcpdump -i eth0 dst 192.168.1    # 也可以指定范围
19# 抓取source为192.168.*.*的包, 使用-n 则只是为了显示ip,而不是主机名,
20~ ]# tcpdump -i eth0 -n src 192.168
21# 抓取包长度小于800的包
22~ ]# tcpudmp -i eth0 -n less 800
23# 抓取包长度大于800的包
24~ ]# tcpdump -i eth0 -n greater 800
25# 只抓取tcp包
26~ ]# tcpdump -i eth0 -n tcp
27# 只抓取udp包
28~ ]# tcpdump -i eth0 -n udp
29# 只抓取icmp的包,internet控制包
30~ ]# tcpdump -i eth0 -n icmp  

2、组合使用

来个复杂点的示例:

1tcpdump tcp -i eth1 -t -s 0 -c 100 and dst port ! 22 and src net 192.168.1.0/24 -w target.cap
2(1)tcp: ip icmp arp rarp 和 tcp、udp、icmp这些选项等都要放到第一个参数的位置,用来过滤数据报的类型
3(2)-i eth1 : 只抓经过接口eth1的包
4(3)-t : 不显示时间戳
5(4)-s 0 : 抓取数据包时默认抓取长度为68字节。加上-S 0 即为不限制抓的包的大小,就可以抓到完整的数据包
6(5)-c 100 : 只抓取100个数据包
7(6)dst port ! 22 : 不抓取目标端口是22的数据包
8(7)src net 192.168.1.0/24 : 数据包的源网络地址为192.168.1.0/24
9(8)-w target.cap : 保存成cap文件,方便用ethereal(即wireshark)分析

3、组合使用及tshark配合

 1查看数据库执行的sql
 2/usr/sbin/tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | egrep -i 'SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL'
 3查看是哪些蜘蛛在抓取内容
 4/usr/sbin/tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'
 5用tcpdump嗅探80端口的访问看看谁最高
 6tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20
 7tcpdump -nnvvvS -s 0 -U dst 119.37.194.35 and dst port 110 |tshark |awk '/USER|PASS/'
 8tcpdump -i eth1 -nnvvvS -s 0 -U|tshark -V -R oicq
 9tshark -i eth1 -V -R smtp
10tshark -i eth1 -V -R oicq|grep "OICQ Number"