Tcpreplay是Aaron Turner为UNIX(和Cygwin下的Win32)操作系统编写的一套BSD GPLv3许可工具,它允许您使用提前捕获的libpcap格式的流量文件来测试各种网络设备。
它允许你指定客户端或服务器流量,重写第2层、第3层和第4层报头,以及将流量回放到其他目标设备(如交换机、路由器、防火墙、NIDS和IPS)。
这里列出使用步骤:记录,重写和重播。
使用tcpdump捕获流量
按需调整参数,使用tcpdump命令捕获流量,例如:
tcpdump -i eth0 -s 1500 port 2058 and udp and dst host 64.84.30.75 -w hvtel-netflow.pcap
重写捕获文件
根据您的测试目标重写pcap文件格式,例如:
tcprewrite -C --dstipmap=64.84.30.75/32:52.25.39.134/32 --srcipmap=0.0.0.0/0:65.23.160.131/32 --enet-smac=00:50:56:B8:75:C2 --enet-dmac=00:23:05:5C:00:45 --infile=hvtel-netflow.pcap --outfile=hvtel-netflow-rewrite-131.pcap
这里列出一些参数及用法,其他信息可以执行“man tcprewrite”查看或查看相关文章。
-C, --fixcsum, Force recalculation of IPv4/TCP/UDP header checksums. Causes each IPv4/v6 packet to have their checksums recalcualted and fixed. Automatically enabled for packets modified with --seed, --pnat, --endpoints or --fixlen.
--srcipmap and/or --dstipmap, To apply different rules to the source and destination IP addresses in packets.
--enet-dmac and --enet-smac, To specify the new destination and source MAC addresses.
--infile and --outfile, To provide it an input pcap file and the name of the output pcap file.
请注意,对于同一网段的设备,指定每个设备的网卡MAC地址就可以让tcpreplay工具在它们之间发送流量。
对于不在同一网段中的设备,则需要使用 --enet-smac 指定发送方网关的MAC地址。 例如,我们要将流量从64.84.30.75发送到52.25.39.134。 我们首先需要获得64.84.30.75的网关的MAC地址。然后使用 --enet-dmac 指定所获取的MAC地址(示例中是 00:23:05:5C:00:45 )。
[root@Hill_Valley_Tel ~]# arp -adickey-fa-1.calix.com (64.84.30.8) at 00:0C:29:10:EC:09 [ether] on eth0? (64.84.30.1) at 00:23:05:5C:00:45 [ether] on eth0
重放修改后的流量文件
如需更多信息,请“man tcpreplay”或查看“tcpreplay”相关文章。这里列出一个tcpreplay命令的例子。
tcpreplay -i eth0 -tK --loop=5 hvtel-netflow-rewrite-131.pcap
其他
如果遇到如下的错误,
tcpreplay: error while loading shared libraries: libdnet.1: cannot open shared object file: No such file or directory
这是tcpreplay中的错误,您可以使用以下命令修复。
cd /usr/lib/ln -s libdnet.so.1 libdnet.1
参考: