Use tcpdump packet capture tools

The basic syntax of tcpdump

TCPDump may be transmitted in the packet network is completely intercepted from the analysis provided. It supports filtering for the network layer, protocol, host, network, or port, and provide and, or, not logic statements and other information to help you get rid of useless. And then analyze the content they need

基本语法
 tcpdump [ -AbdDefhHIJKlLnNOpqStuUvxX# ] [ -B buffer_size ]
               [ -c count ]
               [ -C file_size ] [ -G rotate_seconds ] [ -F file ]
               [ -i interface ] [ -j tstamp_type ] [ -m module ] [ -M secret ]
               [ --number ] [ -Q in|out|inout ]
               [ -r file ] [ -V file ] [ -s snaplen ] [ -T type ] [ -w file ]
               [ -W filecount ]
               [ -E spi@ipaddr algo:secret,...  ]
               [ -y datalinktype ] [ -z postrotate-command ] [ -Z user ]
               [ --time-stamp-precision=tstamp_precision ]
               [ --immediate-mode ] [ --version ]
               [ expression ]
#选项参数说明
-A:以ASCII编码打印每个报文(不包括链路层的头),这对分析网页来说很方便; 
-a:将网络地址和广播地址转变成名字; 
-c:指定监听的数据包数量,达到数量后,tcpdump就会停止抓包; 
-C:用于判断用 -w 选项将报文写入的文件的大小是否超过这个值,如果超过了就新建文件(文件名后缀是1、2、3依次增加); 
-d:将匹配信息包的代码以人们能够理解的汇编格式给出; 
-dd:将匹配信息包的代码以c语言程序段的格式给出; 
-ddd:将匹配信息包的代码以十进制的形式给出;
-D:列出当前主机的所有网卡编号和名称,可以用于选项 -i; 
-e:在输出行打印出数据链路层的头部信息; 
-f:将外部的Internet地址以数字的形式打印出来; 
-F:从指定的文件中读取表达式,忽略其它的表达式; 
-i:指定抓包的网络接口,默认监听网卡编号数字最小的网卡(any为监听全部网卡); 
-l:如果没有使用 -w 选项,就可以将报文打印到 标准输出终端(此时这是默认); 
-n:显示ip,而不是主机名; 
-N:不列出域名; 
-O:不将数据包编码最佳化; 
-p:不让网络界面进入混杂模式; 
-q:快速输出,仅列出少数的传输协议信息; 
-r:从指定的文件中读取包(这些包一般通过-w选项产生); 
-s:指定抓包显示一行的宽度,-s0表示可按包长显示完整的包,经常和-A一起用,默认截取长度为60个字节,但一般ethernet MTU都是1500字节。所以,要抓取大于60字节的包时,使用默认参数就会导致包数据丢失; 
-S:用绝对而非相对数值列出TCP关联数; 
-t:在输出的每一行不打印时间戳; 
-tt:在输出的每一行显示未经格式化的时间戳记; 
-T:将监听到的包直接解释为指定的类型的报文,常见的类型有rpc (远程过程调用)和snmp(简单网络管理协议); 
-v:输出一个稍微详细的信息,例如在ip包中可以包括ttl和服务类型的信息; 
-vv:输出详细的报文信息;比-v输出的内容更为详细 
-x/-xx/-X/-XX:以十六进制显示包内容,几个选项只有细微的差别,详见man手册; 
-w:直接将包写入文件中,并不分析和打印出来; 
expression:用于筛选的逻辑表达式;

Ethereal commonly used combinations of commands
-i interface: tcpdump need to listen to specify interfaces
-n digitally to address explicitly otherwise explicitly for the host name; prohibit resolve ip address
-nn addition -n role also display port a numeric value, otherwise displays the port service name; prohibit resolve address and port number
of the head -X output data packets, in hex and ASCII two ways simultaneously output
-vv produces more verbose output

Common expression capture keywords
1, Type: host (host address), NET (network address), Port (port)
2, a source of the packet is determined: src (source address of the packet), DST (packet destination address); default crawling all packets
3, grab a packet of those protocols: ip, tcp, icmp, arp , udp , etc.
4. other keywords: gateway (gateway), less, Greater, Broadcast
5, combination of expressions: not, and (&&), or (||) (!)

Commonly used packet capture command

#打印TCP会话中的的开始和结束数据包, 并且数据包的源或目的不是本地网络上的主机.(nt: localnet, 实际使用时要真正替换成本地网络的名字))
tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net localnet'

#打印所有源或目的端口是80, 网络层协议为IPv4, 并且含有数据,而不是SYN,FIN以及ACK-only等不含数据的数据包.(ipv6的版本的表达式可做练习)
tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
(nt: 可理解为, 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[]'.)

#打印长度超过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 程序产生的数据包时可用到此表达式 .(nt: 'echo reuqest' 与 'echo reply' 这两种类型的ICMP数据包通常由ping程序产生))
tcpdump 'icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply'

#抓包的详细解析
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)分析

Ethereal test

#指定协议、端口、主机的监听
root@ubuntu:~#tcpdump -i eth0 -nn -X -vv icmp and ip host 10.0.0.11
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
15:19:16.784605 IP (tos 0x0, ttl 64, id 62972, offset 0, flags [DF], proto ICMP (1), length 84)
    10.0.0.10 > 10.0.0.11: ICMP echo request, id 1889, seq 1, length 64
	0x0000:  4500 0054 f5fc 4000 4001 3098 0a00 000a  E..T..@[email protected].....
	0x0010:  0a00 000b 0800 53ec 0761 0001 769d 815e  ......S..a..v..^
	0x0020:  0000 0000 e3e2 0200 0000 0000 1011 1213  ................
	0x0030:  1415 1617 1819 1a1b 1c1d 1e1f 2021 2223  .............!"#
	0x0040:  2425 2627 2829 2a2b 2c2d 2e2f 3031 3233  $%&'()*+,-./0123
	0x0050:  3435 3637                                4567
15:19:16.784644 IP (tos 0x0, ttl 64, id 41563, offset 0, flags [none], proto ICMP (1), length 84)
    10.0.0.11 > 10.0.0.10: ICMP echo reply, id 1889, seq 1, length 64
	0x0000:  4500 0054 a25b 0000 4001 c439 0a00 000b  E..T.[[email protected]....
	0x0010:  0a00 000a 0000 5bec 0761 0001 769d 815e  ......[..a..v..^
	0x0020:  0000 0000 e3e2 0200 0000 0000 1011 1213  ................
	0x0030:  1415 1617 1819 1a1b 1c1d 1e1f 2021 2223  .............!"#
	0x0040:  2425 2627 2829 2a2b 2c2d 2e2f 3031 3233  $%&'()*+,-./0123
	0x0050:  3435 3637                                4567

#抓取指定端口的数据报文
root@ubuntu:~# tcpdump port 80 -i eth0 
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
17:07:36.696161 IP 10.0.0.11.47254 > ubuntu.http: Flags [S], seq 1472611405, win 29200, options [mss 1460,sackOK,TS val 3190386911 ecr 0,nop,wscale 7], length 0
17:07:36.696193 IP ubuntu.http > 10.0.0.11.47254: Flags [S.], seq 1659438311, ack 1472611406, win 28960, options [mss 1460,sackOK,TS val 777612776 ecr 3190386911,nop,wscale 7], length 0
17:07:36.696818 IP 10.0.0.11.47254 > ubuntu.http: Flags [.], ack 1, win 229, options [nop,nop,TS val 3190386911 ecr 777612776], length 0
17:07:36.696916 IP 10.0.0.11.47254 > ubuntu.http: Flags [P.], seq 1:75, ack 1, win 229, options [nop,nop,TS val 3190386912 ecr 777612776], length 74: HTTP: HEAD / HTTP/1.1
17:07:36.696925 IP ubuntu.http > 10.0.0.11.47254: Flags [.], ack 75, win 227, options [nop,nop,TS val 777612777 ecr 3190386912], length 0
17:07:36.697060 IP ubuntu.http > 10.0.0.11.47254: Flags [P.], seq 1:247, ack 75, win 227, options [nop,nop,TS val 777612777 ecr 3190386912], length 246: HTTP: HTTP/1.1 200 OK
17:07:36.697152 IP 10.0.0.11.47254 > ubuntu.http: Flags [.], ack 247, win 237, options [nop,nop,TS val 3190386912 ecr 777612777], length 0
17:07:36.697446 IP 10.0.0.11.47254 > ubuntu.http: Flags [.], ack 248, win 237, options [nop,nop,TS val 3190386912 ecr 777612778], length 0

#打印所有源或目的端口是80, 网络层协议为IPv4, 并且含有数据,而不是SYN,FIN以及ACK-only等不含数据的数据包
root@ubuntu:~# tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
17:13:19.377320 IP 10.0.0.11.47258 > ubuntu.http: Flags [P.], seq 3187218586:3187218660, ack 843194358, win 229, options [nop,nop,TS val 3190729598 ecr 777955463], length 74: HTTP: HEAD / HTTP/1.1
17:13:19.377432 IP ubuntu.http > 10.0.0.11.47258: Flags [P.], seq 1:247, ack 74, win 227, options [nop,nop,TS val 777955463 ecr 3190729598], length 246: HTTP: HTTP/1.1 200 OK

Reference article: https://www.cnblogs.com/htcheng/p/11747852.html

Guess you like

Origin www.cnblogs.com/-one/p/12600004.html