tcpreplay是什么?

  简单的说, tcpreplay是一种pcap包的重放工具, 它可以将用ethreal, wireshark工具抓下来的包原样或经过任意修改后重放回去. 它允许你对报文做任意的修改(主要是指对2层, 3层, 4层报文头), 指定重放报文的速度等, 这样tcpreplay就可以用来复现抓包的情景以定位bug, 以极快的速度重放从而实现压力测试。
  tcpreplay本身包含了几个辅助工具(tcpprep、tcprewrite、tcpreplay和tcpbridge), 用于准备发包的cache, 重写报文等。这也是 Tcpreplay 的第一个字母大写T的原因 。

   * tcpprep - 简单的说就是划分哪些包是client的, 哪些是server的, 一会发包的时候client的包从一个网卡发, server的包可能从另一个网卡发。

        即区分pcap数据包的流向,即区分出客户端和服务器。
      * tcprewrite - 简单的说就是修改2层, 3层, 4层报文头部。

        即改写pcap数据包的2-4层的头部信息,即MAC地址、IP地址和PORT等。
      * tcpreplay - 真正发包, 可以选择主、从网卡, 发包速度等。

        即回放pcap文件中的数据包。

   * tcpreplay-edit-更写pcap数据并回放,将tcprewrite和tcpreplat一条命令实现。
      * tcpbridge - bridge two network segments with the power of tcprewrite。

  以下是官网的原文。

The Tcpreplay suite includes the following tools:

  • tcpprep - multi-pass pcap file pre-processor which determines packets as client or server and creates cache files used by tcpreplay and tcprewrite
  • tcprewrite - pcap file editor which rewrites TCP/IP and Layer 2 packet headers
  • tcpreplay - replays pcap files at arbitrary speeds onto the network
  • tcpliveplay - Replays network traffic stored in a pcap file on live networks using new TCP connections
  • tcpreplay-edit - replays & edits pcap files at arbitrary speeds onto the network
  • tcpbridge - bridge two network segments with the power of tcprewrite
  • tcpcapinfo - raw pcap file decoder and debugger

  官网

http://tcpreplay.synfin.net/

  在CentOS里安装tcpreplay,总的来说,有如下两种安装方法。

  第一种安装方法:(推荐)

  直接yum -y install tcpreplay即可。如果找不到tcpreplay包 先执行 yum -y install epel-release

yum -y install tcpreplay

[root@datatest ~]# tcpreplay -V
tcpreplay version: 4.2.5 (build git:v4.2.5)
Copyright 2013-2017 by Fred Klassen <tcpreplay at appneta dot com> - AppNeta
Copyright 2000-2012 by Aaron Turner <aturner at synfin dot net>
The entire Tcpreplay Suite is licensed under the GPLv3
Cache file supported: 04
Not compiled with libdnet.
Compiled against libpcap: 1.4.0
64 bit packet counters: enabled
Verbose printing via tcpdump: enabled
Packet editing: disabled
Fragroute engine: disabled
Injection method: PF_PACKET send()
Not compiled with netmap
[root@datatest ~]#

  第二种安装方法源码安装:https://blog.csdn.net/ohhpo/article/details/44827623

Tcpreplay的初步使用

tcpreply常用命令:

tcpreplay -i e1000g0 -M 10 -l 5000 /export/home/quincy/apa_lte/diameter.pcap

tcpreplay -i ens33 -M 10 -l 100 bacnet_test.pcap

-i <nic> Primary interface to send traffic out of 双网卡回放报文必选参数,指定主接口。

-M:<multiple> Set replay speed to given multiple 可选参数,指定一个倍数值,就是比默认发送速率要快多少倍的速率发送报文。 加大发送的速率后,对于 DUT 可能意味着有更多的并发连接和连接数,特别是对 于 BT 报文的重放, 因为连接的超时是固定的, 如果速率增大的话, 留在 session
表中的连接数量增大,还可以通过修改连接的超时时间来达到该目的。

-l <loop> Specify number of times to loop 可选参数,指定循环的次数。

  1、查看Tcpreplay里的tcpreplay的帮助文档命令

