Linux命令之tcpdump命令

一、tcpdump简介

        tcpdump是Linux中强大的网络数据采集分析工具之一,根据使用者的定义对网络上的数据包进行截获的包分析工具。用简单的语言概括就是dump the traffic on a network,是一个运行在linux平台可以根据使用者需求对网络上传输的数据包进行捕获的抓包工具,windows平台有sniffer等工具,tcpdump可以将网络中传输的数据包的“包头”全部捕获过来进程分析,其支持网络层、特定的传输协议、数据发送和接收的主机、网卡和端口的过滤,并提供and、or、not等语句进行逻辑组合捕获数据包或去掉不用的信息。作为互联网上经典的的系统管理员必备工具,tcpdump以其强大的功能,灵活的截取策略,成为每个高级的系统管理员分析网络,排查问题等所必备的工具之一。
        tcpdump提供了源代码,公开了接口,因此具备很强的可扩展性,对于网络维护和入侵者都是非常有用的工具。tcpdump存在于基本的 FreeBSD系统中,由于它需要将网络接口设置为混杂模式,普通用户不能正常执行,但具备root权限的用户可以直接执行它来获取网络上的信息。因此系统中存在网络分析工具主要不是对本机安全的威胁,而是对网络上的其他计算机的安全存在威胁。
       基本上tcpdump的总的输出格式为:系统时间 来源主机.端口 > 目标主机.端口 数据包参数。

二、tcpdump参数说明

-a #将网络地址和广播地址转变成名字
-A #以ASCII格式打印出所有分组,并将链路层的头最小化
-b #数据链路层上选择协议,包括ip/arp/rarp/ipx都在这一层
-c #指定收取数据包的次数,即在收到指定数量的数据包后退出tcpdump
-d #将匹配信息包的代码以人们能够理解的汇编格式输出
-dd #将匹配信息包的代码以c语言程序段的格式输出
-ddd #将匹配信息包的代码以十进制的形式输出
-D #打印系统中所有可以监控的网络接口
-e #在输出行打印出数据链路层的头部信息
-f #将外部的Internet地址以数字的形式打印出来,即不显示主机名
-F #从指定的文件中读取表达式,忽略其他的表达式
-i #指定监听网络接口
-l #使标准输出变为缓冲形式,可以数据导出到文件
-L #列出网络接口已知的数据链路
-n #不把网络地址转换为名字
-N 不输出主机名中的域名部分,例如www.baidu.com只输出www
-nn #不进行端口名称的转换
-P #不将网络接口设置为混杂模式
-q #快速输出,即只输出较少的协议信息
-r #从指定的文件中读取数据,一般是-w保存的文件
-w #将捕获到的信息保存到文件中,且不分析和打印在屏幕
-s #从每个组中读取在开始的snaplen个字节,而不是默认的68个字节
-S #将tcp的序列号以绝对值形式输出,而不是相对值
-T #将监听到的包直接解析为指定的类型的报文,常见的类型有rpc(远程过程调用)和snmp(简单网络管理协议)
-t #在输出的每一行不打印时间戳
-tt #在每一行中输出非格式化的时间戳
-ttt #输出本行和前面以后之间的时间差
-tttt #在每一行中输出data处理的默认格式的时间戳
-u #输出未解码的NFS句柄
-v #输出稍微详细的信息,例如在ip包中可以包括ttl和服务类型的信息
-vv#输出相信的保报文信息

三、tcpdump使用示例

1、抓取指定网卡流量

