From f90caf30d9481c717d2c574bda70635d9b927258 Mon Sep 17 00:00:00 2001 From: fab Date: Sat, 22 Feb 2025 11:41:33 +0100 Subject: [PATCH] Update json2apache.py JSON bug fixed. --- json2apache.py | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/json2apache.py b/json2apache.py index f1ab45c..fe01a62 100644 --- a/json2apache.py +++ b/json2apache.py @@ -151,22 +151,6 @@ def generate_apache_waf(rules: List[Dict]) -> None: raise -def main() -> None: - """ - Main function to execute the script. - """ - try: - logging.info("[*] Loading OWASP rules...") - owasp_rules = load_owasp_rules(INPUT_FILE) - - logging.info(f"[*] Generating Apache WAF configs from {len(owasp_rules)} rules...") - generate_apache_waf(owasp_rules) - - logging.info("[✔] Apache ModSecurity configurations generated successfully.") - except Exception as e: - logging.critical(f"[!] Script failed: {e}") - exit(1) - def load_json(file_path): """ Load and parse JSON file. @@ -197,21 +181,26 @@ def main(): rules = [] rule_id = 1000 # Initial rule ID - for rule in json_data.get('rules', []): - pattern = rule.get('pattern') - category = rule.get('category') + # Check if json_data is a dictionary and contains the 'rules' key + if isinstance(json_data, dict): + for rule in json_data.get('rules', []): + pattern = rule.get('pattern') + category = rule.get('category') - if not pattern or any(unsupported in pattern for unsupported in UNSUPPORTED_PATTERNS): - logging.info(f"[!] Skipping unsupported pattern: {pattern}") - continue - - if validate_regex(pattern): - rules.append(MODSEC_RULE_TEMPLATE.format(pattern=pattern, rule_id=rule_id, category=category)) - rule_id += 1 + if not pattern or any(unsupported in pattern for unsupported in UNSUPPORTED_PATTERNS): + logging.info(f"[!] Skipping unsupported pattern: {pattern}") + continue + if validate_regex(pattern): + rules.append(MODSEC_RULE_TEMPLATE.format(pattern=pattern, rule_id=rule_id, category=category)) + rule_id += 1 + else: + logging.error("[!] Invalid JSON format: Expected a dictionary with a 'rules' key.") + return + output_file_path = OUTPUT_DIR / "rules.conf" write_rules_to_file(rules, output_file_path) - + logging.info(f"[+] Generated rules.conf in {output_file_path}") if __name__ == "__main__": - main() \ No newline at end of file + main()