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/"
|
||||
|
||||
def load_owasp_rules(file_path):
|
||||
with open(file_path, "r") as f:
|
||||
return json.load(f)
|
||||
try:
|
||||
with open(file_path, "r") as 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):
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
config_file = os.path.join(OUTPUT_DIR, "middleware.toml")
|
||||
|
||||
with open(config_file, "w") as f:
|
||||
f.write("[http.middlewares]\n")
|
||||
for rule in rules:
|
||||
f.write(f"[http.middlewares.bad_bot_block_{rule['category']}]\n")
|
||||
f.write(f" [http.middlewares.bad_bot_block_{rule['category']}.plugin.badbot]\n")
|
||||
f.write(f" userAgent = [\"{rule['pattern']}\"]\n")
|
||||
print(f"[+] Traefik WAF rules generated at {config_file}")
|
||||
try:
|
||||
with open(config_file, "w") as f:
|
||||
f.write("[http.middlewares]\n\n")
|
||||
rule_counter = 1 # Unique identifier for each middleware
|
||||
|
||||
# Group rules by category
|
||||
grouped_rules = {}
|
||||
for rule in rules:
|
||||
category = rule.get("category", "default")
|
||||
if category not in grouped_rules:
|
||||
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}")
|
||||
except IOError as e:
|
||||
print(f"[-] Error writing to file: {e}")
|
||||
exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
owasp_rules = load_owasp_rules("owasp_rules.json")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user