Tcpreplay 、tcpprep、tcprewrite 修改报文使用教程

转自:

Tcpreplay工具使用指导 - 云+社区 - 腾讯云简单的说, tcpreplay是一种pcap包的重放工具, 它可以将用ethreal, wireshark工具抓下来的包原样或经过任意修改后重放回去。它允许你对...https://cloud.tencent.com/developer/article/1661862

目录

1. Tcpreplay的介绍

2. tcpprep

2.1 tcpprep帮助文档

2.2 tcpprep举例

3. tcprewrite

3.1 tcprewrite举例

3.2 修改二层头

3.3 修改三层头

3.4 修改四层头

4. 修改5-7层数据


1. Tcpreplay的介绍

简单的说, tcpreplay是一种pcap包的重放工具, 它可以将用ethreal, wireshark工具抓下来的包原样或经过任意修改后重放回去。它允许你对报文做任意的修改(主要是指对2层, 3层,4层报文头), 指定重放报文的速度等, 这样tcpreplay就可以用来复现抓包的情景以定位bug,以极快的速度重放从而实现压力测试。

Tcpreplay包含几个辅助工具:

tcpprep:划分client和server,可以将client的报文从一个网卡发包,server的报文从一个网卡发包;

tcprewrite:修改2层、3层、4层的报文头部;

tcpreplay:真正的发包,可以选择主网卡、从网卡和发包速率等。

后文中均以mysql.pcap报文为例,报文内容如下:

2. tcpprep

tcpprep工具会生成一个cache文件。

2.1 tcpprep帮助文档

[root@x11 tcpreplay_mysql_test]# tcpprep -h

tcpprep (tcpprep) - Create a tcpreplay cache cache file from a pcap file.
Usage:  tcpprep [ -<flag> [<val>] | --<name>[{=| }<val>] ]...

   -a, --auto=str             Auto-split mode
   -c, --cidr=str             CIDR-split mode
   -r, --regex=str            Regex-split mode
   -p, --port                 Port-split mode
   -e, --mac=str              Source MAC split mode
       --reverse              Matches to be client instead of server
   -C, --comment=str          Embedded cache file comment
       --no-arg-comment       Do not embed any cache file comment
   -x, --include=str          Include only packets matching rule
   -X, --exclude=str          Exclude any packet matching this rule
   -o, --cachefile=str        Output cache file
   -i, --pcap=str             Input pcap file to process
   -P, --print-comment=str    Print embedded comment in the specified cache file
   -I, --print-info=str       Print basic info from the specified cache file
   -S, --print-stats=str      Print statistical information about the specified cache file
   -s, --services=str         Load services file for server ports
   -N, --nonip                Send non-IP traffic out server interface
   -R, --ratio=str            Ratio of client to server packets
   -m, --minmask=num          Minimum network mask length in auto mode
   -M, --maxmask=num          Maximum network mask length in auto mode
   -v, --verbose              Print decoded packets via tcpdump to STDOUT
   -A, --decode=str           Arguments passed to tcpdump decoder
   -V, --version              Print version information
   -h, --less-help            Display less usage information and exit
   -H, --help                 display extended usage information and exit
   -!, --more-help            extended usage information passed thru pager
       --save-opts[=arg]      save the option state to a config file
       --load-opts=str        load options from a config file

Options are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
tcpprep is a 'pcap(3)' file pre-processor which creates a cache file which
provides "rules" for 'tcprewrite(1)' and 'tcpreplay(1)' on how to process
and send packets.

2.2 tcpprep举例

  • 根据源IP,举例一:
[root@x11 tcpreplay_mysql_test]# tcpprep -c 10.5.8.244/24 -i mysql.pcap -o mysql.cach

将文件夹中的mysql.pcap报文中的地址为10.5.8.244/24的流设置为client端,其余流设置为server端。

  • 自动模式,举例二:
[root@x11 tcpreplay_mysql_test]# tcpprep -a client -i mgcp.pcap -o mgcp.cach

上面的命令采用自动/client模式指定分包模式. 自动模式这里按我的理解解释一下:

tcpprep在自动模式下认为有下面行为的IP为client端:

1.发TCP SYN包的一方;

2.发DNS包的一方;