[root@datatest ~]# tcpreplay -h
tcpreplay (tcpreplay) - Replay network traffic stored in pcap files
Usage:  tcpreplay [ -<flag> [<val>] | --<name>[{=| }<val>] ]... <pcap_file(s)>-q, --quiet                Quiet mode
安静模式-a
精确的时间(使用高速 cpu 发包)-d
输出调试信息(0-5,默认 0)-T, --timer=str            Select packet timing mode: select, ioport, gtod, nano--maxsleep=num         Sleep for no more then X milliseconds between packets-v, --verbose              Print decoded packets via tcpdump to STDOUT
可选参数,每发送一个报文都以 tcpdump 风格打印对应信息-A, --decode=str           Arguments passed to tcpdump decoder
可选参数,在使用 tcpdump 风格打印输出信息时,同时再调用 tcpdump 中的参数-K, --preload-pcap         Preloads packets into RAM before sending-c
双网卡回放报文必选参数,后跟文件名-C, --cachefile=str        Split traffic via a tcpprep cache file
把报文存在内部缓存中-N
获得网络接口和出口-2, --dualfile             Replay two files at a time from a network tap-i, --intf1=str            Client to server/RX/primary traffic output interface
双网卡回放报文必选参数,指定从接口-I, --intf2=str Server to client/TX/secondary traffic output interface--listnics List available network interfaces and exit
双网卡回放报文必选参数,指定主接口-l, --loop=num Loop through the capture file X times--loopdelay-ms=num Delay between loops in milliseconds --pktlen Override the snaplen and use the actual packet len
可选参数,指定循环次数-S
制定包长度-L, --limit=num            Limit the number of packets to send--duration=num         Limit the number of seconds to send
限制发包数量-x, --multiplier=str       Modify replay speed to a given multiple-p, --pps=str              Replay packets at a given packets/sec
可选参数,指定每秒发送报文的个数。制定该参数,其他速率相关的参数被忽略,
打印信息不会有速率和每秒发送报文的统计-M, --mbps=str             Replay packets at a given Mbps
以 Mbps(兆字节每秒)发送报文-m
可选参数,指定一个倍数值,比默认发送速度快多少倍发送报文-t, --topspeed             Replay packets as fast as possible
以最快的速度回放报文-o, --oneatatime           Replay one packet at a time for each user input--pps-multi=num        Number of packets to send for each time interval--unique-ip            Modify IP addresses each loop iteration to generate unique flows--unique-ip-loops=str  Number of times to loop before assigning new unique ip--no-flow-stats        Suppress printing and tracking flow count, rates and expirations--flow-expiry=num      Number of inactive seconds before a flow is considered expired
一次回放一个报文-P, --pid                  Print the PID of tcpreplay at startup--stats=num            Print statistics every X seconds, or every loop if '0'
可选参数,表示在输出信息中打印 PID 信息,用于单用户和单账户模式下暂停和重
启程序-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 fileOptions are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
tcpreplay is a tool for replaying network traffic from files saved with
tcpdump or other tools which write pcap(3) files.Please send bug reports to:  <tcpreplay-users@lists.sourceforge.net>
[root@datatest ~]#

  Tcpreplay 根据 cach 文件来回放 pcap 文件中的数据。

  tcpreplay小例子

[root@datatest ~]#  tcpreplay --intf1=eth1 --intf2=eth0 –t --cachefile=cach_test.cach http_rewrite.pcap

  --intf1=eth1 是指主接口是 eth1, 服务器-> 客户端的数据包通过这个接口发送。

      服务器和客户端的区分是从 tcpprep 的处理结果 cach_test.cach 中得到的。
  --intf2=eth0 是指从接口是 eth0,客户端->服务器的数据包通过这个接口发送。
  --cachefile=cach_test_cach 是指 tcpreplay 用 下面的tcpprep 的处理结果--cach_test.cach 来区分方向。
  http_rewrite.pcap 是指 tcpreplay 发送的是来自 http_rewrite.pcap 这个文件中的数据包 。

[root@datatest ~]#  tcpreplay –i eth1 –I eth0 –c ceshiftp.cach ceshiftp.pcap

  -i, --intf1这是数据重放的时候,的从接口。     -I, --intf2这是数据重放的时候,的主接口。  –c是双网卡回放报文必选参数,后跟文件名

  在linux下, 用ifconfig命令可以获得接口的名字。

  但在cygwin下必须用下面的命令获得接口的名字。
  在cygwin下获得接口的名字:

$ tcpreplay --listnics

  假设在windows下的cygwin里安装的tcpreplay

