在上一节讲了一些tcpdump常用功能,本期我们继续学习tcpdump后续功能。

流量过滤

    tcpdump不仅支持单个过滤表达式过滤,也还支持多个过滤表达式。但需要注意的是传入的过滤表达式如含有Shell通配符,需将过滤表达式放在单引号内,以预防Shell对其进行解释和通配。如果需要深入学习过滤表达式,可以使用以下命令进行查看:

man pcap-filter

1、只抓取UDP数据包

[root@localhost ~]# tcpdump -i ens5f1 -c 2 'udp'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
14:37:38.852304 IP gateway.35428 > 239.255.255.250.ssdp: UDP, length 395
14:37:38.852458 IP gateway.35428 > 239.255.255.250.ssdp: UDP, length 467
2 packets captured
15 packets received by filter
0 packets dropped by kernel

tcpdump另外还支持如下的协议类型:

ip、ip6、arp、tcp、rarp

2、抓取固定来源的数据包

    对一台主机而言,后台运行的网络有很多个,所连接的服务器也会有很多个。那如果我们只想查看某一个服务器和主机的数据包情况了?在tcpdump中也支持这样的数据包抓包,但需要设置src(source)dst(destination)就可以了,同时tcpdum也支持各条件间逻辑运算,如andor等。

    如果tcpdump未指定dst和src,则默认全匹配,而不区分是dst还是src

[root@localhost ~]# tcpdump -i ens5f1 -t -c 5 'udp' or 'dst 114.114.114.114' or 'src 192.168.8.1'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
IP6 fe80::b072:b447:2702:3fca.55407 > ff02::1:3.hostmon: UDP, length 33
IP 192.168.8.101.52468 > 224.0.0.252.hostmon: UDP, length 33
IP localhost.localdomain.52349 > gateway.domain: 14923+ PTR? 3.0.0.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.f.f.ip6.arpa. (90)
IP gateway.domain > localhost.localdomain.52349: 14923 NXDomain 0/1/0 (154)
IP localhost.localdomain.43621 > gateway.domain: 25497+ PTR? a.c.f.3.2.0.7.2.7.4.4.b.2.7.0.b.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa. (90)
5 packets captured
14 packets received by filter
0 packets dropped by kernel

3、抓取特定端口的数据包

[root@localhost ~]# tcpdump -i ens5f1 -c 10 'src port 53 and udp or dst port 80'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
15:05:42.618107 IP localhost.localdomain.32980 > tsa03s06-in-f14.1e100.net.http: Flags [S], seq 712530441, win 29200, options [mss 1460,sackOK,TS val 792506576 ecr 0,nop,wscale 7], length 0
15:05:42.623275 IP gateway.domain > localhost.localdomain.51286: 19244 1/0/0 PTR tsa03s06-in-f14.1e100.net. (85)
15:05:42.627834 IP gateway.domain > localhost.localdomain.57579: 54387 NXDomain* 0/1/0 (101)
15:05:42.632411 IP gateway.domain > localhost.localdomain.37002: 35102 NXDomain* 0/1/0 (101)
15:05:43.259504 IP gateway.domain > localhost.localdomain.44708: 24032 1/0/0 A 116.62.82.159 (50)
15:05:43.261047 IP gateway.domain > localhost.localdomain.44708: 36078 0/1/0 (107)
15:05:43.337416 IP gateway.domain > localhost.localdomain.37886: 32328 1/0/0 A 118.178.109.187 (53)
15:05:43.339220 IP gateway.domain > localhost.localdomain.37886: 47697 0/1/0 (110)
15:05:43.344380 IP gateway.domain > localhost.localdomain.45436: 31901 6/0/0 CNAME images0.cnblogs.com.cdn.dnsv1.com., CNAME 1069954.p23.tc.cdntip.com., A 180.101.217.192, A 180.101.217.119, A 180.101.217.115, A 180.153.100.201 (181)
15:05:43.356015 IP gateway.domain > localhost.localdomain.45436: 24484 2/1/0 CNAME images0.cnblogs.com.cdn.dnsv1.com., CNAME 1069954.p23.tc.cdntip.com. (174)
10 packets captured
13 packets received by filter
0 packets dropped by kernel

tcpdump还支持的过滤类型如下所示:

- port:端口
- host:主机,如'host 112.65.12.245'
- portranger:端口范围,如'dst portrange 0-22'
- net:网络段,如'dst net 192.168'
如未设置过滤类型,则默认为主机(host)

4、抓取属于两台服务器之间的数据包

