mirror of
https://github.com/fabriziosalmi/patterns.git
synced 2025-12-17 17:55:48 +00:00
Update owasp2traefik.py
Should fix resulting exported rules to match Traefik standards.
This commit is contained in:
parent
93271ca213
commit
c7a580c983
@ -4,20 +4,50 @@ import json
|
|||||||
OUTPUT_DIR = "waf_patterns/traefik/"
|
OUTPUT_DIR = "waf_patterns/traefik/"
|
||||||
|
|
||||||
def load_owasp_rules(file_path):
|
def load_owasp_rules(file_path):
|
||||||
|
try:
|
||||||
with open(file_path, "r") as f:
|
with open(file_path, "r") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"[-] Error: File '{file_path}' not found.")
|
||||||
|
exit(1)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print(f"[-] Error: Invalid JSON in '{file_path}'.")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
def generate_traefik_conf(rules):
|
def generate_traefik_conf(rules):
|
||||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||||
config_file = os.path.join(OUTPUT_DIR, "middleware.toml")
|
config_file = os.path.join(OUTPUT_DIR, "middleware.toml")
|
||||||
|
|
||||||
|
try:
|
||||||
with open(config_file, "w") as f:
|
with open(config_file, "w") as f:
|
||||||
f.write("[http.middlewares]\n")
|
f.write("[http.middlewares]\n\n")
|
||||||
|
rule_counter = 1 # Unique identifier for each middleware
|
||||||
|
|
||||||
|
# Group rules by category
|
||||||
|
grouped_rules = {}
|
||||||
for rule in rules:
|
for rule in rules:
|
||||||
f.write(f"[http.middlewares.bad_bot_block_{rule['category']}]\n")
|
category = rule.get("category", "default")
|
||||||
f.write(f" [http.middlewares.bad_bot_block_{rule['category']}.plugin.badbot]\n")
|
if category not in grouped_rules:
|
||||||
f.write(f" userAgent = [\"{rule['pattern']}\"]\n")
|
grouped_rules[category] = []
|
||||||
|
grouped_rules[category].append(rule)
|
||||||
|
|
||||||
|
# Write grouped rules to the TOML file
|
||||||
|
for category, rules_in_category in grouped_rules.items():
|
||||||
|
f.write(f"[http.middlewares.bad_bot_block_{category}]\n")
|
||||||
|
f.write(f" [http.middlewares.bad_bot_block_{category}.plugin.badbot]\n")
|
||||||
|
f.write(" userAgent = [\n")
|
||||||
|
unique_rules = set() # Use a set to deduplicate rules
|
||||||
|
for rule in rules_in_category:
|
||||||
|
# Escape special characters in the pattern
|
||||||
|
pattern = rule['pattern'].replace('"', '\\"').replace("\\", "\\\\")
|
||||||
|
unique_rules.add(f' "{pattern}"')
|
||||||
|
f.write(",\n".join(unique_rules) + "\n")
|
||||||
|
f.write(" ]\n\n")
|
||||||
|
|
||||||
print(f"[+] Traefik WAF rules generated at {config_file}")
|
print(f"[+] Traefik WAF rules generated at {config_file}")
|
||||||
|
except IOError as e:
|
||||||
|
print(f"[-] Error writing to file: {e}")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
owasp_rules = load_owasp_rules("owasp_rules.json")
|
owasp_rules = load_owasp_rules("owasp_rules.json")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user