获取更多相关的嵌入式开发工具,可收藏系列博文,持续更新中:
tcpdump简介
Linux作为网络服务器,特别是作为路由器和网关时,数据的采集和分析是不可少的。TcpDump是Linux中强大的网络数据采集分析工具之一。
Tcpdump源码官网
http://www.tcpdump.org
安装步骤
-
下载tcpdump、libpcap
libpcap-1.8.1.tar.gz tcpdump-4.9.0.tar.gz
安装libpcap
-
解压缩
tar -vxzf libpcap-1.8.1.tar.gz 注:小技巧:Linux下一般压缩文件后缀为.tar.bz2和.tar.gz, 它们解压命令有两三个选项是一致的: xf(v),前者再加上j选项,后者再加上z选项。 -
进入目录
cd libpcap-1.8.1/ -
配置
3536: CC=arm-hisiv400-linux-gcc ./configure --host=arm-hisiv400-linux --target=arm-hisiv400-linux --program-prefix=arm-hisiv400-linux- --with-pcap=linux --prefix=$(pwd)/../output/3536 3519: CC=arm-hisiv600-linux-gcc ./configure --host=arm-hisiv600-linux --target=arm-hisiv600-linux --program-prefix=arm-hisiv600-linux- --with-pcap=linux --prefix=$(pwd)/../output/3519a 3519av100: CC=arm-himix200-linux-gcc ./configure --host=arm-himix200-linux --target=arm-himix200-linux --program-prefix=arm-himix200-linux- --with-pcap=linux --prefix=$(pwd)/../output/3519av100 开发机: ./configure --with-pcap=linux --prefix=$(pwd)/../output/pc 编译参数说明: 1) CC:编译gdb使用的交叉编译工具链,3536和3519的工具不一样 2) --host:编译出来的gdb运行在什么机器上 3) --target:要调试的目标板 4) --program-prefix:编译生成可执行文件的前缀 5) --prefix:make install的的位置 注:–target=arm-linux意思是说目标平台是运行于ARM体系结构的linux内核; –program-prefix=arm-linux-是指生成的可执行文件的前缀, 比如arm-linux-gdb, –prefix是指生成的可执行文件安装在哪个目录,这个目录需要根据实际情况作选择。 如果该目录不存在,会自动创建,当然,权限足够的话。 -
编译、安装
make make install -
完成,继续安装tcpdump
安装tcpdump
-
解压缩
tar -vxf tcpdump-4.9.0.tar.gz 注:小技巧:Linux下一般压缩文件后缀为.tar.bz2和.tar.gz, 它们解压命令有两三个选项是一致的: xf(v),前者再加上j选项,后者再加上z选项。 -
进入目录
cd tcpdump-4.9.0/ -
配置
3536: CC=arm-hisiv400-linux-gcc ./configure --host=arm-hisiv400-linux --target=arm-hisiv400-linux --program-prefix=arm-hisiv400-linux- --prefix=$(pwd)/../output/3536 3519: CC=arm-hisiv600-linux-gcc ./configure --host=arm-hisiv600-linux --target=arm-hisiv600-linux --program-prefix=arm-hisiv600-linux- --prefix=$(pwd)/../output/3519a 3519av100: CC=arm-himix200-linux-gcc ./configure --host=arm-himix200-linux --target=arm-himix200-linux --program-prefix=arm-himix200-linux- --prefix=$(pwd)/../output/3519av100 开发机: ./configure --prefix=$(pwd)/../output/pc 编译参数说明: 1) CC:编译gdb使用的交叉编译工具链,3536和3519的工具不一样 2) --host:编译出来的gdb运行在什么机器上 3) --target:要调试的目标板 4) --program-prefix:编译生成可执行文件的前缀 5) --prefix:make install的的位置 注:–target=arm-linux意思是说目标平台是运行于ARM体系结构的linux内核; –program-prefix=arm-linux-是指生成的可执行文件的前缀, 比如arm-linux-gdb, –prefix是指生成的可执行文件安装在哪个目录,这个目录需要根据实际情况作选择。 如果该目录不存在,会自动创建,当然,权限足够的话。 -
编译,安装
make make install -
过程中间报错:
configure: error: Report this to tcpdump-workers@lists.tcpdump.org, and include the config.log file in your report. If you have downloaded libpcap from tcpdump.org, and built it yourself, please also include the config.log file from the libpcap source directory, the Makefile from the libpcap source directory, and the output of the make process for libpcap, as this could be a problem with the libpcap that was built, and we will not be able to determine why this is happening, and thus will not be able to fix it, without that information, as we have not been able to reproduce this problem ourselves. 原因: /usr/bin/pcap-config 文件不对,需要替换(root权限才能操作) 解决: su cp xxx/pcap-config /usr/bin 输出: ls ../output/xxx/sbin/tcpdump
TcpDump的使用
命令功能
tcpdump:在网络上抓取并转储流量报文。
命令格式
tcpdump采用命令行方式对接口的数据包进行筛选抓取,其丰富特性表现在灵活的表达式上。
不带任何选项的tcpdump,默认会抓取第一个网络接口,且只有将tcpdump进程终止才会停止抓包。
Usage: tcpdump [-aAbdDefhHIJKlLnNOpqStuUvxX#] [ -B size ] [ -c count ]
[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
[ -i interface ] [ -j tstamptype ] [ -M secret ] [ --number ]
[ -Q in|out|inout ]
[ -r file ] [ -s snaplen ] [ --time-stamp-precision precision ]
[ --immediate-mode ] [ -T type ] [ --version ] [ -V file ]
[ -w file ] [ -W filecount ] [ -y datalinktype ] [ -z postrotate-command ]
[ -Z user ] [ expression ]
注意:
tcpdump只能抓取流经本机的数据包!!
命令参数:
tcpdump -a |--将网络地址和广播地址转变成名字;
-A
-B
-c |--指定要抓取的包数量。注意,是最终要获取这么多个包。例如,指定"-c 10"将获取10个包,但可能已经处理了100个包,只不过只有10个包是满足条件的包。
-C |--在将一个原始分组写入文件之前,检查文件当前的大小是否超过了参数file_size中指定的大小。如果超过了指定大小,则关闭当前文件,然后在打开一个新的文件。
-d |--将匹配信息包的代码以人们能够理解的汇编格式给出;
-dd |--将匹配信息包的代码以c语言程序段的格式给出;
-ddd |--将匹配信息包的代码以十进制的形式给出;
-D |--列出可用于抓包的接口。将会列出接口的数值编号和接口名,它们都可以用于"-i"后。
-e |--输出的每行中都将包括数据链路层头部信息,例如源MAC和目标MAC。
-G
-f |--将外部的Internet地址以数字的形式打印出来;
-F |--从文件中读取抓包的表达式。若使用该选项,则命令行中给定的其他表达式都将失效。
-i |--指定tcpdump需要监听的接口。若未指定该选项,将从系统接口列表中搜寻编号最小的已配置好的接口
-I |--使标准输出变为缓冲行形式;
-j
-m
-M
-n |--对地址以数字方式显式,否则显式为主机名,也就是说-n选项不做主机名解析。
-nn |--指定将每个监听到的数据包中的域名转换成IP、端口从应用名称转换成端口号后显示
-N |--不打印出host的域名部分。例如tcpdump将会打印'nic'而不是'nic.ddn.mil'。
-p |--将网卡设置为非混杂模式,不能与host或broadcast一起使用;
-P |--指定要抓取的包是流入还是流出的包。可以给定的值为"in"、"out"和"inout",默认为"inout"。
-q |--快速打印输出。即打印很少的协议相关信息,从而输出行都比较简短。
-r |--从指定的文件中读取包(这些包一般通过-w选项产生);
-S |--将tcp的序列号以绝对值形式输出,而不是相对值。
-s |--从每个分组中读取最开始的snaplen个字节,而不是默认的68个字节。
-v |--输出一个稍微详细的信息,例如在ip包中可以包括ttl和服务类型的信息;
-vv |--输出详细的报文信息;
-V
-s |--设置tcpdump的数据包抓取长度为len,如果不设置默认将会是68字节。
-t |--在输出的每一行不打印时间戳;
-T |--将监听到的包直接解释为指定的类型的报文,常见的类型有rpc (远程过程调用)和snmp(简单网络管理协议;)
-v |--当分析和打印的时候,产生详细的输出。
-vv |--产生比-v更详细的输出。
-vvv |--产生比-vv更详细的输出。
-w |--将抓包数据输出到文件中而不是标准输出。
-W
-X |--输出包的头部数据,会以16进制和ASCII两种方式同时输出。
-XX |--输出包的头部数据,会以16进制和ASCII两种方式同时输出,更详细。
-E
-y
-z
-Z
常用的tcpdump表达式:
表达式用于筛选输出哪些类型的数据包,如果没有给定表达式,所有的数据包都将输出,否则只输出表达式为true的包。在表达式中出现的shell元字符建议使用单引号包围。
tcpdump的表达式由一个或多个"单元"组成,每个单元一般包含ID的修饰符和一个ID(数字或名称)。有三种修饰符:
(1).type:指定ID的类型。
可以给定的值有host/net/port/portrange。例如"host foo","net 128.3","port 20","portrange 6000-6008"。默认的type为host。
(2).dir:指定ID的方向。
可以给定的值包括src/dst/src or dst/src and dst,默认为src or dst。例如,"src foo"表示源主机为foo的数据包,"dst net 128.3"表示目标网络为128.3的数据包,"src or dst port 22"表示源或目的端口为22的数据包。
(3).proto:通过给定协议限定匹配的数据包类型。
常用的协议有tcp/udp/arp/ip/ether/icmp等,若未给定协议类型,则匹配所有可能的类型。例如"tcp port 21","udp portrange 7000-7009"。
所以,一个基本的表达式单元格式为"proto dir type ID"
常用命令示例:
指定网口
tcpdump -i eth1 -n
//监视指定网口eth1上的数据包(如果不用-i参数,默认tcpdump只会监视第一个网络接口,如eth0。-n是以数字方式显示地址)
指定监听协议类型
tcpdump -i eth1 -n tcp
tcpdump -i eth1 -n udp
tcpdump -i eth1 -n arp
tcpdump -i eth1 -n ip
tcpdump -i eth1 -n ether
tcpdump -i eth1 -n icmp
//监视 eth1 上的 icmp 数据包(也可以是其他类型的包 如tcp/udp/arp/ip/ether/icmp等)
指定监听IP
tcpdump -i eth1 -n host 172.16.81.25
tcpdump -i eth1 -n src host 172.16.81.25
tcpdump -i eth1 -n dst host 172.16.81.25
//监视 eth1 上的 IP 为 xxx.xxx.xxx.xxx 的数据包(不区分源IP和目的IP)
//监视 eth1 上的 IP 为 xxx.xxx.xxx.xxx 的数据包(src 就是过滤源地址)
//监视 eth1 上的 IP 为 xxx.xxx.xxx.xxx 的数据包(dst 就是过滤目的地址)
指定监听端口
tcpdump -i eth1 -n port 22223
tcpdump -i eth1 -n src port 22223
tcpdump -i eth1 -n dst port 22223
//监视指定网口 eth1 上 端口 为 22223 的数据包(不区分源端口和目的端口)
//监视指定网口 eth1 上 端口 为 22223 的数据包(src 过滤源端口)
//监视指定网口 eth1 上 端口 为 22223 的数据包(dst 过滤目的端口)
指定监听网段
tcpdump -i eth1 -n net 172.16
tcpdump -i eth1 -n src net 172.16
tcpdump -i eth1 -n dst net 172.16
//监视指定网口 eth1 上 网段 为 xxx.xxx 的数据包(不区分源地址和目的地址)
//监视指定网口 eth1 上 网段 为 xxx.xxx 的数据包(src 过滤源地址)
//监视指定网口 eth1 上 网段 为 xxx.xxx 的数据包(dst 过滤目的地址)
指定监听mac地址
tcpdump -i eth1 -n ether src 00:a0:c9:01:01:01
tcpdump -i eth1 -n ether dst 00:a0:c9:01:01:01
//监视指定网口 eth1 上 mac 地址为 xx:xx:xx:xx:xx:xx 的数据包(源mac地址)
//监视指定网口 eth1 上 mac 地址为 xx:xx:xx:xx:xx:xx 的数据包(目的mac地址)
将上面的规则整合到一起.
tcpdump -i eth1 -n host 172.16.81.25 and port 22223 and net 172.16 and ether src 00:a0:c9:01:01:01 and icmp -w dump.pcap
//将上面的规则整合在一起
解析监听内容
tcpdump -r dump.pcap
//读取 dump.pcap 中的内容
参考: