以下将给出9个使用tcpdump的例子,以说明tcpdump的具体使用方法。

 

1.针对特定网口抓包(-i选项)

当我们不加任何选项执行tcpdump时,tcpdump将抓取通过所有网口的包;使用-i选项,我们可以在某个指定的网口抓包:

复制代码
linux:/tmp/lx # tcpdump -i eth0tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes10:50:28.607429 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 116 win 6495110:50:28.607436 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 116:232(116) ack 1 win 1286410:50:30.384195 arp who-has 128.128.128.35 tell 128.128.128.35
复制代码

以上例子中,tcpdump抓取所有通过eth0的包。

 

2.抓取指定数目的包(-c选项)

默认情况下tcpdump将一直抓包,直到按下”ctrl+c”中止,使用-c选项我们可以指定抓包的数量:

复制代码
linux:/tmp/lx # tcpdump -c 2 -i eth0tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes10:58:05.656104 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1210443473:1210443589(116) ack 2583117929 win 1286410:58:05.657074 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 116 win 652112 packets captured6 packets received by filter0 packets dropped by kernel
复制代码

以上例子中,只针对eth0网口抓2个包。

 

3.将抓到包写入文件中(-w选项)

使用-w选项,我们可将抓包记录到一个指定文件中,以供后续分析

复制代码
linux:/tmp/lx # tcpdump -w 20120606.pcap -i eth0tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes75 packets captured150 packets received by filter0 packets dropped by kernel
复制代码

应当保存为.pcap后缀的文件,方便我们使用wireshark等工具读取分析。

 

4.读取tcpdump保存文件(-r选项)

对于保存的抓包文件,我们可以使用-r选项进行读取:

复制代码
linux:/tmp/lx # tcpdump -r 20120606.pcapreading from file 20120606.pcap, link-type EN10MB (Ethernet)11:01:57.392907 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1210446405:1210446457(52) ack 2583119957 win 1286411:01:57.392917 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 52:168(116) ack 1 win 1286411:01:57.393649 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 52 win 65327
复制代码

 

5.抓包时不进行域名解析(-n选项)

默认情况下,tcpdump抓包结果中将进行域名解析,显示的是域名地址而非ip地址,使用-n选项,可指定显示ip地址。

 

6.增加抓包时间戳(-tttt选项)

使用-tttt选项,抓包结果中将包含抓包日期:

linux:/tmp/lx # tcpdump -n -tttt -i eth02012-06-0611:14:59.539736 IP 10.71.171.140.22 > 10.70.121.95.1787: P 1:53(52) ack 100 win 75042012-06-0611:14:59.539754 IP 10.71.171.140.22 > 10.70.121.95.1787: P 53:105(52) ack 100 win 75042012-06-0611:14:59.539770 IP 10.71.171.140.22 > 10.70.121.95.1787: P 105:157(52) ack 100 win 7504

 

7.指定抓包的协议类型

我们可以只抓某种协议的包,tcpdump支持指定以下协议:ip,ip6,arp,tcp,udp,wlan等。以下例子只抓取arp协议的包:

复制代码
linux:/tmp/lx # tcpdump -i eth0 arptcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes11:22:26.948656 arp who-has 10.10.1.30 tell 10.10.1.2611:22:27.017406 arp who-has 10.10.1.30 tell 10.10.1.2611:22:27.078803 arp who-has 10.10.1.30 tell 10.10.1.26
复制代码

 

8.指定抓包端口

如果想要对某个特定的端口抓包,可以通过以下命令:

复制代码
linux:/tmp/lx # tcpdump -i eth0 port 22tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes11:41:04.387547 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 1216136825 win 6475111:41:04.387891 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1:233(232) ack 0 win 1608011:41:04.398973 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: P 0:52(52) ack 233 win 64519
复制代码

 

9.抓取特定目标ip和端口的包

网络包的内容中,包含了源ip地址、端口和目标ip、端口,我们可以根据目标ip和端口过滤tcpdump抓包结果,以下命令说明了此用法:

linux:/tmp/lx # tcpdump -i eth0 dst 10.70.121.92 and port 22

 

**********************************************************************************************************************

先来看一个比较基本的用法:

tcpdump -i eth0

其中,eth0为参数值,表示需要抓包的网口,这是个必需参数哦。

tcpdump支持很多的关键字,下面先看几个例子:

(例1)tcpdump -i eth0 host 192.168.0.250 -----在网口eth0上抓取主机地址为192.168.0.250的所有数据包

(例2)tcpdump -i eth0 net 192.168.0.0/24 ------ 在网口eth0上抓取网络地址为192.168.0.0/24的所有数据包

(例3)tcpdump -i eth0 port 80 ------ 在网口eth0上抓取端口为80的所有数据包(注意,这里不区分是源端口还是目的端口)

当然,我们也可以指定源端口或目的端口

(例4)tcpdump -i eth0 src port 80 and dst port6100 --- 在网口eth0上抓取源端口为80且目的端口为6100的数据包,这里用到了and逻辑运算符,后面再介绍

(例5)tcpdump -i eth0 icmp --- 在网口eth0上抓取所有icmp协议的数据包

以上几个例子,可以大致体现出tcpdump的基本用法。

除了这三种类型的关键字外,还有其他重要的关键字,如:gateway,broadcast,less,greater,还有三种逻辑运算,取非运算是'not'、'!',与运算符是'and'、'&&'、

或运算符是'or'、'||',这些关键字可以组合起来构成强大的组合条件来满足我们的需求。

先看看tcpdump的具体参数及意义:

-i:指定tcpdump监听的网络接口

-s:指定要监听数据包的长度

-c:指定要监听的数据包数量,达到指定数量后自动停止抓包

-w:指定将监听到的数据包写入文件中保存

-A:指定将每个监听到的数据包以ACSII可见字符打印

-n:指定将每个监听到数据包中的域名转换成IP地址后显示

-nn:指定将每个监听到的数据包中的域名转换成IP、端口从应用名称转换成端口号后显示

-e:指定将监听到的数据包链路层的信息打印出来,包括源mac和目的mac,以及网络层的协议

-p:将网卡设置为非混杂模式,不能与host或broadcast一起使用

-r:指定从某个文件中读取数据包

-S:指定打印每个监听到的数据包的TCP绝对序列号而非相对序列号

 

 

OK,参数介绍先到这里,下面看几个具体例子

 

tcpdump -i eth0 -s 1400 -nn host 192.168.0.250 and ! 192.168.0.74 and icmp -e

抓取网口eth0上192.168.0.250与除192.168.0.74外的其他主机之间的icmp报文

 

tcpdump -i eth0 -s 1400 -nn tcp and \(host 192.168.0.250 and ! 192.168.0.74\)

抓取网口eth0上192.168.0.250与除192.168.0.74外的所有tcp数据包,这里用到了括号,注意,在tcpdump中使用括号时必须用转义

tcpdump -i eth0 ether src or dst 00:21:85:6C:D9:A3

抓取网口eth0上源mac地址或目的mac地址为00:21:85:6C:D9:A3的所有数据包,注意,这里的mac地址格式必须以':'分隔。