-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMAC_Visualiser.py
215 lines (165 loc) · 5.59 KB
/
MAC_Visualiser.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
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
import csv
import matplotlib.pyplot as plt
import datetime as dt
import matplotlib.dates as mdates
import math
from manuf import manuf
# Script that visualises measured MAC-Tracking data
# input date file
file_path = 'measurments/Output.csv'
# function shows MAC occurrences over time in a scatter-plot
# giving each unique MAC address a unique color
def show_MACoverTime():
x = []
y = []
c = []
s = []
Mac_list = []
# list of MAC's to be high lighted
Special_MAC = {'Niels': '94:65:2d:2d:14:17', 'Jetse': '34:80:b3:f0:30:69', 'Mark': 'c0:ee:fb:92:d6:01'}
data = csv.reader(open(file_path), delimiter=' ')
for row in data:
MAC_scr = row[1]
time_stamp = row[0]
if MAC_scr not in Mac_list:
Mac_list.append(MAC_scr)
x.append(dt.datetime.fromtimestamp(int(float(time_stamp))))
y.append(Mac_list.index(MAC_scr))
if MAC_scr in Special_MAC.values():
s.append(20) # 75
else:
s.append(20) # 4
c.append(((float.fromhex(MAC_scr[-2:]) / 0xff),
(float.fromhex(MAC_scr[-5:-3]) / 0xff),
(float.fromhex(MAC_scr[-8:-6]) / 0xff)))
print(len(Mac_list))
fig, ax = plt.subplots()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%a %d %H:%M:%S'))
ax.set_xlim([min(x) - dt.timedelta(hours=1), max(x) + dt.timedelta(hours=1)])
plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=8))
plt.gca().xaxis.set_minor_locator(mdates.MinuteLocator(interval=60))
sc = plt.scatter(x, y, c=c, s=s)
plt.gcf().autofmt_xdate()
annot = ax.annotate("", xy=(0, 0), xytext=(20, 20), textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
# functions used to indicate selected data-point in plot
def update_annot(ind):
ax.set_title(Mac_list[y[ind['ind'][0]]] + " " + x[ind['ind'][0]].strftime("%d %H:%M:%S"))
def hover(event):
vis = annot.get_visible()
if event.inaxes == ax:
cont, ind = sc.contains(event)
if cont:
update_annot(ind)
annot.set_visible(True)
fig.canvas.draw_idle()
else:
if vis:
annot.set_visible(False)
fig.canvas.draw_idle()
fig.canvas.mpl_connect("motion_notify_event", hover)
plt.show()
# function shows a pie chard of vendor types
def show_vendors():
other = 0.032 # percentage threshold of being put under 'Others'
sizes = {}
vendors = {}
total = 0
data = csv.reader(open(file_path), delimiter=' ')
for row in data:
MAC_scr = row[1][0:8]
if MAC_scr in sizes:
sizes[MAC_scr] += 1
else:
sizes[MAC_scr] = 1
total += 1
p = manuf.MacParser(update=True)
for key, value in sizes.items():
vendor = p.get_manuf(key + ':00:00:00')
if vendor in vendors:
vendors[vendor] += value
else:
vendors[vendor] = value
vendors2 = {"other": 0}
for key, value in vendors.items():
if value < total * other:
vendors2["other"] += value
else:
vendors2[key] = value
plt.pie(vendors2.values(), labels=vendors2.keys(), autopct='%1.1f%%', shadow=True, startangle=140)
plt.show()
# function shows the number of unique MAC occurrences per timeslot in a bar-graph
def show_uniqueMacoverTime():
timeslot = 1800 # size of timeslot in seconds
lastHour = 0.0
lastTime = 0.
y = []
x = []
Mac_list = []
data = csv.reader(open(file_path), delimiter='\t')
for row in data:
MAC_scr = row[1]
time_stamp = int(float(row[0]))
if lastTime == 0.0:
lastTime = time_stamp - (time_stamp % timeslot)
if time_stamp > lastTime + timeslot:
x.append(dt.datetime.fromtimestamp(int(float(lastTime))))
y.append(len(Mac_list))
Mac_list = []
lastTime = lastTime + timeslot
if MAC_scr not in Mac_list:
Mac_list.append(MAC_scr)
fig, ax = plt.subplots()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%a %d %H:%M:%S')) #
ax.set_xlim([min(x) - dt.timedelta(hours=1), max(x) + dt.timedelta(hours=1)])
plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=4))
plt.gca().xaxis.set_minor_locator(mdates.MinuteLocator(interval=30))
plt.gcf().autofmt_xdate()
plt.bar(x, y, width=0.019) # , align='center'
plt.show()
# function shows the number of unique MAC occurrences per timeslot in a bar-graph averaged over multple days
def show_uniqueMacoverTimeOneDay():
timeslot = 1800 # size of timeslot in seconds
y = []
x = []
Mac_list = []
lastTime = 0.0
data = csv.reader(open(file_path), delimiter='\t')
for row in data:
MAC_scr = row[1]
time_stamp = int(float(row[0]))
if lastTime == 0.0:
lastTime = time_stamp - (time_stamp % timeslot)
if time_stamp > lastTime + timeslot:
x.append(dt.datetime.fromtimestamp(int(float(lastTime))))
y.append(len(Mac_list))
Mac_list = []
lastTime = lastTime + timeslot
if MAC_scr not in Mac_list:
Mac_list.append(MAC_scr)
x2 = [dt.datetime(2018, 2, 27, 0, 0, 0) + dt.timedelta(seconds=timeslot * x) for x in
range(0, int(86400 / timeslot))]
y2 = [0] * int(86400 / timeslot)
for date, count in zip(x, y):
if 1520031660 < date.timestamp() < 1520115839:
hour = date.hour
minute = date.minute
index = int(hour * (3600 / timeslot) + math.floor(minute * (60 / timeslot)))
y2[index] += count
fig, ax = plt.subplots()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
ax.set_xlim([min(x2) - dt.timedelta(hours=0), max(x2) + dt.timedelta(hours=0.5)])
plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=2))
plt.gca().xaxis.set_minor_locator(mdates.MinuteLocator(interval=30))
plt.gcf().autofmt_xdate()
plt.bar(x2, y2, width=0.018, align='edge') # , align='center'
# plt.plot(x, y)
plt.show()
print("started")
# select function to use
show_MACoverTime()
# show_vendors()
# show_uniqueMacoverTime()
# show_uniqueMacoverTimeOneDay()