Skip to content

Commit 09d7fc4

Browse files
AmmarAbouZormarcmo
authored andcommitted
Add replay pcap script & move scripts to one directory
* Provide python script for replaying pcap files. * Move developing scripts to their own directory adjusting their paths in readme too.
1 parent 87c1bef commit 09d7fc4

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

contribution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ To check if you have all the pre-requisite installed or not, chipmunk provides t
1616
script for this purpose. After cloning the repo run following command in your preferred terminal.
1717

1818
```
19-
sh developing/check.sh
19+
sh developing/scripts/check.sh
2020
```
2121

2222
If everything is installed then script should print success messages.
@@ -69,7 +69,7 @@ This project uses few dependencies from other languages and to install them run
6969
following command in terminal.
7070

7171
```
72-
sh developing/install.sh
72+
sh developing/scripts/install.sh
7373
```
7474

7575
We are using [yarn](https://yarnpkg.com/) as a package manager. Installing yarn is simple

developing/scripts/replay_pcap.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
TCP Packet Replay Script from PCAP File
3+
4+
This script reads a PCAP file, extracts TCP packets containing raw payload data,
5+
and replays them over a TCP server while maintaining the original timing.
6+
7+
Usage:
8+
python replay_pcap.py [-h] [--port PORT] pcap_file
9+
10+
Arguments:
11+
- pcap_file (str): Path to the PCAP file to be replayed.
12+
- --port (int, optional): TCP port to listen on (default: 9999).
13+
14+
Example:
15+
python script.py traffic.pcap --port 8080
16+
17+
Requirements:
18+
- Python 3
19+
- scapy (install using `pip install scapy`)
20+
21+
"""
22+
23+
import socket
24+
from scapy.all import rdpcap, TCP, Raw
25+
import argparse
26+
import time
27+
28+
# Parse command-line arguments
29+
parser = argparse.ArgumentParser(description="Replay TCP packets from a PCAP file.")
30+
parser.add_argument("pcap_file", help="Path to the PCAP file")
31+
parser.add_argument(
32+
"--port", type=int, default=9999, help="Port to listen on (default: 9999)"
33+
)
34+
args = parser.parse_args()
35+
36+
PCAP_FILE = args.pcap_file
37+
PORT = args.port
38+
39+
# Read packets from the specified PCAP file
40+
packets = rdpcap(PCAP_FILE)
41+
42+
# Server setup
43+
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
44+
server.bind(("0.0.0.0", PORT))
45+
server.listen(1)
46+
47+
print(f"Listening on port {PORT}...")
48+
conn, addr = server.accept()
49+
print(f"Connection from {addr}")
50+
51+
# Establish timing for replay
52+
if packets[0].time:
53+
start_time = packets[0].time
54+
else:
55+
start_time = time.time()
56+
57+
count = 0
58+
59+
for pkt in packets:
60+
if pkt.haslayer(TCP) and pkt.haslayer(Raw): # Send only raw data
61+
print(f"Sending {len(pkt[Raw].load)} bytes, packet {count}")
62+
count += 1
63+
delay = float(pkt.time) - start_time
64+
time.sleep(max(0, float(delay)))
65+
conn.send(pkt[Raw].load)
66+
start_time = float(pkt.time)
67+
68+
conn.close()
69+
server.close()
70+
print("Replay finished.")

0 commit comments

Comments
 (0)