|
| 1 | +import json |
| 2 | +import pathlib |
| 3 | +import re |
| 4 | + |
| 5 | +import click |
| 6 | +from click import Path |
| 7 | +from cyclonedx.model.bom import Bom, Tool |
| 8 | +from cyclonedx.model.component import Component, ComponentType |
| 9 | +from cyclonedx.output.json import JsonV1Dot4CbomV1Dot0 |
| 10 | + |
| 11 | +from cbom import __version__ |
| 12 | +from cbom.cryptocheck import cryptocheck |
| 13 | +from cbom.parser import algorithm |
| 14 | + |
| 15 | + |
| 16 | +@click.group(context_settings={ |
| 17 | + 'max_content_width': 120, |
| 18 | + 'show_default': True |
| 19 | +}) |
| 20 | +@click.version_option(__version__, '--version', '-v') |
| 21 | +def cryptobom(): |
| 22 | + """\b |
| 23 | + _ _ __ \b |
| 24 | + | | | | / _| \b |
| 25 | + ___ _ __ _ _ _ __ | |_ ___ | |__ ___ _ __ ___ | |_ ___ _ __ __ _ ___ \b |
| 26 | + / __|| '__|| | | || '_ \ | __| / _ \ | '_ \ / _ \ | '_ ` _ \ ______ | _| / _ \ | '__| / _` | / _ \ \b |
| 27 | + | (__ | | | |_| || |_) || |_ | (_) || |_) || (_) || | | | | ||______|| | | (_) || | | (_| || __/ \b |
| 28 | + \___||_| \__, || .__/ \__| \___/ |_.__/ \___/ |_| |_| |_| |_| \___/ |_| \__, | \___| \b |
| 29 | + __/ || | __/ | \b |
| 30 | + |___/ |_| |___/ |
| 31 | +
|
| 32 | + Welcome to cryptobom-forge! |
| 33 | +
|
| 34 | + This script is intended to be used in conjunction with the SARIF output from the CodeQL cryptography experimental |
| 35 | + queries. |
| 36 | +
|
| 37 | + You can use this script to generate a cryptographic bill of materials (CBOM) for a repository, and analyse your |
| 38 | + cryptographic inventory for weak and non-pqc-safe cryptography. |
| 39 | + """ |
| 40 | + |
| 41 | + |
| 42 | +@cryptobom.command(context_settings={ |
| 43 | + 'default_map': { |
| 44 | + 'application_name': 'root', |
| 45 | + 'cryptocheck_output_file': 'cryptocheck.sarif' |
| 46 | + } |
| 47 | +}) |
| 48 | +@click.argument('path', type=Path(exists=True, path_type=pathlib.Path), required=True) |
| 49 | +@click.option('--application-name', '-n', help='Root application name') |
| 50 | +@click.option('--cryptocheck', '-cc', 'enable_cryptocheck', is_flag=True, help='Enable crypto vulnerability scanning') |
| 51 | +@click.option('--exclude', '-e', 'exclusion_pattern', metavar='REGEX', help='Exclude CodeQL findings in file paths that match <REGEX>') |
| 52 | +@click.option('--output-file', '-o', help='CBOM output file') |
| 53 | +@click.option('--rules-file', '-r', type=Path(exists=True, path_type=pathlib.Path), help='Custom ruleset for cryptocheck analysis') |
| 54 | +@click.option('--cryptocheck-output-file', help='Cryptocheck analysis output file') |
| 55 | +def generate(path, application_name, enable_cryptocheck, exclusion_pattern, output_file, rules_file, cryptocheck_output_file): |
| 56 | + """Generate a CBOM from CodeQL SARIF output.""" |
| 57 | + cbom = Bom() |
| 58 | + cbom.metadata.component = Component(name=application_name, type=ComponentType.APPLICATION) |
| 59 | + |
| 60 | + if exclusion_pattern: |
| 61 | + exclusion_pattern = re.compile(exclusion_pattern) |
| 62 | + |
| 63 | + if path.is_file(): |
| 64 | + _process_file(cbom, path, exclusion_pattern=exclusion_pattern) |
| 65 | + else: |
| 66 | + for file_path in [*list(path.glob('*.sarif')), *list(path.glob('*.json'))]: |
| 67 | + _process_file(cbom, file_path, exclusion_pattern=exclusion_pattern) |
| 68 | + |
| 69 | + if enable_cryptocheck: |
| 70 | + cryptocheck_output = cryptocheck.validate_cbom(cbom, rules_file) |
| 71 | + with open(cryptocheck_output_file, 'w') as file: |
| 72 | + click.echo(message=json.dumps(cryptocheck_output, indent=4, sort_keys=True), file=file) |
| 73 | + |
| 74 | + cbom = json.loads(JsonV1Dot4CbomV1Dot0(cbom).output_as_string(bom_format='CBOM')) |
| 75 | + if output_file: |
| 76 | + with open(output_file, 'w') as file: |
| 77 | + click.echo(message=json.dumps(cbom, indent=4), file=file) |
| 78 | + else: |
| 79 | + click.echo(message=json.dumps(cbom, indent=4)) |
| 80 | + |
| 81 | + |
| 82 | +def start(): |
| 83 | + try: |
| 84 | + cryptobom() |
| 85 | + except Exception as e: |
| 86 | + click.secho(str(e), fg='red') |
| 87 | + |
| 88 | + |
| 89 | +def _process_file(cbom, query_file, exclusion_pattern=None): |
| 90 | + with open(query_file) as query_output: |
| 91 | + query_output = json.load(query_output)['runs'][0] |
| 92 | + |
| 93 | + driver = query_output['tool']['driver'] |
| 94 | + cbom.metadata.tools.add(Tool( |
| 95 | + vendor=driver['organization'], |
| 96 | + name=driver['name'], |
| 97 | + version=driver.get('version', driver.get('semanticVersion')) # fixme: sarif misaligned |
| 98 | + )) |
| 99 | + |
| 100 | + for result in query_output['results']: |
| 101 | + result = result['locations'][0]['physicalLocation'] |
| 102 | + if not exclusion_pattern or not exclusion_pattern.fullmatch(result['artifactLocation']['uri']): |
| 103 | + algorithm.parse_algorithm(cbom, result) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + start() |
0 commit comments