-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMisc.py
220 lines (181 loc) · 6.16 KB
/
Misc.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
216
217
218
219
220
""" Misc Module
Contains Various function that does not really fit elsewhere, or that are used in different places.
"""
import json
import csv
import os
import time
from pyroute2 import IPRoute, IW
from wifi import Cell, Scheme
import ipaddress
def is_wireless(interface:str):
'''
Takes a name and returns True if it is a valid wireless interface name.
'''
ipr = IPRoute()
iw = IW()
try:
# Get index interface
idx = ipr.link_lookup(ifname=interface)[0]
# get wireless info
iw_info = iw.get_interface_by_ifindex(idx)
if iw_info:
return True
except IndexError:
return False
return False
def init_MAC_DB():
'''
Imports the MAC address file and return a dictionnary, to search for vendors from MAC address later.
'''
res = {}
with open("fMAC_DB.json") as f :
res = json.load(f)
return res
def MAC_to_vendor(MAC_prefix:str , MAC_DB:dict) :
'''
Takes a MAC address and a MAC/vendor dict.
MAC addresses/prefixes must be keys and vendors, values.
'''
if len(MAC_prefix) < 8 :
return "Unknown"
elif MAC_prefix in MAC_DB :
return MAC_DB[MAC_prefix]["vendorName"]
else :
if MAC_prefix[-2] == ':' :
return MAC_to_vendor(MAC_prefix[:-2] , MAC_DB)
else :
return MAC_to_vendor(MAC_prefix[:-1] , MAC_DB)
def get_netaddr(target:str , subnet:str):
'''
Takes an host ip address and a subnet mask number (CIDR notation), and returns the network address
'''
cidr_notation = target+'/'+subnet
network = ipaddress.ip_network(cidr_notation, strict=False)
return str(network.network_address)
def open_menu(options:list , title="Menu" ):
'''
Takes a list of options and a title, display a menu with given choices.
Returns the choosen option's index.
'''
print(title)
i = 0
while i<=len(options)-1:
print(i , " " , options[i])
i+=1
choice = -1
while choice not in range(0,len(options)):
u_input = input("Select option (0-"+str(len(options) -1)+") : \n")
if u_input.isdigit() :
choice = int(u_input)
if choice not in range(0,len(options)):
print("\nIncorrect option given. Try again.\n")
return choice
def list_available_ap(MACDB:dict , interface:str):
'''
Takes the MAC/vendor dict and an interface, gather the access points
detected by the interface, displays and returns them.
/!\\ if your interface is already connected to an access point, you will only see this ap.
'''
print("\n")
networks = [
{
"ssid": n.ssid,
"address": n.address,
"signal": n.signal,
"quality": n.quality,
"frequency": n.frequency,
"encryption": n.encryption_type if n.encrypted else "-- NONE --" ,
"channel": n.channel,
"vendor": MAC_to_vendor(n.address.upper() , MACDB )
} for n in list(Cell.all(interface))
]
display_aps(networks)
return networks
def gather_scans():
'''
Gather all scans in ./logs/scans and returns a list of tuple ( ap_dict , scan_name )
'''
log_data = []
log_name_list = os.listdir("./logs/scans/")
for log in log_name_list :
print("Gathering "+log+" ...")
with open("./logs/scans/"+log , 'r') as f :
reader = csv.DictReader(f , delimiter = ',')
for row in reader :
log_data.append((row , log))
return(log_data)
def log_this_csv(collection:list, subdir="", name=''):
'''
Takes a list of access points and stores it in a csv file.
'''
if name == '' :
name = input("\n Saisissez un nom/lieu :\n")
if not os.path.isdir("./logs"):
os.mkdir("./logs")
if not os.path.isdir("./logs/" + subdir):
os.mkdir("./logs/" + subdir)
filepath = "./logs/" + subdir + name + "-" + time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) + ".csv"
with open(filepath, "w+") as f:
writer = csv.DictWriter(f, fieldnames=collection[0].keys())
writer.writeheader()
for item in collection:
writer.writerow(item)
f.flush() # Vider le buffer Python
os.fsync(f.fileno()) # Vider le buffer du système d'exploitation
print("Log saved at location :\n" + filepath + "\n")
def process_scans(name:str):
'''
Gets all the scan logs in /logs/scans and saves them in a json file which
allows fast searches.
'''
result = {}
if not os.path.isdir("./logs"):
os.mkdir("./logs")
if not os.path.isdir("./logs/maps"):
os.mkdir("./logs/maps")
scans = gather_scans()
for scan in scans :
if scan[1] not in result :
result[scan[1]] = {}
result[scan[1]][scan[0]['address']] = scan[0]
filepath = "./logs/maps/" + name + "-" + time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) + ".json"
with open(filepath , '+w') as f :
json.dump(result , f)
print("Scan Map saved at location : " + filepath)
def display_aps(ap_list:list) :
'''
Takes a list of access point dicts, and displays them nicely with
SSID,MAC address,signal,quality,frequency,encryption,channel and vendor.
'''
print("Access points :\n")
print(" {:16} {:16} {:10} {:10} {:16} {:10} {:10} {:10}"
.format(
"SSID",
"MAC",
"signal",
"quality",
"frequency",
"encryption",
"channel",
"vendor"
)
)
i = 1
for n in ap_list :
if n["ssid"] != None :
print("{} {:16} {:16} {:10} {:10} {:16} {:10} {:10} {:10}"
.format(
i,
str(n["ssid"]),
str(n["address"]),
str(n["signal"]),
str(n["quality"]),
str(n["frequency"]),
str(n["encryption"]),
str(n["channel"]),
str(n["vendor"])
)
)
i+=1
print("\n")