[root@localhost ~]# tcpdump -i ens5f1 -c 10 'host 192.168.8.8 and (www.cnblogs.com or www.baidu.com)'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
15:23:03.918266 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [P.], seq 660681023:660681721, ack 812631894, win 238, length 698
15:23:03.930901 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [.], ack 698, win 944, length 0
15:23:03.939766 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [.], seq 1:1461, ack 698, win 944, length 1460
15:23:03.939783 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [.], ack 1461, win 261, length 0
15:23:03.939950 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [.], seq 1461:2921, ack 698, win 944, length 1460
15:23:03.939961 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [.], ack 2921, win 284, length 0
15:23:03.940137 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [P.], seq 2921:4030, ack 698, win 944, length 1109
15:23:03.940146 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [.], ack 4030, win 307, length 0
15:23:03.940484 IP 115.239.210.27.https > localhost.localdomain.47234: Flags [P.], seq 4030:4155, ack 698, win 944, length 125
15:23:03.940496 IP localhost.localdomain.47234 > 115.239.210.27.https: Flags [.], ack 4155, win 307, length 0
10 packets captured
62 packets received by filter
0 packets dropped by kernel

常用协议数据报格式

    在此对协议不做深入讲解,仅列出TCP/IP的数据报格式,有兴趣的话,可以自行查阅相关资料。

  • IP协议数据报格式如下所示:

  • 常用协议编号如下所示:

  • TCP协议数据报格式如下所示:

  • UDP协议数据报格式如下所示:

抓包进阶

    在明白各常用协议的数据报格式后,我们先来看看以下几个示例:

1、抓取本机与百度之间TCP三次握手中的第一个网络包,且排除主机为cnblogs的数据包

[root@localhost ~]# tcpdump -i ens5f1 -c 5 'tcp[tcpflags] & tcp-syn != 0 and dst host www.baidu.com and not dst host www.cnblogs.com'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
15:39:26.311628 IP localhost.localdomain.53062 > 115.239.211.112.https: Flags [S], seq 735716502, win 29200, options [mss 1460,sackOK,TS val 794530269 ecr 0,nop,wscale 7], length 0
15:39:26.505795 IP localhost.localdomain.53078 > 115.239.211.112.https: Flags [S], seq 4031881731, win 29200, options [mss 1460,sackOK,TS val 794530463 ecr 0,nop,wscale 7], length 0
15:39:26.505863 IP localhost.localdomain.53080 > 115.239.211.112.https: Flags [S], seq 867030794, win 29200, options [mss 1460,sackOK,TS val 794530463 ecr 0,nop,wscale 7], length 0
15:39:29.222164 IP localhost.localdomain.53100 > 115.239.211.112.https: Flags [S], seq 4207431041, win 29200, options [mss 1460,sackOK,TS val 794533180 ecr 0,nop,wscale 7], length 0
15:39:29.235246 IP localhost.localdomain.53102 > 115.239.211.112.https: Flags [S], seq 548060444, win 29200, options [mss 1460,sackOK,TS val 794533193 ecr 0,nop,wscale 7], length 0
5 packets captured
15 packets received by filter
0 packets dropped by kernel

2、抓取IP包长度大于500的数据包

[root@localhost ~]# tcpdump -i ens5f1 -c 5 'ip[2:2]>500'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens5f1, link-type EN10MB (Ethernet), capture size 262144 bytes
15:35:27.613243 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [.], seq 1864189501:1864198141, ack 2730205778, win 229, length 8640
15:35:27.613255 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [P.], seq 8640:9410, ack 1, win 229, length 770
15:35:28.181058 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [.], seq 9410:13730, ack 11, win 229, length 4320
15:35:28.181073 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [.], seq 13730:18050, ack 11, win 229, length 4320
15:35:28.181090 IP localhost.localdomain.5902 > 112.65.12.245.35445: Flags [.], seq 18050:22370, ack 11, win 229, length 4320
5 packets captured
6 packets received by filter
0 packets dropped by kernel

proto[expr:size]详细解释如下所示:

proto:就是protocol的缩写,表示协议的名称。可以指定的协议类型大致如下:

- ether:链路层协议  - fddi:链路层协议
- wlan:链路层协议 - ppp:链路层协议
- link:链路层协议 - ip:网络层
- arp:网络层 - rarp:网络层
- icmp:网络层 - ip6:网络层
- tcp:传输层 - udp:传输层

expr:用来指定数据包偏移量,表示从某个协议数据包的第多个位开始提取内容,默认起始位置为0

size:表示从expr之后提取多少个字节

- 如果只设置expr,而未设置size,则默认提取1个字节
- ip[2:2]:则提取第3和第4个字节
- ip[0]:则提取ip协议的第1个字节

比较运算符:常用的比较运算符如下所示:

 <  >  <=  >=  =  !=

为了增加可读性,TCP协议数据报头中有6个tcpflags标志字段域,在tcpdump中可以用以下方式进行表达:

tcp-urg tcp-ack tcp-psuh tcp-rst tcp-syn tcp-fin

逻辑运算符:如果一个过滤表达式中包含多个过滤表达式,则可以使用逻辑运算符进行串联

- ! / not:表示非关系
- && / and :表示与关系
- || / or:表示或关系

要详细理解上面的这些示例,需要对常用协议的数据报格式有足够的了解和掌握。

[参考资料]

1.计算机网络(第七版) 谢希仁

