centos下 Tcpreplay 重放数据(流量采集重放)
tcpreplay是什么?
簡單的說, tcpreplay是一種pcap包的重放工具, 它可以將用ethreal, wireshark工具抓下來的包原樣或經(jīng)過任意修改后重放回去. 它允許你對報文做任意的修改(主要是指對2層, 3層, 4層報文頭), 指定重放報文的速度等, 這樣tcpreplay就可以用來復現(xiàn)抓包的情景以定位bug, 以極快的速度重放從而實現(xiàn)壓力測試。
tcpreplay本身包含了幾個輔助工具(tcpprep、tcprewrite、tcpreplay和tcpbridge), 用于準備發(fā)包的cache, 重寫報文等。這也是 Tcpreplay 的第一個字母大寫T的原因 。
?? * tcpprep - 簡單的說就是劃分哪些包是client的, 哪些是server的, 一會發(fā)包的時候client的包從一個網(wǎng)卡發(fā), server的包可能從另一個網(wǎng)卡發(fā)。
即區(qū)分pcap數(shù)據(jù)包的流向,即區(qū)分出客戶端和服務器。
??? * tcprewrite - 簡單的說就是修改2層, 3層, 4層報文頭部。
即改寫pcap數(shù)據(jù)包的2-4層的頭部信息,即MAC地址、IP地址和PORT等。
??? * tcpreplay - 真正發(fā)包, 可以選擇主、從網(wǎng)卡, 發(fā)包速度等。
即回放pcap文件中的數(shù)據(jù)包。
* tcpreplay-edit-更寫pcap數(shù)據(jù)并回放,將tcprewrite和tcpreplat一條命令實現(xiàn)。
??? * tcpbridge - bridge two network segments with the power of tcprewrite。
? 以下是官網(wǎng)的原文。
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
官網(wǎng)
http://tcpreplay.synfin.net/在CentOS里安裝tcpreplay,總的來說,有如下兩種安裝方法。
第一種安裝方法:(推薦)
直接yum -y install tcpreplay即可。
[root@datatest ~]# 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 ~]#第二種安裝方法:
在Centos中安裝TCPReplay
Tcpreplay的初步使用
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 發(fā)包)-d 輸出調(diào)試信息(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 可選參數(shù),每發(fā)送一個報文都以 tcpdump 風格打印對應信息-A, --decode=str Arguments passed to tcpdump decoder 可選參數(shù),在使用 tcpdump 風格打印輸出信息時,同時再調(diào)用 tcpdump 中的參數(shù)-K, --preload-pcap Preloads packets into RAM before sending-c 雙網(wǎng)卡回放報文必選參數(shù),后跟文件名-C, --cachefile=str Split traffic via a tcpprep cache file 把報文存在內(nèi)部緩存中-N 獲得網(wǎng)絡接口和出口-2, --dualfile Replay two files at a time from a network tap-i, --intf1=str Client to server/RX/primary traffic output interface 雙網(wǎng)卡回放報文必選參數(shù),指定從接口-I, --intf2=str Server to client/TX/secondary traffic output interface--listnics List available network interfaces and exit 雙網(wǎng)卡回放報文必選參數(shù),指定主接口-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 可選參數(shù),指定循環(huán)次數(shù)-S 制定包長度-L, --limit=num Limit the number of packets to send--duration=num Limit the number of seconds to send 限制發(fā)包數(shù)量-x, --multiplier=str Modify replay speed to a given multiple-p, --pps=str Replay packets at a given packets/sec 可選參數(shù),指定每秒發(fā)送報文的個數(shù)。制定該參數(shù),其他速率相關的參數(shù)被忽略, 打印信息不會有速率和每秒發(fā)送報文的統(tǒng)計-M, --mbps=str Replay packets at a given Mbps 以 Mbps(兆字節(jié)每秒)發(fā)送報文-m 可選參數(shù),指定一個倍數(shù)值,比默認發(fā)送速度快多少倍發(fā)送報文-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' 可選參數(shù),表示在輸出信息中打印 PID 信息,用于單用戶和單賬戶模式下暫停和重 啟程序-V, --version Print version information 現(xiàn)實版本號-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 根據(jù) cach 文件來回放 pcap 文件中的數(shù)據(jù)。
? tcpreplay小例子
[root@datatest ~]# tcpreplay --intf1=eth1 --intf2=eth0 –t --cachfile=cach_test.cach http_rewrite.pcap--intf1=eth1 是指主接口是 eth1, 服務器-> 客戶端的數(shù)據(jù)包通過這個接口發(fā)送。
服務器和客戶端的區(qū)分是從 tcpprep 的處理結(jié)果 cach_test.cach 中得到的。
--intf2=eth0 是指從接口是 eth0,客戶端->服務器的數(shù)據(jù)包通過這個接口發(fā)送。
--cachfile=cach_test_cach 是指 tcpreplay 用 下面的tcpprep 的處理結(jié)果--cach_test.cach 來區(qū)分方向。
http_rewrite.pcap 是指 tcpreplay 發(fā)送的是來自 http_rewrite.pcap 這個文件中的數(shù)據(jù)包 。
? -i, --intf1這是數(shù)據(jù)重放的時候,的從接口。 ? ??-I, --intf2這是數(shù)據(jù)重放的時候,的主接口。 –c是雙網(wǎng)卡回放報文必選參數(shù),后跟文件名
在linux下, 用ifconfig命令可以獲得接口的名字。
但在cygwin下必須用下面的命令獲得接口的名字。
在cygwin下獲得接口的名字:
?
假設在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)從中可以看出這臺機器有兩塊網(wǎng)卡, 一塊叫做%1, 另一塊叫做%2. 下面就可以指定網(wǎng)卡重發(fā)包了:
用tcpreplay發(fā)包:
? 這條命令是把out.pcap包,?按照mgcp.cach里劃分的主機包和客戶端包的方式, 將主機包從網(wǎng)卡%1發(fā)出去, 將客戶端包從網(wǎng)卡%2發(fā)出去.
注:修改目的IP的MAC地址的時候,MAC地址不是目的機網(wǎng)卡物理地址,而是源機器所連接的交換機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 刪除以太網(wǎng)最后一幀的校驗和(刪除最后 2 個字節(jié))-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填充或截取包的數(shù)據(jù)以匹配包頭長度-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 可選參數(shù),在使用 tcpdump 風格輸出信息時,同時再調(diào)用 tcpdump 中的參數(shù)-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?只保證能把包送出去,?至于包真正能到達的地址,?我認為還是根據(jù)原來的包的?IP?和?mac,
如果是在同一網(wǎng)段,可能需要把?mac?地址改成測試設備的?mac,如果需要經(jīng)過網(wǎng)關,?就得將?IP?地址改為測試設備的?IP?以及端口號 。
tcpreplay只保證能把包送出去, 至于包真正能到達的地址, 我認為還是根據(jù)原來的包的IP和mac. 如果是在同一網(wǎng)段, 可能需要把mac地址改成測試設備的mac, 如果需要經(jīng)過網(wǎng)關, 就得將IP地址改為測試設備的IP以及端口號。
tcprewrite的基本格式是(請注意命令中是沒有換行符的, 只為了閱讀的方便添加了換行符): 詳請可以用tcprewrite ?命令查詢詳情。
解釋一下:
該命令的輸入?yún)?shù)是input.pcap和input.cach文件, 結(jié)果將另存為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。
舉個小例子
該命令將修改后的包的
主機端的二層, 三層, 四層頭分別為: 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該命令將根據(jù)數(shù)字 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 網(wǎng)段的 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 網(wǎng)段改為 10.77.0.0/16 網(wǎng)段
[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 網(wǎng)段改為 10.77.0.0/16 網(wǎng)段。
? 修改2層頭
1)?修改MAC地址
如果不指定cache文件, 將把所有包的源mac地址和目的mac地址都改寫成00:44:66:FC:29:AF和00:55:22:AF:C6:37:
指定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
經(jīng)常客戶的抓包帶有VLAN頭域, 這些包如果不去掉VLAN頭是沒有辦法在自己的交換機上replay的, tcprewrite提了去掉或添加VLAN的方法:
去掉vlan很簡單:
添加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)?修改二層協(xié)議名:
好像是將Ethernet協(xié)議頭轉(zhuǎn)成Cisco HDLC或其它二層協(xié)議? 這部分沒有真正用過, 需要的
人自行參考tcpreplay官方網(wǎng)站:?http://tcpreplay.synfin.net/wiki/manual
???修改3層頭
從版本3.4.2開始, tcprewrite開始支持ipv6協(xié)議, tcpreplay升級蠻快。 tcprewrite修改IP地址后會自動幫你計算校驗和, 這點還是蠻周到的。
命令行傳入IPv6地址的時候要使用方括號, 例如: [2001::dead:beef]或[2001::/16]
1)?修改目的IP
根據(jù)cache文件里的標識, 將server的IP改為10.10.1.1, client的IP改為10.10.1.2:
2)修改IP地址的網(wǎng)絡部分
注: 2)和3)沒有驗證過。
眾所周知, IP地址同網(wǎng)絡部分和主機部分組成, 下面的命令可以將子網(wǎng)地址為10.0.0.0/8或192.168.0.0/16的IP改成子網(wǎng)為172.16.0.0/12:
下面的命令是基于client包或server包修改子網(wǎng)地址:
[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 --skipbroadcast3)?修改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:
2)?強制計算傳輸層校驗和:
有些應用可能不計算傳輸層的校驗和, 可以讓tcpwrite強制計算一下:
修改5-7層數(shù)據(jù)
tcpwrite對5-7層的修改非常有限, 頂多也就是抓包沒有抓全, 中間的應用層數(shù)據(jù)丟了。tcpwrite將沒有抓到的數(shù)據(jù)補成全0, 或者修改tcp/udp的長度字節(jié), 或者將該包丟棄. 有需要的直接參考官方資料吧。
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 一般情況下都需要的參數(shù),表示按模式自動分離通信流量生成 cach 文件 -c, --cidr=str CIDR-split mode 可選參數(shù), 表示分離流量時采用 CIDR(無類別域間路由選擇,是一個在 internet 上創(chuàng)建附加地址的方法,這些地址提供給服務提供商,再由服務提供商分配給客戶。 CIDR 將路由集中起來,使一個 IP 地址代表主要骨干提供商提供的幾千個 IP 地址, 從而減輕 internet 路由器的負擔) -r, --regex=str Regex-split mode 可選參數(shù),表示使用 regex 模式分離通信流量,有點類似于 CIDR 模式,但是它匹 配的是服務器源 IP -p, --port Port-split mode 可選參數(shù),基于目的端口來分離通信流量,它區(qū)分的依據(jù)是任何 0-1023 端口都是 服務器的端發(fā)出的報文,其他的端口都是客戶端發(fā)出的報文 -e, --mac=str Source MAC split mode 表示基于服務器源 MAC 地址分離通信流量?--reverse Matches to be client instead of server-C, --comment=str Embedded cache file comment 可選參數(shù),表示在 cach 文件中嵌入注釋內(nèi)容,用于注釋說明 cach 文件的內(nèi)容。使用位置不要放在最后,生成 cach 后可以使用-P 查看寫入的內(nèi)容。--no-arg-comment Do not embed any cache file comment-x, --include=str Include only packets matching rule 重要可選參數(shù),表示按照參數(shù)定義的需求來定義發(fā)送報文-X, --exclude=str Exclude any packet matching this rule 可選參數(shù),是-x 的取反-o, --cachefile=str Output cache file生成 cach 文件必帶參數(shù),后跟 cach 后綴的文件名,表示這個輸出的 cach 文 件以這個文件名命名-i, --pcap=str Input pcap file to process生成 cach 文件必帶參數(shù),后跟 pcap 文件名,表示這個 pcap 文件需要處理-P, --print-comment=str Print embedded comment in the specified cache file 可選參數(shù),表示查看 cach 文件的內(nèi)容-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 文件的統(tǒng)計信-s, --services=str Load services file for server ports表示從服務器端口下載服務文件-N, --nonip Send non-IP traffic out server interface 表示從服務器端口發(fā)送無 IP 流量-R, --ratio=str Ratio of client to server packets 可選參數(shù),一個比例值。服務器端發(fā)起的連接數(shù)和客戶端發(fā)起的連接數(shù)的比例,值 大于 2 就視為服務器端-m, --minmask=num Minimum network mask length in auto mode 可選參數(shù),在選用 router 模式時使用,表示最小掩碼,掩碼默認是 30-M, --maxmask=num Maximum network mask length in auto mode 可選參數(shù),在選用 router 模式時使用,表示最大掩碼,默認是 8-v, --verbose Print decoded packets via tcpdump to STDOUT 可選參數(shù),顯示 tcpprep 生成 cach 文件的處理過程。信息的隨時大于-A, --decode=str Arguments passed to tcpdump decoder 可選參數(shù),在實驗 tcpdump 風格打印輸出信息時,同時再調(diào)用 tcpdump 中的參數(shù)-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 處理與發(fā)送報文做準備。
如果你正打算在兩塊網(wǎng)卡上使用 tcpreplay 的話,那么 tcpprep 就是用來決定每一個報文(packet)從哪一個接口發(fā)出。通過使用這樣一個分離的程序來建立一個 cach 文件,tcpreplay 就可以根據(jù)這個 cach 文件通過自身的計算來分離流量,高速率的發(fā)送報文。cach 文件的作用主要是加速報文的發(fā)送,cach 文件中存放著 pcap 文件中每個幀的編號和時間戳等信息,以達到 tcpreplay 回放時可以更加快速的發(fā)送報文的目的。?
舉個例子,?如果你用?wireshark 或者ethereal抓了一個?pcap?文件,里面可能既有?A?地址發(fā)給?B?地址的包,也有?B地址發(fā)給?A?址的包,用?tcpprep?工具可以指定從?A?到?B?的包從主網(wǎng)卡發(fā)出,?從?B?到?A?的包從次網(wǎng)卡發(fā)出。?
tcpprep?用法舉例?
1、根據(jù)報文源?IP?確定?client/server?報文
上面的命令指定所有源?IP?為?172.22.64.2/24?的包,?都將從主網(wǎng)卡發(fā)出,?其它的從次網(wǎng)卡發(fā)出 。輸入文件是mgcp.pcap, 輸出文件為mgcp.cach。
2、使用自動模式確定?client/server?報文?
[root@datatest ~]# tcpprep -a client -i mgcp.pcap -o mgcp.cach上面的命令采用自動/client?模式指定分包模式。
自動模式這里按我的理解解釋一下:
tcpprep?在自動模式下認為有下面行為的?IP?為?client?端:
1、發(fā)?TCP SYN?包的一方
2、發(fā)?DNS?包的一方
3、 收入到?ICMP-Port Unreachable?的一方;
? ? ? 認為有下面行為的一方為?server?端:
1、發(fā)?TCPSyn/Ack?的一方
2、發(fā)?DNS?應答的一方
3、發(fā)?ICMP-Port Unreachable?的一方
而被認定為?server?的那一方發(fā)的那些包,?將從主網(wǎng)卡發(fā)出,?被認定為?client?的包則從次網(wǎng)卡發(fā)出.?而自動/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 實時修改包數(shù)據(jù)并回放,它是將 tcprewrite 和 tcpreplay 用一條命令實現(xiàn)。其好處是修改包數(shù)據(jù)不會新生成 pcap 文件。如果是需要不斷的改寫一個包文件并回放建議使用 tcpreplay-edit,如果是需要一次改寫一個包文件并多次回放建議使用 tcprewrite 和 tcpreplay 的結(jié)合,這樣具有更好的回放速率。?
tcpreplay-edit小例子
編寫腳本,不斷改寫包文件的?IP?地址并回放:?
比如,你自己寫個test.sh腳本,然后賦予chmod 777 test.sh。執(zhí)行就是
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 done5、查看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總結(jié) (以下只是一個小例子來完整跑一遍)
通過?Tcpreplay?來修改、轉(zhuǎn)發(fā)通信流量需要考慮的一共需要考慮以下?3?點:
1、?確定哪有數(shù)據(jù)包是從客戶端到服務器端的,哪有是從服務器端到客戶端的
2 、確定新的?MAC、IP、Port
3、確定回放速率、循環(huán)次數(shù)、執(zhí)行方式?
第一步:
用?tcpreplay?分離源/目的端口的流量。
這種情況下,認為所有目的端口小于1024 的,將被視為客戶端->服務器的包。
認為所有目的端口大于1024 的,否則視為服務器->客戶端的包。
該信息被存儲在 tcpprp 的一個名叫 example.cach 的文件夾中。
--port?根據(jù)端口號區(qū)分數(shù)據(jù)包的流向。
--cachfile?指定輸出的?cach?文件的名字,即在這里要輸出為example.cach
--pcap?指定要處理的數(shù)據(jù)包文件,即在這里要處理的是example.pcap。
其實。tcpprep?支持許多的其他的模式,分離端口模式是其中的一種 。也就是說,第一步,大家自行選擇。
第二步:
使用?tcprewrite?更改?ip?地址到本地網(wǎng)絡:
這個例子里,我們想要所有的流量來自于?172.16.0.1?和?172.16.5.35。我們想要一個?IP?是“客戶端”,一個?IP?是“服務器端 。
?
? tcprewrite?改寫數(shù)據(jù)包:
--endpoints?指定數(shù)據(jù)包的?client、server?端的?ip?地址
--cachfie?上一步預處理的輸出文件,即example.cach
--infile?輸入?pcap?文件,即example.pcap。
--outfile?改寫后的?pcap?文件 ,即new.pcap。
第三步:用?tcpreplay,發(fā)送流量通過服務提供商
tcpreplay --intf1=eth0 --intf2=eth1 --cachfile=example.cach new.pcap 因為我們要分離?2?個接口(eth0?和?eth1)之間的通信,我們使用第一步中創(chuàng)建的?cach?文件,第二步中創(chuàng)建的?new.pacp。
然后使用?tcpreplay?重發(fā)數(shù)據(jù)包。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的centos下 Tcpreplay 重放数据(流量采集重放)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu16.04caffe训练mn
- 下一篇: 二十三、中断(控制计算机硬件,避免使用0