-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcap_parser.py
More file actions
397 lines (325 loc) · 14.6 KB
/
Copy pathpcap_parser.py
File metadata and controls
397 lines (325 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
from scapy.all import *
import string
import argparse
import os
from collections import defaultdict
import time
import re
import difflib
# Constants
TIME_FORMAT = "%d/%m/%Y %H:%M:%S"
PROTOCOLS = {"tcp": TCP, "udp": UDP, "icmp": ICMP, "all": None}
MIN_PAYLOAD_SIZE = 5
GENERIC_SEPARATOR = "\n\n" + "=" * 40 + "\n\n"
TCP_SEPARATOR = "\n" + "=" * 40 + "\n"
CIPHERED_PORTS = {22, 443, 465, 636, 993, 995, 3389} # TCP ports for SSH, HTTPS, SMTPS, LDAPS, IMAPS, POP3S, RDP
class Colors:
BLUE = '\033[94m'
YELLOW = '\033[93m'
GREEN = '\033[92m'
RED = '\033[91m'
RESET = '\033[0m'
def colorize_pii(data):
pii_patterns = [
(r"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})", "email"),
(r"(password|pass|pwd|secret|token|api_key|auth_key|access_key|cookie|session)", "potential credentials"),
(r"(credit|card|cc_num|card_no)", "potential credit card"),
(r"(ssn|social_security_number)", "potential SSN"),
(r"(cvv)", "potential CVV"),
(r"(user|username|login|user_id|usr)", "potential username"),
(r"(cred)", "potential credential"),
(r"([a-zA-Z0-9+/]{15,})", "potential high-entropy string"),
]
for pattern, _ in pii_patterns:
data = re.sub(pattern, lambda m: f"{Colors.RED}{m.group(0)}{Colors.RESET}", data, flags=re.IGNORECASE)
return data
def reconstruct_dns(payload):
try:
dns_packet = DNS(payload)
# Check if it's a DNS query (opcode 0) and has a question name
if dns_packet.qr == 0 and dns_packet.qd and dns_packet.qd.qname:
return dns_packet.qd.qname.decode('utf-8', 'ignore').rstrip('.')
except Exception as e:
return None
return None
def extract_streams(pcap_file, dest_ip=None, src_ip=None, src_port=None, dst_port=None):
tcp_streams = defaultdict(list)
udp_packets = []
icmp_packets = []
unknown_protocol_packets = []
print(f"[!] Reading pcap file: {pcap_file}. This can take a while.")
packets = rdpcap(pcap_file)
for packet in packets:
try:
if IP not in packet:
continue
src_ip = packet[IP].src
dst_ip = packet[IP].dst
if dest_ip and dst_ip != dest_ip:
continue
if src_ip and src_ip != packet[IP].src:
continue
if src_port and TCP in packet and packet[TCP].sport != src_port:
continue
if dst_port and TCP in packet and packet[TCP].dport != dst_port:
continue
if src_port and UDP in packet and packet[UDP].sport != src_port:
continue
if dst_port and UDP in packet and packet[UDP].dport != dst_port:
continue
timestamp = int(packet.time)
formatted_utc_time = time.strftime(TIME_FORMAT, time.gmtime(timestamp))
# TCP
if TCP in packet:
payload = bytes(packet[TCP].payload)
if len(payload) >= MIN_PAYLOAD_SIZE:
stream_id = tuple(sorted([
(src_ip, packet[TCP].sport),
(dst_ip, packet[TCP].dport)
]))
tcp_streams[stream_id].append({
"time": formatted_utc_time,
"timestamp": packet.time,
"src_ip": src_ip,
"src_port": packet[TCP].sport,
"dst_ip": dst_ip,
"dst_port": packet[TCP].dport,
"payload": payload
})
# UDP
elif UDP in packet:
payload = bytes(packet[UDP].payload)
if len(payload) >= MIN_PAYLOAD_SIZE:
udp_packets.append({
"time": formatted_utc_time,
"src_ip": src_ip,
"src_port": packet[UDP].sport,
"dst_ip": dst_ip,
"dst_port": packet[UDP].dport,
"payload": payload
})
# ICMP
elif ICMP in packet:
payload = bytes(packet[ICMP].payload)
if len(payload) >= MIN_PAYLOAD_SIZE:
icmp_packets.append({
"time": formatted_utc_time,
"src_ip": src_ip,
"dst_ip": dst_ip,
"payload": payload
})
# Unknown
else:
unknown_protocol_packets.append({
"time": formatted_utc_time,
"src_ip": src_ip,
"dst_ip": dst_ip,
"payload": b"UNKNOWN PROTOCOL"
})
except Exception:
continue
for stream_id in tcp_streams:
tcp_streams[stream_id].sort(key=lambda p: p["timestamp"])
return {
"TCP": tcp_streams,
"UDP": udp_packets,
"ICMP": icmp_packets,
"UNKNOWN": unknown_protocol_packets
}
def get_printable_characters(input_string):
return ''.join([char for char in input_string if char in string.printable])
def reconstruct_non_printable_payload(payload_bytes, src_port, dst_port):
"""
Reconstructs a payload by extracting only printable characters,
and indicates if non-printable chars were present.
Returns the reconstructed string if applicable, otherwise None.
"""
if src_port in CIPHERED_PORTS or dst_port in CIPHERED_PORTS:
return None
decoded_payload = payload_bytes.decode('utf-8', 'ignore')
printable_payload = get_printable_characters(decoded_payload)
# Check if non-printable characters were present
# A simple heuristic: if the length changes after stripping, or if the original decode had issues
# and there's still printable data.
if len(printable_payload) < len(decoded_payload) or (len(payload_bytes) > 0 and not decoded_payload and len(printable_payload) > 0):
if len(printable_payload) >= MIN_PAYLOAD_SIZE: # Only reconstruct if there's enough printable data
return f"[RECONSTRUCTED NON-PRINTABLE PAYLOAD]:\n{printable_payload}"
return None
def reset_file(file_name):
with open(file_name, 'w') as file:
file.write("")
def print_tcp_streams(title, tcp_streams, output_file=None, grep=None, raw=False, color=Colors.RESET):
if not tcp_streams:
return
for stream_id, packets in tcp_streams.items():
ip_port_1, ip_port_2 = stream_id
stream_str = f"{ip_port_1[0]}:{ip_port_1[1]} <-> {ip_port_2[0]}:{ip_port_2[1]}"
data_lines = []
for packet in packets:
time_str = packet.get("time", "")
src_ip = packet.get("src_ip", "")
dst_ip = packet.get("dst_ip", "")
src_port = packet.get("src_port", "")
dst_port = packet.get("dst_port", "")
payload = packet.get("payload", b"")
try:
decoded = payload.decode('utf-8').strip()
clean = get_printable_characters(decoded)
except:
if not raw:
continue
clean = payload.hex()
if (not len(clean) >= MIN_PAYLOAD_SIZE):
continue
if (clean[:4] == "HTTP"):
clean = "\n" + clean
data_lines.append(clean)
if not data_lines:
continue
header = f"{time_str} | {src_ip}:{src_port} -> {dst_ip}:{dst_port} |\n\n"
data = "\n".join(data_lines).strip()
if grep and grep.lower() not in data.lower():
continue
data = colorize_pii(data)
if output_file:
with open(output_file, 'a') as f:
f.write(data)
print(TCP_SEPARATOR)
print(f"{color}{title} Stream: {stream_str}{Colors.RESET}\n{header}{data}")
def print_flat_packets(title, packets, output_file=None, grep=None, raw=False, color=Colors.RESET):
if not packets:
return
print(GENERIC_SEPARATOR)
print(f"[!] Printing {title} packets\n" + "=" * 40)
displayed_dns_queries = set()
processed_payloads = [] # To store payloads that have already been processed/printed
all_packets_to_print = []
for packet_info in packets:
time_str = packet_info.get("time", "")
src_ip = packet_info.get("src_ip", "")
dst_ip = packet_info.get("dst_ip", "")
src_port = packet_info.get("src_port", "")
dst_port = packet_info.get("dst_port", "")
payload = packet_info.get("payload", b"")
packet_string = ""
clean_payload = "" # Initialize clean_payload
skip_packet = False
if dst_port == 53 or src_port == 53:
dns_query = reconstruct_dns(payload)
if dns_query:
if dns_query not in displayed_dns_queries:
packet_string = f"{time_str} | {src_ip}:{src_port} -> {dst_ip}:{dst_port} | DNS Query: {dns_query}"
displayed_dns_queries.add(dns_query)
clean_payload = dns_query # For similarity check
else:
skip_packet = True
else:
# DNS packet but couldn't reconstruct query, treat as regular UDP payload
reconstructed_payload = reconstruct_non_printable_payload(payload, src_port, dst_port)
if reconstructed_payload:
clean_payload = reconstructed_payload
else:
try:
clean_payload = get_printable_characters(payload.decode('utf-8').strip())
except:
if not raw:
skip_packet = True
else:
clean_payload = payload.hex()
if not skip_packet and not len(clean_payload) >= MIN_PAYLOAD_SIZE:
skip_packet = True
if not skip_packet:
if src_port and dst_port:
packet_string = f"{time_str} | {src_ip}:{src_port} -> {dst_ip}:{dst_port} |\n\n{clean_payload}"
else:
packet_string = f"{time_str} | {src_ip} -> {dst_ip} |\n\n{clean_payload}"
else:
# Not a DNS packet, treat as regular UDP payload
reconstructed_payload = reconstruct_non_printable_payload(payload, src_port, dst_port)
if reconstructed_payload:
clean_payload = reconstructed_payload
else:
try:
clean_payload = get_printable_characters(payload.decode('utf-8').strip())
except:
if not raw:
skip_packet = True
else:
clean_payload = payload.hex()
if not skip_packet and not len(clean_payload) >= MIN_PAYLOAD_SIZE:
skip_packet = True
if not skip_packet:
if src_port and dst_port:
packet_string = f"{time_str} | {src_ip}:{src_port} -> {dst_ip}:{dst_port} |\n\n{clean_payload}"
else:
packet_string = f"{time_str} | {src_ip} -> {dst_ip} |\n\n{clean_payload}"
if skip_packet:
continue
# Apply PII highlighting to the content
highlighted_line = colorize_pii(packet_string)
# Apply grep filter
if grep and grep.lower() not in highlighted_line.lower():
continue
# Check for similarity before adding to all_packets_to_print
is_similar = False
for existing_payload in processed_payloads:
# Use SequenceMatcher to calculate similarity
similarity_ratio = difflib.SequenceMatcher(None, clean_payload, existing_payload).ratio()
if similarity_ratio >= 0.80: # 80% similarity threshold
is_similar = True
break
if not is_similar:
all_packets_to_print.append(highlighted_line)
processed_payloads.append(clean_payload) # Store the clean payload for future comparisons
# Now print all collected lines with separators
for i, line in enumerate(all_packets_to_print):
if i > 0:
print(GENERIC_SEPARATOR) # Print separator before each line except the first
else:
print("\n\n")
print(f"{color}{title} Packet: {Colors.RESET}{line}")
# Write to file if output_file is specified
if output_file:
with open(output_file, 'a') as f:
f.write(line + "\n") # Write the line to file, then a newline
def main():
parser = argparse.ArgumentParser(description='Extract protocol streams from a pcap file.')
parser.add_argument('pcap_file', help='Path to the input pcap file')
parser.add_argument('--dest-ip', help='Destination IP address to filter packets')
parser.add_argument('--src-ip', help='Source IP address to filter packets')
parser.add_argument('--src-port', type=int, help='Source port to filter packets')
parser.add_argument('--dst-port', type=int, help='Destination port to filter packets')
parser.add_argument('--output-file', help='Output file to save the results')
parser.add_argument('--grep', type=str, help='Only display packets containing this grep term (case-insensitive)')
parser.add_argument('--raw', action='store_true', help='Outputs the data in raw format (including bad chars)')
parser.add_argument('--proto', choices=['tcp', 'udp', 'icmp'], default='all',
help='Protocol to extract (default: all)')
args = parser.parse_args()
if args.output_file:
reset_file(args.output_file)
try:
with open(args.pcap_file, "rb"):
pass
except Exception as e:
print(f"[!] Error opening file: {e}\n")
parser.print_usage()
return
streams = extract_streams(
args.pcap_file,
dest_ip=args.dest_ip,
src_ip=args.src_ip,
src_port=args.src_port,
dst_port=args.dst_port
)
proto = args.proto.upper()
if proto == "TCP" or proto == "ALL":
print_tcp_streams("TCP", streams["TCP"], output_file=args.output_file, grep=args.grep, raw=args.raw, color=Colors.BLUE)
if proto == "UDP" or proto == "ALL":
print_flat_packets("UDP", streams["UDP"], output_file=args.output_file, grep=args.grep, raw=args.raw, color=Colors.YELLOW)
if proto == "ICMP" or proto == "ALL":
print_flat_packets("ICMP", streams["ICMP"], output_file=args.output_file, grep=args.grep, raw=args.raw, color=Colors.GREEN)
if proto == "ALL":
print_flat_packets("UNKNOWN", streams["UNKNOWN"], output_file=args.output_file, grep=args.grep, raw=args.raw)
if __name__ == "__main__":
main()