$ tcpreplay --listnics
|Available network interfaces:
|Alias   Name    Description
|%0      \Device\NPF_GenericDialupAdapter
|        Adapter for generic dialup and VPN capture
|%1      \Device\NPF_{6B508B29-B3E3-4D0B-892F-02914AC9A668}
|    Intel(R) 82566DM Gigabit Network Connection (Microsoft's Packet
|    Scheduler)
|%2      \Device\NPF_{CBCE38CA-1FAD-4AEB-89DF-FD2D8EF861FA}
|    D-Link DFE-530TX PCI Fast Ethernet Adapter (rev.C)
|    (Microsoft's Packet Scheduler)
|%3      \Device\NPF_{ABB813FE-3C51-49A3-8146-16CD2C4507C3}
|    D-Link DFE-530TX PCI Fast Ethernet Adapter (rev.C)
|    (Microsoft's Packet Scheduler)

  从中可以看出这台机器有两块网卡, 一块叫做%1, 另一块叫做%2. 下面就可以指定网卡重发包了:
  用tcpreplay发包:

[root@datatest ~]#  tcpreplay -c mgcp.cach -i %1 -I %2 out.pcap

  这条命令是把out.pcap包, 按照mgcp.cach里划分的主机包和客户端包的方式, 将主机包从网卡%1发出去, 将客户端包从网卡%2发出去.

  注:修改目的IP的MAC地址的时候,MAC地址不是目的机网卡物理地址,而是源机器所连接的交换机MAC地址。

2、查看Tcpreplay里的tcprewrite的帮助文档命令

[root@datatest ~]# tcprewrite -h
tcprewrite (tcprewrite) - Rewrite the packets in a pcap file.
Usage:  tcprewrite [ -<flag> [<val>] | --<name>[{=| }<val>] ]...-r, --portmap=str          Rewrite TCP/UDP ports
重写 tcp/udp 端口-s, --seed=num             Randomize src/dst IPv4/v6 addresses w/ given seed
随机改写 IP 地址-N, --pnat=str             Rewrite IPv4/v6 addresses using pseudo-NAT
通过伪 NAT 重写 ip 地址-S, --srcipmap=str         Rewrite source IPv4/v6 addresses using pseudo-NAT-D, --dstipmap=str         Rewrite destination IPv4/v6 addresses using pseudo-NAT-e, --endpoints=str        Rewrite IP addresses to be between two endpoints
在最后 2 个点之间重写 ip 地址-b, --skipbroadcast        Skip rewriting broadcast/multicast IPv4/v6 addresses
不重写广播/多播 IP 地址-C, --fixcsum              Force recalculation of IPv4/TCP/UDP header checksums
强制重新计算 TP/RCP/UDP 校验和-m, --mtu=num              Override default MTU length (1500 bytes)--mtu-trunc            Truncate packets larger then specified MTU
指定 MTU-E, --efcs                 Remove Ethernet checksums (FCS) from end of frames--ttl=str              Modify the IPv4/v6 TTL/Hop Limit--tos=num              Set the IPv4 TOS/DiffServ/ECN byte--tclass=num           Set the IPv6 Traffic Class byte--flowlabel=num        Set the IPv6 Flow Label
删除以太网最后一帧的校验和(删除最后 2 个字节)-F, --fixlen=str           Pad or truncate packet data to match header length--fuzz-seed=num        Fuzz 1 in X packets.  Edit bytes, length, or emulate packet drop--fuzz-factor=num      Set the Fuzz 1 in X packet ratio (default 1 in 8 packets)--skipl2broadcast      Skip rewriting broadcast/multicast Layer 2 addresses--dlt=str              Override output DLT encapsulation--enet-dmac=str        Override destination ethernet MAC addresses--enet-smac=str        Override source ethernet MAC addresses--enet-subsmac=str     Substitute MAC addresses--enet-mac-seed=num    Randomize MAC addresses--enet-mac-seed-keep-bytes=num Randomize MAC addresses--enet-vlan=str        Specify ethernet 802.1q VLAN tag mode--enet-vlan-tag=num    Specify the new ethernet 802.1q VLAN tag value--enet-vlan-cfi=num    Specify the ethernet 802.1q VLAN CFI value--enet-vlan-pri=num    Specify the ethernet 802.1q VLAN priority--hdlc-control=num     Specify HDLC control value--hdlc-address=num     Specify HDLC address--user-dlt=num         Set output file DLT type--user-dlink=str       Rewrite Data-Link layer with user specified data填充或截取包的数据以匹配包头长度-i, --infile=str           Input pcap file to be processed
处理输出 pcap 文件-o, --outfile=str          Output pcap file
输出 pcap 文件-c, --cachefile=str        Split traffic via tcpprep cache file
使用 tcpprep cach 文件分离流量-v, --verbose              Print decoded packets via tcpdump to STDOUT
tcpdump 风格打印对应信息-A, --decode=str           Arguments passed to tcpdump decoder--skip-soft-errors     Skip writing packets with soft errors
可选参数,在使用 tcpdump 风格输出信息时,同时再调用 tcpdump 中的参数-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 fileOptions are specified by doubled hyphens and their name or by a single
hyphen and the flag character.Please send bug reports to:  <tcpreplay-users@lists.sourceforge.net>
[root@datatest ~]#

  简单地说, tcprewrite 就是改写 pcap 包里的报文头部, 包括 2 层, 3 层, 4 层,即 MAC 地址、IP 地址和PORT 等。tcpreplay 只保证能把包送出去, 至于包真正能到达的地址, 我认为还是根据原来的包的 IP 和 mac,

如果是在同一网段,可能需要把 mac 地址改成测试设备的 mac,如果需要经过网关, 就得将 IP 地址改为测试设备的 IP 以及端口号 。

  tcpreplay只保证能把包送出去, 至于包真正能到达的地址, 我认为还是根据原来的包的IP和mac. 如果是在同一网段, 可能需要把mac地址改成测试设备的mac, 如果需要经过网关, 就得将IP地址改为测试设备的IP以及端口号。
  tcprewrite的基本格式是(请注意命令中是没有换行符的, 只为了阅读的方便添加了换行符): 详请可以用tcprewrite ?命令查询详情。

[root@datatest ~]#   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。

  举个小例子

[root@datatest ~]# tcprewrite --enet-smac=11:22:22:22:22:22,22:22:22:22:22:22  \
--enet-dmac=FF:FF:FF:FF:FF:FF   \
--endpoints=192.168.0.1:192.168.0.11    \
--portmap=5070:5061,9060:5060    \
-i success.pcap -o out.pcap -c success.cach

  该命令将修改后的包的

  主机端的二层, 三层, 四层头分别为: 11:22:22:22:22:22,192.168.0.1,5061,

  客户端包的二层, 三层, 四层头分别为: 22:22:22:22:22:22,192.168.0.11, 5060。

[root@datatest ~]#  tcprewrite --seed=6 -i success.pcap -o out.pcap -c success.cach

  该命令将根据数字 6 按特征算法生成一个新的 server_ip 和 client_ip。

[root@datatest ~]#  tcprewrite --pmap=192.168.0.0/16:10.77.0.0/16,172.16.0.0/12:10.1.0.0/24    \
-i success.pcap -o out.pcap -c success.cach

  该命令将把主机端和客户端 192.168.0.0/16、172.16.0.0/12 网段的 ip 地址分别改为 10.77.0.0/16、10.1.0.0/24。

[root@datatest ~]#   tcprewrite --srcipmap=192.168.0.0/16:10.77.0.0/16   \
-i success.pcap -o out.pcap -c success.cach

  该命令将把主机端和客户端的源 ip 为 192.168.0.0/16 网段改为 10.77.0.0/16 网段

[root@datatest ~]#   tcprewrite --dstipmap=172.16.0.0/12:10.1.0.0/24
-i success.pcap -o out.pcap -c success.cach

  该命令将把主机端和客户端的目的 ip 为 192.168.0.0/16 网段改为 10.77.0.0/16 网段。

  修改2层头

    1) 修改MAC地址
    如果不指定cache文件, 将把所有包的源mac地址和目的mac地址都改写成00:44:66:FC:29:AF和00:55:22:AF:C6:37:

[root@datatest ~]#  tcprewrite --enet-dmac=00:55:22:AF:C6:37 --enet-smac=00:44:66:FC:29:AF --infile=input.pcap --outfile=output.pcap

  指定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@datatest ~]#  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=input.cache --infile=input.pcap --outfile=output.pcap

  2) 修改802.1q VLAN
  经常客户的抓包带有VLAN头域, 这些包如果不去掉VLAN头是没有办法在自己的交换机上replay的, tcprewrite提了去掉或添加VLAN的方法:
    去掉vlan很简单:

