-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkrouter.py
529 lines (462 loc) · 19.3 KB
/
krouter.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/usr/bin/env python3
from configparser import ConfigParser, ExtendedInterpolation
from simple_term_menu import TerminalMenu
from datetime import datetime
from utils import arguments
from utils import mkdir
from utils.packagemanager import PackageManager
from utils import richard as r
import logging
import os
import psutil
import re
import shutil
import socket
import subprocess
import sys
import time
# App - directories and filepaths.
app_filepath = __file__
app_dir = os.path.dirname(__file__)
bckup_dir = os.path.join(app_dir, "backups")
hostapd_dir = "/etc/hostapd"
# App - relative filepaths.
config_ini_fp = "configs/config.ini"
# Create required dirs.
directories = [bckup_dir, hostapd_dir]
dirs = [mkdir.mkdir(directory) for directory in directories]
[print(f"Created directory: {d}") for d in dirs if d is not None]
[r.logging.warning(f"Missing directory was created: {d}") for d in dirs if d is not None]
# Argparse - init and parse.
args = arguments.parser.parse_args()
def read_ini(ini_fp, converters="", delimiters='^'):
''' Read 'ini' config file and return ConfigParser Object '''
converters = converters
delimiters = delimiters
cp_obj = ConfigParser(
allow_no_value=True,
converters=converters,
delimiters=delimiters,
interpolation=ExtendedInterpolation()
)
cp_obj.optionxform = str
if os.path.exists(ini_fp):
cp_obj.read(ini_fp)
else:
r.logging.warning(f"ConfigParser file failed to load: '{ini_fp}'")
return None
return cp_obj
def time_stamp():
'''Time Stamp used for start/stop and archive filename'''
current_time = datetime.now()
return current_time.strftime("%b-%d-%y-%H-%M-%S")
def copy_file(src, dst):
''' Copy file from src to dst via the shutil module.
Return filepath.
arg(s) src:str, dst:str'''
try:
filepath = shutil.copy(src, dst)
except IOError as e:
raise e
except Exception as e:
raise e
else:
return filepath
def backup_file(filepath, timestamp):
bckup_subdir = os.path.join(bckup_dir, timestamp)
bckup_ext = filepath + ".bak"
mkdir.mkdir(bckup_subdir)
bckup_fp = os.path.join(bckup_subdir, os.path.basename(bckup_ext))
if os.path.isfile(filepath):
fp = copy_file(filepath, bckup_fp)
return {os.path.relpath(fp)}
def manage_service(action, service_name):
try:
command = ['sudo', 'systemctl', action, service_name]
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
print(f"Success: '{' '.join(command)}'")
else:
print(f"Failed to {action} {service_name}. Error: {result.stderr.decode()}")
except Exception as e:
print(f"An error occurred: {e}")
def run_cmd(cmd):
try:
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
if result.returncode == 0:
return cmd
else:
r.logging.debug(f'Error: {result.stderr.decode()}')
return f'FAILED: {cmd}'
except Exception as e:
print(f'An error occurred: {e}')
class BaseMenu:
opt_packages_install = "[-] Packages - Install required packages"
opt_packages_view = "[-] Packages - Show/hide installed packages"
opt_services_start = "[-] Services - Enable & start services"
opt_services_restart = "[-] Services - Restart services"
opt_firewall_toggle = "[-] Firewall - Enable/disable firewall rules"
opt_firewall_mitmproxy_toggle = "[-] Firewall - Enable/disable mitmproxy rules"
opt_dhcp_leases_refresh = "[-] DHCP Leases - Refresh screen to view new leases"
opt_exit = "[-] Exit"
opt_back = "[-] Back"
OPTIONS = [
opt_packages_install,
opt_packages_view,
opt_services_start,
opt_services_restart,
opt_firewall_toggle,
opt_firewall_mitmproxy_toggle,
opt_dhcp_leases_refresh
]
def __init__(self, cp_obj):
self.cp_obj = cp_obj
self.menu_options = self.OPTIONS + [i for i in self.cp_obj.sections()] + [self.opt_exit]
self.menu = TerminalMenu(self.menu_options)
def present_menu(self):
index = self.menu.show()
selected_menu_item = self.menu_options[index]
return selected_menu_item
class MainMenu(BaseMenu):
def __init__(self, cp_obj):
super().__init__(cp_obj)
self.menu_options = self.OPTIONS + [self.opt_exit]
self.menu = TerminalMenu(self.menu_options)
class Session:
def __init__(self, cp_obj, services):
self.cp_obj = cp_obj
self.interfaces = {k:v for k,v in self.cp_obj["interface"].items()}
self.packages = [k for k in self.cp_obj["packages"]]
self.services = services
self.packages_toggle = False
self.firewall_toggle = True
self.mitmproxy_toggle = False
self.fw_lst = []
self.mitm_lst = []
self.loglevel_numeric_value = r.logger.getEffectiveLevel()
self.loglevel_name = r.logging.getLevelName(self.loglevel_numeric_value )
# Toggle clear screen function off when loglevel is DEBUG..
if not self.loglevel_name == 'DEBUG':
os.system("clear")
def check_interface(self, interface):
''' Sets icon based on inteface status, I.e. Up, down or not found '''
addrs = psutil.net_if_addrs()
stats = psutil.net_if_stats()
if interface in stats:
is_up = stats[interface].isup
if is_up:
return f"[green]up[/green] {interface}"
else:
return f"[orange3]down[/orange3] {interface}"
else:
return f"[red]not-found[/red] {interface}"
def run_firewall_router(self, rules):
''' Firewall Router rules - backup, flush and apply new rules for router to function '''
self.fw_lst = []
for rule in rules:
# Remove rule if toggle is false.
if not self.firewall_toggle:
rule = rule.replace('-A', '-D')
try:
result = subprocess.run(rule, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
if result.returncode == 0:
if not self.firewall_toggle:
self.fw_lst.append(f' {rule}')
else:
self.fw_lst.append(f' :fire:{rule}')
else:
r.logging.debug(f'Error: {result.stderr.decode()}')
self.fw_lst.append(f'[red]failed:[/red]{rule}')
except Exception as e:
print(f'An error occurred: {e}')
return self.fw_lst
def run_firewall_mitmproxy(self, rules):
''' Firewall Router rules - backup, flush and apply new rules for router to function '''
self.mitm_lst = []
for rule in rules:
# Remove rule if toggle is false.
if not self.mitmproxy_toggle:
rule = rule.replace('-A', '-D')
try:
result = subprocess.run(rule, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
if result.returncode == 0:
if not self.mitmproxy_toggle:
self.mitm_lst.append(f' {rule}')
else:
self.mitm_lst.append(f' :fire:{rule}')
else:
r.logging.debug(f'Error: {result.stderr.decode()}')
self.mitm_lst.append(f'[red]failed:[/red]{rule}')
except Exception as e:
print(f'An error occurred: {e}')
return self.mitm_lst
def parse_hostapd_config(self, file_path):
''' Parse hostapd config file '''
hostapd_lst = []
with open(file_path, 'r') as file:
for line in file:
if line.startswith('ssid='):
ssid = f"SSID: {line.split('=')[1].strip()}"
hostapd_lst.append(ssid)
elif line.startswith('wpa_passphrase='):
psk = f" PSK: {line.split('=')[1].strip()}"
hostapd_lst.append(psk)
elif line.startswith('ieee80211w='):
ieee80211w = f"{line.split('=')[1].strip()}"
if ieee80211w == '0':
ieee80211w = f" MFP: 0 (Disabled)"
elif ieee80211w == '1':
ieee80211w = f" MFP: 1 (Optional)"
elif ieee80211w == '2':
ieee80211w = f" MFP: 2 (Required)"
hostapd_lst.append(ieee80211w)
return hostapd_lst
def check_package_installed(self, package):
''' Sets icon based on package installation status. '''
try:
result = subprocess.run(['dpkg-query', '-W', '-f=${Status}', package],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if 'install ok installed' in result.stdout.decode():
return f"[green]installed[/green] {package}"
else:
return f"[red]not found[/red] {package}"
except Exception as e:
return f"[red]failed[/red] {package}"
def check_service_status(self, service):
''' Sets icon based on service status '''
try:
result = subprocess.run(['systemctl', 'is-active', f'{service}'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
status = result.stdout.strip()
if status == 'active':
return f"[green]active[/green] {service}"
elif status == 'inactive':
return f"[red]inactive[/red] {service}"
elif status == 'failed':
return f"[red]failed[/red] {service}"
except Exception as e:
print(f" An error occured: {e}")
def parse_dhcp_leases(self, file_path):
''' Parse DHCPv4 active leases '''
leases = {}
with open(file_path, 'r') as f1:
lease_data = f1.read()
lease_blocks = lease_data.split('lease ')
for block in lease_blocks[1:]:
ipv4_match = re.search(r'(\d+\.\d+.\d+.\d+)', block)
mac_match = re.search(r'hardware ethernet ([0-9a-fA-F:]+);', block)
if ipv4_match and mac_match:
ipv4_address = ipv4_match.group(1)
mac_address = mac_match.group(1)
leases[ipv4_address] = mac_address
return leases
def parse_dhcpv6_leases(self, file_path):
''' Parse DHCPv6 active leases '''
leases = {}
with open(file_path, 'r') as f1:
lease_data = f1.read()
lease_blocks = lease_data.split('iaaddr')
for block in lease_blocks[1:]:
ipv6_match = re.search(r'([0-9a-fA-F:]+)', block)
state_match = re.search(r'binding state active', block)
if ipv6_match and state_match:
ipv6_address = ipv6_match.group(1)
leases[ipv6_address] = "Active"
return leases
def print_header(self) -> None:
''' Print Session Banner '''
# Toggle clear screen function off when loglevel is DEBUG.
if not self.loglevel_name == 'DEBUG':
os.system("clear")
# Whitespaces
wp = 3*' '
# Network Interfaces:
int_lst = []
for interface in self.interfaces.values():
result = self.check_interface(interface)
int_lst.append(f'{wp}{result}')
list_text = r.Text("\n".join(int_lst))
service_panel = r.Panel(f'Interfaces:\n{list_text}')
r.console.print(service_panel)
# Assigned IP Addresses:
ip_lst = []
addresses = psutil.net_if_addrs()
for interface, addr_list in addresses.items():
if interface in self.interfaces.values():
for addr in addr_list:
if addr.family == socket.AF_INET:
ip_lst.append(f'{wp}{interface}: {addr.address}')
elif addr.family == socket.AF_INET6 and not addr.address.startswith('fe80::'):
ip_lst.append(f'{wp}{interface}: {addr.address}')
list_text = r.Text("\n".join(ip_lst))
service_panel = r.Panel(f'IP Addresses:\n{list_text}')
r.console.print(service_panel)
# Hostapd:
hostapd_lst = []
hostapd_fp = self.cp_obj['hostapd']['fp']
for item in self.parse_hostapd_config(hostapd_fp):
hostapd_lst.append(f"{wp}{item}")
list_text = r.Text("\n".join(hostapd_lst))
service_panel = r.Panel(f'Wireless:\n{list_text}')
r.console.print(service_panel)
# Firewall:
list_text = r.Text("\n".join(self.fw_lst))
service_panel = r.Panel(f'Firewall Rules:\n{list_text}')
r.console.print(service_panel)
# Mitmproxy:
list_text = r.Text("\n".join(self.mitm_lst))
service_panel = r.Panel(f'Mitmproxy Rules:\n{list_text}')
r.console.print(service_panel)
# Packages:
pkg_lst = []
for package in self.packages:
result = self.check_package_installed(package)
pkg_lst.append(f' {wp}{result}')
list_text = r.Text("\n".join(pkg_lst))
service_panel = r.Panel(f'Packages:\n{list_text}')
if self.packages_toggle:
r.console.print(service_panel)
# Services:
service_lst = []
for service in self.services:
result = self.check_service_status(service)
if result is not None:
service_lst.append(f'{wp}{result}')
list_text = r.Text("\n".join(service_lst))
service_panel = r.Panel(f'Services:\n{list_text}')
r.console.print(service_panel)
# DHCP Leases:
dhcpd_leases_fp = '/var/lib/dhcp/dhcpd.leases'
ipv4_leases = self.parse_dhcp_leases(dhcpd_leases_fp)
leases_list = []
for ipv4, mac in ipv4_leases.items():
ipv4_mac = f' {wp}{ipv4} / {mac}'
leases_list.append(ipv4_mac)
dhcpdv6_leases_fp = '/var/lib/dhcp/dhcpd6.leases'
ipv6_leases = self.parse_dhcpv6_leases(dhcpdv6_leases_fp)
for ipv6, status in ipv6_leases.items():
leases_list.append(f' {wp}{ipv6}')
list_text = r.Text("\n".join(leases_list))
service_panel = r.Panel(f'DHCP Leases:\n{list_text}')
r.console.print(service_panel)
def main():
# Config Parser Objects
config_obj = read_ini(config_ini_fp, delimiters='=')
# Services List
service_lst = ['networking.service', 'isc-dhcp-server', 'radvd', 'dnsmasq', 'hostapd']
# Session Manager
session = Session(config_obj, service_lst)
# Session - Firewall rules.
fw_router_rules = [v for v in config_obj['firewall-router'].values()]
session.run_firewall_router(fw_router_rules)
# ConfigParser - Packages List
packages = [k for k in config_obj["packages"]]
# Config Files - Time stamp for backups.
ts = time_stamp()
# Config Files - ignored sections
sections_ignored = ['interface', 'firewall-router', 'firewall-mitmproxy', 'packages']
# Config Files - Create Config Files list.
config_files = []
for section in config_obj.sections():
if section not in sections_ignored:
config_files.append(section)
# Config Files - Backup.
backups = False
try:
backups = []
if config_files != None:
for config_file in config_files:
fp = config_obj[config_file]["fp"]
results = backup_file(fp, ts)
backups.append(results)
except Exception as e:
raise e
else:
backups = True
# Write Config Files.
wrote_config_files = False
try:
results = []
for config_file in config_files:
fp = config_obj[config_file]["fp"]
option = config_obj[config_file]["option"]
with open(fp, "w") as f1:
f1.write(option)
results.append(fp)
except Exception as e:
raise e
else:
wrote_config_files = True
while True:
### Main-menu ###
session.print_header()
if backups:
r.console.print(f'Backups: {bckup_dir}')
main_menu = MainMenu(config_obj)
selected_main_menu_item = main_menu.present_menu()
r.logging.debug(f'Main Menu Option Selected: {selected_main_menu_item}')
# PackageManager - Install
if selected_main_menu_item == main_menu.opt_packages_install:
pm = PackageManager()
pm.update_cache()
# Install required packages.
try:
[pm.install_package(package) for package in packages]
except Exception as e:
print(f"{e}")
# Newly installed packages.
if pm.new_packages:
[print(f"Installed: {package}") for package in pm.new_packages]
input("Press Enter to continue")
# PackageManager - View
if selected_main_menu_item == main_menu.opt_packages_view:
session.packages_toggle = not session.packages_toggle
# Services - Start
elif selected_main_menu_item == main_menu.opt_services_start:
for service in service_lst:
if service == 'hostapd':
manage_service('unmask', service)
for service in service_lst:
manage_service('enable', service)
for service in service_lst:
manage_service('start', service)
# reload sysctl
command = 'sudo sysctl -p /etc/sysctl.conf'
run_cmd(command)
input("Press Enter to continue")
# Services - Restart
elif selected_main_menu_item == main_menu.opt_services_restart:
for service in service_lst:
manage_service('stop', service)
for service in service_lst:
manage_service('start', service)
# reload sysctl
command = 'sudo sysctl -p /etc/sysctl.conf'
run_cmd(command)
input("Press Enter to continue")
elif selected_main_menu_item == main_menu.opt_firewall_toggle:
session.firewall_toggle = not session.firewall_toggle
# Firewall rules.
fw_router_rules = [v for v in config_obj['firewall-router'].values()]
session.run_firewall_router(fw_router_rules)
elif selected_main_menu_item == main_menu.opt_firewall_mitmproxy_toggle:
session.mitmproxy_toggle = not session.mitmproxy_toggle
# Firewall Mitmproxy rules.
fw_mitmproxy_rules = [v for v in config_obj['firewall-mitmproxy'].values()]
session.run_firewall_mitmproxy(fw_mitmproxy_rules)
# Exit
elif selected_main_menu_item == main_menu.opt_exit:
session.firewall_toggle = False
session.mitmproxy_toggle = False
# Firewall rules - Revert before exting.
fw_router_rules = [v for v in config_obj['firewall-router'].values()]
session.run_firewall_router(fw_router_rules)
# Firewall Mitmproxy rules - Revert before exting.
fw_mitmproxy_rules = [v for v in config_obj['firewall-mitmproxy'].values()]
session.run_firewall_mitmproxy(fw_mitmproxy_rules)
sys.exit()
sys.exit()
if __name__ == '__main__':
main()