|
| 1 | +import csv |
| 2 | +import io |
| 3 | +import logging |
| 4 | + |
| 5 | +import requests |
| 6 | +from aboutcode.pipeline import LoopProgress |
| 7 | +from dateutil import parser as dateparser |
| 8 | +from django.db import DataError |
| 9 | + |
| 10 | +from vulnerabilities.models import Alias |
| 11 | +from vulnerabilities.models import Exploit |
| 12 | +from vulnerabilities.models import VulnerabilityReference |
| 13 | +from vulnerabilities.models import VulnerabilityRelatedReference |
| 14 | +from vulnerabilities.pipelines import VulnerableCodePipeline |
| 15 | + |
| 16 | + |
| 17 | +class ExploitDBImproverPipeline(VulnerableCodePipeline): |
| 18 | + """ |
| 19 | + ExploitDB Improver Pipeline: Fetch ExploitDB data, iterate over it to find the vulnerability with |
| 20 | + the specified alias, and create or update the ref and ref-type accordingly. |
| 21 | + """ |
| 22 | + |
| 23 | + license_expression = "GPL-2.0" |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def steps(cls): |
| 27 | + return ( |
| 28 | + cls.fetch_exploits, |
| 29 | + cls.add_exploit, |
| 30 | + ) |
| 31 | + |
| 32 | + def fetch_exploits(self): |
| 33 | + exploit_db_url = ( |
| 34 | + "https://gitlab.com/exploit-database/exploitdb/-/raw/main/files_exploits.csv" |
| 35 | + ) |
| 36 | + |
| 37 | + try: |
| 38 | + response = requests.get(exploit_db_url) |
| 39 | + response.raise_for_status() |
| 40 | + except requests.exceptions.HTTPError as http_err: |
| 41 | + self.log( |
| 42 | + f"Failed to fetch the Exploit-DB Exploits: {exploit_db_url} - {http_err}", |
| 43 | + level=logging.ERROR, |
| 44 | + ) |
| 45 | + raise |
| 46 | + |
| 47 | + self.exploit_data = io.StringIO(response.text) |
| 48 | + |
| 49 | + def add_exploit(self): |
| 50 | + csvreader = csv.reader(self.exploit_data) |
| 51 | + header = next(csvreader) |
| 52 | + |
| 53 | + raw_data = list(csvreader) |
| 54 | + fetched_exploit_count = len(raw_data) |
| 55 | + |
| 56 | + vulnerability_exploit_count = 0 |
| 57 | + progress = LoopProgress(total_iterations=fetched_exploit_count, logger=self.log) |
| 58 | + |
| 59 | + for row in progress.iter(raw_data): |
| 60 | + vulnerability_exploit_count += add_vulnerability_exploit(row, header, self.log) |
| 61 | + |
| 62 | + self.log( |
| 63 | + f"Successfully added {vulnerability_exploit_count:,d} exploit-db vulnerability exploit" |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def add_vulnerability_exploit(row, header, logger): |
| 68 | + vulnerability = None |
| 69 | + aliases = row[11].split(";") |
| 70 | + |
| 71 | + for raw_alias in aliases: |
| 72 | + try: |
| 73 | + if alias := Alias.objects.get(alias=raw_alias): |
| 74 | + vulnerability = alias.vulnerability |
| 75 | + break |
| 76 | + except Alias.DoesNotExist: |
| 77 | + continue |
| 78 | + |
| 79 | + if not vulnerability: |
| 80 | + logger(f"No vulnerability found for aliases {aliases}") |
| 81 | + return 0 |
| 82 | + |
| 83 | + add_exploit_references(row[11], row[16], row[1], vulnerability, logger) |
| 84 | + |
| 85 | + date_added = parse_date(row[header.index("date_added")]) |
| 86 | + source_date_published = parse_date(row[header.index("date_published")]) |
| 87 | + source_date_updated = parse_date(row[header.index("date_updated")]) |
| 88 | + |
| 89 | + try: |
| 90 | + Exploit.objects.update_or_create( |
| 91 | + vulnerability=vulnerability, |
| 92 | + data_source="Exploit-DB", |
| 93 | + defaults={ |
| 94 | + "date_added": date_added, |
| 95 | + "description": row[header.index("description")], |
| 96 | + "known_ransomware_campaign_use": row[header.index("verified")], |
| 97 | + "source_date_published": source_date_published, |
| 98 | + "exploit_type": row[header.index("type")], |
| 99 | + "platform": row[header.index("platform")], |
| 100 | + "source_date_updated": source_date_updated, |
| 101 | + "source_url": row[header.index("source_url")], |
| 102 | + }, |
| 103 | + ) |
| 104 | + except DataError as e: |
| 105 | + logger( |
| 106 | + f"Failed to Create the Vulnerability Exploit-DB: {e}", |
| 107 | + level=logging.ERROR, |
| 108 | + ) |
| 109 | + return 1 |
| 110 | + |
| 111 | + |
| 112 | +def add_exploit_references(ref_id, direct_url, path, vul, logger): |
| 113 | + url_map = { |
| 114 | + "file_url": f"https://gitlab.com/exploit-database/exploitdb/-/blob/main/{path}", |
| 115 | + "direct_url": direct_url, |
| 116 | + } |
| 117 | + |
| 118 | + for key, url in url_map.items(): |
| 119 | + if url: |
| 120 | + try: |
| 121 | + ref, created = VulnerabilityReference.objects.update_or_create( |
| 122 | + url=url, |
| 123 | + defaults={ |
| 124 | + "reference_id": ref_id, |
| 125 | + "reference_type": VulnerabilityReference.EXPLOIT, |
| 126 | + }, |
| 127 | + ) |
| 128 | + |
| 129 | + if created: |
| 130 | + VulnerabilityRelatedReference.objects.get_or_create( |
| 131 | + vulnerability=vul, |
| 132 | + reference=ref, |
| 133 | + ) |
| 134 | + |
| 135 | + except DataError as e: |
| 136 | + logger( |
| 137 | + f"Failed to Create the Vulnerability Reference For Exploit-DB: {e}", |
| 138 | + level=logging.ERROR, |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +def parse_date(date_string): |
| 143 | + if date_string: |
| 144 | + try: |
| 145 | + date_obj = dateparser.parse(date_string).date() |
| 146 | + return date_obj.strftime("%Y-%m-%d") |
| 147 | + except (ValueError, TypeError, Exception) as e: |
| 148 | + logging.error(f"Error while parsing ExploitDB date '{date_string}': {e}") |
| 149 | + return |
0 commit comments