@@ -54,9 +54,17 @@ def log_changes(changes):
54
54
with open (log_file_path , "a" ) as log_file :
55
55
log_file .write (f"\n Changes made: { changes } " )
56
56
57
+ def banner_UFW ():
58
+ messagebox .showerror ("ALERT" ,"CIS recommends you to install ufw, you can proceed with the installation in the configure section" )
59
+ return
57
60
58
61
def is_ufw_installed ():
59
- return bool (os .system ("command -v ufw >/dev/null 2>&1" ) == 0 )
62
+ try :
63
+ return bool (os .system ("command -v ufw >/dev/null 2>&1" ) == 0 )
64
+ except FileNotFoundError :
65
+ # Handle the FileNotFoundError
66
+ banner_UFW ()
67
+
60
68
61
69
def ensure_ufw_installed ():
62
70
root = tk .Tk ()
@@ -143,7 +151,7 @@ def is_ufw_enabled():
143
151
return 'Status: active' in result .stdout
144
152
except FileNotFoundError :
145
153
# Handle the FileNotFoundError
146
- messagebox . showerror ( "Error" , "'ufw' executable not found. Please ensure that UFW is installed." )
154
+ banner_UFW ( )
147
155
return False
148
156
except subprocess .CalledProcessError as e :
149
157
# If an error occurs while running the command
@@ -248,7 +256,9 @@ def is_loopback_interface_configured():
248
256
for unconfigured_rule in unconfigured_rules :
249
257
print ("\033 [33m" , unconfigured_rule , "\033 [0m" )
250
258
return False
251
-
259
+ except FileNotFoundError :
260
+ # Handle the FileNotFoundError
261
+ banner_UFW ()
252
262
except ValueError as ve :
253
263
print ("Error:" , ve )
254
264
except TypeError as ve :
@@ -257,42 +267,53 @@ def is_loopback_interface_configured():
257
267
print ("Error:" , ve )
258
268
259
269
def ensure_loopback_configured ():
260
- print ("""
261
- \033 [91m================ Configuring the Loopback Interface =================\033 [0m
262
-
263
- Loopback traffic is generated between processes on the machine and is typically critical to
264
- the operation of the system. The loopback interface is the only place that loopback network
265
- (127.0.0.0/8 for IPv4 and ::1/128 for IPv6) traffic should be seen. All other interfaces
266
- should ignore traffic on this network as an anti-spoofing measure.
267
- """ )
268
- if not is_loopback_interface_configured ():
269
- print ("\n All loopback interfaces are not configured, do you want to configure them, " )
270
- var = y_n_choice ()
271
- var .lower ()
272
- if var == 'y' or var == 'yes' or var == '' :
273
- line = """
274
- User enabled configuring lo interfaces,
275
- Commands executed when configuring loopback interfaces:
276
- ufw allow in on lo
277
- ufw allow out on lo
278
- ufw deny in from 127.0.0.0/8
279
- ufw deny in from ::1
280
-
281
- """
282
- log_changes (line )
283
- print ("\n Enabling configurations on lo interfaces..." )
284
- os .system ("ufw allow in on lo" )
285
- os .system ("ufw allow out on lo" )
286
- os .system ("ufw deny in from 127.0.0.0/8" )
287
- os .system ("ufw deny in from ::1" )
288
- elif var == 'n' or var == 'no' :
289
- line = "Loopback interface not configured"
270
+ try :
271
+ print ("""
272
+ \033 [91m================ Configuring the Loopback Interface =================\033 [0m
273
+
274
+ Loopback traffic is generated between processes on the machine and is typically critical to
275
+ the operation of the system. The loopback interface is the only place that loopback network
276
+ (127.0.0.0/8 for IPv4 and ::1/128 for IPv6) traffic should be seen. All other interfaces
277
+ should ignore traffic on this network as an anti-spoofing measure.
278
+ """ )
279
+ if not is_loopback_interface_configured ():
280
+ print ("\n All loopback interfaces are not configured, do you want to configure them, " )
281
+ var = y_n_choice ()
282
+ var .lower ()
283
+ if var == 'y' or var == 'yes' or var == '' :
284
+ line = """
285
+ User enabled configuring lo interfaces,
286
+ Commands executed when configuring loopback interfaces:
287
+ ufw allow in on lo
288
+ ufw allow out on lo
289
+ ufw deny in from 127.0.0.0/8
290
+ ufw deny in from ::1
291
+
292
+ """
293
+ log_changes (line )
294
+ print ("\n Enabling configurations on lo interfaces..." )
295
+ os .system ("ufw allow in on lo" )
296
+ os .system ("ufw allow out on lo" )
297
+ os .system ("ufw deny in from 127.0.0.0/8" )
298
+ os .system ("ufw deny in from ::1" )
299
+ elif var == 'n' or var == 'no' :
300
+ line = "Loopback interface not configured"
301
+ log_changes (line )
302
+ print ("\n " ,line )
303
+ else :
304
+ line = "Loopback interface already configured"
290
305
log_changes (line )
291
306
print ("\n " ,line )
292
- else :
293
- line = "Loopback interface already configured"
294
- log_changes (line )
295
- print ("\n " ,line )
307
+ except ValueError as ve :
308
+ print ("Error:" , ve )
309
+ except TypeError as ve :
310
+ print ("Error:" , ve )
311
+ except AttributeError as ve :
312
+ print ("Error:" , ve )
313
+ except FileNotFoundError :
314
+ # Handle the FileNotFoundError
315
+ banner_UFW ()
316
+
296
317
#check if ufw outbound connections are already configured
297
318
def is_ufw_outbound_connections_configured ():
298
319
try :
@@ -304,7 +325,9 @@ def is_ufw_outbound_connections_configured():
304
325
else :
305
326
print ("\033 [91mThe following outbound rule is not configured: ufw allow out on all" )
306
327
return False
307
-
328
+ except FileNotFoundError :
329
+ # Handle the FileNotFoundError
330
+ banner_UFW ()
308
331
except subprocess .CalledProcessError as e :
309
332
print ("Error:" , e )
310
333
return False
@@ -352,7 +375,7 @@ def get_allow_deny():
352
375
root .withdraw ()
353
376
while True :
354
377
try :
355
- allw_dny = simpledialog .askstring ("Enter rule (allow or deny): " , prompt = " " ).lower ()
378
+ allw_dny = simpledialog .askstring ("Outbound Configurations" , " Enter rule (allow or deny): " ).lower ()
356
379
if allw_dny not in ['allow' , 'deny' ]:
357
380
raise ValueError ("Invalid rule. Please enter either 'allow' or 'deny'." )
358
381
elif allw_dny is None :
@@ -379,7 +402,7 @@ def get_network_address():
379
402
root .withdraw ()
380
403
while True :
381
404
try :
382
- netadd = simpledialog .askstring ("Enter network address (in the format xxx.xxx.xxx.xxx): " , prompt = " " )
405
+ netadd = simpledialog .askstring ("Outbound Configurations" , " Enter network address (in the format xxx.xxx.xxx.xxx): " )
383
406
address_parts = netadd .split ('.' )
384
407
# Use a regular expression to check if the input matches the expected format
385
408
if not re .match (r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$' , netadd ) or not is_valid_network_address (address_parts ):
@@ -400,7 +423,7 @@ def get_proto():
400
423
root .withdraw ()
401
424
while True :
402
425
try :
403
- proto = simpledialog .askstring ("Enter protocol (tcp or udp): " , prompt = " " ).lower ()
426
+ proto = simpledialog .askstring ("Outbound Configurations" , " Enter protocol (tcp or udp): " ).lower ()
404
427
if proto not in ['tcp' , 'udp' ]:
405
428
raise ValueError ("Invalid protocol. Please enter either 'tcp' or 'udp'." )
406
429
elif proto is None :
@@ -420,7 +443,7 @@ def get_mask():
420
443
root .withdraw ()
421
444
while True :
422
445
try :
423
- mask = int (simpledialog .askstring ("Enter the whole number value of the subnet mask (16-32): " , prompt = " " ).lower ())
446
+ mask = int (simpledialog .askstring ("Outbound Configurations" , " Enter the whole number value of the subnet mask (16-32): " ).lower ())
424
447
if 16 <= mask <= 32 :
425
448
return str (mask )
426
449
elif mask is None :
@@ -455,7 +478,7 @@ def get_port_number(script_path):
455
478
while True :
456
479
try :
457
480
ports_list = get_ports_as_a_list (script_path )
458
- p_no = simpledialog .askinteger ("Enter the index number of the port to be configured:" , prompt = " " )
481
+ p_no = simpledialog .askinteger ("Outbound Configurations" , " Enter the index number of the port to be configured:" )
459
482
460
483
# Check if the user pressed Cancel
461
484
@@ -608,7 +631,9 @@ def scan_system_configuration():
608
631
print ("Default deny policy is configured." )
609
632
is_ufw_outbound_connections_configured ()
610
633
611
-
634
+ except FileNotFoundError :
635
+ # Handle the FileNotFoundError
636
+ banner_UFW ()
612
637
except ValueError as ve :
613
638
print ("Error:" ,ve )
614
639
except TypeError as ve :
0 commit comments