抓包工具

有时候,也许你会有这样的需求,想看一下某个网卡上都有哪些数据包,尤其是当你初步判定你的服务器上有流量攻击。这时,使用抓包工具来抓一下数据包,就可以知道有哪些 IP 在攻击你了。

  • 命令语法
    tcpdump (参数)

  • 命令选项
    -i:指定网卡名,使用指定的网络送出数据包;
    -c:指定数量;
    -w:指定存放位置;
    -r:=read,从指定文件查看数据包数据;

用法:

# tcpdump -nn -i ens33 		//第一个n表示以数字形式显示IP,如果不加该选项会显示成主机名
# tcpdump -nn ens33 port 22 	//(not port 22)指定端口为22的(非22的)
# tcpdump -nn ens33 port 22 and host 192.168.8.1 	//指定多个条件(host:主机,后面跟主机名或IP)
#tcpdump -nn -i ens33 -c 10 -w /tmp/1.cap 	//指定抓包数量和存放位置

  • 抓取数据包并指定存放位置
[root@localhost ~]# 
[root@localhost ~]# tcpdump -nn -i ens33 -c 10 -w /tmp/1.cap
tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes
10 packets captured
12 packets received by filter
0 packets dropped by kernel
[root@adai003 ~]# file /tmp/1.cap    //查看抓取的数据包
/tmp/1.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 65535)

说明:使用 tcpdump -r 命令查看数据包内容


  • 查看抓取的数据包内容
[root@adai003 ~]# tcpdump -r /tmp/1.cap
reading from file /tmp/1.cap, link-type EN10MB (Ethernet)
18:42:15.311230 IP adai003.ssh > 192.168.8.1.61445: Flags [P.], seq 1594109651:1594109799, ack 208567947, win 295, length 148
18:42:15.311978 IP 192.168.8.1.61445 > adai003.ssh: Flags [.], ack 148, win 16316, length 0
18:42:16.296782 IP adai003.ssh > 192.168.8.1.61445: Flags [.], seq 148:3068, ack 1, win 295, length 2920

抓包工具 tshark

  • 安装命令
yum install -y wireshark

用法:

  • 查看指定网卡 80 端口的 1 个 web 服务的访问情况(类似于 web 的访问日志): // 可以很清楚的查看服务器的时间,ip,访问的域名及访问的链接
[root@localhost ~]# tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.reques