参考:
条件准备:安装相关插件tcpdump、wireshark
[root@apitest ~]# yum install tcpdump
[root@apitest ~]# yum install wireshark
1、默认情况下,直接启动tcpdump将监视第一个网络接口上所有流过的数据包。
[root@shenzhe-243 logs]# tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
15:18:07.555653 IP shenzhen-192-168-1-243.ssh > 192.168.1.134.61071: Flags [P.], seq 2893532511:2893532751, ack 797965885, win 171, length 240
15:18:07.556072 IP 192.168.1.134.61071 > shenzhen-192-168-1-243.ssh: Flags [.], ack 240, win 16317, length 0
2、监视指定网络接口的数据包
[root@shenzhen-243 logs]# tcpdump -i eth3
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth3, link-type EN10MB (Ethernet), capture size 65535 bytes
15:20:11.434016 IP 120.237.113.182.55989 > 113.57.219.13.telnet: UDP, length 27
15:20:11.434377 IP 120.237.113.182.60550 > ns6.gd.cnmobile.net.domain: 25898+ PTR? 13.219.57.113.in-addr.arpa. (44)
3、捕获所有183.14.133.13 的主机收到的和发出的所有的数据包
[root@shenzhen-243 logs]# tcpdump host 183.14.133.13
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
15:21:40.409324 IP 183.14.133.13.13007 > 120.237.113.182.60000: UDP, length 35
15:21:42.183927 IP 183.14.133.13.16138 > 192.168.1.57.8881: UDP, length 12
4、获取主机183.14.133.13除了和主机192.168.1.57之外所有主机通信的ip包
[root@shenzhen-243 logs]# tcpdump host 183.14.133.13 and ! 192.168.1.57
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
15:25:23.054432 IP 183.14.133.13.13005 > 120.237.113.182.60000: UDP, length 35
15:25:25.398892 IP 183.14.133.13.13007 > 120.237.113.182.60000: UDP, length 35
5、获取主机183.14.133.13udp60000接收或发出的所有包
[root@shenzhen-243 logs]# tcpdump udp port 60000 and host 183.14.133.13
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
15:30:13.039861 IP 183.14.133.13.13005 > 120.237.113.182.60000: UDP, length 35
15:30:15.383703 IP 183.14.133.13.13007 > 120.237.113.182.60000: UDP, length 35
6、tcpdump 与wireshark
Wireshark(以前是ethereal)是Windows下非常简单易用的抓包工具。但在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)分析