Linux 下tcpdump命令应用

来源:互联网 发布:php url传递多个参数 编辑:程序博客网 时间:2024/05/01 13:54

tcpdump 可以将网络中传送的数据包的“头”完全截获下来提供分析。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。

作为互联网上经典的的系统管理员必备工具,tcpdump以其强大的功能,灵活的截取策略,成为每个高级的系统管理员分析网络,排查问题等所必备的工具之一,tcpdump提供了源代码,公开了接口,因此具备很强的可扩展性,对于网络维护和入侵者都是非常有用的工具,由于它需要将网络接口设置为混杂模式,普通用户不能正常执行,但具备root权限的用户可以直接执行它来获取网络上的信息。

那么如何去使用tcpdump呢?

tcpdump是一个命令,可以带许多参数,用到不少的关键字。

第一种是关于类型的关键字,主要包括host,net,port

第二种是确定传输方向的关键字,主要包括src , dst ,dst or src, dst and src ,这些关键字指明了传输的方向

第三种是协议的关键字,主要包括fddi,ip,arp,rarp,tcp,udp等类型

其他重要的关键字如下:gateway, broadcast,less,greater

三种逻辑运算,取非运算是 'not ' '! ', 与运算是'and','&&;或运算 是'or' ,'||';这些关键字可以组合起来构成强大的组合条件来满足人们的需要

使用举例:

想获取某张网卡上经过的所有数据包:

[root@fengzhong 桌面]# tcpdump -i eth1tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes11:10:54.674383 ARP, Request who-has 192.168.8.1 tell 192.168.8.63, length 4611:10:54.676628 ARP, Request who-has 192.168.8.1 tell 192.168.8.188, length 2811:10:54.681024 ARP, Reply 192.168.8.1 is-at cc:b2:55:5b:1a:4a (oui Unknown), length 4611:10:54.681056 IP 192.168.8.188.58784 > 192.168.8.1.domain: 6591+ PTR? 1.8.168.192.in-addr.arpa. (42)11:10:54.743698 IP 192.168.8.1.domain > 192.168.8.188.58784: 6591 NXDomain* 0/1/0 (119)11:10:54.743999 IP 192.168.8.188.59307 > 192.168.8.1.domain: 17827+ PTR? 63.8.168.192.in-addr.arpa. (43)11:10:54.879556 IP 192.168.8.53.netbios-ns > 192.168.8.255.netbios-ns: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST11:10:55.091324 IP 192.168.8.1.domain > 192.168.8.188.59307: 17827 NXDomain* 0/1/0 (120)11:10:55.091829 IP 192.168.8.188.32939 > 192.168.8.1.domain: 18876+ PTR? 188.8.168.192.in-addr.arpa. (44)11:10:55.240670 IP 192.168.8.1.domain > 192.168.8.188.32939: 18876 NXDomain* 0/1/0 (121)
想获取某个主机IP 全部的收发数据包信息:

[root@fengzhong 桌面]# tcpdump host 192.168.8.184 -i eth1tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes11:20:26.897363 IP 192.168.8.184.netbios-dgm > 192.168.8.255.netbios-dgm: NBT UDP PACKET(138)11:20:28.610013 IP 192.168.8.184.49775 > 224.0.0.252.hostmon: UDP, length 2411:20:28.710719 IP 192.168.8.184.49775 > 224.0.0.252.hostmon: UDP, length 2411:20:28.911059 IP 192.168.8.184.netbios-ns > 192.168.8.255.netbios-ns: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST11:20:29.660392 IP 192.168.8.184.netbios-ns > 192.168.8.255.netbios-ns: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST11:20:30.410641 IP 192.168.8.184.netbios-ns > 192.168.8.255.netbios-ns: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST11:20:50.568428 IP 192.168.8.184.51602 > 224.0.0.252.hostmon: UDP, length 2411:20:50.668873 IP 192.168.8.184.51602 > 224.0.0.252.hostmon: UDP, length 24
想要获取主机A (192.168.8.184)和主机B(192.168.8.188)之间的收发数据包:

