diff --git a/config/shared_config.json b/config/shared_config.json index 9278659..4803fc3 100644 --- a/config/shared_config.json +++ b/config/shared_config.json @@ -2,6 +2,9 @@ "__title_Bjorn__": "Settings", "manual_mode": false, "websrv": true, + "web_auth_enabled": false, + "web_auth_user": "bjorn", + "web_auth_pass": "bjorn", "web_increment ": false, "debug_mode": true, "scan_vuln_running": false, @@ -74,7 +77,8 @@ ], "mac_scan_blacklist": [ "00:11:32:c4:71:9b", - "00:11:32:c4:71:9a" + "00:11:32:c4:71:9a", + "2c:cf:67:a7:0a:81" ], "ip_scan_blacklist": [ "192.168.1.1", @@ -103,5 +107,8 @@ "timewait_telnet": 0, "timewait_ftp": 0, "timewait_sql": 0, - "timewait_rdp": 0 -} + "timewait_rdp": 0, + "web_auth": true, + "web_auth_username": "bjorn", + "web_auth_password": "test2" +} \ No newline at end of file diff --git a/shared.py b/shared.py index 3ebe912..92ca54b 100644 --- a/shared.py +++ b/shared.py @@ -115,6 +115,9 @@ def get_default_config(self): "__title_Bjorn__": "Settings", "manual_mode": False, "websrv": True, + "web_auth_enabled": False, + "web_auth_user": "bjorn", + "web_auth_pass": "bjorn", "web_increment ": False, "debug_mode": True, "scan_vuln_running": False, diff --git a/webapp.py b/webapp.py index f447b99..59a81f1 100644 --- a/webapp.py +++ b/webapp.py @@ -9,6 +9,7 @@ import os import gzip import io +import base64 from logger import Logger from init_shared import shared_data from utils import WebUtils @@ -19,6 +20,12 @@ # Set the path to the favicon favicon_path = os.path.join(shared_data.webdir, '/images/favicon.ico') +# Set Basic Auth variables +web_auth = (shared_data.config["web_auth_enabled"] and shared_data.config["web_auth_enabled"] == True) +web_auth_user = shared_data.config["web_auth_user"] +web_auth_pass = shared_data.config["web_auth_pass"] + + class CustomHandler(http.server.SimpleHTTPRequestHandler): def __init__(self, *args, **kwargs): self.shared_data = shared_data @@ -56,9 +63,24 @@ def serve_file_gzipped(self, file_path, content_type): content = file.read() self.send_gzipped_response(content, content_type) + def basic_auth_check(self): + return self.headers.get('Authorization') == None or self.headers.get('Authorization') != 'Basic ' + base64.b64encode(f"{web_auth_user}:{web_auth_pass}".encode()).decode() + + def do_AUTHHEAD(self): + logger.info("Sending Auth Header") + self.send_response(401) + self.send_header('WWW-Authenticate','Basic realm="BJORN"') + self.send_header('Content-Type','text/html') + self.end_headers() + def do_GET(self): + # Check Web Auth + if web_auth and self.basic_auth_check(): + self.do_AUTHHEAD() + return # Handle GET requests. Serve the HTML interface and the EPD image. if self.path == '/index.html' or self.path == '/': + #logger.info("Serving Index Page") self.serve_file_gzipped(os.path.join(self.shared_data.webdir, 'index.html'), 'text/html') elif self.path == '/config.html': self.serve_file_gzipped(os.path.join(self.shared_data.webdir, 'config.html'), 'text/html') @@ -116,6 +138,10 @@ def do_GET(self): super().do_GET() def do_POST(self): + # Check Web Auth + if web_auth and self.basic_auth_check(): + self.do_AUTHHEAD() + return # Handle POST requests for saving configuration, connecting to Wi-Fi, clearing files, rebooting, and shutting down. if self.path == '/save_config': self.web_utils.save_configuration(self)