2024-12-21 09:05:54 +01:00
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
|
|
|
|
|
|
|
WAF_DIR = "waf_patterns/apache"
|
2024-12-21 22:51:02 +08:00
|
|
|
APACHE_WAF_DIR = "/etc/modsecurity.d/"
|
2024-12-21 22:43:34 +08:00
|
|
|
|
2024-12-21 09:05:54 +01:00
|
|
|
APACHE_CONF = "/etc/apache2/apache2.conf"
|
|
|
|
|
INCLUDE_STATEMENT = "IncludeOptional /etc/modsecurity.d/*.conf"
|
|
|
|
|
|
2024-12-21 22:43:34 +08:00
|
|
|
|
|
|
|
|
|
2024-12-21 09:05:54 +01:00
|
|
|
def copy_waf_files():
|
|
|
|
|
logging.info("Copying Apache WAF patterns...")
|
|
|
|
|
os.makedirs(APACHE_WAF_DIR, exist_ok=True)
|
2024-12-21 22:43:34 +08:00
|
|
|
list_of_files = os.listdir(WAF_DIR)
|
|
|
|
|
workaround = "{"
|
|
|
|
|
for conf_file in list_of_files:
|
|
|
|
|
# print(conf_file)
|
|
|
|
|
if conf_file.endswith('.conf'):
|
|
|
|
|
subprocess.run(["cp", f"{WAF_DIR}/{conf_file}", APACHE_WAF_DIR], check=True)
|
|
|
|
|
# print("Match")
|
|
|
|
|
workaround = workaround[:-1] # removes the last comma
|
|
|
|
|
workaround += "}"
|
|
|
|
|
print(workaround)
|
|
|
|
|
|
|
|
|
|
|
2024-12-21 09:05:54 +01:00
|
|
|
|
|
|
|
|
def update_apache_conf():
|
|
|
|
|
logging.info("Ensuring WAF patterns are included in apache2.conf...")
|
|
|
|
|
|
|
|
|
|
with open(APACHE_CONF, "r") as f:
|
|
|
|
|
config = f.read()
|
|
|
|
|
|
|
|
|
|
if INCLUDE_STATEMENT not in config:
|
|
|
|
|
logging.info("Adding WAF include to apache2.conf...")
|
|
|
|
|
with open(APACHE_CONF, "a") as f:
|
|
|
|
|
f.write(f"\n{INCLUDE_STATEMENT}\n")
|
|
|
|
|
else:
|
|
|
|
|
logging.info("WAF patterns already included in apache2.conf.")
|
|
|
|
|
|
|
|
|
|
def reload_apache():
|
|
|
|
|
logging.info("Reloading Apache to apply new WAF rules...")
|
|
|
|
|
subprocess.run(["apachectl", "configtest"], check=True)
|
|
|
|
|
subprocess.run(["systemctl", "reload", "apache2"], check=True)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-12-21 22:43:34 +08:00
|
|
|
|
2024-12-21 09:05:54 +01:00
|
|
|
copy_waf_files()
|
|
|
|
|
update_apache_conf()
|
|
|
|
|
reload_apache()
|
|
|
|
|
logging.info("[✔] Apache configured with latest WAF rules.")
|