|
| 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 logging |
| 10 | +import re |
| 11 | +from pathlib import Path |
| 12 | +from typing import Iterable |
| 13 | +from typing import List |
| 14 | + |
| 15 | +from vulnerabilities.importer import AdvisoryData |
| 16 | +from vulnerabilities.importer import GitImporter |
| 17 | +from vulnerabilities.importer import Reference |
| 18 | +from vulnerabilities.utils import build_description |
| 19 | +from vulnerabilities.utils import dedupe |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class FireyeImporter(GitImporter): |
| 25 | + spdx_license_expression = "CC-BY-SA-4.0 AND MIT" |
| 26 | + license_url = "https://github.com/mandiant/Vulnerability-Disclosures/blob/master/README.md" |
| 27 | + notice = """ |
| 28 | + Copyright (c) Mandiant |
| 29 | + The following licenses/licensing apply to this Mandiant repository: |
| 30 | + 1. CC BY-SA 4.0 - For CVE related information not including source code (such as PoCs) |
| 31 | + 2. MIT - For source code contained within provided CVE information |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + super().__init__(repo_url="git+https://github.com/mandiant/Vulnerability-Disclosures") |
| 36 | + |
| 37 | + def advisory_data(self) -> Iterable[AdvisoryData]: |
| 38 | + self.clone() |
| 39 | + files = filter( |
| 40 | + lambda p: p.suffix in [".md", ".MD"], Path(self.vcs_response.dest_dir).glob("**/*") |
| 41 | + ) |
| 42 | + for file in files: |
| 43 | + if Path(file).stem == "README": |
| 44 | + continue |
| 45 | + try: |
| 46 | + with open(file) as f: |
| 47 | + yield parse_advisory_data(f.read()) |
| 48 | + except UnicodeError: |
| 49 | + logger.error(f"Invalid file {file}") |
| 50 | + |
| 51 | + |
| 52 | +def parse_advisory_data(raw_data) -> AdvisoryData: |
| 53 | + """ |
| 54 | + Parse a fireeye advisory repo and return an AdvisoryData or None. |
| 55 | + These files are in Markdown format. |
| 56 | + """ |
| 57 | + raw_data = raw_data.replace("\n\n", "\n") |
| 58 | + md_list = raw_data.split("\n") |
| 59 | + md_dict = md_list_to_dict(md_list) |
| 60 | + |
| 61 | + database_id = md_list[0][1::] |
| 62 | + summary = md_dict.get(database_id[1::]) or [] |
| 63 | + description = md_dict.get("## Description") or [] |
| 64 | + impact = md_dict.get("## Impact") # not used but can be used to get severity |
| 65 | + exploit_ability = md_dict.get("## Exploitability") # not used |
| 66 | + cve_ref = md_dict.get("## CVE Reference") or [] |
| 67 | + tech_details = md_dict.get("## Technical Details") # not used |
| 68 | + resolution = md_dict.get("## Resolution") # not used |
| 69 | + disc_credits = md_dict.get("## Discovery Credits") # not used |
| 70 | + disc_timeline = md_dict.get("## Disclosure Timeline") # not used |
| 71 | + references = md_dict.get("## References") or [] |
| 72 | + |
| 73 | + return AdvisoryData( |
| 74 | + aliases=get_aliases(database_id, cve_ref), |
| 75 | + summary=build_description(" ".join(summary), " ".join(description)), |
| 76 | + references=get_references(references), |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def get_references(references): |
| 81 | + """ |
| 82 | + Return a list of Reference from a list of URL reference in md format |
| 83 | + >>> get_references(["- http://1-4a.com/cgi-bin/alienform/af.cgi"]) |
| 84 | + [Reference(reference_id='', url='http://1-4a.com/cgi-bin/alienform/af.cgi', severities=[])] |
| 85 | + >>> get_references(["- [Mitre CVE-2021-42712](https://www.cve.org/CVERecord?id=CVE-2021-42712)"]) |
| 86 | + [Reference(reference_id='', url='https://www.cve.org/CVERecord?id=CVE-2021-42712', severities=[])] |
| 87 | + """ |
| 88 | + urls = [] |
| 89 | + for ref in references: |
| 90 | + if ref.startswith("- "): |
| 91 | + urls.append(matcher_url(ref[2::])) |
| 92 | + else: |
| 93 | + urls.append(matcher_url(ref)) |
| 94 | + |
| 95 | + return [Reference(url=url) for url in urls if url] |
| 96 | + |
| 97 | + |
| 98 | +def matcher_url(ref) -> str: |
| 99 | + """ |
| 100 | + Returns URL of the reference markup from reference url in Markdown format |
| 101 | + """ |
| 102 | + markup_regex = "\[([^\[]+)]\(\s*(http[s]?://.+)\s*\)" |
| 103 | + matched_markup = re.findall(markup_regex, ref) |
| 104 | + if matched_markup: |
| 105 | + return matched_markup[0][1] |
| 106 | + else: |
| 107 | + return ref |
| 108 | + |
| 109 | + |
| 110 | +def get_aliases(database_id, cve_ref) -> List: |
| 111 | + """ |
| 112 | + Returns a List of Aliases from a database_id and a list of CVEs |
| 113 | + >>> get_aliases("MNDT-2021-0012",["CVE-2021-44207"]) |
| 114 | + ['CVE-2021-44207', 'MNDT-2021-0012'] |
| 115 | + """ |
| 116 | + cve_ref.append(database_id) |
| 117 | + return dedupe(cve_ref) |
| 118 | + |
| 119 | + |
| 120 | +def md_list_to_dict(md_list): |
| 121 | + """ |
| 122 | + Returns a dictionary of md_list from a list of a md file splited by \n |
| 123 | + >>> md_list_to_dict(["# Header","hello" , "hello again" ,"# Header2"]) |
| 124 | + {'# Header': ['hello', 'hello again'], '# Header2': []} |
| 125 | + """ |
| 126 | + md_dict = {} |
| 127 | + md_key = "" |
| 128 | + for md_line in md_list: |
| 129 | + if md_line.startswith("#"): |
| 130 | + md_dict[md_line] = [] |
| 131 | + md_key = md_line |
| 132 | + else: |
| 133 | + md_dict[md_key].append(md_line) |
| 134 | + return md_dict |
0 commit comments