tcpreplay and tcprewrite
tcprewrite edits a pcap (IPs, ports, MACs, VLAN, TTL, checksums, link type) and writes a new file; tcpreplay sends a pcap out of an interface again; tcpreplay-edit does both at once. They are used to test firewalls, IDS and other devices with real recorded traffic. Linux (apt/dnf) and macOS (brew); Windows needs WSL. Lab: replay 192.168.148.10, sink 192.168.148.20. Replay only inside an isolated lab.
128.1 Why is it used?
- Test an IDS, firewall or load balancer with real traffic instead of synthetic tests.
- Reproduce a bug from a customer capture inside your own lab.
- Stress test: replay at top speed or in a loop.
- Anonymise or adapt a capture (new IPs/MACs/ports) before sharing or replaying.
128.2 How it works
- Step 1
1.A capture from another network is replayed as it is
- Step 2
2.The packet crosses the lab switch and reaches the sink
- Step 3
3.MAC and IP do not belong to the sink: the sink drops it
- Step 4
4.tcprewrite changes MAC + IP (and fixes checksums)
- Step 5
5.Same packet replayed again: the sink's tcpdump receives it
- Step 1
1.tcpprep marks every packet as client (primary) or server (secondary)
- Step 2
2.tcpreplay -i eth0 -I eth1 -c in.cache file.pcap
- Step 3
3.Client packets go out of eth0 into one side of the inline firewall
- Step 4
4.Server replies go out of eth1 into the other side
- Step 5
5.The firewall sees both directions, like real traffic
128.3 A capture of our own lab
timeout 15 tcpdump -i eth0 -nn -w orig.pcap \ 'ip host 192.168.148.20' &ping -c 2 -i 0.3 192.168.148.20wget -qO- http://192.168.148.20/ping -c 1 -s 1400 192.168.148.20tcpdump -r orig.pcap -nn -thello-n12816 packets captured0 packets dropped by kernel$ tcpdump -r orig.pcap -nn -tIP 192.168.148.10 > 192.168.148.20: ICMP echo requestIP 192.168.148.20 > 192.168.148.10: ICMP echo replyIP 192.168.148.10.57620 > 192.168.148.20.80: Flags [S]IP 192.168.148.20.80 > 192.168.148.10.57620: Flags [S.] ... length 90: HTTP: GET / HTTP/1.1 ... length 17: HTTP: HTTP/1.0 200 OKIP 192.168.148.10 > 192.168.148.20: ICMP ... length 1408IP 192.168.148.20 > 192.168.148.10: ICMP ... length 1408128.4 Editing IP addresses and ports
- --srcipmap changes only source fields, --dstipmap only destination fields (so only requests changed).
- --pnat maps a whole subnet in both directions (192.168.148.0/24 -> 10.148.0.0/24).
- --portmap=80:8080 rewrites the port (also lists and ranges: 8000-8999:80).
- -s 42 randomizes IPs but keeps pairs consistent.
tcprewrite -i orig.pcap -o ipmap.pcap \ --srcipmap=192.168.148.10/32:10.10.0.10/32 \ --dstipmap=192.168.148.20/32:10.20.0.20/32tcprewrite -i orig.pcap -o pnat.pcap \ --pnat=192.168.148.0/24:10.148.0.0/24tcprewrite -i orig.pcap -o port.pcap \ --portmap=80:8080tcprewrite -i orig.pcap -o seed.pcap -s 42$ tcpdump -r ipmap.pcap -nn -t icmpIP 10.10.0.10 > 10.20.0.20: ICMP echo requestIP 192.168.148.20 > 192.168.148.10: ICMP echo reply$ tcpdump -r pnat.pcap -nn -t icmpIP 10.148.0.10 > 10.148.0.20: ICMP echo requestIP 10.148.0.20 > 10.148.0.10: ICMP echo reply$ tcpdump -r port.pcap -nn -t tcpIP 192.168.148.10.57620 > 192.168.148.20.8080: Flags [S], seq 1539080596, win 64240, ...IP 192.168.148.20.8080 > 192.168.148.10.57620: Flags [S.], seq 3136506240, ack 1539080597, ...$ tcpdump -r seed.pcap -nn -t icmpIP 117.125.116.142 > 117.125.116.140: ICMP echo requestIP 117.125.116.140 > 117.125.116.142: ICMP echo reply128.5 tcpprep cache and endpoints
- tcpprep --auto=bridge marks each packet Primary (client) or Secondary (server): 8 + 8 here.
- tcprewrite -c cache --endpoints=10.0.0.1:10.0.0.2 turns the whole conversation into one between those two IPs.
- The same cache drives two-NIC replay (-i / -I) and per-direction MACs.
tcpprep --auto=bridge --pcap=orig.pcap \ --cachefile=orig.cachetcpprep --print-stats=orig.cachetcprewrite -i orig.pcap -o ep.pcap \ -c orig.cache \ --endpoints=10.0.0.1:10.0.0.2$ tcpprep --print-stats=orig.cachePrimary packets: 8Secondary packets: 8Skipped packets: 0------------------------------Total packets: 16$ tcpdump -r ep.pcap -nn -tIP 10.0.0.1 > 10.0.0.2: ICMP echo requestIP 10.0.0.2 > 10.0.0.1: ICMP echo replyIP 10.0.0.1.57620 > 10.0.0.2.80: Flags [S], ...IP 10.0.0.2.80 > 10.0.0.1.57620: Flags [S.], ...128.6 IP header fields, length and checksums
- TCP checksums in a capture taken on the sender are 'incorrect' (offload fills them later); --fixcsum recalculates them.
- --ttl=10 and --tos=184 (0xb8 = EF) rewrite header fields; -C fixes checksums afterwards.
- --mtu=1000 --mtu-trunc cut the 1408-byte ping down to 1000 bytes.
tcprewrite -i orig.pcap -o fixed.pcap --fixcsumtcprewrite -i orig.pcap -o ttl.pcap \ --ttl=10 --tos=184 -Ctcprewrite -i orig.pcap -o mtu.pcap \ --mtu=1000 --mtu-trunc$ tcpdump -r orig.pcap -nn -t -vv tcp | grep -o cksum..cksum 0xa99e (incorrect -> 0xcce9)cksum 0xa99e (incorrect -> 0xd4f5)$ tcpdump -r fixed.pcap -nn -t -vv tcp | grep -o cksum..cksum 0xcce9 (correct)cksum 0xd4f5 (correct)$ tcpdump -r ttl.pcap -nn -t -v icmpIP (tos 0xb8, ttl 10, id 32357, offset 0, flags [DF], proto ICMP (1), length 84)$ tcpdump -r mtu.pcap -nn -t -v 'icmp and len > 900'IP (tos 0x0, ttl 64, id 32402, offset 0, flags [DF], proto ICMP (1), length 1000) 192.168.148.10 > 192.168.148.20: ICMP echo request, id 48, seq 0, length 980128.7 Fuzzing
- --fuzz-seed sets the random seed; --fuzz-factor=2 alters or drops bytes in about every second packet.
- Result: a missing reply, a broken frame and a garbage timestamp, useful for parser testing.
tcprewrite -i orig.pcap -o fuzz.pcap \ --fuzz-seed=7 --fuzz-factor=2tcpdump -r fuzz.pcap -nn -tIP 192.168.148.10 > 192.168.148.20: ICMP echo request, id 46, seq 0, length 64IP 192.168.148.10 > 192.168.148.20: ICMP echo request, id 46, seq 1, length 64IP 192.168.148.20 > 192.168.148.10: ICMP echo reply, id 46, seq 1, length 64 [|ether]IP 192.168.148.20.80 > 192.168.148.10.57620: Flags [S.], ... TS val 4293752809 ...128.8 Layer 2: MAC and VLAN
- --enet-smac / --enet-dmac set the MACs; with a cache, two values give client frames one MAC and server frames the other.
- --enet-vlan=add --enet-vlan-tag=100 --enet-vlan-pri=5 adds a VLAN tag (98 -> 102 bytes); --enet-vlan=del removes it.
- --enet-subsmac replaces one MAC; --enet-mac-seed randomizes all.
tcprewrite -i orig.pcap -o mac.pcap \ --enet-smac=02:00:00:00:01:28 \ --enet-dmac=02:42:c0:a8:94:14tcprewrite -i orig.pcap -o mac2.pcap \ -c orig.cache \ --enet-dmac=02:00:00:00:00:aa,02:00:00:00:00:bbtcprewrite -i orig.pcap -o vlan.pcap \ --enet-vlan=add --enet-vlan-tag=100 \ --enet-vlan-pri=5 --enet-vlan-cfi=0$ tcpdump -r mac.pcap -nn -t -e icmp02:00:00:00:01:28 > 02:42:c0:a8:94:14, ... request02:00:00:00:01:28 > 02:42:c0:a8:94:14, ... reply$ tcpdump -r mac2.pcap -nn -t -e icmp02:42:c0:a8:94:0a > 02:00:00:00:00:aa, ethertype IPv4 ... 192.168.148.10 > 192.168.148.20: ICMP echo request02:42:c0:a8:94:14 > 02:00:00:00:00:bb, ethertype IPv4 ... 192.168.148.20 > 192.168.148.10: ICMP echo reply$ tcpdump -r vlan.pcap -nn -t -e 'vlan and icmp'02:42:c0:a8:94:0a > 02:42:c0:a8:94:14, ethertype 802.1Q (0x8100), length 102: vlan 100, p 5, ethertype IPv4 (0x0800), 192.168.148.10 > 192.168.148.20: ICMP echo request, id 46, seq 0128.9 Output data link type (DLT)
- A -i any capture is Linux cooked v2 with no Ethernet header; --dlt=enet adds one (link type EN10MB).
- --dlt=hdlc writes a Cisco HDLC header instead (98 -> 88 bytes).
- Others: jnpr_eth, pppserial, user (--user-dlt, --user-dlink).
tcpdump -i any -nn -c 2 -w sll.pcap icmptcprewrite -i sll.pcap -o sll-eth.pcap \ --dlt=enet \ --enet-smac=02:42:c0:a8:94:0a \ --enet-dmac=02:42:c0:a8:94:14tcprewrite -i orig.pcap -o hdlc.pcap \ --dlt=hdlc --hdlc-address=0x0F --hdlc-control=0$ tcpdump -r sll.pcap -nn -t -ereading from file sll.pcap, link-type LINUX_SLL2eth0 Out ifindex 2 02:42:c0:a8:94:0a ethertype IPv4 ... 192.168.148.10 > 192.168.148.20: ICMP echo request$ tcpdump -r sll-eth.pcap -nn -t -ereading from file sll-eth.pcap, link-type EN10MB02:42:c0:a8:94:0a > 02:42:c0:a8:94:14, ethertype IPv4 (0x0800), length 98: 192.168.148.10 > 192.168.148.20: ICMP echo request, id 265, seq 0, length 64$ tcpdump -r hdlc.pcap -nn -t -e icmpreading from file hdlc.pcap, link-type C_HDLC (Cisco HDLC)unicast, ethertype IPv4 (0x0800), length 88: 192.168.148.10 > 192.168.148.20: ICMP echo request128.10 fragroute (in the file only)
- --fragroute=frag.conf with 'ip_frag 512' split the big ping into 3 fragments: offsets 0, 512, 1024.
- --fragdir=c2s fragments only client packets (needs the cache). Used for IDS evasion tests.
printf 'ip_frag 512\n' > frag.conftcprewrite -i orig.pcap -o frag.pcap \ --fragroute=frag.conftcpdump -r frag.pcap -nn -t -v \ 'icmp and (ip[6:2] & 0x3fff != 0)'IP (... id 32402, offset 0, flags [+], length 532) 192.168.148.10 > 192.168.148.20: ICMP echo requestIP (... id 32402, offset 512, flags [+], length 532)IP (... id 32402, offset 1024, flags [none], length 404)IP (... id 18732, offset 0, flags [+], length 532) 192.168.148.20 > 192.168.148.10: ICMP echo replyIP (... id 18732, offset 512, flags [+], length 532)IP (... id 18732, offset 1024, flags [none], length 404)| Area | Options |
|---|---|
| IP / port | --srcipmap, --dstipmap, --pnat (IPv4/IPv6), --portmap, -s seed, --endpoints (cache), --skipbroadcast, --tcp-sequence |
| IP header | -C/--fixcsum, --ttl (=, +, -), --tos, --tclass, --flowlabel, --fixlen pad|trunc|del, --mtu, --mtu-trunc, --efcs |
| Fuzzing | --fuzz-seed, --fuzz-factor |
| Layer 2 | --enet-smac/dmac (two values with cache), --enet-subsmac, --enet-mac-seed, --enet-vlan add|del, -tag, -pri, -cfi, -proto 802.1ad |
| DLT | --dlt enet|hdlc|jnpr_eth|pppserial|user, --user-dlt, --user-dlink, --hdlc-address, --hdlc-control |
| Files | -i/-o, -c cache, --fragroute, --fragdir c2s|s2c|both, --skip-soft-errors, -v, --save-opts/--load-opts |
128.11 tcpreplay: interfaces and input
- replay.pcap: IPs mapped to unused lab addresses, MAC pointed at the sink, checksums fixed.
- --listnics lists interfaces; -i eth0 sends with original timing; the sink's tcpdump saw requests and replies.
- -I eth1 + -c cache = two-NIC replay; -2 a.pcap b.pcap for two taps; -K preloads into RAM; a directory can be replayed.
MAP=192.168.148.10/32:192.168.148.110/32,\192.168.148.20/32:192.168.148.120/32tcprewrite -i orig.pcap -o replay.pcap \ --pnat=$MAP --fixcsum \ --enet-smac=02:00:00:00:01:28 \ --enet-dmac=02:42:c0:a8:94:14tcpreplay --listnicstcpreplay -i eth0 replay.pcap$ tcpreplay --listnicsAvailable network interfaces:eth0anynflognfqueue$ tcpreplay -i eth0 replay.pcapActual: 16 packets (4072 bytes) sent in 0.313576 secondsRated: 12985.6 Bps, 0.103 Mbps, 51.02 pps Successful packets: 16 Failed packets: 0[sink] $ tcpdump -i eth0 -nn -t -c 4 \ 'host 192.168.148.120 and icmp'IP 192.168.148.110 > 192.168.148.120: ICMP echo requestIP 192.168.148.120 > 192.168.148.110: ICMP echo replyIP 192.168.148.110 > 192.168.148.120: ICMP echo requestIP 192.168.148.120 > 192.168.148.110: ICMP echo reply4 packets captured128.12 Replay speed and timing
| Option | Meaning | Lab: 16 packets took |
|---|---|---|
| (default) | Original capture timing | 0.313576 s |
| -t / --topspeed | As fast as possible | 0.000295 s (110.42 Mbps) |
| -x 2 | Multiplier: 2x speed | 0.158092 s |
| -M 1 | Fixed rate in Mbps | 0.033013 s (0.986 Mbps) |
| -p 10 | Packets per second | 1.50 s (10.63 pps) |
| -o | One packet per Enter key (debug) | - |
- -x, -p, -M, -t, -o are mutually exclusive; --pps-multi, --maxsleep and -T (timer) fine-tune timing.
128.13 Limits and looping
- -L 5 sends only 5 packets; -l 3 plays the file 3 times (48 packets); -l 0 loops forever.
- --duration=2 stops an infinite loop after 2 seconds.
- --unique-ip shifts IPs every loop so the device sees new flows; --loopdelay-ms waits between loops.
tcpreplay -i eth0 -t -L 5 replay.pcaptcpreplay -i eth0 -t -l 3 replay.pcaptcpreplay -i eth0 -p 10 -l 0 --duration=2 replay.pcaptcpreplay -i eth0 -t -l 3 --unique-ip -q replay.pcapActual: 5 packets (466 bytes) sent in 0.000167 seconds$ tcpreplay -i eth0 -t -l 3 replay.pcapActual: 48 packets (12216 bytes) sent in 0.000737 secondsRated: 16575305.2 Bps, 132.60 Mbps, 65128.90 pps[sink] $ tcpdump -i eth0 -nn -q -c 48 'host ...120'48 packets captured$ tcpreplay -i eth0 -p 10 -l 0 --duration=2 replay.pcapActual: 21 packets (4538 bytes) sent in 2.00 secondsRated: 2265.1 Bps, 0.018 Mbps, 10.48 pps$ tcpreplay -i eth0 -t -l 3 --unique-ip -q replay.pcapActual: 48 packets (12216 bytes) sent in 0.001374 secondsFlows: 12 flows, 8733.62 fps, 48 unique flow packets[sink] IP 192.168.148.109 > 192.168.148.121: ICMP echo request[sink] IP 192.168.148.121 > 192.168.148.109: ICMP echo reply128.14 Statistics, netmap and tcpreplay-edit
- --stats=1 prints a line every second; Actual = packets/time, Rated = real speed, Flows = connections.
- Failed, Truncated and Retried must be 0, otherwise check NIC buffers or MTU.
- -v decodes every packet; -q prints only final stats.
- netmap (--netmap / -i netmap:eth0) gives full line rate but needs the kernel module; this build had none.
- tcpreplay-edit = tcprewrite options + tcpreplay in one step, no new file ('Packet editing: enabled').
tcpreplay -i eth0 -p 10 -l 2 --stats=1 replay.pcapActual: 12 packets (1056 bytes) sent in 1.10 secondsRated: 959.8 Bps, 0.007 Mbps, 10.90 ppsActual: 22 packets (4612 bytes) sent in 2.10 secondsRated: 2193.1 Bps, 0.017 Mbps, 10.46 ppsTest complete: ...Actual: 32 packets (8144 bytes) sent in 3.13 secondsRated: 2597.7 Bps, 0.020 Mbps, 10.20 ppsFlows: 4 flows, 1.27 fps, 32 unique flow packets, ...Statistics for network device: eth0 Successful packets: 32 Failed packets: 0 Truncated packets: 0 Retried packets (ENOBUFS): 0 Retried packets (EAGAIN): 0tcpreplay -Vtcpreplay -i netmap:eth0 -t replay.pcapInjection method: PF_PACKET send()Not compiled with netmap$ tcpreplay -i netmap:eth0 -t replay.pcapFatal Error: Unable to parse args: Unable to connect to netmap interface netmap:eth0. Ensure netmap module is installed (see INSTALL).tcpreplay -V | grep editingtcpreplay-edit -V | grep editingMAP2=192.168.148.10/32:192.168.148.210/32,\192.168.148.20/32:192.168.148.220/32tcpreplay-edit -i eth0 --pnat=$MAP2 \ --enet-smac=02:00:00:00:01:28 \ --enet-dmac=02:42:c0:a8:94:14 \ -L 2 orig.pcapPacket editing: disabledPacket editing: enabledActual: 2 packets (196 bytes) sent in 0.000254 seconds Failed packets: 0[sink] $ tcpdump -i eth0 -nn -t -c 2 \ 'host 192.168.148.220 and icmp'IP 192.168.148.210 > 192.168.148.220: ICMP echo requestIP 192.168.148.220 > 192.168.148.210: ICMP echo reply2 packets captured128.15 Common mistakes
- Replaying captures from the sender without --fixcsum: the IDS ignores TCP packets with bad checksums.
- Replaying without rewriting MAC/IP: the target drops the packets.
- Trusting a file just because tcprewrite gave no error: the old 4.4.4 build added a VLAN tag without growing the frame.
- Replaying on a production network.
# Alpine 3.20 package: tcprewrite 4.4.4, --enet-vlan=add --enet-vlan-tag=100$ tcpdump -r bug.pcap -nn -t -e 'vlan and icmp'02:42:c0:a8:94:0a > 02:42:c0:a8:94:14, ethertype 802.1Q (0x8100), length 98: vlan 100, p 0, ethertype IPv4 (0x0800), truncated-ip - 4 bytes missing! 192.168.148.10 > 192.168.148.20: ICMP echo request128.16 Interview tips
- Testing a firewall with recorded traffic: isolated lab -> tcpprep cache -> tcprewrite --pnat, --enet-dmac, --fixcsum -> tcpreplay -i eth0 -I eth1 -c in.cache -> check stats.
- Know the speed options: default timing, -x, -M, -p, --topspeed.
- Checksum offload explains 'incorrect' checksums in captures taken on the sender.
- Always verify the rewritten file with tcpdump -r before replaying.