在很多场景下需要在抓到包后如何进行回放。
实现包的回放可以使用tcpreplay,该工具可以直接通过apt-get命令安装,apt-get install tcpreplay.
官网地址:http://tcpreplay.synfin.net/
最新的:
https://sourceforge.net/projects/tcpreplay/files/latest/download?source=files
如果需要修改目的ip地址,例如:
tcprewrite --infile=a.pcap --outfile=a2.pcap --dstipmap=192.168.159.14:192.168.159.15 --enet-dmac=00:0c:29:d6:0f:61 --fixcsum
还会帮助进行修改校验值。
tcpreplay本身包含了几个辅助工具, 用于准备发包的cache, 重写报文等.
-
-
- 1.1.1 移植到android系统
-
(需要提前安装libnet包和libpcap包,其中libnet的下载链接如下,本次使用1.1.3版本:
http://packetfactory.openwall.net/projects/libnet/,.
./configure --prefix=/usr/local/libnet --host=arm-linux --target=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc
修改Makefile文件中的CFLAGS加入-static
Libpcap使用(本次使用1.8.1版本)./configure --prefix=/usr/local/libpcap --host=arm-linux --target=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc --with-pcap=linux
从官网下载tcpreplay后解压,
关于libnet和libpcap的路径来指定相关路径。
./configure --host=arm-linux --target=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc --with-libdnet=/usr/local/libnet --with-libpcap=/usr/local/libpcap CFLAGS=-I/usr/local/libpcap/include LDFLAGS="-static"
然后执行make即可。
_______________________________________________
出现libpcap<0.5类似错误
答:--with-libpcap指定了正确的libpcap路径。
出现:fakepcap.h:40:7: error: conflicting types for ‘pcap_datalink_val_to_description’
答:注释掉fakepcap.h中该行。
出现:do_packets.c:(.text+0x421): undefined reference to `timerdiv'
答:将timerdiv 内联函数复制到do_packets.c文件中。
出现:/tcpreplay: error while loading shared libraries: libpcap.so.1: cannot open shared object file: No such file or directory
答:将libpcap.so.1所在目录添加到文件/etc/ld.so.conf中,然后在执行 ldconfig。
出现:cidr.c:235:16: error: #elif with no expression
答:注释掉235行。
出现:checking for libpcap version... configure: error: Libpcap versions < 0.7.2 are not supported
Please upgrade to version 0.7.2 or better
答:注释configure中
#if test x$libpcap_ver8 = xyes ; then
# { $as_echo "$as_me:${as_lineno-$LINENO}: result: >= 0.8.0" >&5
#$as_echo ">= 0.8.0" >&6; }
#elif test x$libpcap_ver7 = xyes ; then
# { $as_echo "$as_me:${as_lineno-$LINENO}: result: >= 0.7.2" >&5
#$as_echo ">= 0.7.2" >&6; }
#else
# as_fn_error $? "Libpcap versions < 0.7.2 are not supported
# Please upgrade to version 0.7.2 or better" "$LINENO" 5
#fi
出现:pcap/pcap.h: No such file or directory
答:安装libpcap-dev包。交叉编译寻找头文件路径需要设置一下:
在Makefile中增加如下,因为pcap/pcap.h位于/usr/local/include/目录中:
在configure时候加上CFLAGS和LDFLAGS参数
出现:warning: library search path "/usr/local/libpcap/lib" is unsafe for cross-compilation
答:相关的文件路径放在的系统默认(非交叉编译)的路径下面,系统认为是不安全的,故有警告提示。
出现:tcpreplay: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 2.6.16, not stripped
答:在gcc-arm工具包中,找到ld-2.18.so
(位于/arm-2014.05/arm-none-linux-gnueabi/libc/lib),将其软连接成ld-linux.so.3即可。
出现系统:打不开文件
答:先重新挂在根目录,# mount -o remount /
然后在android系统中创建lib文件,在目录中创建软链接
ln -s ld-2.18.so ld-linux.so.3
PS:可以将/arm-2014.05/arm-none-linux-gnueabi/libc/lib都复制到/lib目录中,省事。
-
-
- 1.1.2 使用
-
先使用工具tcpprep来处理pcap文件,区分客户端和服务器端。例如:
tcpprep --port --cachfile=example.cach --pcap=example.pcap
--port 根据端口号区分数据包的流向,小于1024就是服务端,大于1024就是客户端。
--cachfile 指定输出的 cach 文件的名字,即在这里要输出为example.cach
--pcap 指定要处理的数据包文件,即在这里要处理的是example.pcap。
然后使用tcprewrite,
tcprewrite --endpoints=172.16.0.1:172.16.5.35 --cachfile=example.cach --infile=example.pcap --outfile=new.pcap
--endpoints 指定数据包的 client、server 端的 ip 地址
--cachfie 上一步预处理的输出文件,即example.cach
--infile 输入 pcap 文件,即example.pcap。
--outfile 改写后的 pcap 文件 ,即new.pcap。
最后使用tcpreplay来进行播放:
tcpreplay -c example.cach -i eth0 -I eth1 example.pcap
该命令把example.pcap包, 按照example.cach里划分的主机包和客户端包的方式, 将主机包从网卡eth0发出去, 将客户端包从网卡eth1发出去.
-
-
- 1.1.3 实例
-
先抓两个包:
./tcpdump host 116.175.28.229 -c 2 -w 1.pcap
./tcpprep --auto=client --pcap=1.pcap --cachefile=input.cache
./tcpreplay -c input.cache -i wlan0 -I wlan0 1.pcap
然后使用tcpdump进行抓包