利用wireshark组件editcap和tshark删除报文的不关注层详解

目录

一:概要

二:要处理的报文说明

三:报文处理

步骤1:删除VLAN层

步骤2:过滤带目标层报文

步骤3:分别切割

步骤4:合并报文


一:概要

经常与报文打交道的都知道在不同的应用场景中,pcap报文往往会根据场景应用相应的分层。有时候我们拿到报文后,我们并不关心某些分层,如我只关心TCP,IP和载荷层,其他的分层如GTP,GRE,VLAN等我并不关心,但是这些层的存在又加大了报文分析的难度,所以有必要在分析之前去掉这些不关注的分层。在此我们用到wireshark的两个组件editcap和tshark,下面我们将利用这两个组件组合使用去切割和组合报文。

二:要处理的报文说明

本次要处理的报文有带GTP的,GRE的,VLAN的,也有不带GTP和GRE的报文,目标就是去掉VLAN层,对于带了GTP和GRE层的报文去掉该分层。

带VLAN层的报文

带GTP层的报文

带GRE层的报文

正常报文(没有以上层的报文)

三:报文处理

步骤1:删除VLAN层

由于所有报文都带有VLAN层,第一步利用editcap去掉VLAN层

editcap用法

SYNOPSIS

editcap [ -a <frame:comment> ] [ -A <start time> ] [ -B <stop time> ] [ -c <packets per file> ] [ -C [offset:]<choplen> ] [ -E <error probability> ] [ -F <file format> ] [ -h ] [ -i <seconds per file> ] [ -o <change offset> ] [ -L ] [ -r ] [ -s <snaplen> ] [ -S <strict time adjustment> ] [ -t <time adjustment> ] [ -T <encapsulation type> ] [ -v ] [ --inject-secrets <secrets type>,<file> ] [ --discard-all-secrets ] [ --capture-comment <comment> ] [ --discard-capture-comment ] infile outfile [ packet#[-packet#] ... ]

editcap -d | -D <dup window> | -w <dup time window> [ -v ] [ -I <bytes to ignore> ] [ --skip-radiotap-header ] infile outfile

editcap [ -V ]

用到的选项是-C参数来删除数据包的指定字节部分

-C [offset:]<choplen>

Sets the chop length to use when writing the packet data. Each packet is chopped by <choplen> bytes of data. Positive values chop at the packet beginning while negative values chop at the packet end.

If an optional offset precedes the <choplen>, then the bytes chopped will be offset from that value. Positive offsets are from the packet beginning, while negative offsets are from the packet end.

This is useful for chopping headers for decapsulation of an entire capture, removing tunneling headers, or in the rare case that the conversion between two file formats leaves some random bytes at the end of each packet. Another use is for removing vlan tags.

NOTE: This option can be used more than once, effectively allowing you to chop bytes from up to two different areas of a packet in a single pass provided that you specify at least one chop length as a positive value and at least one as a negative value. All positive chop lengths are added together as are all negative chop lengths.

命令如下

editcap -C 12:4 cpe.pcap choped_http.pcap

解释:按照VLAN报文协议从12个字节往后的四个字节即为VLAN层,注意不包括第12个字节(从1开始计数),即从第13个字节开始的4个字节,cpe.pcap为原始报文,choped_http.pcap为处理后的报文

步骤2:过滤带目标层报文

由于不是所有报文都带gtp和gre层报文,所以要先过滤出来带gtp的报文,带gre的报文以及正常的报文,用到的是tshark组件

tshark的 [  -2  ] [  -a  <捕捉自动停止条件>] ... [  -b  <捕捉环形缓冲区选项>] ... [  -B  <捕获缓冲区大小>] [  -c  <捕获分组计数>] [  - ?  <配置文件>] [  -d  <层型> == <选择>,<译码作为协议>] [  -D  ] [  -e  <字段>] [  -E  <现场打印选项>] [  -f  <捕获筛选>] [  -F  <文件格式>] [  -g  ] [  -h ] [  -H  <输入hosts文件>] [  -i  <采集界面> | - ] [  -I  ] [  -K  <密钥表>] [  -l  ] [  -L  ] [  -n  ] [  -N  <名称解析标志>] [  -o  <偏好设置>] ... [  -O  <协议>] [  -p  ] [  -P  ] [  -q  ] [  -Q  ] [  -r  <INFILE>] [  -R  <读过滤>] [  -s  <捕获的Snaplen>] [  -S  <分隔符>] [  -t  一个|广告| adoy | D | DD | E | - [R | U | UD | udoy] [  -T  领域| PDML | PS | PSML |文] [  -u  <秒类型>] [  -v  ] [  -V  ] [  -w  <OUTFILE> | - ] [  -W  <文件格式选项>] [  -x  ] [  -X <分机选项>] [  -y  <捕获链接类型>] [  -Y  <显示过滤>] [  -z  <统计>] [  --capture注释  <评论> ] [<捕获筛选>]

用到的参数选项是-r,-Y,-F,-w

利用-r参数读取pcap文件,-F参数指定pcap文件格式,-Y参数指定过滤条件,-w参数保存处理的报文

-Y|--display-filter <displaY filter>

Cause the specified filter (which uses the syntax of read/display filters, rather than that of capture filters) to be applied before printing a decoded form of packets or writing packets to a file. Packets matching the filter are printed or written to file; packets that the matching packets depend upon (e.g., fragments), are not printed but are written to file; packets not matching the filter nor depended upon are discarded rather than being printed or written.

Use this instead of -R for filtering using single-pass analysis. If doing two-pass analysis (see -2) then only packets matching the read filter (if there is one) will be checked against this filter.

命令如下:

tshark -r choped_http.pcap -Y "gtp" -F pcap -w  gtp.pcap
tshark -r choped_http.pcap -Y "not gre and not gtp" -F pcap -w  ip_normal.pcap
tshark -r choped_http.pcap -Y "gre" -F pcap -w  gre.pcap

步骤3:分别切割

分别对过滤过来的gtp.pcap和gre.pcap进行步骤一的删除层操作

命令如下:

editcap -C 14:44 gtp.pcap ip_gtp.pcap
editcap -C 14:24 gre.pcap ip_gre.pcap

步骤4:合并报文

最后合并处理后的报文

命令如下:

mergecap ip_*.pcap -w cpe_ip.pcap -F pcap

经过以上4个步骤即达到了我们的目的,去掉VLAN层,GTP层和GRE层。

处理前的报文如下:

处理后的报文如下:

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ftzchina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值