|
| 1 | +import csv |
| 2 | +import json |
| 3 | +import io |
| 4 | + |
| 5 | +from cvss import parser as cvss_parser |
| 6 | +from dateutil import parser |
| 7 | +from datetime import datetime |
| 8 | +from dojo.models import Finding |
| 9 | + |
| 10 | + |
| 11 | +class VeracodeScaParser(object): |
| 12 | + |
| 13 | + vc_severity_mapping = { |
| 14 | + 1: 'Info', |
| 15 | + 2: 'Low', |
| 16 | + 3: 'Medium', |
| 17 | + 4: 'High', |
| 18 | + 5: 'Critical' |
| 19 | + } |
| 20 | + |
| 21 | + def get_scan_types(self): |
| 22 | + return ["Veracode SourceClear Scan"] |
| 23 | + |
| 24 | + def get_label_for_scan_types(self, scan_type): |
| 25 | + return "Veracode SourceClear Scan" |
| 26 | + |
| 27 | + def get_description_for_scan_types(self, scan_type): |
| 28 | + return "Veracode SourceClear CSV or JSON report format" |
| 29 | + |
| 30 | + def get_findings(self, file, test): |
| 31 | + if file is None: |
| 32 | + return () |
| 33 | + |
| 34 | + if file.name.strip().lower().endswith(".json"): |
| 35 | + return self._get_findings_json(file, test) |
| 36 | + |
| 37 | + return self.get_findings_csv(file, test) |
| 38 | + |
| 39 | + def _get_findings_json(self, file, test): |
| 40 | + """Load a Veracode SCA file in JSON format""" |
| 41 | + data = json.load(file) |
| 42 | + |
| 43 | + embedded = data.get("_embedded") |
| 44 | + findings = [] |
| 45 | + if not embedded: |
| 46 | + return findings |
| 47 | + |
| 48 | + for issue in embedded.get("issues", []): |
| 49 | + if issue.get('issue_type') != 'vulnerability': |
| 50 | + continue |
| 51 | + |
| 52 | + date = parser.parse(issue.get("created_date")) |
| 53 | + library = issue.get("library") |
| 54 | + component_name = library.get("name") |
| 55 | + if library.get("id") and library.get("id").startswith("maven:"): |
| 56 | + component_name = library.get("id").split(":")[2] |
| 57 | + component_version = library.get("version") |
| 58 | + |
| 59 | + vulnerability = issue.get("vulnerability") |
| 60 | + vuln_id = vulnerability.get("cve") |
| 61 | + if vuln_id and not (vuln_id.startswith("cve") or vuln_id.startswith("CVE")): |
| 62 | + vuln_id = "CVE-" + vuln_id |
| 63 | + cvss_score = issue.get("severity") |
| 64 | + if vulnerability.get("cvss3_score"): |
| 65 | + cvss_score = vulnerability.get("cvss3_score") |
| 66 | + severity = self.__cvss_to_severity(cvss_score) |
| 67 | + |
| 68 | + description = 'This library has known vulnerabilities.\n' |
| 69 | + description += \ |
| 70 | + "**CVE:** {0} ({1})\n" \ |
| 71 | + "CVS Score: {2} ({3})\n" \ |
| 72 | + "Project name: {4}\n" \ |
| 73 | + "Title: \n>{5}" \ |
| 74 | + "\n\n-----\n\n".format( |
| 75 | + vuln_id, |
| 76 | + date, |
| 77 | + cvss_score, |
| 78 | + severity, |
| 79 | + issue.get("project_name"), |
| 80 | + vulnerability.get('title')) |
| 81 | + |
| 82 | + finding = Finding(test=test, |
| 83 | + title=f"{component_name}:{component_version} | {vuln_id}", |
| 84 | + description=description, |
| 85 | + severity=severity, |
| 86 | + component_name=component_name, |
| 87 | + component_version=component_version, |
| 88 | + static_finding=True, |
| 89 | + dynamic_finding=False, |
| 90 | + unique_id_from_tool=issue.get("id"), |
| 91 | + date=date, |
| 92 | + nb_occurences=1) |
| 93 | + |
| 94 | + if vuln_id: |
| 95 | + finding.unsaved_vulnerability_ids = [vuln_id] |
| 96 | + |
| 97 | + if vulnerability.get("cvss3_vector"): |
| 98 | + cvssv3_vector = vulnerability.get("cvss3_vector") |
| 99 | + if not cvssv3_vector.startswith("CVSS:3.1/"): |
| 100 | + cvssv3_vector = "CVSS:3.1/" + cvssv3_vector |
| 101 | + vectors = cvss_parser.parse_cvss_from_text(cvssv3_vector) |
| 102 | + if len(vectors) > 0: |
| 103 | + finding.cvssv3 = vectors[0].clean_vector() |
| 104 | + |
| 105 | + if vulnerability.get("cwe_id"): |
| 106 | + cwe = vulnerability.get("cwe_id") |
| 107 | + if cwe: |
| 108 | + if cwe.startswith("CWE-") or cwe.startswith("cwe-"): |
| 109 | + cwe = cwe[4:] |
| 110 | + if cwe.isdigit(): |
| 111 | + finding.cwe = int(cwe) |
| 112 | + |
| 113 | + finding.references = "\n\n" + issue.get("_links").get("html").get("href") |
| 114 | + if (issue.get('Ignored') and issue.get('Ignored').capitalize() == 'True' or |
| 115 | + issue.get('issue_status') and issue.get('issue_status').capitalize() == 'Resolved'): |
| 116 | + finding.is_mitigated = True |
| 117 | + finding.active = False |
| 118 | + |
| 119 | + findings.append(finding) |
| 120 | + |
| 121 | + return findings |
| 122 | + |
| 123 | + def get_findings_csv(self, file, test): |
| 124 | + content = file.read() |
| 125 | + if type(content) is bytes: |
| 126 | + content = content.decode('utf-8') |
| 127 | + reader = csv.DictReader(io.StringIO(content), delimiter=',', quotechar='"') |
| 128 | + csvarray = [] |
| 129 | + |
| 130 | + for row in reader: |
| 131 | + csvarray.append(row) |
| 132 | + |
| 133 | + findings = [] |
| 134 | + for row in csvarray: |
| 135 | + if row.get('Issue type') != 'Vulnerability': |
| 136 | + continue |
| 137 | + |
| 138 | + issueId = row.get('Issue ID', None) |
| 139 | + if not issueId: |
| 140 | + # Workaround for possible encoding issue |
| 141 | + issueId = list(row.values())[0] |
| 142 | + library = row.get('Library', None) |
| 143 | + if row.get('Package manager') == 'MAVEN' and row.get('Coordinate 2'): |
| 144 | + library = row.get('Coordinate 2') |
| 145 | + version = row.get('Version in use', None) |
| 146 | + vuln_id = row.get('CVE', None) |
| 147 | + if vuln_id and not (vuln_id.startswith("cve") or vuln_id.startswith("CVE")): |
| 148 | + vuln_id = "CVE-" + vuln_id |
| 149 | + |
| 150 | + severity = self.fix_severity(row.get('Severity', None)) |
| 151 | + cvss_score = float(row.get('CVSS score', 0)) |
| 152 | + date = datetime.strptime(row.get('Issue opened: Scan date'), '%d %b %Y %H:%M%p %Z') |
| 153 | + description = 'This library has known vulnerabilities.\n' |
| 154 | + description += \ |
| 155 | + "**CVE:** {0} ({1})\n" \ |
| 156 | + "CVS Score: {2} ({3})\n" \ |
| 157 | + "Project name: {4}\n" \ |
| 158 | + "Title: \n>{5}" \ |
| 159 | + "\n\n-----\n\n".format( |
| 160 | + vuln_id, |
| 161 | + date, |
| 162 | + cvss_score, |
| 163 | + severity, |
| 164 | + row.get('Project'), |
| 165 | + row.get('Title')) |
| 166 | + |
| 167 | + finding = Finding(test=test, |
| 168 | + title=f"{library}:{version} | {vuln_id}", |
| 169 | + description=description, |
| 170 | + severity=severity, |
| 171 | + component_name=library, |
| 172 | + component_version=version, |
| 173 | + static_finding=True, |
| 174 | + dynamic_finding=False, |
| 175 | + unique_id_from_tool=issueId, |
| 176 | + date=date, |
| 177 | + nb_occurences=1) |
| 178 | + |
| 179 | + finding.unsaved_vulnerability_ids = [vuln_id] |
| 180 | + if cvss_score: |
| 181 | + finding.cvssv3_score = cvss_score |
| 182 | + |
| 183 | + if (row.get('Ignored') and row.get('Ignored').capitalize() == 'True' or |
| 184 | + row.get('Status') and row.get('Status').capitalize() == 'Resolved'): |
| 185 | + finding.is_mitigated = True |
| 186 | + finding.active = False |
| 187 | + |
| 188 | + findings.append(finding) |
| 189 | + |
| 190 | + return findings |
| 191 | + |
| 192 | + def fix_severity(self, severity): |
| 193 | + severity = severity.capitalize() |
| 194 | + if severity is None: |
| 195 | + severity = "Medium" |
| 196 | + elif "Unknown" == severity or "None" == severity: |
| 197 | + severity = "Info" |
| 198 | + return severity |
| 199 | + |
| 200 | + @classmethod |
| 201 | + def __cvss_to_severity(cls, cvss): |
| 202 | + if cvss >= 9: |
| 203 | + return cls.vc_severity_mapping.get(5) |
| 204 | + elif cvss >= 7: |
| 205 | + return cls.vc_severity_mapping.get(4) |
| 206 | + elif cvss >= 4: |
| 207 | + return cls.vc_severity_mapping.get(3) |
| 208 | + elif cvss > 0: |
| 209 | + return cls.vc_severity_mapping.get(2) |
| 210 | + else: |
| 211 | + return cls.vc_severity_mapping.get(1) |
0 commit comments