[root@test1 home]# tcpdump -i ens33 > net.log
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
^C30 packets captured
32 packets received by filter
0 packets dropped by kernel
[root@test1 home]# ll
total 4
-rw-r–r--. 1 root root 2791 Jan 7 22:12 net.log
drwx------. 8 wuhs wuhs 259 Jan 6 22:46 wuhs
[root@test1 home]# cat net.log |more
22:11:53.174729 IP test1.ssh > 192.168.0.32.59891: Flags [P.], seq 1086712261:1086712457, ack 4132323338, win 255, length 196
22:11:53.175902 IP 192.168.0.32.59891 > test1.ssh: Flags [.], ack 196, win 16256, length 0
22:11:53.176831 IP test1.42072 > ns5.hn.chinamobile.com.domain: 28338+ PTR? 32.0.168.192.in-addr.arpa. (43)
22:11:53.184178 IP ns5.hn.chinamobile.com.domain > test1.42072: 28338 NXDomain 0/1/0 (98)
22:11:53.186275 IP test1.34380 > ns5.hn.chinamobile.com.domain: 22847+ PTR? 124.0.168.192.in-addr.arpa. (44)
22:11:53.192446 IP ns5.hn.chinamobile.com.domain > test1.34380: 22847 NXDomain 0/1/0 (99)
22:11:53.192999 IP test1.58010 > ns5.hn.chinamobile.com.domain: 14707+ PTR? 124.211.142.211.in-addr.arpa. (46)
22:11:53.199945 IP ns5.hn.chinamobile.com.domain > test1.58010: 14707 1/0/0 PTR ns5.hn.chinamobile.com. (128)
22:11:53.668170 ARP, Request who-has 192.168.0.203 tell 192.168.0.233, length 46
22:11:53.669161 IP test1.52743 > ns5.hn.chinamobile.com.domain: 54266+ PTR? 203.0.168.192.in-addr.arpa. (44)
22:11:53.675674 IP ns5.hn.chinamobile.com.domain > test1.52743: 54266 NXDomain 0/1/0 (99)
22:11:53.676790 IP test1.54029 > ns5.hn.chinamobile.com.domain: 43114+ PTR? 233.0.168.192.in-addr.arpa. (44)
22:11:53.683412 IP ns5.hn.chinamobile.com.domain > test1.54029: 43114 NXDomain 0/1/0 (99)
22:11:54.670083 ARP, Request who-has 192.168.0.203 tell 192.168.0.233, length 46
22:11:55.136239 IP 192.168.0.189.60983 > 239.255.255.250.ssdp: UDP, length 173

2、抓取指定主机之间的流量

[root@test1 home]# tcpdump -i ens33 host 192.168.0.124 and 192.168.0.186
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
22:20:57.364431 IP test1 > 192.168.0.186: ICMP echo request, id 37769, seq 1, length 64
22:20:57.365428 IP 192.168.0.186 > test1: ICMP echo reply, id 37769, seq 1, length 64
22:20:58.367506 IP test1 > 192.168.0.186: ICMP echo request, id 37769, seq 2, length 64
22:20:58.368513 IP 192.168.0.186 > test1: ICMP echo reply, id 37769, seq 2, length 64
22:21:07.991371 IP 192.168.0.186.35352 > test1.ssh: Flags [S], seq 1009237867, win 29200, options [mss 1460,sackOK,TS val 529144878 ecr 0,nop,wscale 7], length 0
22:21:07.991590 IP test1.ssh > 192.168.0.186.35352: Flags [S.], seq 1477171684, ack 1009237868, win 28960, options [mss 1460,sackOK,TS val 281386347 ecr 529144878,nop,wscale 7], length 0
22:21:07.992537 IP 192.168.0.186.35352 > test1.ssh: Flags [.], ack 1, win 229, options [nop,nop,TS val 529144880 ecr 281386347], length 0
22:21:07.993199 IP 192.168.0.186.35352 > test1.ssh: Flags [P.], seq 1:22, ack 1, win 229, options [nop,nop,TS val 529144880 ecr 281386347], length 21
22:21:07.993248 IP test1.ssh > 192.168.0.186.35352: Flags [.], ack 22, win 227, options [nop,nop,TS val 281386349 ecr 529144880], length 0
22:21:08.030505 IP test1.ssh > 192.168.0.186.35352: Flags [P.], seq 1:22, ack 22, win 227, options [nop,nop,TS val 281386386 ecr 529144880], length 21

3、抓取指定协议的流量

[root@test1 home]# tcpdump -i ens33 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
22:26:11.963976 IP test1 > 192.168.0.186: ICMP echo request, id 37884, seq 1, length 64
22:26:11.964964 IP 192.168.0.186 > test1: ICMP echo reply, id 37884, seq 1, length 64
22:26:12.964888 IP test1 > 192.168.0.186: ICMP echo request, id 37884, seq 2, length 64
22:26:12.965884 IP 192.168.0.186 > test1: ICMP echo reply, id 37884, seq 2, length 64

4、抓取指定端口流量

[root@test1 home]# tcpdump -i ens33 port 53
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
22:27:48.507883 IP test1.39494 > ns5.hn.chinamobile.com.domain: 32423+ A? www.baidu.com. (31)
22:27:48.509413 IP test1.45369 > ns5.hn.chinamobile.com.domain: 50564+ PTR? 124.211.142.211.in-addr.arpa. (46)
22:27:48.515003 IP ns5.hn.chinamobile.com.domain > test1.39494: 32423 3/0/0 CNAME www.a.shifen.com., A 183.232.231.172, A 183.232.231.174 (121)
22:27:48.515580 IP ns5.hn.chinamobile.com.domain > test1.45369: 50564 1/0/0 PTR ns5.hn.chinamobile.com. (128)
22:27:48.516916 IP test1.36967 > ns5.hn.chinamobile.com.domain: 59184+ PTR? 124.0.168.192.in-addr.arpa. (44)
22:27:48.523423 IP ns5.hn.chinamobile.com.domain > test1.36967: 59184 NXDomain 0/1/0 (99)
22:27:48.541507 IP test1.33735 > ns5.hn.chinamobile.com.domain: 20522+ PTR? 172.231.232.183.in-addr.arpa. (46)
22:27:48.547433 IP ns5.hn.chinamobile.com.domain > test1.33735: 20522 1/0/0 PTR localhost. (115)

