linux针对端口进行抓包,tcpdump抓包使用详解

tcpdump能帮助我们捕捉并保存网络包,保存下来的网络包可用于分析网络负载情况,包可通过tcpdump命令解析,也可以保存成后缀为pcap的文件,使用wireshark等软件进行查看。

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

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

linux:/tmp/lx # tcpdump -i eth0

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

10:50:28.607429 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 116 win 64951

10:50:28.607436 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 116:232(116) ack 1 win 12864

10: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 eth0

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

10:58:05.656104 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1210443473:1210443589(116) ack 2583117929 win 12864

10:58:05.657074 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 116 win 65211

2 packets captured

6 packets received by filter

0 packets dropped by kernel

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

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

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

linux:/tmp/lx # tcpdump -w 20120606.pcap -i eth0

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

75 packets captured

150 packets received by filter

0 packets dropped by kernel

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

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

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

linux:/tmp/lx # tcpdump -r 20120606.pcap

reading 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 12864

11:01:57.392917 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 52:168(116) ack 1 win 12864

11: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 eth0

2012-06-06 11:14:59.539736 IP 10.71.171.140.22 > 10.70.121.95.1787: P 1:53(52) ack 100 win 7504

2012-06-06 11:14:59.539754 IP 10.71.171.140.22 > 10.70.121.95.1787: P 53:105(52) ack 100 win 7504

2012-06-06 11: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 arp

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

11:22:26.948656 arp who-has 10.10.1.30 tell 10.10.1.26

11:22:27.017406 arp who-has 10.10.1.30 tell 10.10.1.26

11:22:27.078803 arp who-has 10.10.1.30 tell 10.10.1.26

8.指定抓包端口

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

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

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes

11:41:04.387547 IP 10.70.121.92.autodesk-lm > 10.71.171.140.ssh: . ack 1216136825 win 64751

11:41:04.387891 IP 10.71.171.140.ssh > 10.70.121.92.autodesk-lm: P 1:233(232) ack 0 win 16080

11: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

10. tcpdump 与wireshark

Wireshark(以前是ethereal)是Windows下非常简单易用的抓包工具(也有Linux版本)。但在Linux下很难找到一个好用的图形化抓包工具。

还好有Tcpdump。我们可以用Tcpdump + Wireshark 的完美组合实现:在 Linux 里抓包,然后在Windows 里分析包。

总结:

tcpdump tcp -i eth1 -t -s 0 -c 100 and dst port ! 22 and src net 192.168.1.0/24 -w ./target.cap

(1)tcp: ip icmp arp rarp 和 tcp、udp、icmp这些选项等都要放到第一个参数的位置,用来过滤数据报的类型

(2)-i eth1 : 只抓经过接口eth1的包

(3)-t : 不显示时间戳

(4)-s 0 : 抓取数据包时默认抓取长度为68字节。加上-S 0 后可以抓到完整的数据包

(5)-c 100 : 只抓取100个数据包

(6)dst port ! 22 : 不抓取目标端口是22的数据包

(7)src net 192.168.1.0/24 : 数据包的源网络地址为192.168.1.0/24

(8)-w ./target.cap : 保存成cap文件,方便用ethereal(即wireshark)分析

tcpdump -i eth0 -nnX port 21

选项:

-i 指定网卡接口

-nn 将数据包中的域名与服务转为ip和端口

-A 以ASCII码方式显示每一个数据包(不会显示数据包中链路层头部信息). 在抓取包含网页数据的数据包时, 可方便查看数据

-X 以十六进制和ASCII码的显示数据包内容

port 指定监听端口

[root@www ~]# tcpdump -i eth0 -nnX port 21

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

17:54:08.047349 IP 59.37.125.48.3128 > 172.16.0.11.21: Flags [S], seq 913042176, win 8192, options [mss 1412,nop,wscale 0,nop,nop,sackOK], length 0

0x0000: 4500 0034 01f4 4000 3106 e35f 3b25 7d30 E..4..@.1.._;%}0

0x0010: ac10 000b 0c38 0015 366b eb00 0000 0000 .....8..6k......

0x0020: 8002 2000 bd1e 0000 0204 0584 0103 0300 ................

0x0030: 0101 0402 ....

17:54:08.047378 IP 172.16.0.11.21 > 59.37.125.48.3128: Flags [S.], seq 3851070068, ack 913042177, win 14600, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0

0x0000: 4500 0034 0000 4000 4006 d653 ac10 000b E..4..@.@..S....

0x0010: 3b25 7d30 0015 0c38 e58a aa74 366b eb01 ;%}0...8...t6k..

0x0020: 8012 3908 13cf 0000 0204 05b4 0101 0402 ..9.............

0x0030: 0103 0307

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值