一、抓包命令概述

tcpdump可以将网络中传送的数据包的完全截获下来提供分析。不带参数的tcpdump会收集网络中所有的信息包头,数据量巨大,所以需要过滤。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来去掉无用的信息。

注意:退出命令行管理前,请务必通过<Ctrl> + <C> 中断抓包操作。抓包操作会因为telnet或者ssh退出而停止,如需要不退出,须在命令行下使用命令system terminal idle-timeout 0设置为终端不超时。

二、抓包命令详解

命令格式:system tcpdump

显示从端口捕获的数据包的报文头以及简单的统计信息。可以使用<Ctrl> + <C> 中断输出。

RG-WALL# system tcpdump -i any host 192.168.83.228 or 192.168.83.237 -n

tcpdump: WARNING: Promiscuous mode not supported on the "any" device

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

listening on any, link-type LINUX_SLL (Linux cooked), capture size 96 bytes

14:20:08.641037 X@dummy IP 192.168.83.237.520 > 224.0.0.9.520: RIPv2, Response,length: 24

14:20:08.641092 X@eth0 IP 192.168.83.237.520 > 224.0.0.9.520: RIPv2, Response, length: 24

14:20:12.434276 R@eth0 IP 192.168.83.228.59920 > 224.0.0.252.5355: UDP, length 22

14:20:12.539475 R@eth0 IP 192.168.83.228.59920 > 224.0.0.252.5355: UDP, length 22

14:20:12.742689 R@eth0 IP 192.168.83.228.137 > 192.168.83.255.137: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST

14:20:13.506788 R@eth0 IP 192.168.83.228.137 > 192.168.83.255.137: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST

14:20:14.271120 R@eth0 IP 192.168.83.228.137 > 192.168.83.255.137: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST

14:20:15.403108 R@eth0 IP 192.168.83.228.63493 > 224.0.0.252.5355: UDP, length 22

14:20:15.503625 R@eth0 IP 192.168.83.228.63493 > 224.0.0.252.5355: UDP, length 22

14:20:15.706703 R@eth0 IP 192.168.83.228.137 > 192.168.83.255.137: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST

10 packets captured                                               //抓到的数据包数量

20 packets received by filter                                  //由过滤所得的总数据包数量

0 packets dropped by kernel                                 //被核心所丢弃的数据包

关键字介绍

tcpdump的表达式为正则表达式,一般有以下几种:

第一种是关于类型的关键字,主要包括host,net, port。例如 host 210.27.48.2,指明 210.27.48.2是一台主机,net 202.0.0.0 指明 202.0.0.0是一个网络地址,port 23 指明端口号是23。如果没有指定类型,缺省的类型是host。

第二种是确定传输方向的关键字,主要包括src , dst ,dst or src, dst and src ,这些关键字指明了传输的方向。举例说明,src 210.27.48.2 ,指明ip包中源地址是210.27.48.2 , dst net 202.0.0.0 指明目的网络地址是202.0.0.0 。如果没有指明方向关键字,则缺省是src or dst关键字。

第三种是协议的关键字,主要包括ip, arp, tcp, udp 等。如果没有指定任何协议,则tcpdump将会监听所有协议的信息包。

还有三种逻辑运算,取非运算是not’‘;与运算是and'   '&&;或运算是or '||'。

常用选项介绍

-i      :  后面接要监听的网络接口,如eth0,lo,ppp0等。

-n     : 不把网络地址转换成名字 。

-nn   :  不进行端口名称的转换。

-q     : 快速输出。只输出较少的协议信息。

-t      : 不在每一行中输出时间戳。

-v      : 输出一个稍微详细的信息。

-vv   :输出详细的报文信息。

三、输出结果介绍

基本上tcpdump总的的输出格式为:系统时间 来源主机.端口 > 目标主机.端口 数据包参数

tcpdump 的输出格式与协议有关。

下面介绍几种典型的tcpdump命令的输出信息。

(1)ARP包的输出信息

命令:system tcpdump arp

RG-WALL# system tcpdump arp

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

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

14:52:36.688876 R@eth0 arp who-has 192.168.83.123 tell 192.168.83.251

14:52:37.692907 R@eth0 arp who-has 192.168.83.123 tell 192.168.83.251

14:52:38.692858 R@eth0 arp who-has 192.168.83.123 tell 192.168.83.251

14:52:39.692876 R@eth0 arp who-has 192.168.83.123 tell 192.168.83.251

14:52:39是时间戳,692876是ID号,R@eth0表明主机收到该分组,arp表明是ARP请求包,who-has 192.168.83.123 tell 192.168.83.251表明是主机192.168.83.251请求主机192.168.83.123的MAC地址。

(2)TCP包的输出信息

命令:system tcpdump tcp

RG-WALL# system tcpdump tcp

15:50:26.547536 X@eth0 IP 172.18.22.89.443 > 172.18.22.72.63247: F 1:1(0) ack 2 win 65494

15:50:26是时间戳,547536是ID号,X@eth0表明从主机发出该分组,IP表明是IP数据包,172.18.22.89.443是源地址和端口,172.18.22.72.63247是目的地址和端口,src > dst:表明从源地址到目的地址, flags是TCP报文中的标志信息,S 是SYN标志, F (FIN), P (PUSH) , R (RST) "." (没有标记); 1是报文中数据的顺序号, ack 2表示下次期望的顺序号为2, win 表示接收缓存的窗口大小。

(3)UDP包的输出信息

命令:system tcpdump udp

RG-WALL# system tcpdump udp

16:21:46.333034 R@eth0 IP 172.18.22.73.49983 > 224.0.0.252.5355: UDP, length 22

16:21:46是时间戳,333034是ID号,R@eth0表明主机收到该分组,IP表明是IP数据包,172.18.22.73.49983表示主机1的地址和端口,224.0.0.252.5355表示主机2的地址和端口。 上面的输出行表明从主机1的port1端口发出的一个UDP报文 到主机2的port2端口,类型是UDP, 包的长度是lenth。

四、举例

(1)抓取本机(192.168.31.147)和主机114.114.114.114之间的数据包。

system tcpdump -n -i eth0 host 192.168.31.147 and 114.114.114.114

(2)抓取全部进入服务器192.168.31.147与192.168.31.157的数据包。

system tcpdump -n -i eth0 dst 192.168.31.147 or 192.168.31.157

(3)抓取所有210.27.48.1收到和发出的数据包。

system tcpdump host 210.27.48.1

(4)抓取端口21收到和发出的数据包。

system tcpdump -i eth0 -nn port 21

(5)抓取主机210.27.48.1收到和发出的telnet数据包。

system tcpdump tcp port 23 and host 210.27.48.1