本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:

Linux 基础教程 30-tcpdump命令-2的更多相关文章

  1. Linux 基础教程 37-进程命令

    pidof     我们知道每个小孩一出生就会一个全国唯一的编号来对其进行标识,用于以后上学,办社保等,就是我们的身份证号.那么在Linux系统中,用来管理运行程序的标识叫做PID,就是大家熟知的进程 ...

  2. Linux 基础教程 32-解压缩命令

        将文件压缩后对提升数据传输效率,降低传输带宽,管理备份数据都有非常重要的功能,因此文件压缩解压技能就成为必备技能.相对于Windows中的文件解压缩工具百花争艳,在Linux中的解压缩工具则要 ...

  3. 嵌入式LINUX基础教程 第2版

    嵌入式LINUX基础教程  第2版 目录 第1章 入门 11.1 为什么选择Linux 11.2 嵌入式Linux现状 21.3 开源和GPL 21.4 标准及相关组织 31.4.1 Linux标准基 ...

  4. Linux基础 - 系统优化及常用命令

    目录 Linux基础系统优化及常用命令 Linux基础系统优化 网卡配置文件详解 ifup,ifdown命令 ifconfig命令 ifup,ifdown命令 ip命令 用户管理与文件权限篇 创建普通 ...

  5. Linux基础系统优化及常用命令

    # Linux基础系统优化及常用命令 [TOC] ## Linux基础系统优化 Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. - ...

  6. Linux基础01 学会使用命令帮助

    Linux基础01 学会使用命令帮助 概述 在linux终端,面对命令不知道怎么用,或不记得命令的拼写及参数时,我们需要求助于系统的帮助文档:linux系统内置的帮助文档很详细,通常能解决我们的问题, ...

  7. Linux基础教程 linux中使用find命令搜索文件常用方法记录

    find是linux非常强大的搜索命令,通过man find查看find手册,可以发现find的说明一屏接一屏,估计要看完也得花不少时间.兄弟连Linux培训 小编总结了下,整理出find常用的使用方 ...

  8. GStreamer基础教程12 - 常用命令工具

    摘要 GStreamer提供了不同的命令行工具用于快速的查看信息以及验证Pipeline的是否能够正确运行,在平时的开发过程中,我们也优先使用GStreamer的命令行工具验证,再将Pipeline集 ...

  9. Embedded Linux Primer----嵌入式Linux基础教程--2.4节--嵌入式Linux发行版

    嵌入式Linux发行版 究竟什么是Linux发行版?在Linux内核引导之后,它期望找到并挂载根文件系统.当一个匹配的根文件系统已经挂载上,启动脚本开始运行大量程序和系统要求的工具.这些程序经常调用其 ...

  10. Linux基础教程

    Linux基础教程之<Linux就该这么学>之学习笔记第一篇... ========================= 一.Basic Linux Commands    基本的Linux ...

随机推荐

  1. TimeExit 界面无点击定时退出类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  2. springmvc 使用Jackson的配置

    <!--start:使用Jackson 1.x的配置,需要导入的jar包:jackson-core-lpgl-xxx.jar.jackson-mapper-lgpl-xxx.jar --> ...

  3. SpringCloud初体验:三、Feign 服务间调用(FeignClient)、负载均衡(Ribbon)、容错/降级处理(Hystrix)

    FeignOpenFeign Feign是一种声明式.模板化的HTTP客户端. 看了解释过后,可以理解为他是一种 客户端 配置实现的策略,它实现 服务间调用(FeignClient).负载均衡(Rib ...

  4. vs2012,2013 update 离线下载(知识库)

    由于微软提供的update是在线安装的. 加上layout参数可以全部下载完再安装. 命令行或批处理 VS2013.4.exe /layout

  5. ASI和TS流有什么区别

    TS流是信源码流,最高码率为44.209 Mbit/s,它是经过信源编码后的压缩码流,为了使欲传输的信源信息在传输速率一定的条件下更快更多地传输,还要把数据进行压缩,也就是通过信源编码去掉信息中多余的 ...

  6. emacs之配置8,gdb调试设置

    emacsConfig/gdb-setting.el (global-set-key [(f5)] 'gud-go) (global-set-key [(f7)] 'gud-step) (global ...

  7. 微信小程序获取用户openid,头像昵称信息,后台java代码

    https://blog.csdn.net/qq_39851704/article/details/79025557

  8. mac下搭建appium记录

    要安装的东西: jdk(要配置环境) , sdk(要配置环境) ,node(要配置环境), python(要配置环境) ,appium(要配置环境),appium-python-client ,xco ...

  9. 0001_mysql 5.7.25安装初始化

    一.   下载mysql https://dev.mysql.com/downloads/mysql/ 二.   选择社区版本 三.   选择版本下载: 四.   跳过注册直接下载: 五.   解压后 ...

  10. Tutorial: Getting Started with Spring Security

    This tutorial will cover a basic scenario where it  integrates Spring Security, using database-backe ...