diff --git a/scripts/convert-rules.py b/scripts/convert-rules.py old mode 100755 new mode 100644 index 3701e42..11e9ae8 --- a/scripts/convert-rules.py +++ b/scripts/convert-rules.py @@ -3,17 +3,52 @@ import json import yaml import argparse +import re -def trufflehog_output(y): +def trufflehogv2_output(y): output = {} for i in y["patterns"]: if i["pattern"]["confidence"] != "high": continue output.update({i["pattern"]["name"]: i["pattern"]["regex"]}) - + return json.dumps(output, indent=4, sort_keys=True) - + + +def trufflehogv3_output(y): + output = [] + for i in y["patterns"]: + each_name = i["pattern"]["name"] + each_regex = i["pattern"]["regex"] + # each_confidence = i["pattern"]["confidence"] + + keywords_list = re.sub(r"[.,;:?!_\-]", " ", each_name).split() + output.append( + { + "name": each_name, + "keywords": list(keywords_list), + "regex": {each_name: each_regex}, + } + ) + + return output + + +def jsluice_output(y): + for key, value in y.items(): + for item in value: + pattern = item["pattern"] + pattern["value"] = pattern["regex"] + pattern["severity"] = pattern["confidence"] + del pattern["regex"] + del pattern["confidence"] + + del_patterns = y["patterns"] + del_pattern = [item["pattern"] for item in del_patterns] + + return json.dumps(del_pattern, indent=4) + def gitleaks_output(y): s = 'title = "gitleaks config"' @@ -34,18 +69,26 @@ def main(arg): f = open(arg.database_file, "r") y = yaml.safe_load(f.read()) f.close() - + output_string = "" ext_string = "" - if arg.output_type == "trufflehog": - output_string = trufflehog_output(y) + if arg.output_type == "trufflehogv2": + output_string = trufflehogv2_output(y) ext_string = "json" elif arg.output_type == "gitleaks": output_string = gitleaks_output(y) ext_string = "toml" - - if arg.export_filename is not None: + elif arg.output_type == "trufflehogv3": + output_string = yaml.dump(trufflehogv3_output(y), sort_keys=False) + ext_string = "yml" + elif arg.output_type == "jsluice": + output_string = jsluice_output(y) + ext_string = "json" + + if arg.export_filename is not None: f = open(f"{arg.export_filename}.{ext_string}", "w") + if arg.output_type == "trufflehogv3": + f.write("detectors:\n") f.write(output_string) f.close() else: @@ -53,10 +96,24 @@ def main(arg): if __name__ == "__main__": - parser = argparse.ArgumentParser(description='Convert yaml database file to rules for trufflehog or gitleaks') - parser.add_argument("--db", dest = "database_file", required = True, help = "The yaml database file") - parser.add_argument("--type", dest= "output_type", required = True, choices=['trufflehog', 'gitleaks'], help = "Supported output types: trufflehog, gitleaks") - parser.add_argument('--export', dest="export_filename", help = "Give filename, extension toml/json will be added") + parser = argparse.ArgumentParser( + description="Convert yaml database file to rules for trufflehogv2, trufflehogv3, jsluice or gitleaks" + ) + parser.add_argument( + "--db", dest="database_file", required=True, help="The yaml database file" + ) + parser.add_argument( + "--type", + dest="output_type", + required=True, + choices=["trufflehogv2", "trufflehogv3", "jsluice", "gitleaks"], + help="Supported output types: trufflehog, jsluice, gitleaks", + ) + parser.add_argument( + "--export", + dest="export_filename", + help="Give filename, extension toml/json/yaml will be added", + ) args = parser.parse_args() - + main(args)