Repository navigation
Expand file tree
/
Copy pathNetworyAnalyzer.py
More file actions
454 lines (408 loc) · 17.5 KB
/
Copy pathNetworyAnalyzer.py
File metadata and controls
454 lines (408 loc) · 17.5 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# AADI'S NETWORK ANALYZER.
# This python program analyzes captured network packets in CSV and allows the user to classify them according to source traffic, destination traffic
# protocols and also map the network traffic with its connections in a visual format.
# Suspected network addresses can also be marked and mapped.
# An additional GeoLocator feature is also provided.
#
# MIT License
# Copyright (c) 2022 Aadith Sukumar
# Created by AADITH SUKUMAR
# GitHub: https://github.com/aadi1011
# LinkedIn: https://linkedin.com/in/aadith-sukumar/
# Importing required libraries
from cgitb import html
import os
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
# Library to track geolocation
import geoip2.database
# Pyfiglet for intro ASCII art. Download by "$pip install pyfiglet".
# Documentation at https://pypi.org/project/pyfiglet/0.7/
import pyfiglet
from pyvis.network import Network
# banner() function for ASCII Word Art in the terminal. banner() is called at start of every function after
# clearing screen to stay on top of program
# effectively pinned as the header of the program
def banner():
result = pyfiglet.figlet_format("Aadi's Network Analyzer", font = "slant" ) #pyfiglet module helps create the art
print(result)
# Main menu function. Actual program starts with the start_screen() function
def menu2(data_file):
banner()
# menu option
print("0. Go Back")
print("1. Show Data")
print("2. Build Graphs")
print("3. Trace Suspected Address")
print("4. Find public IP address GeoLocation")
# menu for master options
option_menu2 = int(input("Choose an option: \n")) # Take user choice for menu
if(option_menu2 == 1): #Proceeds to show_data()
os.system('cls')
show_data(data_file)
elif(option_menu2==2): #Proceeds to graph_data()
os.system('cls')
graph_data(data_file)
elif(option_menu2==3): #Proceeds to suspect()
os.system('cls')
suspect(data_file)
elif(option_menu2==4): #Proceeds to GeoLoc()
os.system('cls')
GeoLoc(data_file)
elif(option_menu2==0): #Goes back
os.system('cls')
start_screen()
else: #If invalid option given by user
os.system('cls')
print("Invalid input given. Please try again.")
os.system('pause')
os.system('cls')
menu2(data_file)
#show_data function includes all the text-based diplayable options provided by the program.
def show_data(data_file):
banner()
print("0. Go Back")
print("1. Show first 10 readings")
print("2. Show source and counts")
print("3. Show destination and counts")
print("4. Show protocols and counts")
print("5. Show all traffic of a protocol")
sub_option2 = int(input("Choose option: "))
if(sub_option2==1):
try:
print(data_file.head(10)) #pandas head() prints the first 10 results of csv
os.system('pause')
os.system('cls')
show_data(data_file)
except KeyError:
os.system('cls')
banner()
print("Invalid CSV Format provided. Please upload a valid CSV file of Wireshark export format.")
os.system('pause')
os.system('cls')
start_screen()
elif(sub_option2==2):
try:
sources=data_file.groupby("Source").Source.count() #groups the csv data by the 'Source' filter and sorts them by their count
print(sources.sort_values())
os.system('pause')
os.system('cls')
show_data(data_file)
except KeyError:
os.system('cls')
banner()
print("Invalid CSV Format provided. Please upload a valid CSV file of Wireshark export format.")
os.system('pause')
os.system('cls')
start_screen()
elif(sub_option2==3):
try:
dest=data_file.groupby("Destination").Destination.count() #groups the csv data by the 'Destination' filter and sorts them by their count
print(dest.sort_values())
os.system('pause')
os.system('cls')
show_data(data_file)
except KeyError:
os.system('cls')
banner()
print("Invalid CSV Format provided. Please upload a valid CSV file of Wireshark export format.")
os.system('pause')
os.system('cls')
start_screen()
elif(sub_option2==4):
try:
protocol=data_file.groupby("Protocol").Protocol.count() #groups the csv data by the 'Protocol' filter and sorts them by their count
print(protocol.sort_values())
os.system('pause')
os.system('cls')
show_data(data_file)
except KeyError:
os.system('cls')
banner()
print("Invalid CSV Format provided. Please upload a valid CSV file of Wireshark export format.")
os.system('pause')
os.system('cls')
start_screen()
elif(sub_option2==5):
try:
ProtoSearch = input("Enter the protocol you want to search (case sensitive): ") #User input to search all connections for a protocol.
#Allowing more than default '10' values to be printed
pd.set_option('display.max_rows', 500)
print(data_file.loc[data_file['Protocol']==ProtoSearch, ["Time","Source","Destination","Protocol","Length"]])
os.system('pause')
os.system('cls')
#Setting values back to 10 (default)
pd.set_option('display.max_rows', 10)
show_data(data_file)
except KeyError:
os.system('cls')
banner()
print("Invalid CSV Format provided. Please upload a valid CSV file of Wireshark export format.")
os.system('pause')
os.system('cls')
start_screen()
elif(sub_option2==0): #Goes back to main menu
os.system('cls')
menu2(data_file)
else:
os.system('cls')
print("Invalid input given. Please try again.")
os.system('pause')
os.system('cls')
show_data(data_file)
#graph_data() provides options for graphical display of data
def graph_data(data_file):
banner()
print("0. Go Back")
print("1. Display NodeView of traffic")
print("2. Display EdgeView of traffic")
print("3. Display network map based on traffic")
print("4. Display bar graph based on protocol")
sub2_option2=int(input("Choose an option:"))
if(sub2_option2==1):
#the map of the network with its start points and end points are first gathered before mapping.
network = nx.from_pandas_edgelist(data_file, source="Source", target="Destination", edge_attr=True) #file and other attributes mentioned and stored in network variable.
print(network.nodes()) # Prints the nodal view of the network map
os.system('pause')
os.system('cls')
graph_data(data_file)
elif(sub2_option2==2):
network = nx.from_pandas_edgelist(data_file, source="Source", target="Destination", edge_attr=True)
print(network.edges()) #Prints the edge view of the network map with its source and destination
os.system('pause')
os.system('cls')
graph_data(data_file)
elif(sub2_option2==3):
network = nx.from_pandas_edgelist(data_file, source="Source", target="Destination", edge_attr=True)
graph_option=int(input("\n1. Show dynamic graph in HTML View.\n2. Show image graph (.png): "))
if(graph_option==1):
net=Network(notebook=False, height='1000px',width='1500px')
net.from_nx(network)
# To print out html, we need to extract the directory path as Python can't read relative paths directive
dirname = os.path.dirname(__file__) #Stores directory path in 'dirname'
filename = os.path.join(dirname, 'networkgraph.html') #file is given appended to directory
net.show(filename) #full path is called through appending in the 'dirname' and 'filename'
#print(html_plotter)
os.system('pause')
os.system('cls')
graph_data(data_file)
elif(graph_option==2):
network = nx.from_pandas_edgelist(data_file, source="Source", target="Destination", edge_attr=True)
nx.draw_circular(network, with_labels=True) #network map is drawn with the connections made from the network variable.
#Network map is plotted (on a new window if running from terminal)
plt.show()
os.system('pause')
os.system('cls')
graph_data(data_file)
else:
os.system('cls')
print("Invalid input given. Please try again.")
os.system('pause')
os.system('cls')
graph_data(data_file)
elif (sub2_option2==4):
protocol=data_file.groupby("Protocol").Protocol.count()
x = list(protocol.index) #x is the list of protocols
y = list(protocol.values) #y is the list of counts of the protocols
plt.bar(x, y, width=0.5, color='red')
plt.plot(x, y,marker='o', color='black')
plt.xlabel('Protocol')
plt.ylabel('Communications')
plt.title('No. of Communications per Protocol')
plt.show() #plots the bar graph
os.system('pause')
os.system('cls')
graph_data(data_file)
elif(sub2_option2==0):
os.system('cls')
menu2(data_file)
else:
os.system('cls')
print("Invalid input given. Please try again.")
os.system('pause')
os.system('cls')
graph_data(data_file)
# If any suspected address found in network, suspect() marks it separately and displays information about its connections.
def suspect(data_file):
banner()
#taking input of suspect
suspect_ad=input("Enter suspected address: ")
print("Suspect loaded\n")
#loading network map data (as started in L116)
network = nx.from_pandas_edgelist(data_file, source="Source", target="Destination", edge_attr=True)
#Suspect source and destination connection information is grapped and stored in two different variable and printed.
suspect_source_info=data_file.loc[data_file["Source"]==suspect_ad] #takes the data from the captured file and cross-checks the suspect's connections as source
suspect_dest_info=data_file.loc[data_file["Destination"]==suspect_ad] #takes the data from the captured file and cross-checks the suspect's connections as destination
print("Captured source network information of suspect: \n",suspect_source_info)
print("\n\nCaptured destination network information of suspect: \n",suspect_dest_info)
#prompt to show graphical network map with suspected isolated with its connections.
suspect_graph_option=input("\nPress Y to show suspect network graph (any other key to go back): ")
if(suspect_graph_option=='y' or suspect_graph_option=='Y'):
try:
pos=nx.spring_layout(network) #the spring_layour positions nodes using Fruchterman-Reingold force-directed algorithm
#Safe networks marked isolated and with green colour and other parameters
nx.draw(network, pos, node_color="green", node_size=300, with_labels=True)
#Suspect marked in red by program and larger size to show prominence
options = {"node_size":1000, "node_color":"r"}
nx.draw_networkx_nodes(network, pos, nodelist=[suspect_ad],**options)
plt.show() #Network map is plotted (on a new window if running from terminal)
os.system('\npause')
os.system('cls')
menu2(data_file)
except nx.exception.NetworkXError:
os.system('cls')
banner()
print("Suspect not in network. Please try again.")
os.system('pause')
os.system('cls')
menu2(data_file)
else:
os.system('cls')
menu2(data_file)
#GeoLoc() uses geoip2 and geolite2 tools to locate and return the origin country of a public IP Address
def GeoLoc(data_file):
banner()
print("\n GEOLOCATION TOOL: \nFinds country location of provided public address using GeoIP2 module.")
print("NOTE: Requires GeoLite2-Country.mmdb file installed in path. Only works on PUBLIC IP addreses.\n")
geo_option=input("Print 1 to continue, 0 to go back: ")
if(geo_option=='1'):
#loads the GeoLite2-Country.mmdb file into the geoip2 database
reader = geoip2.database.Reader("GeoLite2-Country.mmdb")
geoloc_input=input("Enter Public IP Address to locate: ")
try:
#Checks in the database for the input public IP address
response = reader.country(geoloc_input)
print(response.country.name)
os.system('pause')
os.system('cls')
menu2(data_file)
#Private IP Address/Reserved IP Address are not located by geoip2 tool and error thrown
except geoip2.errors.AddressNotFoundError:
os.system('cls')
banner()
print("Address not in database")
os.system('pause')
os.system('cls')
GeoLoc(data_file)
except ValueError:
os.system('cls')
banner()
print("Invalid Input")
os.system('pause')
os.system('cls')
GeoLoc(data_file)
except OSError:
os.system('cls')
banner()
print("Invalid Input")
os.system('pause')
os.system('cls')
GeoLoc(data_file)
except TypeError:
os.system('cls')
banner()
print("Invalid Input. Please provide an input")
os.system('pause')
os.system('cls')
GeoLoc(data_file)
elif(geo_option=='0'):
os.system('cls')
menu2(data_file)
else:
os.system('cls')
banner()
print("Invalid input given, please try again.")
os.system('pause')
os.system('cls')
GeoLoc(data_file)
#About page of this application
def about():
os.system('cls')
banner()
print("**Network Analyzer Program**")
print("Network Analyzer analyzes the network information available in CSV format, captured using Wireshark or any other network/packet sniffing/capturing tool.")
print("This is developed solely for use in Windows OS and developmen is underway for Linux distros as well.\n")
print("Created By: AADITH SUKUMAR")
print("GitHub: https://github.com/aadi1011")
print("LinkedIn: https://www.linkedin.com/in/aadith-sukumar/")
os.system('pause')
os.system('cls')
start_screen()
#Prompted exit menu of application
def exit():
os.system('cls')
banner()
exit_option=input("Are You Sure?(Y/N): ")
if(exit_option=='Y' or exit_option=='y'):
os.system('cls')
print("Thank you for using Network Analyzer!")
elif(exit_option=='N' or exit_option=='n'):
os.system('cls')
start_screen()
else:
exit()
#APPLICATION STARTS WITH THIS FUNCTION
# Start screen with the basic menu options and that leads to other functions of the application.
def start_screen():
os.system('cls')
banner()
print("\n\t\t***** MENU *****\n")
print("1. Start")
print("2. About")
print("3. Exit")
try:
menu_input=int(input("Enter your choice (1/2/3): "))
if(menu_input==1):
os.system('cls')
print("Let's start with Network Analysis:\n\n")
try:
# Complete file path of CSV file required. Error(L268) if missing file path provided.
file_path = input("Enter complete csv file path with readings: ")
# CSV file read and imported as pandas dataframes and stored in data_file variable.
data_file = pd.read_csv(file_path)
os.system('cls')
print("Data loaded successfully!\n\n")
menu2(data_file)
except FileNotFoundError:
os.system('cls')
banner()
print("\n\nERROR: FILE NOT FOUND. Enter valid file path.")
os.system('pause')
os.system('cls')
start_screen()
except PermissionError:
os.system('cls')
banner()
print("\n\nPermission Error: Admin privileges required to run this command. Please try again.")
os.system('pause')
os.system('cls')
start_screen()
except OSError:
os.system('cls')
banner()
print("\n\nERROR: Invalid argument. Please enter without quote marks.")
os.system('pause')
os.system('cls')
start_screen()
elif(menu_input==2):
about()
elif(menu_input==3):
exit()
else:
os.system('cls')
banner()
print("\n\nInvalid Input.")
os.system('pause')
os.system('cls')
start_screen()
# ValueError exception prevents program from crashing when no required inputs are passed
except ValueError:
os.system('cls')
banner()
print("\n\nPlease enter an input.")
os.system('pause')
os.system('cls')
start_screen()
#Calls the start_screen() and effectively the application
start_screen()