5、抓取指定源地址和目标地址之间的流量

[root@test1 home]# tcpdump -i ens33 ip src 192.168.0.124 and ip dst www.baidu.com
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
22:30:03.204344 IP test1 > localhost: ICMP echo request, id 37931, seq 1, length 64
22:30:04.206629 IP test1 > localhost: ICMP echo request, id 37931, seq 2, length 64
22:30:19.404977 IP test1.40184 > localhost.http: Flags [S], seq 177499301, win 29200, options [mss 1460,sackOK,TS val 281937760 ecr 0,nop,wscale 7], length 0
22:30:19.434589 IP test1.40184 > localhost.http: Flags [.], ack 390322186, win 229, length 0
22:30:19.435665 IP test1.40184 > localhost.http: Flags [P.], seq 0:78, ack 1, win 229, length 78: HTTP: HEAD / HTTP/1.1
22:30:19.466826 IP test1.40184 > localhost.http: Flags [.], ack 333, win 237, length 0
22:30:19.467996 IP test1.40184 > localhost.http: Flags [F.], seq 78, ack 333, win 237, length 0
22:30:19.497128 IP test1.40184 > localhost.http: Flags [.], ack 334, win 237, length 0

6、抓取指定网段的流量

[root@test1 home]# tcpdump -i ens33 net 192.168.0.0/24 and port 53
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
22:33:26.883823 IP test1.49764 > ns5.hn.chinamobile.com.domain: 10753+ A? www.baidu.com. (31)
22:33:26.885198 IP test1.52670 > ns5.hn.chinamobile.com.domain: 14754+ PTR? 124.211.142.211.in-addr.arpa. (46)
22:33:26.890873 IP ns5.hn.chinamobile.com.domain > test1.49764: 10753 3/0/0 CNAME www.a.shifen.com., A 183.232.231.174, A 183.232.231.172 (121)
22:33:26.891461 IP ns5.hn.chinamobile.com.domain > test1.52670: 14754 1/0/0 PTR ns5.hn.chinamobile.com. (128)
22:33:26.892935 IP test1.38224 > ns5.hn.chinamobile.com.domain: 9422+ PTR? 124.0.168.192.in-addr.arpa. (44)
22:33:26.899410 IP ns5.hn.chinamobile.com.domain > test1.38224: 9422 NXDomain 0/1/0 (99)
22:33:26.923087 IP test1.59743 > ns5.hn.chinamobile.com.domain: 50071+ PTR? 174.231.232.183.in-addr.arpa. (46)
22:33:26.929322 IP ns5.hn.chinamobile.com.domain > test1.59743: 50071 1/0/0 PTR localhost. (115)

四、tcpdump总结

1、命令语法

#tcpdump [协议类型] [源或目标] [主机名称或IP] [or/and/not/!条件组合] [源或目标] [主机名或IP] [or/and/not/!条件组合] [端口] [端口号] …… [or/and/not/!条件组合] [条件]
#tcpdump ip dst 192.168.56.1 and src 192.168.56.210 and port 80 and host ! www.baidu.com

2、关键字

  • 关于数据类型的关键字:
    包括host、port、net,例如host 192.168.1.1表示这是一台主机,net 192.168.0.0/24表示这是一个网络地址,port 22指明端口号是22,如果没有指明类型,则默认的类型是host。
  • 数据传输方向的关键字:
    包括src、dst、dst or src、dst and src,这些关键字指明了传输的方向,比如src 192.168.1.1说明数据包源地址是192.168.1.1,dst net 192.168.0.0/24指明目的网络地址是192.168.0.0,默认是监控主机对主机的src和dst,即默认监听本机和目标主机的所有数据。
  • 协议关键字:
    包括ip、arp、rarp、tcp、udp等,
  • 其他关键字:
    • 运算类型的:or、and、not、!
    • 辅助功能型的:gateway、less、broadcast、greater

猜你喜欢

转载自blog.csdn.net/carefree2005/article/details/112358665