3. 收入到 ICMP-Port Unreachable的一方。

认为有下面行为的一方为server端:

1.发TCP Syn/Ack的一方;

2.发DNS应答的一方;

3.发ICMP-Port Unreachable的一方。

而被认定为server的那一方发的那些包, 将从主网卡发出, 被认定为client的包则从次网卡发出. 而自动/client模式将所有没有认出的包都归为client, 同理自动/server模式将没有认出的包都归为server.这种模式貌似不如按IP地址分类的方式好。

3. tcprewrite

简单地说, tcprewrite就是改写pcap包里的报文头部, 包括2层, 3层, 4层, 5-7层. 从3.0版本以后, 所有改写pcap报文头的操作都从tcpreplay中移到了tcprewrite里了,使用tcprewrite对packet进行修改可以有两个方法, 一种方法是一次修改一项, 生成一个

文件, 再拿这个文件作为输入文件..., 直到完成最后的修改, 如:

tcprewrite --option1=xxx -c input.cach -i input.pcap -o 1.pcap
tcprewrite --option2=xxx -c input.cach -i 1.pcap -o 2.pcap
...
tcprewrite --optionN=xxx -c input.cach -i N-1.pcap -o N.pcap

还有一种方法是一股脑的把所有的选项放到一个命令里完成, 如:

tcprewrite --option1=xxx --option2=xxx ... --optionN=xxx -i input.pcap -c input.cach -o out.pcap

两种方法都是可行的, 也各有利弊. 第一方法明了, 但是复杂, 第二种方法简单但不容易理解. 我的建议是先用第一种方法做试验, 容易调试, 等修改成功了以后再把所有的选项合到一起, 在实际使用的时候用第二种方法. 下面我先给出一个例子, 然后再逐个分析一

下用tcprewrite是如何修改二层, 三层, 四层, 5-7层头的, 以便理解tcprewrite的工作原理。tcprewrite的基本格式是(请注意命令中是没有换行符的, 只为了阅读的方便添加了换行符): 详情可以用tcprewrite ?命令查询详情.#tcprewrite的格式:

$ tcprewrite --enet-smac=host_src_mac,client_src_mac   \
              --enet-dmac=host_dst_mac, client_dst_mac \
             --endpoints=host_dst_ip:client_dst_ip    \
              --portmap=old_port1:new_port1,old_port2, new_port2 \
              -i input.pcap -c input.cach -o out.pcap

解释一下, 该命令的输入参数是input.pcap和input.cach文件, 结果将另存为out.pcap文件. 该命令将所有input.pcap包里的主机包(由input.cach文件指定哪些包是主机包, 哪些包是客户端包)的源mac地址, 目的mac地址, 目的IP地址分别改为 :host_src_mac, host_dst_mac和host_dst_ip, 客户端包源mac地址, 目的mac地址, 目的IP地址分别改为:client_src_mac, client_dst_mac和client_dst_ip, 将端口号由old_port1改为new_port1, 将端口号由old_port2改为new_port2。

3.1 tcprewrite举例

  • 修改报文的源IP、目的IP、源mac和目的mac
[root@x11 tcpreplay_mysql_test]# tcprewrite --enet-smac=11:11:11:11:11:11,22:22:22:22:22:22 --enet-dmac=22:22:22:22:22:22,11:11:11:11:11:11 --endpoints=10.0.0.1:20.0.0.2 -i mysql.pcap -c mysql.cach -o out.pcap

操作完成后查看文件夹下多了一个out.pcap

[root@x11 tcpreplay_mysql_test]# ll
total 188
-rw-r--r-- 1 root root   120 Apr 24 16:04 mysql.cach
-rw-r--r-- 1 root root 91941 Apr 24 16:00 mysql.pcap
-rw-r--r-- 1 root root 91941 Apr 24 16:05 out.pcap

使用wireshark打开out.pcap发现源IP、目的IP、源mac和目的mac都被修改了

3.2 修改二层头

  • 修改MAC地址

如果不指定cache文件, 将把所有包的源mac地址和目的mac地址都改写成12:23:34:45:56:67和66:66:66:66:66:66

