|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/nexB/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | +import csv |
| 10 | +import gzip |
| 11 | +import logging |
| 12 | +import urllib.request |
| 13 | +from datetime import datetime |
| 14 | +from typing import Iterable |
| 15 | + |
| 16 | +from vulnerabilities import severity_systems |
| 17 | +from vulnerabilities.importer import AdvisoryData |
| 18 | +from vulnerabilities.importer import Importer |
| 19 | +from vulnerabilities.importer import Reference |
| 20 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 21 | + |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +class EPSSImporter(Importer): |
| 26 | + """Exploit Prediction Scoring System (EPSS) Importer""" |
| 27 | + |
| 28 | + advisory_url = "https://epss.cyentia.com/epss_scores-current.csv.gz" |
| 29 | + spdx_license_expression = "unknown" |
| 30 | + importer_name = "EPSS Importer" |
| 31 | + |
| 32 | + def advisory_data(self) -> Iterable[AdvisoryData]: |
| 33 | + response = urllib.request.urlopen(self.advisory_url) |
| 34 | + with gzip.open(response, "rb") as f: |
| 35 | + lines = [l.decode("utf-8") for l in f.readlines()] |
| 36 | + |
| 37 | + epss_reader = csv.reader(lines) |
| 38 | + model_version, score_date = next( |
| 39 | + epss_reader |
| 40 | + ) # score_date='score_date:2024-05-19T00:00:00+0000' |
| 41 | + published_at = datetime.strptime(score_date[11::], "%Y-%m-%dT%H:%M:%S%z") |
| 42 | + |
| 43 | + next(epss_reader) # skip the header row |
| 44 | + for epss_row in epss_reader: |
| 45 | + cve, score, percentile = epss_row |
| 46 | + |
| 47 | + if not cve or not score or not percentile: |
| 48 | + logger.error(f"Invalid epss row: {epss_row}") |
| 49 | + continue |
| 50 | + |
| 51 | + severity = VulnerabilitySeverity( |
| 52 | + system=severity_systems.EPSS, |
| 53 | + value=score, |
| 54 | + scoring_elements=percentile, |
| 55 | + published_at=published_at, |
| 56 | + ) |
| 57 | + |
| 58 | + references = Reference( |
| 59 | + url=f"https://api.first.org/data/v1/epss?cve={cve}", |
| 60 | + severities=[severity], |
| 61 | + ) |
| 62 | + |
| 63 | + yield AdvisoryData( |
| 64 | + aliases=[cve], |
| 65 | + references=[references], |
| 66 | + url=self.advisory_url, |
| 67 | + ) |
0 commit comments