1. 安装

yum install -y tcpdump

2. tcpdump常用命令

# 检测系统可用接口
命令:tcpdump -D

#抓取所有经过 eth0,目的或源地址是 192.168.29.162 的网络数据
命令:tcpdump -n -i eth0 host 192.168.29.162

# 源地址
命令:tcpdump -i eth1 src host 192.168.29.162

# 目的地址
命令:tcpdump -i eth1 dst host 192.168.29.162

#抓取当前服务器eth0网卡端口8080的网络数据
命令:tcpdump -n -i eth0 port 8080

#抓取mysql执行的sql语句
命令:tcpdump -i eth1 -s 0 -l -w - dst port 3306 | strings

#抓取mysql通讯的网络包(.cap文件用wireshark打开,或者使用.pcap文件格式)
命令:tcpdump -n -nn -tttt -i eth0 -s 65535 'port 3306' -w 20160505mysql.cap

#抓取SMTP数据
命令:tcpdump -i eth1 'tcp[tcpflags] & tcp-syn != 0 and tcp[tcpflags] & tcp-ack != 0'

#抓取HTTP GET数据,"GET "的十六进制是 47455420
命令:tcpdump -i eth1 'tcp[(tcp[12]>>2):4] = 0x47455420'

#抓取SSH返回,"SSH-"的十六进制是 0x5353482D
命令:tcpdump -i eth1 'tcp[(tcp[12]>>2):4] = 0x5353482D'

#实时抓取端口号8080的GET包,然后写入GET.log
命令:tcpdump -i eth0 '((port 8080) and (tcp[(tcp[12]>>2):4]=0x47455420))' -nnAl -w /tmp/GET.log

#抓取指定SYN个数,-c 参数指定抓多少个包。
命令:time tcpdump -nn -i eth0 'tcp[tcpflags] = tcp-syn' -c 10

#抓取源地址段是192.168.91.0/24,且目标端口是8171的tcp报文。
命令:tcpdump -i ens33 src net 192.168.91.0/24 and dst port 8171 and tcp

#抓取源地址段是192.168.91.0/24的icmp报文
命令:tcpdump -i ens33 src net 192.168.91.0/24 and icmp

#抓取源或者目的地址为192.168.91.0/24地址段的icmp报文,且使用-nn使得ip和端口都不做反向解析。
命令:tcpdump -i ens33 -nn net 192.168.91.0/24 and icmp

3. 结合tcpkill命令干掉tcp连接

yum install -y dsniff

tcpkill -i any -9 host ip地址
tcpkill -i any -9 port 端口

其中过滤规则的使用与tcpdump一样,请参考tcpdump的过滤规则。