mirror of
https://github.com/fabriziosalmi/patterns.git
synced 2025-12-17 09:45:34 +00:00
Create import_apache_waf.py
This commit is contained in:
parent
caa5d3a192
commit
1974e98a13
39
import_apache_waf.py
Normal file
39
import_apache_waf.py
Normal file
@ -0,0 +1,39 @@
|
||||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||
|
||||
WAF_DIR = "waf_patterns/apache"
|
||||
APACHE_WAF_DIR = "/etc/modsecurity.d/"
|
||||
APACHE_CONF = "/etc/apache2/apache2.conf"
|
||||
INCLUDE_STATEMENT = "IncludeOptional /etc/modsecurity.d/*.conf"
|
||||
|
||||
def copy_waf_files():
|
||||
logging.info("Copying Apache WAF patterns...")
|
||||
os.makedirs(APACHE_WAF_DIR, exist_ok=True)
|
||||
subprocess.run(["cp", "-R", f"{WAF_DIR}/*", APACHE_WAF_DIR], check=True)
|
||||
|
||||
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__":
|
||||
copy_waf_files()
|
||||
update_apache_conf()
|
||||
reload_apache()
|
||||
logging.info("[✔] Apache configured with latest WAF rules.")
|
||||
Loading…
x
Reference in New Issue
Block a user