[root@datatest ~]#  tcprewrite --enet-vlan=del --infile=input.pcap --outfile=output.pcap

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

[root@datatest ~]#  tcprewrite --enet-vlan=add --enet-vlan-tag=40 --enet-vlan-cfi=1 --enet-vlan-pri=4 --infile=input.pcap --outfile=output.pcap

  3) 修改二层协议名:
  好像是将Ethernet协议头转成Cisco HDLC或其它二层协议? 这部分没有真正用过, 需要的
人自行参考tcpreplay官方网站: http://tcpreplay.synfin.net/wiki/manual

修改3层头
  从版本3.4.2开始, tcprewrite开始支持ipv6协议, tcpreplay升级蛮快。 tcprewrite修改IP地址后会自动帮你计算校验和, 这点还是蛮周到的。

  命令行传入IPv6地址的时候要使用方括号, 例如: [2001::dead:beef]或[2001::/16]
  1) 修改目的IP
  根据cache文件里的标识, 将server的IP改为10.10.1.1, client的IP改为10.10.1.2:

[root@datatest ~]#  tcprewrite --endpoints=10.10.1.1:10.10.1.2 --cachefile=input.cache --infile=input.pcap --outfile=output.pcap --skipbroadcast

  2)修改IP地址的网络部分
    注: 2)和3)没有验证过。

    众所周知, IP地址同网络部分和主机部分组成, 下面的命令可以将子网地址为10.0.0.0/8或192.168.0.0/16的IP改成子网为172.16.0.0/12:

[root@datatest ~]#  tcprewrite --pnat=10.0.0.0/8:172.16.0.0/12,192.168.0.0/16:172.16.0.0/12 --infile=input.pcap --outfile=output.pcap --skipbroadcast

  下面的命令是基于client包或server包修改子网地址:

[root@datatest ~]#   tcprewrite --pnat=10.0.0.0/8:192.168.0.0/24 --pnat=10.0.0.0/8:192.168.1.0/24 --cachefile=input.cache --infile=input.pcap --outfile=output.pcap --skipbroadcast

  3) 修改IP头的其它部分:

  修改IPv4头的TOS为50

[root@datatest ~]#  tcprewrite --tos=50 --infile=input.pcap --outfile=output.pcap

  将IPv6头Traffic Class值改为33

[root@datatest ~]#  tcprewrite --tclass=33 --infile=input.pcap --outfile=output.pcap

  修改Flow Label field:

[root@datatest ~]#  tcprewrite --flowlabel=67234 --infile=input.pcap --outfile=output.pcap

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

  1) 修改端口号
    将80端口号改为8080, 22改为8022:

[root@datatest ~]#  tcprewrite --portmap=80:8080,22:8022 --infile=input.pcap --outfile=output.pcap

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

[root@datatest ~]#  tcprewrite --fixcsum --infile=input.pcap --outfile=output.pcap

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

3、查看Tcpreplay里的tcpprep的帮助文档命令

