TCP網(wǎng)絡流量回放工具tcpreplay使用說明
1. 什么是tcpreplay
引用一段tcpreplay官網(wǎng)的話來解釋什么是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工具抓下來的包原樣或經(jīng)過任意修改后重放回去. 它允許你對報文做任意的修改(主要是指對2層, 3層, 4層報文頭), 指定重放報文的速度等, 這樣tcpreplay就可以用來復現(xiàn)抓包的情景以定位bug, 以極快的速度重放從而實現(xiàn)壓力測試.
輸入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本身包含了幾個輔助工具, 用于準備發(fā)包的cache, 重寫報文等:
tcpprep - 簡單的說就是劃分哪些包是client的, 哪些是server的, 一會發(fā)包的時候client的包從一個網(wǎng)卡發(fā), server的包可能從另一個網(wǎng)卡發(fā)
tcprewrite - 簡單的說就是修改2層, 3層, 4層報文頭部
tcpreplay - 真正發(fā)包, 可以選擇主柳骄、從網(wǎng)卡, 發(fā)包速度等
tcpbridge - 利用tcprewrite的功能實現(xiàn)兩個網(wǎng)絡部分的橋接**
1.2 利用tcpreplay直接發(fā)包
tcpreplay -i p5p1 -M 3000 -K --preload-pcap -l 0 GD_SanFang_GET_FIFO_0150*
1.3 利用tcpprep進行預處理后發(fā)包
第一步:預處理生成Cache,命令為
tcpprep -a client -i test.pacp -o test.cache
這條命令將PCAP文件分成客戶端和服務端,默認為客戶端。發(fā)送時packet將分別從客戶端和服務端發(fā)出刮吧。
第二步:重寫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
為了獲取更高的發(fā)送速度沪蓬,可以把文件放到/dev/shm目錄下彤钟,最高速度有1倍左右的加速。重放命令為
2.什么是pcap文件
2.1 定義
針對網(wǎng)絡接口跷叉、端口和協(xié)議的數(shù)據(jù)包截取逸雹。
2.2 獲取數(shù)據(jù)包
假定你要截取網(wǎng)絡接口eth1,端口號6881的tcp數(shù)據(jù)包云挟。數(shù)據(jù)文件保存為test.pcap梆砸。
tcpdump -w test.pcap -i eth1 tcp port 6881
如果要同時截取udp端口號33210和33220的數(shù)據(jù)包
tcpdump -w test.pcap -i eth1 tcp port 6881 or udp \( 33210 or 33220 \)
2.3 保存文件讀取數(shù)據(jù)包
選項 -nn 不把網(wǎng)絡IP和端口號轉(zhuǎn)換成名字,r(read)讀取包
tcpdump -nnr test.pcap```
可以添加 -tttt 選項使時間戳格式更加可讀
tcpdump -ttttnnr test.pcap```
針對IP截取數(shù)據(jù)需向tcpdump指明目的IP和端口的交集(intersection)园欣,使用and運算符帖世。比如要嗅探的目的IP為10.168.28.22,tcp端口號22沸枯。
tcpdump -w test.pcap dst 10.168.28.22 and tcp port 22```
嗅探數(shù)據(jù)包大小缺省為96 bytes日矫,可以指定 -s 改變?nèi)笔≈怠?
tcpdump -w test.pcap -s 1550 dst 10.168.28.22 and tcp port 22```
有些版本的tcpdump允許指定端口范圍,下述指令為針對一定端口范圍截取數(shù)據(jù)。沒有指定 -w辉饱,將不會把截取的數(shù)據(jù)包保存到文件而是直接輸出到屏幕搬男。
tcpdump tcp portrange 20-24```
## **3. 什么是tcpdump**
>Linux作為網(wǎng)絡服務器,特別是作為路由器和網(wǎng)關時彭沼,數(shù)據(jù)的采集和分析是不可少的缔逛。TcpDump是Linux中強大的網(wǎng)絡數(shù)據(jù)采集分析工具之一。用簡單的話來定義tcpdump,就是:dump the traffic on a network褐奴,根據(jù)使用者的定義對網(wǎng)絡上的數(shù)據(jù)包進行截獲的包分析工具按脚。它支持針對網(wǎng)絡層、協(xié)議敦冬、主機辅搬、網(wǎng)絡或端口的過濾,并提供and脖旱、or堪遂、not等邏輯語句來幫助你去掉無用的信息。
#### **版本信息**![tcpreplay -V](http://upload-images.jianshu.io/upload_images/3406513-6a1e32c991fea1ac?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
## **4.相關鏈接**
[Tcpreplay讓協(xié)議測試從此無憂](http://www.cnblogs.com/uyunops/p/5917125.html)
[Tcpreplay命令](http://man.linuxde.net/tcpreplay)
[Usage Examples](http://tcpreplay.synfin.net/wiki/usage#UsageExamples)