[root@fengzhong 桌面]# tcpdump -i eth1 host 192.168.8.184 and 192.168.8.188tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes11:30:36.375707 ARP, Request who-has 192.168.8.188 tell 192.168.8.184, length 4611:30:36.375757 ARP, Reply 192.168.8.188 is-at 00:0c:29:ed:6e:80 (oui Unknown), length 2811:30:36.375878 IP 192.168.8.184 > 192.168.8.188: ICMP echo request, id 1, seq 1, length 4011:30:36.375911 IP 192.168.8.188 > 192.168.8.184: ICMP echo reply, id 1, seq 1, length 4011:30:37.377503 IP 192.168.8.184 > 192.168.8.188: ICMP echo request, id 1, seq 2, length 4011:30:37.377539 IP 192.168.8.188 > 192.168.8.184: ICMP echo reply, id 1, seq 2, length 4011:30:38.380612 IP 192.168.8.184 > 192.168.8.188: ICMP echo request, id 1, seq 3, length 4011:30:38.380652 IP 192.168.8.188 > 192.168.8.184: ICMP echo reply, id 1, seq 3, length 4011:30:41.376425 ARP, Request who-has 192.168.8.184 tell 192.168.8.188, length 28

想要获取主机A (192.168.8.184)和主机B(192.168.8.188)之间的收发的IP数据包:

想获取主机A 所有收到和发出的 ssh数据包:

tcpdump tcp port 22 and host 192.168.8.188 


希望获取发往主机名为192.168.8.188 的所有数据包

tcpdump -i eth1 dst host 192.168.8.188 


同样,监视某个主机数据包发出,可以如此

tcpdump -i eth1 src host 192.168.8.188 

想要获取主机A与主机B之外的其他主机的收发数据

tcpdump -i eth1 host 192.168.8.188 and ! 192.168.8.120

想要获取主机A与主机B或者主机C之间的收发数据

tcpdump -i eth1 host 192.168.8.188 and \(192.168.8.120 or 192.168.8.184 \)


tcpdump  命令参数

(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)分析

tcpdump -i eth1 -c 10 -s 0 -t -w ./targat.cap host 192.168.8.188 and \(192.168.8.120 or 192.168.8.184 \)

以16进制显示所有数据。

 tcpdump tcp port 22 -i eth1 -c 100 -s 0 -t -nn -X and host 192.168.8.188

[root@fengzhong 桌面]# tcpdump tcp port 22 -i eth1 -c 100 -s 0 -t -nn -X and host 192.168.8.188tcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytesIP 192.168.8.184.50875 > 192.168.8.188.22: Flags [P.], seq 3608998664:3608998716, ack 971889385, win 15060, length 520x0000:  4500 005c 27b4 4000 4006 8023 c0a8 08b8  E..\'.@.@..#....0x0010:  c0a8 08bc c6bb 0016 d71c f308 39ed dae9  ............9...0x0020:  5018 3ad4 e8d6 0000 4956 e1dd e328 3ee4  P.:.....IV...(>.0x0030:  eb7a 4d0b 8cd5 2891 ae30 e9cf 2486 c79e  .zM...(..0..$...0x0040:  0326 4900 b286 3f61 b0e0 6e72 9efb 80e2  .&I...?a..nr....0x0050:  5771 ea7a b562 bbfe c5b5 9dc3            Wq.z.b......IP 192.168.8.188.22 > 192.168.8.184.50875: Flags [P.], seq 1:53, ack 52, win 279, length 520x0000:  4510 005c 42cd 4000 4006 64fa c0a8 08bc  E..\B.@.@.d.....0x0010:  c0a8 08b8 0016 c6bb 39ed dae9 d71c f33c  ........9......<0x0020:  5018 0117 178d 0000 c363 439b a6ff e5a4  P........cC.....0x0030:  9e6e a8fa 0e26 da81 0bea e56d 3c19 8e81  .n...&.....m<...0x0040:  dbe5 d12b 18df 1aa1 7689 053d e5f0 6f2d  ...+....v..=..o-0x0050:  b76e 2447 f5f8 7540 174d ced2            .n$G..u@.M..

抓取http 数据包举例

tcpdump -i eth1 -nvv -X -s 0  host 192.168.8.188 and tcp port 80



0 0
原创粉丝点击