-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap_convert.py
64 lines (57 loc) · 1.95 KB
/
pcap_convert.py
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
#!/usr/bin/python3
################################################################################
# File: pcap_convert.py
# Author: Andrew M. Lamarra
# Modified: 9/26/2016
# Purpose: PaloAlto provides the packets of the threats it detects in text form
# The user will copy that text, save it to the same directory as this
# script, and run it. The output will strip the packet headers and
# show only the ASCII values of the packet data. This will make it
# more readable and easier to find (ctrl+F) content.
################################################################################
# Read in the data
with open("example.txt", "r") as f:
data = f.read()
# Remove any trailing tabs or newlines
while data[len(data)-1] == "\n" or data[len(data)-1] == "\t":
data = data[:len(data)-1]
# Break the string up into a list of lines
lst = data.split("\n")
# Separate the data by the individual packets (this will be a 2D array)
packets = []
msg = ""
for line in lst:
if line[0] != "\t":
packets.append([])
else:
packets[len(packets)-1].append(line)
print("Number of packets = {}\n".format(len(packets)))
msg += "Number of packets = {}\n\n".format(len(packets))
# Strip out everything but the raw bytes
# As well as packet headers (first 54 bytes, 108 characters)
for x in range(len(packets)):
temp = ""
for y in range(len(packets[x])):
temp += "".join(packets[x][y].split(" ")[2:-1])
packets[x] = temp[108:]
# Print everything
for packet in packets:
print("PACKET {}".format(packets.index(packet)+1))
msg += "PACKET {}\n".format(packets.index(packet)+1)
i = 0
while i < len(packet):
char = packet[i:i+2]
if int(char, 16) > 127:
char = "3f"
print(chr(int(char, 16)), end="")
msg += chr(int(char, 16))
i += 2
if packet[i-2:] != "0a" and packet[i-2:] != "0d":
print("\n")
msg += "\n"
if packet[i-4:i-2] != "0a" and packet[i-4:i-2] != "0d":
print("\n")
msg += "\n"
# Also write it to a file
with open("cap_modified.txt", "w") as f:
f.write(msg)