[root@datatest ~]# 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
一般情况下都需要的参数,表示按模式自动分离通信流量生成 cach 文件 -c, --cidr=str             CIDR-split mode
可选参数, 表示分离流量时采用 CIDR(无类别域间路由选择,是一个在 internet
上创建附加地址的方法,这些地址提供给服务提供商,再由服务提供商分配给客户。
CIDR 将路由集中起来,使一个 IP 地址代表主要骨干提供商提供的几千个 IP 地址,
从而减轻 internet 路由器的负担) -r, --regex=str            Regex-split mode
可选参数,表示使用 regex 模式分离通信流量,有点类似于 CIDR 模式,但是它匹
配的是服务器源 IP -p, --port                 Port-split mode
可选参数,基于目的端口来分离通信流量,它区分的依据是任何 0-1023 端口都是
服务器的端发出的报文,其他的端口都是客户端发出的报文 -e, --mac=str              Source MAC split mode
表示基于服务器源 MAC 地址分离通信流量 --reverse              Matches to be client instead of server-C, --comment=str          Embedded cache file comment
可选参数,表示在 cach 文件中嵌入注释内容,用于注释说明 cach 文件的内容。使用位置不要放在最后,生成 cach 后可以使用-P 查看写入的内容。--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
可选参数,是-x 的取反-o, --cachefile=str        Output cache file生成 cach 文件必带参数,后跟 cach 后缀的文件名,表示这个输出的 cach 文
件以这个文件名命名-i, --pcap=str             Input pcap file to process生成 cach 文件必带参数,后跟 pcap 文件名,表示这个 pcap 文件需要处理-P, --print-comment=str    Print embedded comment in the specified cache file
可选参数,表示查看 cach 文件的内容-I, --print-info=str       Print basic info from the specified cache file
表示打印 cach 文件的基本信息-S, --print-stats=str      Print statistical information about the specified cache file
表示打印 cach 文件的统计信-s, --services=str         Load services file for server ports表示从服务器端口下载服务文件-N, --nonip                Send non-IP traffic out server interface
表示从服务器端口发送无 IP 流量-R, --ratio=str            Ratio of client to server packets
可选参数,一个比例值。服务器端发起的连接数和客户端发起的连接数的比例,值
大于 2 就视为服务器端-m, --minmask=num          Minimum network mask length in auto mode
可选参数,在选用 router 模式时使用,表示最小掩码,掩码默认是 30-M, --maxmask=num          Maximum network mask length in auto mode
可选参数,在选用 router 模式时使用,表示最大掩码,默认是 8-v, --verbose              Print decoded packets via tcpdump to STDOUT
可选参数,显示 tcpprep 生成 cach 文件的处理过程。信息的随时大于-A, --decode=str           Arguments passed to tcpdump decoder
可选参数,在实验 tcpdump 风格打印输出信息时,同时再调用 tcpdump 中的参数-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.Please send bug reports to:  <tcpreplay-users@lists.sourceforge.net>
[root@datatest ~]#

tcpprep 是一个在 tcprewrite 和 tcpreplay 之前使用的 pcap 文件的处理程序。使用 tcpprep 的目的就是建立一个 cach 文件,用于分离通信流量中的两方(通常叫做 主要的/次要的 或者 客户端/服务器),为 tcprewrite 和 tcpreplay 处理与发送报文做准备。

  如果你正打算在两块网卡上使用 tcpreplay 的话,那么 tcpprep 就是用来决定每一个报文(packet)从哪一个接口发出。通过使用这样一个分离的程序来建立一个 cach 文件,tcpreplay 就可以根据这个 cach 文件通过自身的计算来分离流量,高速率的发送报文。cach 文件的作用主要是加速报文的发送,cach 文件中存放着 pcap 文件中每个帧的编号和时间戳等信息,以达到 tcpreplay 回放时可以更加快速的发送报文的目的。

  举个例子, 如果你用 wireshark 或者ethereal抓了一个 pcap 文件,里面可能既有 A 地址发给 B 地址的包,也有 B地址发给 A 址的包,用 tcpprep 工具可以指定从 A 到 B 的包从主网卡发出, 从 B 到 A 的包从次网卡发出。 
  
  tcpprep 用法举例 
1、根据报文源 IP 确定 client/server 报文

[root@datatest ~]#  tcpprep -c 172.22.64.2/24 -i mgcp.pcap -o mgcp.cach 

  上面的命令指定所有源 IP 为 172.22.64.2/24 的包, 都将从主网卡发出, 其它的从次网卡发出 。输入文件是mgcp.pcap, 输出文件为mgcp.cach。

2、使用自动模式确定 client/server 报文

 [root@datatest ~]#  tcpprep -a client -i mgcp.pcap -o mgcp.cach

  上面的命令采用自动/client 模式指定分包模式。

  自动模式这里按我的理解解释一下:
  tcpprep 在自动模式下认为有下面行为的 IP 为 client 端:

    1、发 TCP SYN 包的一方

    2、发 DNS 包的一方

    3、 收入到 ICMP-Port Unreachable 的一方;

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

    1、发 TCPSyn/Ack 的一方

    2、发 DNS 应答的一方

    3、发 ICMP-Port Unreachable 的一方

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

4、查看Tcpreplay里的tcpreplay-edit的帮助文档命令

