Last active
November 15, 2024 04:32
-
-
Save dualfade/0e66929fd39e4f54a309ad5611718b76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# spdb2lsluice.py | |
# convert secrets-patterns-db to jsluice secrets format | |
# @dualfade | |
# NOTE: refs -- | |
# https://gist.github.com/dualfade/20f3a56bbbc386be0995a30bf2741169 -- | |
# https://github.com/BishopFox/jsluice/tree/main/cmd/jsluice#custom-secret-matchers -- | |
# https://github.com/mazen160/secrets-patterns-db -- | |
__author__ = "dualfade" | |
__email__ = "dualfade[at]vadersecurity.com" | |
__version__ = "0.1" | |
__license__ = "MIT" | |
import sys | |
import json | |
import yaml | |
import argparse | |
from icecream import ic | |
def load_yaml(file: str) -> str: | |
with open(file, "r") as stream: | |
try: | |
return yaml.safe_load(stream) | |
except yaml.YAMLError as error: | |
ic(error) | |
def update_values(loaded: dict) -> dict: | |
"""convert secrets-patterns-db to jsluice secrets format""" | |
""" jq '.[] | .[] | .pattern | .["value"] = .regex | .["severity"] = .confidence | del (.regex, .confidence)' | jq -s . """ | |
for key, value in loaded.items(): | |
for item in value: | |
pattern = item["pattern"] | |
pattern["value"] = pattern["regex"] | |
pattern["severity"] = pattern["confidence"] | |
del pattern["regex"] | |
del pattern["confidence"] | |
clean_patterns = loaded["patterns"] | |
clean_pattern = [item["pattern"] for item in clean_patterns] | |
return json.dumps(clean_pattern, indent=4) | |
def write_output(output: str, data: dict) -> tuple[str, dict]: | |
with open(output, "w") as f: | |
f.write(data) | |
ic("[info] output written to %s" % output) | |
def error(err: str) -> str: | |
"""standard error; exit""" | |
ic("[err] application error %s" % err) | |
ic("[err] exiting now.") | |
sys.exit(-1) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-f", "--file", dest="file", help="secrets db yaml file to convert" | |
) | |
parser.add_argument( | |
"-o", "--output", dest="output", help="write jsluice formatted output to file" | |
) | |
args = parser.parse_args() | |
try: | |
if args.file: | |
loaded = load_yaml(args.file) | |
ic("[info] loaded %s" % args.file) | |
ic("[info] converting to jsluice format") | |
jsluice_format = update_values(loaded) | |
ic(jsluice_format) | |
if args.output: | |
write_output(args.output, jsluice_format) | |
ic("[info] output written to %s" % args.output) | |
ic("[info] exiting now.") | |
else: | |
ic("[err] no file provided.") | |
sys.exit(1) | |
except KeyboardInterrupt: | |
sys.stdout.write("\n") | |
sys.stdout.flush() | |
except Exception as err: | |
error(err) |
Author
dualfade
commented
Sep 10, 2024
- Screenshots
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment