tcpreplay学习笔记

TCP网络流量回放工具tcpreplay使用说明

1. 什么是tcpreplay

引用一段tcpreplay官网的话来解释什么是tcpreplay:

Tcpreplay is a suite of BSD licensed tools written by Aaron Turner for UNIX (and Win32 under Cygwin) operating systems which gives you the ability to use previously captured traffic in libpcap format to test a variety of network devices. It allows you to classify traffic as client or server, rewrite Layer 2, 3 and 4 headers and finally replay the traffic back onto the network and through other devices such as switches, routers, firewalls, NIDS and IPS's.Tcpreplay supports both single and dual NIC modes for testing both sniffing and inline devices.

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

输入tcpreplay -H指令得到如下说明

The basic operation of tcpreplay is to resend all packets from the input file(s) at the speed at which they were recorded, or a specified data rate, up to as fast as the hardware is capable.

tcpreplay命令

tcpreplay学习笔记_第1张图片
tcpreplay命令

tcpreplay本身包含了几个辅助工具, 用于准备发包的cache, 重写报文等:

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

  • tcprewrite - 简单的说就是修改2层, 3层, 4层报文头部

  • tcpreplay - 真正发包, 可以选择主、从网卡, 发包速度等

  • tcpbridge - 利用tcprewrite的功能实现两个网络部分的桥接**

1.2 利用tcpreplay直接发包

tcpreplay -i p5p1 -M 3000 -K --preload-pcap -l 0 GD_SanFang_GET_FIFO_0150*

1.3 利用tcpprep进行预处理后发包

第一步:预处理生成Cache,命令为

tcpprep -a client -i test.pacp -o test.cache

这条命令将PCAP文件分成客户端和服务端,默认为客户端。发送时packet将分别从客户端和服务端发出。

第二步:重写IP地址和MAC地址,命令为


tcprewrite -e 192.85.1.2:192.85.2.2 --enet-dmac=00:15:17:2b:ca:14,00:15:17:2b:ca:15 --enet-smac=00:10:f3:19:79:86,00:10:f3:19:79:87 -c test.cache -i test.pcap -o 1.pcap 

这条命令将eth0设为服务端接口,eth1设为客户端接口,重写了IP和MAC,可通过wireshark等工具打开1.pcap,查看修改是否成功。

第三步:重放packet,命令为

tcpreplay -i eth0 -I eth1 -l 1000 -t -c /dev/shm/test.cache /dev/shm/1.pcap

为了获取更高的发送速度,可以把文件放到/dev/shm目录下,最高速度有1倍左右的加速。重放命令为

2.什么是pcap文件

2.1 定义

针对网络接口、端口和协议的数据包截取。

2.2 获取数据包

假定你要截取网络接口eth1,端口号6881的tcp数据包。数据文件保存为test.pcap。

tcpdump -w test.pcap -i eth1 tcp port 6881

如果要同时截取udp端口号33210和33220的数据包

tcpdump -w test.pcap -i eth1 tcp port 6881 or udp \( 33210 or 33220 \)

2.3 保存文件读取数据包

选项 -nn 不把网络IP和端口号转换成名字,r(read)读取包

tcpdump -nnr test.pcap```

可以添加 -tttt 选项使时间戳格式更加可读

tcpdump -ttttnnr test.pcap```

针对IP截取数据需向tcpdump指明目的IP和端口的交集(intersection),使用and运算符。比如要嗅探的目的IP为10.168.28.22,tcp端口号22。

tcpdump -w test.pcap dst 10.168.28.22 and tcp port 22```
嗅探数据包大小缺省为96 bytes,可以指定 -s 改变缺省值。

tcpdump -w test.pcap -s 1550 dst 10.168.28.22 and tcp port 22```

有些版本的tcpdump允许指定端口范围,下述指令为针对一定端口范围截取数据。没有指定 -w,将不会把截取的数据包保存到文件而是直接输出到屏幕。

tcpdump tcp portrange 20-24```

## **3. 什么是tcpdump**
>Linux作为网络服务器,特别是作为路由器和网关时,数据的采集和分析是不可少的。TcpDump是Linux中强大的网络数据采集分析工具之一。用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。

#### **版本信息**![tcpreplay -V](http://upload-images.jianshu.io/upload_images/3406513-6a1e32c991fea1ac?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

## **4.相关链接**
[Tcpreplay让协议测试从此无忧](http://www.cnblogs.com/uyunops/p/5917125.html)

[Tcpreplay命令](http://man.linuxde.net/tcpreplay)

[Usage Examples](http://tcpreplay.synfin.net/wiki/usage#UsageExamples)

你可能感兴趣的:(tcpreplay学习笔记)