[root@datatest ~]# tcpreplay-edit -h
tcpreplay (tcpreplay) - Replay network traffic stored in pcap files
Usage:  tcpreplay-edit [ -<flag> [<val>] | --<name>[{=| }<val>] ]... <pcap_file(s)>-r, --portmap=str          Rewrite TCP/UDP ports-s, --seed=num             Randomize src/dst IPv4/v6 addresses w/ given seed-N, --pnat=str             Rewrite IPv4/v6 addresses using pseudo-NAT-S, --srcipmap=str         Rewrite source IPv4/v6 addresses using pseudo-NAT-D, --dstipmap=str         Rewrite destination IPv4/v6 addresses using pseudo-NAT-e, --endpoints=str        Rewrite IP addresses to be between two endpoints-b, --skipbroadcast        Skip rewriting broadcast/multicast IPv4/v6 addresses-C, --fixcsum              Force recalculation of IPv4/TCP/UDP header checksums-m, --mtu=num              Override default MTU length (1500 bytes)--mtu-trunc            Truncate packets larger then specified MTU-E, --efcs                 Remove Ethernet checksums (FCS) from end of frames--ttl=str              Modify the IPv4/v6 TTL/Hop Limit--tos=num              Set the IPv4 TOS/DiffServ/ECN byte--tclass=num           Set the IPv6 Traffic Class byte--flowlabel=num        Set the IPv6 Flow Label-F, --fixlen=str           Pad or truncate packet data to match header length--fuzz-seed=num        Fuzz 1 in X packets.  Edit bytes, length, or emulate packet drop--fuzz-factor=num      Set the Fuzz 1 in X packet ratio (default 1 in 8 packets)--skipl2broadcast      Skip rewriting broadcast/multicast Layer 2 addresses--dlt=str              Override output DLT encapsulation--enet-dmac=str        Override destination ethernet MAC addresses--enet-smac=str        Override source ethernet MAC addresses--enet-subsmac=str     Substitute MAC addresses--enet-mac-seed=num    Randomize MAC addresses--enet-mac-seed-keep-bytes=num Randomize MAC addresses--enet-vlan=str        Specify ethernet 802.1q VLAN tag mode--enet-vlan-tag=num    Specify the new ethernet 802.1q VLAN tag value--enet-vlan-cfi=num    Specify the ethernet 802.1q VLAN CFI value--enet-vlan-pri=num    Specify the ethernet 802.1q VLAN priority--hdlc-control=num     Specify HDLC control value--hdlc-address=num     Specify HDLC address--user-dlt=num         Set output file DLT type--user-dlink=str       Rewrite Data-Link layer with user specified data-q, --quiet                Quiet mode-T, --timer=str            Select packet timing mode: select, ioport, gtod, nano--maxsleep=num         Sleep for no more then X milliseconds between packets-v, --verbose              Print decoded packets via tcpdump to STDOUT-A, --decode=str           Arguments passed to tcpdump decoder-K, --preload-pcap         Preloads packets into RAM before sending-c, --cachefile=str        Split traffic via a tcpprep cache file-2, --dualfile             Replay two files at a time from a network tap-i, --intf1=str            Client to server/RX/primary traffic output interface-I, --intf2=str            Server to client/TX/secondary traffic output interface--listnics             List available network interfaces and exit-l, --loop=num             Loop through the capture file X times--loopdelay-ms=num     Delay between loops in milliseconds--pktlen               Override the snaplen and use the actual packet len-L, --limit=num            Limit the number of packets to send--duration=num         Limit the number of seconds to send-x, --multiplier=str       Modify replay speed to a given multiple-p, --pps=str              Replay packets at a given packets/sec-M, --mbps=str             Replay packets at a given Mbps-t, --topspeed             Replay packets as fast as possible-o, --oneatatime           Replay one packet at a time for each user input--pps-multi=num        Number of packets to send for each time interval--unique-ip            Modify IP addresses each loop iteration to generate unique flows--unique-ip-loops=str  Number of times to loop before assigning new unique ip-!, --no-flow-stats        Suppress printing and tracking flow count, rates and expirations-", --flow-expiry=num      Number of inactive seconds before a flow is considered expired-P, --pid                  Print the PID of tcpreplay at startup-#, --stats=num            Print statistics every X seconds, or every loop if '0'-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 fileOptions are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
tcpreplay is a tool for replaying network traffic from files saved with
tcpdump or other tools which write pcap(3) files.Please send bug reports to:  <tcpreplay-users@lists.sourceforge.net>
[root@datatest ~]#

  tcpreplay-edit 实时修改包数据并回放,它是将 tcprewrite 和 tcpreplay 用一条命令实现。其好处是修改包数据不会新生成 pcap 文件。如果是需要不断的改写一个包文件并回放建议使用 tcpreplay-edit,如果是需要一次改写一个包文件并多次回放建议使用 tcprewrite 和 tcpreplay 的结合,这样具有更好的回放速率。

  tcpreplay-edit小例子

  编写脚本,不断改写包文件的 IP 地址并回放:

  比如,你自己写个test.sh脚本,然后赋予chmod 777 test.sh。执行就是

for i in {1..255}
do
tcpreplay-edit --endpoints=1.1.2.$i:1.1.1.2 -t -i eth2 -I eth1 -c edit.cach edit.pcap
done

5、查看Tcpreplay里的tcpliveplay的帮助文档命令

[root@datatest ~]# tcpliveplay -H
tcpliveplay (tcpliveplay) - Replays network traffic stored in a pcap file on live networks using new TCP connections
Usage:  tcpliveplay [ -<flag> [<val>] | --<name>[{=| }<val>] ]... \<eth0/eth1> <file.pcap> <Destinatin IP [1.2.3.4]> <Destination mac [0a:1b:2c:3d:4e:5f]> <'random' dst port OR specify dport #>-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- disabled as '--no-load-opts'- may appear multiple timesOptions are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
This program, 'tcpliveplay' replays a captured set of packets using new TCP
connections with the captured TCP payloads against a remote host in order
to do comprehensive vulnerability testings.The following option preset mechanisms are supported:- reading file /usr/bin/.tcpliveplayrc
The basic operation of tcpliveplay is it rewrites the given pcap file in a
scheduled event format and responds with the apporiate packet if the remote
host meets tcp protocal's SEQ/ACK expectation.  Once expectations are met,
then the local packets are sent with the same payload except with new tcp
SEQ & ACK numbers meeting the response from the remote hose.The inputted pcap file are rewritten to start at the first encounter of the
SYN packet for correct operation making this packet be the first action in
the event schedule of local host doing the replay.For more details, please see the Tcpreplay Manual at:
http://tcpreplay.appneta.com
[root@datatest ~]#

6、查看Tcpreplay里的tcpbridge的帮助文档命令

