-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharkrecon.py
160 lines (133 loc) · 5.24 KB
/
sharkrecon.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
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
# Author @SomnathDas
# External Libs
import pyshark
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from time import strftime, localtime
# Utilities
def clear_line(n=1):
LINE_UP = "\033[1A"
LINE_CLEAR = "\x1b[2K"
for i in range(n):
print(LINE_UP, end=LINE_CLEAR)
# Main Class
class UnwireShark:
def __init__(self, filename) -> None:
self.captureFile = pyshark.FileCapture(filename, keep_packets=False)
def getPacketsData(self):
data = []
for idx, packet in enumerate(self.captureFile):
print("[*] Processing: {}".format(idx))
clear_line()
if hasattr(packet, "ipv6") and hasattr(packet, "udp"):
data.append(
{
"src": packet.ipv6.src,
"dst": packet.ipv6.dst,
"proto": packet.highest_layer,
"time": strftime(
"%Y-%m-%d %H:%M:%S",
localtime(packet.sniff_time.timestamp()),
),
"data": packet.udp.payload,
}
)
elif hasattr(packet, "udp") and hasattr(packet, "ip"):
data.append(
{
"src": packet.ip.src,
"dst": packet.ip.dst,
"proto": packet.highest_layer,
"time": strftime(
"%Y-%m-%d %H:%M:%S",
localtime(packet.sniff_time.timestamp()),
),
"data": packet.udp.payload,
}
)
elif (
hasattr(packet, "ip")
and hasattr(packet.ip, "src")
and hasattr(packet.ip, "dst")
):
if not (hasattr(packet, "data") or hasattr(packet, "port")):
data.append(
{
"src": packet.ip.src,
"dst": packet.ip.dst,
"proto": packet.highest_layer,
"time": strftime(
"%Y-%m-%d %H:%M:%S",
localtime(packet.sniff_time.timestamp()),
),
"data": "Empty",
}
)
else:
data.append(
{
"src": packet.ip.src,
"dst": packet.ip.dst,
"port": packet.tcp.port,
"proto": packet.highest_layer,
"time": strftime(
"%Y-%m-%d %H:%M:%S",
localtime(packet.sniff_time.timestamp()),
),
"data": packet.tcp.payload,
}
)
elif hasattr(packet, "arp"):
data.append(
{
"src": packet.arp.src_proto_ipv4,
"dst": packet.arp.dst_proto_ipv4,
"port": "Empty",
"proto": packet.highest_layer,
"time": strftime(
"%Y-%m-%d %H:%M:%S",
localtime(packet.sniff_time.timestamp()),
),
"data": "Empty",
}
)
return data
# Main Class
class PictureTheData:
def __init__(self, csv_filepath) -> None:
self.df = pd.read_csv(csv_filepath)
def showIPFreqGraph(self):
x = np.array((self.df["src"].unique()))
y = np.array(self.df["src"].value_counts())
plt.ylabel("No. of Requests")
plt.xticks(rotation=45)
plt.subplots_adjust(bottom=0.25)
plt.bar(x, y)
plt.show()
def showProtocolFreqGraph(self):
x = np.array((self.df["proto"].unique()))
y = np.array(self.df["proto"].value_counts())
plt.bar(x, y)
plt.show()
def showTimeVsRequestGraph(self):
x = self.df.groupby(["time", "proto"]).size().unstack(fill_value=0)
x.plot(kind="bar", stacked=True)
plt.title("Protocols making up total requests made at a given time")
plt.xlabel("Date and Time")
plt.ylabel("Fraction of Protocols in Req/Res")
plt.xticks(rotation=45)
plt.subplots_adjust(bottom=0.25)
plt.show()
def extractPlainTextData(self):
final = []
for idx, i in enumerate(self.df["data"]):
if i == "Empty":
pass
else:
ascii_data = bytearray.fromhex(i.replace(":", " ")).decode(
errors="replace"
)
src_data = self.df["src"][idx]
final.append([src_data, ascii_data])
return pd.DataFrame(final, columns=["IP Address", "PlainText Data (UTF-8)"])