|
| 1 | +""" Surely a fragile parser, but gets things started and will evolve over time I guess. |
| 2 | +It seems that a lot of the json data does not have any securityData data |
| 3 | +So these are just skipped, since there is nothing much to do with them here |
| 4 | +""" |
| 5 | +import json |
| 6 | +from dojo.models import Finding |
| 7 | + |
| 8 | + |
| 9 | +class SonatypeJSONParser(object): |
| 10 | + # This parser does not deal with licenses information. |
| 11 | + def __init__(self, json_output, test): |
| 12 | + tree = self.parse_json(json_output) |
| 13 | + |
| 14 | + if tree: |
| 15 | + self.items = [data for data in self.get_items(tree, test)] |
| 16 | + else: |
| 17 | + self.items = [] |
| 18 | + |
| 19 | + def parse_json(self, json_output): |
| 20 | + try: |
| 21 | + tree = json.load(json_output) |
| 22 | + except: |
| 23 | + raise Exception("Invalid format") |
| 24 | + |
| 25 | + return tree |
| 26 | + |
| 27 | + def get_items(self, tree, test): |
| 28 | + items = {} |
| 29 | + if 'components' in tree: |
| 30 | + vulnerability_tree = tree['components'] |
| 31 | + |
| 32 | + for node in vulnerability_tree: |
| 33 | + item = get_item(node, test) |
| 34 | + if item is None: |
| 35 | + continue |
| 36 | + # TODO |
| 37 | + unique_key = node['hash'] |
| 38 | + items[unique_key] = item |
| 39 | + |
| 40 | + return items.values() |
| 41 | + |
| 42 | + |
| 43 | +def get_item(vulnerability, test): |
| 44 | + # Following the CVSS Scoring per https://nvd.nist.gov/vuln-metrics/cvss |
| 45 | + if vulnerability['securityData'] is not None and len(vulnerability['securityData']['securityIssues']) >= 1: |
| 46 | + # there can be nothing in the array, or securityData can be null altogether. If the latter, well, nothing much to do? |
| 47 | + # issues is an array, and there can be 2+ of them, e.g. a cve and a sonatype entry or two cves |
| 48 | + # Given the current Finding class, if a cve, will be the main. If not a cve, then CVE ref will remain null due to regex. |
| 49 | + # Others go to references. |
| 50 | + main_finding = vulnerability['securityData']['securityIssues'][0] |
| 51 | + |
| 52 | + if main_finding.get("source") == "cve": |
| 53 | + cve = main_finding.get("reference") |
| 54 | + else: |
| 55 | + # if sonatype of else, will not match Finding model today |
| 56 | + cve = "" |
| 57 | + |
| 58 | + if main_finding['severity'] <= 3.9: |
| 59 | + severity = "Low" |
| 60 | + elif main_finding['severity'] > 4.0 and main_finding['severity'] <= 6.9: |
| 61 | + severity = "Medium" |
| 62 | + elif main_finding['severity'] and main_finding['severity'] <= 8.9: |
| 63 | + severity = "High" |
| 64 | + else: |
| 65 | + severity = "Critical" |
| 66 | + |
| 67 | + references = [] |
| 68 | + if len(vulnerability['securityData']['securityIssues']) > 1: |
| 69 | + for additional_issue in vulnerability['securityData']['securityIssues']: |
| 70 | + references.append("{}, {}, {}, {}, {} ".format( |
| 71 | + additional_issue.get("reference"), |
| 72 | + additional_issue.get("status"), |
| 73 | + additional_issue.get("severity"), |
| 74 | + additional_issue.get("threatCategory"), |
| 75 | + additional_issue.get("url")) |
| 76 | + ) |
| 77 | + |
| 78 | + component_id = '' |
| 79 | + if 'componentIdentifier' in vulnerability: |
| 80 | + if vulnerability['componentIdentifier']['format'] == "maven": |
| 81 | + component_id = "{} {} {}".format( |
| 82 | + vulnerability['componentIdentifier']['coordinates']['artifactId'], |
| 83 | + vulnerability['componentIdentifier']['coordinates']['groupId'], |
| 84 | + vulnerability['componentIdentifier']['coordinates']['version'] |
| 85 | + ) |
| 86 | + elif vulnerability['componentIdentifier']['format'] == "a-name": |
| 87 | + component_id = "{} {} {}".format( |
| 88 | + vulnerability['componentIdentifier']['coordinates']['name'], |
| 89 | + vulnerability['componentIdentifier']['coordinates']['qualifier'], |
| 90 | + vulnerability['componentIdentifier']['coordinates']['version'] |
| 91 | + ) |
| 92 | + |
| 93 | + finding_title = "{} - {}".format( |
| 94 | + main_finding['reference'], |
| 95 | + component_id |
| 96 | + ) |
| 97 | + |
| 98 | + finding_description = "Hash {}\n\n".format(vulnerability['hash']) |
| 99 | + finding_description += component_id |
| 100 | + finding_description += "\n\nPlease check the CVE details for a detailed description. If a sonatype issue, you're out of luck." |
| 101 | + threat_category = main_finding.get("threatCategory", "CVSS vector not provided. ").title() |
| 102 | + status = main_finding['status'] |
| 103 | + score = main_finding.get('severity', "No CVSS score yet.") |
| 104 | + if 'pathnames' in vulnerability: |
| 105 | + file_path = ' '.join(vulnerability['pathnames']) |
| 106 | + else: |
| 107 | + file_path = '' |
| 108 | + |
| 109 | + # create the finding object |
| 110 | + finding = Finding( |
| 111 | + title=finding_title, |
| 112 | + cve=cve, |
| 113 | + test=test, |
| 114 | + severity=severity, |
| 115 | + numerical_severity=Finding.get_numerical_severity(severity), |
| 116 | + description=finding_description, |
| 117 | + mitigation=status, |
| 118 | + references="{}\n{}\n".format(main_finding['url'], "\n".join(references)), |
| 119 | + active=False, |
| 120 | + verified=False, |
| 121 | + false_p=False, |
| 122 | + duplicate=False, |
| 123 | + out_of_scope=False, |
| 124 | + mitigated=None, |
| 125 | + file_path=file_path, |
| 126 | + impact=threat_category, |
| 127 | + static_finding=True |
| 128 | + ) |
| 129 | + |
| 130 | + finding.description = finding.description.strip() |
| 131 | + |
| 132 | + return finding |
| 133 | + else: |
| 134 | + return None |
0 commit comments