[root@datatest ~]# tcpbridge -h
tcpbridge (tcpbridge) - Bridge network traffic across two interfaces
Usage:  tcpbridge [ -<flag> [<val>] | --<name>[{=| }<val>] ]...-r, --portmap=str          Rewrite TCP/UDP ports-s, --seed=num             Randomize src/dst IPv4/v6 addresses w/ given seed-N, --pnat=str             Rewrite IPv4/v6 addresses using pseudo-NAT-S, --srcipmap=str         Rewrite source IPv4/v6 addresses using pseudo-NAT-D, --dstipmap=str         Rewrite destination IPv4/v6 addresses using pseudo-NAT-b, --skipbroadcast        Skip rewriting broadcast/multicast IPv4/v6 addresses-C, --fixcsum              Force recalculation of IPv4/TCP/UDP header checksums-m, --mtu=num              Override default MTU length (1500 bytes)--mtu-trunc            Truncate packets larger then specified MTU-E, --efcs                 Remove Ethernet checksums (FCS) from end of frames--ttl=str              Modify the IPv4/v6 TTL/Hop Limit--tos=num              Set the IPv4 TOS/DiffServ/ECN byte--tclass=num           Set the IPv6 Traffic Class byte--flowlabel=num        Set the IPv6 Flow Label-F, --fixlen=str           Pad or truncate packet data to match header length--fuzz-seed=num        Fuzz 1 in X packets.  Edit bytes, length, or emulate packet drop--fuzz-factor=num      Set the Fuzz 1 in X packet ratio (default 1 in 8 packets)--skipl2broadcast      Skip rewriting broadcast/multicast Layer 2 addresses--dlt=str              Override output DLT encapsulation--enet-dmac=str        Override destination ethernet MAC addresses--enet-smac=str        Override source ethernet MAC addresses--enet-subsmac=str     Substitute MAC addresses--enet-mac-seed=num    Randomize MAC addresses--enet-mac-seed-keep-bytes=num Randomize MAC addresses--enet-vlan=str        Specify ethernet 802.1q VLAN tag mode--enet-vlan-tag=num    Specify the new ethernet 802.1q VLAN tag value--enet-vlan-cfi=num    Specify the ethernet 802.1q VLAN CFI value--enet-vlan-pri=num    Specify the ethernet 802.1q VLAN priority--hdlc-control=num     Specify HDLC control value--hdlc-address=num     Specify HDLC address--user-dlt=num         Set output file DLT type--user-dlink=str       Rewrite Data-Link layer with user specified data-i, --intf1=str            Primary interface (listen in uni-directional mode)-I, --intf2=str            Secondary interface (send in uni-directional mode)-u, --unidir               Send and receive in only one direction--listnics             List available network interfaces and exit-L, --limit=num            Limit the number of packets to send-M, --mac=str              MAC addresses of local NIC's-x, --include=str          Include only packets matching rule-X, --exclude=str          Exclude any packet matching this rule-P, --pid                  Print the PID of tcpbridge at startup-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 fileOptions are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
tcpbridge is a tool for selectively briding network traffic across two
interfaces and optionally modifying the packets in betweenPlease send bug reports to:  <tcpreplay-users@lists.sourceforge.net>
[root@datatest ~]#

7、查看Tcpreplay里的tcppcpinfo的帮助文档命令

[root@datatest ~]# tcpcapinfo -h
/usr/bin/tcpcapinfo: illegal option -- h
tcpcapinfo (Tcpreplay Suite) - Pcap file dissector for debugging broken pcap files
Usage:  tcpcapinfo [ -<flag> [<val>] | --<name>[{=| }<val>] ]... <pcap_file(s)>-V, --version              Print version information-H, --help                 display extended usage information and exit-!, --more-help            extended usage information passed thru pagerOptions are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
tcpcapinfo is a tool for decoding the structure of a pcap(3) file with a
focus on finding broken pcap files and determining how two related pcap
files might differ.Please send bug reports to:  <tcpreplay-users@lists.sourceforge.net>
[root@datatest ~]#

 Tcpreplay总结 (以下只是一个小例子来完整跑一遍)

  通过 Tcpreplay 来修改、转发通信流量需要考虑的一共需要考虑以下 3 点:
    1、 确定哪有数据包是从客户端到服务器端的,哪有是从服务器端到客户端的
    2 、确定新的 MAC、IP、Port
    3、确定回放速率、循环次数、执行方式

  第一步:
    用 tcpreplay 分离源/目的端口的流量。

tcpprep --port --cachefile=example.cach --pcap=example.pcap

  这种情况下,认为所有目的端口小于1024 的,将被视为客户端->服务器的包。

        认为所有目的端口大于1024 的,否则视为服务器->客户端的包。

  该信息被存储在 tcpprp 的一个名叫 example.cach 的文件夹中。
  --port 根据端口号区分数据包的流向。
  --cachefile 指定输出的 cach 文件的名字,即在这里要输出为example.cach
  --pcap 指定要处理的数据包文件,即在这里要处理的是example.pcap。
  其实。tcpprep 支持许多的其他的模式,分离端口模式是其中的一种 。也就是说,第一步,大家自行选择。

  第二步:
  使用 tcprewrite 更改 ip 地址到本地网络:

$ tcprewrite --endpoints=172.16.0.1:172.16.5.35   --cachefile=example.cach --infile=example.pcap --outfile=new.pcap

  这个例子里,我们想要所有的流量来自于 172.16.0.1 和 172.16.5.35。我们想要一个 IP 是“客户端”,一个 IP 是“服务器端 。

  tcprewrite 改写数据包:
    --endpoints 指定数据包的 client、server 端的 ip 地址
    --cachfie 上一步预处理的输出文件,即example.cach
    --infile 输入 pcap 文件,即example.pcap。
    --outfile 改写后的 pcap 文件 ,即new.pcap。

  第三步:用 tcpreplay,发送流量通过服务提供商

