前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Tcpreplay工具使用指导

Tcpreplay工具使用指导

作者头像
菜鸟小白的学习分享
发布2020-07-14 20:08:21
7.4K0
发布2020-07-14 20:08:21
举报

Tcpreplay的介绍

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

Tcpreplay包含几个辅助工具:

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

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

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

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

tcpprep

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

tcpprep帮助文档

代码语言:javascript
复制
[root@x11 tcpreplay_mysql_test]# tcpprep -h
tcpprep (tcpprep) - Create a tcpreplay cache cache file from a pcap file.

Usage: tcpprep [ -[] | --[{=| }] ]...

-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 Embeded 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.

tcpprep举例

根据源IP,举例一:

代码语言:javascript
复制
[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端。

自动模式,举例二:

代码语言:javascript
复制
[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地址分类的方式好。

tcprewrite

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

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
tcprewrite --option1=xxx --option2=xxx ... --optionN=xxx -i input.pcap -c input.cach -o out.pcap

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

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

代码语言:javascript
复制
$ 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。

tcprewrite举例

修改报文的源IP、目的IP、源mac和目的mac

代码语言:javascript
复制
[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

代码语言:javascript
复制
[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都被修改了

修改二层头

修改MAC地址

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

代码语言:javascript
复制
[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如下:

或者

代码语言:javascript
复制
[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地址在前.

代码语言:javascript
复制
[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.

代码语言:javascript
复制
[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很简单:

代码语言:javascript
复制
[root@x11 tcpreplay_mysql_test]# tcprewrite --enet-vlan=del -i mysql_vlan.pcap -o mysql_unvlan.pcap

修改三层头

修改目的IP

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

代码语言:javascript
复制
[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

代码语言:javascript
复制
[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

代码语言:javascript
复制
[root@x11 tcpreplay_mysql_test]# tcprewrite  --tos=50 –infile=mysql.pcap –outfile=mysql_tos.pcap

将IPv6头Traffic Class值改为33

代码语言:javascript
复制
[root@x11 tcpreplay_mysql_test]# tcprewrite  --tclass=33 –infile=mysql.pcap –outfile=mysql_tos.pcap

修改Flow Label field

代码语言:javascript
复制
[root@x11 tcpreplay_mysql_test]#tcprewrite --flowlabel=67234 –infile=mysql.pcap --outfile=mysql_tos.pcap

修改四层头

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

修改端口号

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

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

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

代码语言:javascript
复制
[root@x11 tcpreplay_mysql_test]# tcprewrite  --fixcsum  –infile=mysql.pcap –outfile=mysql_tos.pcap

修改5-7层数据

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 菜鸟小白的学习分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Tcpreplay的介绍
  • tcpprep
    • tcpprep帮助文档
      • tcpprep举例
        • 根据源IP,举例一:
        • 自动模式,举例二:
    • tcprewrite
      • tcprewrite举例
        • 修改报文的源IP、目的IP、源mac和目的mac
        • 修改二层头
        • 修改三层头
        • 修改四层头
        • 修改5-7层数据
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档