Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jsluice conversion #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 70 additions & 13 deletions scripts/convert-rules.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
Expand All @@ -34,29 +69,51 @@ 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:
print(output_string)


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)