[root@x11 tcpreplay_mysql_test]# tcprewrite --enet-smac=12:23:34:45:56:67 --enet-dmac=66:66:66:66:66:66 --infile=mysql.pcap --outfile=mysql_mac.pcap

打开的mysql_mac.pcap如下:

或者

[root@x11 tcpreplay_mysql_test]# tcprewrite --enet-smac=12:23:34:45:56:67 --enet-dmac=66:66:66:66:66:66 -imysql.pcap -o mysql_mac1.pcap

打开的mysql_mac1.pcap如下:

指定cache文件

指定cache文件后, 将server包的目的/源mac地址改写成00:44:66:FC:29:AF/00:66:AA:D1:32:C2, 将client的目的/源mac地址改成: 00:55:22:AF:C6:37/00:22:55:AC:DE:AC, 注意是server地址在前.

[root@x11 tcpreplay_mysql_test]# tcprewrite --enet-dmac=00:44:66:FC:29:AF,00:55:22:AF:C6:37 --enet-smac=00:66:AA:D1:32:C2,00:22:55:AC:DE:AC --cachefile=mysql.cach --infile=mysql.pcap --outfile=mysql_L2.pcap

打开生成的mysql_L2.pcap报文

  • 修改802.1q VLAN

经常客户的抓包带有VLAN头域, 这些包如果不去掉VLAN头是没有办法在自己的交换机上replay的, tcprewrite提供了去掉或添加VLAN的方法:

添加vlan也很简单, 下面的命令将VLAN tag设成40, CFI设成1, VLAN priority设成4.

[root@x11 tcpreplay_mysql_test]# tcprewrite --enet-vlan=add --enet-vlan-tag=40 --enet-vlan-cfi=1 --enet-vlan-pri=4 -i mysql.pcap -o mysql_vlan.pcap

  • 删除vlan很简单:
[root@x11 tcpreplay_mysql_test]# tcprewrite --enet-vlan=del -i mysql_vlan.pcap -o mysql_unvlan.pcap

3.3 修改三层头

  • 修改目的IP

根据cache文件里的标识, 将server的IP改为10.10.1.1, client的IP改为10.10.1.2:

[root@x11 tcpreplay_mysql_test]# tcprewrite --endpoints=10.10.1.1:10.10.1.2 --cachefile=mysql.cach -i mysql.pcap -o mysql_IP.pcap

  • 修改IP的网络部分

IP地址同网络部分和主机部分组成, 下面的命令可以将子网地址为10.0.0.0/8的IP改成子网为172.0.0.0/8

[root@x11 tcpreplay_mysql_test]# tcprewrite --pnat=10.0.0.0/8:172.0.0.0/8 --infile=mysql.pcap --outfile=mysql_network.pcap --skipbroadcast

  • 修改IP头其它部分

修改IPv4头的TOS为50

[root@x11 tcpreplay_mysql_test]# tcprewrite  --tos=50 –infile=mysql.pcap –outfile=mysql_tos.pcap

将IPv6头Traffic Class值改为33

[root@x11 tcpreplay_mysql_test]# tcprewrite  --tclass=33 –infile=mysql.pcap –outfile=mysql_tos.pcap

修改Flow Label field

[root@x11 tcpreplay_mysql_test]#tcprewrite --flowlabel=67234 –infile=mysql.pcap --outfile=mysql_tos.pcap

3.4 修改四层头

和修改IP头一样, 修改4层头的时候tcpwrite会自动计算校验和, 这个就不需要担心了.

  • 修改端口号

将80端口号改为8080, 22改为8022

[root@x11 tcpreplay_mysql_test]# tcprewrite  --portmap=80:8080,22:8022  –infile=mysql.pcap –outfile=mysql_tos.pcap
  • 强制计算传输层校验和:

有些应用可能不计算传输层的校验和, 可以让tcpwrite强制计算一下:

[root@x11 tcpreplay_mysql_test]# tcprewrite  --fixcsum  –infile=mysql.pcap –outfile=mysql_tos.pcap

4. 修改5-7层数据

tcpwrite对5-7层的修改非常有限, 顶多也就是抓包没有抓全, 中间的应用层数据丢了.tcpwrite将没有抓到的数据补成全0, 或者修改tcp/udp的长度字节, 或者将该包丢弃。

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值