Tcpdump:是常用的命令行捕获分析器。它允许用户捕获并显示TCP/IP和其他正通过网络发送或接收的隶属于该计算机的数据包。还可以将网络中传送的数据包的“头”完全截获下来提供分析。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。
- 1.用法案例:
注:在以下案例中,我用类似 192.x.x.x IP地址 来代替实际的IP,用类似于 example.com 域名 代替实际域名。然而 例子中的本地主机名是“stine”,IP地址使用“192.168.0.14”。此外,某些输出出于说明目的已被删除或修改。
从一个特定的以太网接口抓包:
$ tcpdump -i eth0 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 13:40:54.940904 IP stine.local.15342 > example.com.domain: 46527+ PTR? 14.0.168.192.in-addr.arpa. (42) 13:40:54.940918 IP stine.local.15342 > example.com.domain: 46527+ PTR? 14.0.168.192.in-addr.arpa. (42) 13:40:54.963406 IP example.com.domain > stine.local.15342: 46527 NXDomain* 0/1/0 (119) 13:40:54.964613 IP example.com.domain > stine.local.15342: 46527 NXDomain* 0/1/0 (119) 13:40:54.964656 IP stine.local > example.com: ICMP stine.local udp port 15342 unreachable, length 15 ... 9 packets captured 32 packets received by filter 0 packets dropped by kernel
注意:当你不带任何选项执行“tcpdump”时,它会捕获所有流经的所有接口的报文。而”-i”选项则是用来捕获一个既定的以太网接口进行过滤。
仅捕获N个流经eth0接口的数据包:
$ tcpdump -c 2 -i eth0 14:05:49.749471 IP6 fe80::21a:d6ff:fe2b:4641 > ip6-allnodes: ICMP6, router advertisement, length 56 14:05:49.711275 IP stine.local.62198 > example.com.domain: 38572+ PTR? 1.4.6.4.2.6.e....0.8.e.f.ip6.arpa. (90)
用ASCII码显示捕获的数据包:
$ tcpdump -A -i eth0
捕获数据包并将其输出写入到二进制文件(利于今后对包的分析,如 wireshark):
$ tcpdump -w tcpdump-20140103.pcap -i eth0
从以前保存的文件中的读取数据包(例如,用以前的命令):
$ tcpdump -tttt -r tcpdump-20140103.pcap
用他们的实际IP地址来抓包(而不使用默认的反向DNS(RDNS)查找):
$ tcpdump -n -i eth0
使用完整时间戳捕捉数据包:
$ tcpdump -tttt -n -i eth0 2014-01-03 14:19:16.882379 IP6 fe80::21a:d6ff:fe2b:4641 > ff02::1: ICMP6, router advertisement, length 56 2014-01-03 14:19:19.882169 IP6 fe80::21a:d6ff:fe2b:4641 > ff02::1: ICMP6, router advertisement, length 56
$ tcpdump -i eth0 greater 1024
$ tcpdump -i eth0 less 1024
$ tcpdump -i eth0 arp
$ tcpdump -i eth0 not arp and not rarp
$ tcpdump -i eth0 port 22
$ tcpdump -i eth0 dst 192.x.x.x and port 22
- 2.从Tcpdump操作手册派生出的案例
打印所有到达或从sundown发出的包内容:
$ tcpdump host sundown
helios和hot之间,或者helios和ace之间的包:$ tcpdump host helios and \( hot or ace \)
helios的任意主机之间的IP包:$ tcpdump ip host ace and not helios
打印本地主机与Berkeley网络上的主机之间的所有通信数据包(注: ucb-ether, 此处可理解为’Berkeley网络’的网络地址,此表达式最原始的含义可表达为: 打印网络地址为ucb-ether的所有数据包)
tcpdump net ucb-ether
打印所有通过网关snup的ftp数据包(注意, 表达式被单引号括起来了, 这可以防止shell对其中的括号进行错误解析)
tcpdump 'gateway snup and (port ftp or ftp-data)'
打印所有源地址或目标地址是本地主机的IP数据包(如果本地网络通过网关连到了另一网络, 则另一网络并不能算作本地网络.(nt: 此句翻译曲折,需补充).localnet 实际使用时要真正替换成本地网络的名字)
tcpdump ip and not net localnet
打印TCP会话中的的开始和结束数据包, 并且数据包的源或目的不是本地网络上的主机.(注: localnet, 实际使用时要真正替换成本地网络的名字))
tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net localnet'
打印所有源或目的端口是80, 网络层协议为IPv4, 并且含有数据,而不是SYN,FIN以及ACK-only等不含数据的数据包.(ipv6的版本的表达式可做练习)(注: 可理解为, ip[2:2]表示整个ip数据包的长度, (ip[0]&0xf)<<2)表示ip数据包包头的长度(ip[0]&0xf代表包中的IHL域, 而此域的单位为32bit, 要换算成字节数需要乘以4, 即左移2. (tcp[12]&0xf0)>>4 表示tcp头的长度, 此域的单位也是32bit, 换算成比特数为 ((tcp[12]&0xf0) >> 4)<<2,即 ((tcp[12]&0xf0)>>2). ((ip[2:2] – ((ip[0]&0xf)<<2)) – ((tcp[12]&0xf0)>>2)) != 0 表示: 整个ip数据包的长度减去ip头的长度,再减去
tcp头的长度不为0, 这就意味着, ip数据包中确实是有数据.对于ipv6版本只需考虑ipv6头中的’Payload Length’ 与 ‘tcp头的长度’的差值, 并且其中表达方式’ip[]’需换成’ip6[]’.)
tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
打印长度超过576字节, 并且网关地址是snup的IP数据包
tcpdump 'gateway snup and ip[2:2] > 576'
打印所有IP层广播或多播的数据包, 但不是物理以太网层的广播或多播数据报
tcpdump 'ether[0] & 1 = 0 and ip[16] >= 224'
打印除’echo request’和’echo reply’类型以外的ICMP数据包( 比如,需要打印所有非ping 程序产生的数据包时可用到此表达式 .(注: ‘echo reuqest’ 与 ‘echo reply’ 这两种类型的ICMP数据包通常由ping程序产生))
tcpdump 'icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply'
- 3.过滤器
TCP报头格式(TCP V4)(注:一个刻度代表一个位的位置)
源端口(即,源端口号):16位
目的地端口(即,目的地端口号):16位
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Port | Destination Port | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Acknowledgment Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data | |U|A|P|R|S|F| | | Offset| Reserved |R|C|S|S|Y|I| Window | | | |G|K|H|T|N|N| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Checksum | Urgent Pointer | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | data | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
控制位:6位(从左至右):
URG:紧急指针字段显著
ACK:确认字段显著
PSH:推送功能
RST:重置连接
SYN:同步序列号
FIN:从发送方没有更多的数据
OSI模型:All People Seem To Need Data Processing (所有的人看起来都需要数据处理)
层数:7.应用层=> 6.表示层=>5.会话层=>4.传输层=>3.网络层=>2.数据链路层=>1.物理层
标志:
Unskilled = URG(紧急指针有效标志)
Attackers = ACK(应答域有效标志)
Pester = PSH(推送标志)
Real = RST(重置连接标志)
Security = SYN(同步序列号标志)
Folks = FIN(数据结束标志)
U A P R S F 32 16 8 4 2 1
$ tcpdump 'tcp[13] & 2 != 0'
$ tcpdump 'tcp[13] & 32!=0' # OR: 'tcp[13] & 32 == 32' # Capture all URG (URGENT) packets $ tcpdump 'tcp[13] & 16!=0' # OR: 'tcp[13] & 16 == 16' # Capture all ACK (ACKNOWLEDGE) packets $ tcpdump 'tcp[13] & 8!=0' # OR: 'tcp[13] & 8 == 8' # Capture all PSH (PUSH) packets $ tcpdump 'tcp[13] & 4!=0' # OR: 'tcp[13] & 4 == 4' # Capture all RST (RESET) packets $ tcpdump 'tcp[13] & 2!=0' # OR: 'tcp[13] & 2 == 2' # Capture all SYN (SYNCHRONIZE) packets $ tcpdump 'tcp[13] & 1!=0' # OR: 'tcp[13] & 1 == 1' # Capture all FIN (FINISH) packets $ tcpdump 'tcp[13]=18' # OR: 'tcp[13] == 18' # Capture all SYNACK (SYNCHRONIZE/ACKNOWLEDGE) packets
'ether host 00:00:00:00:00:00' # (replace with your MAC) Traffic to or from your MAC address '!ether host 00:00:00:00:00:00' # (replace with your MAC) Traffic not to or from your MAC address 'broadcast' # Broadcast traffic only icmp # ICMP traffic 'icmp[0:2] == 0x0301' # ICMP destination unreachable, host unreachable ip # IPv4 traffic only ip6 # IPv6 traffic only udp # UDP traffic only
$ tcpdump 'tcp[13] = 6'
$ tcpdump 'tcp[tcpflags] & tcp-syn != 0'
$ tcpdump 'tcp[tcpflags] & (tcp-rst) != 0'
- 4.常用过滤器表达式格式:
net A.B.C/n — 所有以IP地址为A.B.C.n 为数据源或者目的地址的数据包
port X — 所有使用X为源端口或目的端口的 TCP 或 UDP 数据包
ether broadcast — 所有以以太网广播为其目标的所有数据包
tcp — 所有 TCP 的数据包
udp — 所有 UDP 的数据包
icmp — 所有 ICMP 的数据包
arp — 所有 ARP 的数据包
