TcpDump的使用
普通情况下,直接启动tcpdump将监视第一个网络界面上所有流过的数据包。
# tcpdump
tcpdump: listening on rl0
22:41:29.128778 10.5.1.155.ssh > 10.5.3.105.41685: P 331248169:331248233(64) ack 4205716752 win 57920
nop,timestamp 166117 2765331> (DF) [tos 0x10]
22:41:29.128938 10.5.3.105.41685 > 10.5.1.155.ssh: . ack 64 win 7176
[tos 0x10]
22:41:29.616392 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:29.616486 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:30.118632 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:30.118756 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:30.120329 10.5.1.155.ssh > 10.5.3.105.41685: P 64:256(192) ack 1 win 57920
2765332> (DF) [tos 0x10]
22:41:30.120544 10.5.3.105.41685 > 10.5.1.155.ssh: . ack 256 win 7176
[tos 0x10]
22:41:30.120661 10.5.1.155.ssh > 10.5.3.105.41685: P 256:704(448) ack 1 win 57920
2765580> (DF) [tos 0x10]
22:41:30.120932 10.5.3.105.41685 > 10.5.1.155.ssh: . ack 704 win 7864
22:41:30.691840 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:30.691935 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:31.119863 10.5.1.155.ssh > 10.5.3.105.41685: P 704:880(176) ack 1 win 57920
2765580> (DF) [tos 0x10]
22:41:31.120003 10.5.1.155.ssh > 10.5.3.105.41685: P 880:1440(560) ack 1 win 57920
2765580> (DF) [tos 0x10]
22:41:31.120065 10.5.3.105.41685 > 10.5.1.155.ssh: . ack 880 win 8552
[tos 0x10]
22:41:31.120309 10.5.3.105.41685 > 10.5.1.155.ssh: . ack 1440 win 8552
[tos 0x10]
22:41:31.198625 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:31.198752 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:31.766946 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
22:41:31.767040 10.5.1.72.45564 > 228.123.123.4.45564: udp 36 [ttl 1]
基本上tcpdump总的的输出格式为:系统时间 ID号 来源主机.端口 > 目标主机.端口 数据包参数
如果在网卡使用混杂模式 系统的日志将会记录
(TEST-WEB1)-root-finance [/var/log]#tail dmesg.today
rl0: promiscuous mode disabled
rl0: promiscuous mode enabled
四、tcpdump参数的使用
-a:将网络地址和广播地址转变成名字;
-d:将匹配信息包的代码以人们能够理解的汇编格式给出;
-dd:将匹配信息包的代码以c语言程序段的格式给出;
-ddd:将匹配信息包的代码以十进制的形式给出;
-e:在输出行打印出数据链路层的头部信息;
-f:将外部的Internet地址以数字的形式打印出来;
-l:使标准输出变为缓冲行形式;
-n:不把网络地址转换成名字;
-t:在输出的每一行不打印时间戳;
-v:输出一个稍微详细的信息,例如在ip包中可以包括ttl和服务类型的信息;
-vv:输出详细的报文信息;
-c:在收到指定的包的数目后,tcpdump就会停止;
-F:从指定的文件中读取表达式,忽略其它的表达式;
-i:指定监听的网络接口,这在计算机具有多个网络界面时非常有用;
-r:从指定的文件中读取包(这些包一般通过-w选项产生);
-w:直接将包写入文件中,并不分析和打印出来;
-T:将监听到的包直接解释为指定的类型的报文,常见的类型有rpc (远程过程 调用)和snmp(简单网络管理协议)
-s:从每个报文中截取snaplen字节的数据,而不是缺省的68(如果是SunOS的NIT,最小值是96).68个字节适用于IP,ICMP,
TCP和UDP,但是有可能截掉名字服务器和NFS报文的协议信息(见下面).输出时如果指定``[|proto]'', tcpdump可以
指出那些捕捉量过小的数据报,这里的proto是截断发生处的协议层名称.注意,采用更大的捕捉范围既增加了处理报
文的时间,又相应的减少了报文的缓冲数量,可能导致报文的丢失。你应该把snaplen设的尽量小,只要能够容纳你需
要的协议信息就可以了.
-S:显示绝对的,而不是相对的TCP序列号
五、TcpDump的运用
1、tcpdump采用命令行方式,它的命令格式为:
tcpdump [ -adeflnNOpqStvx ] [ -c 数量 ] [ -F 文件名 ]
[ -i 网络接口 ] [ -r 文件名] [ -s snaplen ]
[ -T 类型 ] [ -w 文件名 ] [表达式 ]
2、tcpdump的表达式介绍
表达式是一个正则表达式,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关键字。
第三种是协议的关键字,主要包括fddi,ip ,arp,rarp,tcp,udp等类型。Fddi指明是在FDDI(分布式光纤数据接口网络)上的
特定的网络协议,实际上它是"ether"的别名,fddi和ether具有类似的源地址和目的地址,所以可以将fddi协议包当作ether
的包进行处理和分析。其他的几个关键字就是指明了监听的包的协议内容。如果没有指定任何协议,则tcpdump将会监听所有
协议的信息包。
除了这三种类型的关键字之外,其他重要的关键字如下:gateway, broadcast,less,greater,还有三种逻辑运算,取非运算
是 'not ' '! ', 与运算是'and','&&';或运算 是'or' ,'||';
3、实验
(1)想要截获所有10.0.153.39 的主机收到的和发出的所有的数据包:
#tcpdump host 10.0.153.39
(TEST-DNS)-root-financedns[/home/livedoorcn]#tcpdump host 10.0.153.39
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on vr0, link-type EN10MB (Ethernet), capture size 96 bytes
14:38:15.144062 IP finance.livedoor.com.ssh > 10.5.3.105.56745: P 1344499225:1344499417(192) a ck 3235758685\
win 33304
14:38:15.146504 IP 10.5.3.105.56745 > finance.livedoor.com.ssh: . ack 192 win 16022
5735977 122656151>
14:38:16.080527 IP finance.livedoor.com.ssh > 10.5.3.105.56745: P 192:512(320) ack 1 win 33304
timestamp 122656244 5735977>
14:38:16.085164 IP 10.5.3.105.56745 > finance.livedoor.com.ssh: . ack 512 win 16022
5736212 122656244>
(TEST-DNS)-root-financedns[/home/livedoorcn]#ping finance.livedoor.com
PING finance.livedoor.com (10.0.153.39): 56 data bytes
64 bytes from 10.0.153.39: icmp_seq=0 ttl=64 time=0.371 ms
64 bytes from 10.0.153.39: icmp_seq=1 ttl=64 time=0.235 ms
(2)想要截获主机10.0.153.39 和主机10.0.153.132 或10.0.153.38的通信,使用命令:
#tcpdump host 10.0.153.39 and \( 10.0.153.132 or 10.0.153.38 \)
(TEST-WEB1)-root-finance [/home/livedoorcn]#tcpdump host 10.0.153.39 and \(10.0.153.132 or 10.0.153.38\)
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on rl0, link-type EN10MB (Ethernet), capture size 96 bytes
17:33:28.513664 IP 10.0.153.132.49166 > finance.livedoor.com.ssh: P 3076047815:3076047863(48) ack 191506560 \
win 33304
17:33:28.513974 IP finance.livedoor.com.ssh > 10.0.153.132.49166: P 1:65(64) ack 48 win 33304
timestamp 123344768 172221611>
17:33:28.607536 IP 10.0.153.132.49166 > finance.livedoor.com.ssh: . ack 65 win 33304
172221621 123344768>
17:33:31.137588 IP 10.0.153.132.49166 > finance.livedoor.com.ssh: P 48:96(48) ack 65 win 33304
timestamp 172221874 123344768>
17:33:31.137860 IP finance.livedoor.com.ssh > 10.0.153.132.49166: P 65:113(48) ack 96 win 33304
timestamp 123345030 172221874>
17:33:31.237584 IP 10.0.153.132.49166 > finance.livedoor.com.ssh: . ack 113 win 33304
172221884 123345030>
(3)如果想要获取主机10.0.153.39除了和主机10.0.153.132、10.5.3.105之外所有主机通信的ip包,使用命令:
#tcpdump ip host 10.0.153.39 and ! 10.0.153.132 and ! 10.5.3.105
(TEST-WEB1)-root-finance [/home/livedoorcn]#tcpdump ip host 10.0.153.39 and ! 10.0.153.132 and ! 10.5.3.105
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on rl0, link-type EN10MB (Ethernet), capture size 96 bytes
17:36:32.715052 IP test.livedoor.com.64443 > finance.livedoor.com.http: S 1989359758:19893 59758(0) win \
65535
17:36:32.715103 IP finance.livedoor.com.http > test.livedoor.com.64443: S 4236357745:42363 57745(0) ack \
1989359759 win 65535
17:36:32.715211 IP test.livedoor.com.64443 > finance.livedoor.com.http: . ack 1 win 33304
112342868 123363188>
17:36:32.715558 IP test.livedoor.com.64443 > finance.livedoor.com.http: P 1:849(848) ack 1 win 33304\
17:36:32.727829 IP finance.livedoor.com.http > test.livedoor.com.64443: . 1:1449(1448) ack 849 win 33304\
17:36:32.727854 IP finance.livedoor.com.http > test.livedoor.com.64443: . 1449:2897(1448) ack 849 win 33304\
17:36:32.728419 IP test.livedoor.com.64443 > finance.livedoor.com.http: . ack 2897 win 325 80
timestamp 112342869 123363189>
(4)ARP包的TCPDUMP输出信息使用命令
#tcpdump arp
TEST-squid]# tcpdump arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on vr0, link-type EN10MB (Ethernet), capture size 96 bytes
20:08:24.396325 arp who-has test.livedoor.com tell finance.livedoor.com
20:08:24.396341 arp reply test.livedoor.com is-at 00:0d:61:cc:4d:df
分析: 220:08:24是时间戳, 396325是ID号, arp表明是ARP请求包, who-has test.livedoor.com tell finance.\
livedoor.com表明是主机finance.livedoor.com请求主机test.livedoor.com的MAC地址。 00:0d:61:cc:4d:df是主机ICE的MAC地址
(5)过滤指定的接口数据
#tcpdump -i vr1
(TEST-DNS)-root-financedns[/home/livedoorcn]#tcpdump -i vr1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on vr1, link-type EN10MB (Ethernet), capture size 96 bytes
19:19:24.434700 IP 10.4.6.40.ssh > 10.5.3.105.37863: P 923576730:923576922(192) ack 365011995 win 33304\
19:19:24.437520 IP 10.5.3.105.37863 > 10.4.6.40.ssh: . ack 192 win 9320
(TEST-DNS)-root-financedns[/home/livedoorcn]#tcpdump -i vr0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on vr0, link-type EN10MB (Ethernet), capture size 96 bytes
19:19:11.474750 IP dns.livedoor.com.ssh > test.livedoor.com.59301: P 3715345990:3715346182(192) ack 1594125622\
win 33304
19:19:11.474886 IP test.livedoor.com.59301 > dns.livedoor.com.ssh: . ack 192 win 33208
113321413 97785268>
19:19:11.475019 IP test.livedoor.com.ssh > 10.5.3.105.37863: P 923574602:923574794(192) ack 365011755 win \
33304
19:19:11.477552 IP 10.5.3.105.37863 > test.livedoor.com.ssh: . ack 19
(6)过滤的是源主机为10.5.3.105与目的网络为10.4.6.0的报头
#tcpdump src host 10.5.3.105 and dst net 10.4.6.0/24
(TEST-WEB1)-root-finance [/home/livedoorcn]#tcpdump src host 10.5.3.105 and dst net 10.4.6.0/24
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on rl0, link-type EN10MB (Ethernet), capture size 96 bytes
^Z
Suspended
(7)过滤源主机10.0.153.39和目的端口不是telnet的报头
#tcpdump src host 10.0.153.39 and dst port not telnet
(TEST-WEB1)-root-finance [/home/livedoorcn]#tcpdump src host 10.0.153.39 and dst port not telnet
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on rl0, link-type EN10MB (Ethernet), capture size 96 bytes
20:28:48.867836 IP finance.livedoor.com.ssh > 10.5.3.105.54049: P 3905303025:3905303217(192) ack 538379814\
win 33304
20:28:49.862053 IP finance.livedoor.com.63714 > dns.livedoor.com.domain: 49917+ PTR? 105.3.5.10.in-addr.arpa.\
(41)
20:28:49.863859 IP finance.livedoor.com.ssh > 10.5.3.105.54049: P 192:384(192) ack 1 win 33304
timestamp 124396887 10087418>
(8)只过滤源主机10.0.153.39的所有udp报头
#tcpdump udp and src host 10.153.39
(TEST-WEB1)-root-finance [/home/livedoorcn]#tcpdump udp and src host 10.0.153.39
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on rl0, link-type EN10MB (Ethernet), capture size 96 bytes
^Z
Suspended
(9)只过滤源主机10.0.153.39的所有tcp报头
(TEST-DNS)-root-financedns[/home/livedoorcn]#tcpdump -i vr1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on vr1, link-type EN10MB (Ethernet), capture size 96 bytes
19:19:24.434700 IP 10.4.6.40.ssh > 10.5.3.105.37863: P 923576730:923576922(192) ack 365011995 win 33304\
19:19:24.437520 IP 10.5.3.105.37863 > 10.4.6.40.ssh: . ack 192 win 9320
(10)将数据重定向
#tcpdump -l > /home/tcp.txt
(TEST-WEB1)-root-finance [/home/livedoorcn]#cat /home/tcp.txt
20:40:28.980316 IP finance.livedoor.com.ssh > 10.5.3.105.54049: P 3905319025:3905319217(192) ack 538389206 \
win 33304
20:40:28.982823 IP 10.5.3.105.54049 > finance.livedoor.com.ssh: . ack 192 win 6324
10262437 124466797>
20:40:29.972790 IP finance.livedoor.com.63395 > dns.livedoor.com.domain: 13519+ PTR? 105.3.5.10.in-addr.arpa. (41)
20:40:29.974242 IP dns.livedoor.com.domain > finance.livedoor.com.63395: 13519 NXDomain 0/1/0 (118)
(11)不进行IP地址到主机名的转换
#tcpdump -n
(TEST-WEB1)-root-finance [/home/livedoorcn]#tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on rl0, link-type EN10MB (Ethernet), capture size 96 bytes
20:59:06.057524 IP 10.5.3.105.54049 > finance.livedoor.com.ssh: . ack 192 win 7772
10541692 124578503>
(TEST-WEB1)-root-finance [/home/livedoorcn]#tcpdump -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on rl0, link-type EN10MB (Ethernet), capture size 96 bytes
20:59:09.911173 IP 10.5.3.105.54049 > 10.0.153.39.22: . ack 192 win 9220
(12)监视编址到指定端口的TCP或UDP数据包,那么执行以下命令
#tcpdump host 10.0.153.39 and port 80