tcpreplay --intf1=eth0 --intf2=eth1 --cachefile=example.cach new.pcap

  因为我们要分离 2 个接口(eth0 和 eth1)之间的通信,我们使用第一步中创建的 cach 文件,第二步中创建的 new.pacp。
  然后使用 tcpreplay 重发数据包。

linux centos tcpreplay 重放数据相关推荐

  1. centos下 Tcpreplay 重放数据(流量采集重放)

    tcpreplay是什么? 简单的说, tcpreplay是一种pcap包的重放工具, 它可以将用ethreal, wireshark工具抓下来的包原样或经过任意修改后重放回去. 它允许你对报文做任意 ...

  2. mysql数据表名设置大小写不敏感(Linux Centos)

    mysql数据表名设置大小写不敏感(Linux Centos) 0.删除数据表(可选) drop table yourTableName; 1.编辑mysql配置文件 vi /etc/my.cnf 在 ...

  3. Linux 中 rsync 备份数据使用实例

    rsync工具用于将文件和目录从一个位置同步到另一个位置.同步的位置可以在本地服务器或远程服务器上.接下来教大家在Linux 中rsync备份数据使用实例 在Centos中使用下面命令安装rsync: ...

  4. Linux(Centos)之安装Redis及注意事项

    Linux(Centos)之安装Redis及注意事项 转:http://www.cnblogs.com/hanyinglong/p/5036558.html  阅读目录 1.redis简单说明 2.准 ...

  5. Linux退出sqlplus界面,Linux CentOS中使用SQL*Plus启动和关闭数据库

    启动和关闭数据库的常用工具有三个 一.SQL*Plus 在SQL*Plus环境中,用户以SYSDBA身份连接到Oracle后,可以通过命令行方式启动或关闭数据库. 二.OEM(企业管理器) 利用OEM ...

  6. Linux(CentOS)中常用软件安装,使用及异常——MySQL, VmTools

    本文主要是为了记录在工作中遇到的常用软件的安装过程,方便以后遇到相同情形时可以快速的查阅.主要讲述了MySQL, VMTools的安装. 本文的操作系统采用的是CentOS,可以采用shell命令查阅 ...

  7. Mac/Linux/Centos终端中上传文件到Linux云服务器

    Mac/Linux/Centos终端中上传文件到Linux云服务器 1.mac上传文件到Linux服务器 scp 文件名 用户名@服务器ip:目标路径如:scp /Users/test/testFil ...

  8. linux 挂载32T文件系统,linux Centos下磁盘分区及文件系统创建与挂载(示例代码)

    linux Centos下磁盘分区及文件系统创建与挂载 MBR(Master Boot Record)是传统的分区机制,应用于绝大多数使用BIOS的PC设备. 1.MBR支持32bit和64bit系统 ...

  9. 登陆mysql服务器命令_Mysql服务器登陆,启动,停止等基本操作命令介绍(Linux/Centos环境)...

    Mysql服务器登陆,启动,停止等基本操作命令介绍(Linux/Centos环境) 1.  启动MySQL服务 启动MySQL服务的命令为: 命令执行后如图7-5所示,表示启动MySQL服务成功. 也 ...

最新文章

  1. Bitcoin.com开发人员正创建一个用Rust语言编写的BCH全节点
  2. avro和java原生序列化的区别,java原生序列化和Kryo序列化性能比较
  3. java内存图怎么画,jvm内存模型怎么画?简单美观的模型图制作软件
  4. oracle查询案例,2道经典的oracle查询案例
  5. C和汇编混合编程---栈平衡
  6. 计算机boot进入u盘启动,暗影精灵5怎么设置u盘启动 暗影精灵5设置u盘启动方法...
  7. 如何C#中实现在TreeView查找某一节点
  8. 架构设计:数据服务系统0到1落地实现方案
  9. MySQL主从、主主、半同步节点架构的的原理及实验总结
  10. 计算机系统-Unicode字符集与它包含的UTF-8解决方案
  11. 东航期货穿透接口相关资料
  12. 入口文件到控制器 php,tp5.0框架隐藏index.php入口文件及模块和控制器的方法分析...
  13. mac关闭开机启动时的客人用户
  14. Androidstudio控制台分层输出接口日志.类似BeJSON,HiJson格式化JSON
  15. Linux中LANG,LC_ALL,local详解
  16. 离散数学编程实现真值表输出
  17. 忍痛分享五款小众软件,点赞收藏加关注
  18. matlab 正弦曲线平均值,正弦函数和余弦函数的平均值求解
  19. mysql 1236错误_mysql故障~Got fatal error 1236 解决方法
  20. navigateTo:fail can not navigateTo a tabbar page

热门文章

  1. Spring Boot 添加拦截器的配置方式
  2. 简述Linux 文件系统的目录结构
  3. 《JavaScript设计模式与开发实践》阅读摘要
  4. 记:返回方法参数的值(或多个值),
  5. iis8.5部署net项目
  6. Python入门篇-functools
  7. 约瑟夫环(约瑟夫问题)求最后出列的人数
  8. mariadb编译安装流程
  9. memcache缓存服务器(nginx php memcache mysql)
  10. MySQL数据表的创建、查看、插入