-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1526 from ambuj-1211/add-cwe-support-in-multiple-…
…importers Add CWE support in multiple importers
- Loading branch information
Showing
9 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
# | ||
|
||
import logging | ||
import re | ||
import urllib | ||
|
||
import requests | ||
|
@@ -23,6 +24,8 @@ | |
from vulnerabilities.importer import Reference | ||
from vulnerabilities.importer import VulnerabilitySeverity | ||
from vulnerabilities.severity_systems import APACHE_HTTPD | ||
from vulnerabilities.utils import create_weaknesses_list | ||
from vulnerabilities.utils import cwe_regex | ||
from vulnerabilities.utils import get_item | ||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -102,11 +105,14 @@ def to_advisory(self, data): | |
) | ||
) | ||
|
||
weaknesses = get_weaknesses(data) | ||
|
||
return AdvisoryData( | ||
aliases=[alias], | ||
summary=description or "", | ||
affected_packages=affected_packages, | ||
references=[reference], | ||
weaknesses=weaknesses, | ||
url=reference.url, | ||
) | ||
|
||
|
@@ -152,3 +158,97 @@ def fetch_links(url): | |
continue | ||
links.append(urllib.parse.urljoin(url, link)) | ||
return links | ||
|
||
|
||
def get_weaknesses(cve_data): | ||
""" | ||
Extract CWE IDs from CVE data. | ||
Args: | ||
cve_data (dict): The CVE data in a dictionary format. | ||
Returns: | ||
List[int]: A list of unique CWE IDs. | ||
Examples: | ||
>>> mock_cve_data1 = { | ||
... "containers": { | ||
... "cna": { | ||
... "providerMetadata": { | ||
... "orgId": "f0158376-9dc2-43b6-827c-5f631a4d8d09" | ||
... }, | ||
... "title": "mod_macro buffer over-read", | ||
... "problemTypes": [ | ||
... { | ||
... "descriptions": [ | ||
... { | ||
... "description": "CWE-125 Out-of-bounds Read", | ||
... "lang": "en", | ||
... "cweId": "CWE-125", | ||
... "type": "CWE" | ||
... } | ||
... ] | ||
... } | ||
... ] | ||
... } | ||
... } | ||
... } | ||
>>> mock_cve_data2 = { | ||
... "data_type": "CVE", | ||
... "data_format": "MITRE", | ||
... "data_version": "4.0", | ||
... "generator": { | ||
... "engine": "Vulnogram 0.0.9" | ||
... }, | ||
... "CVE_data_meta": { | ||
... "ID": "CVE-2022-28614", | ||
... "ASSIGNER": "[email protected]", | ||
... "TITLE": "read beyond bounds via ap_rwrite() ", | ||
... "STATE": "PUBLIC" | ||
... }, | ||
... "problemtype": { | ||
... "problemtype_data": [ | ||
... { | ||
... "description": [ | ||
... { | ||
... "lang": "eng", | ||
... "value": "CWE-190 Integer Overflow or Wraparound" | ||
... } | ||
... ] | ||
... }, | ||
... { | ||
... "description": [ | ||
... { | ||
... "lang": "eng", | ||
... "value": "CWE-200 Exposure of Sensitive Information to an Unauthorized Actor" | ||
... } | ||
... ] | ||
... } | ||
... ] | ||
... } | ||
... } | ||
>>> get_weaknesses(mock_cve_data1) | ||
[125] | ||
>>> get_weaknesses(mock_cve_data2) | ||
[190, 200] | ||
""" | ||
alias = get_item(cve_data, "CVE_data_meta", "ID") | ||
cwe_strings = [] | ||
if alias: | ||
problemtype_data = get_item(cve_data, "problemtype", "problemtype_data") or [] | ||
for problem in problemtype_data: | ||
for desc in problem.get("description", []): | ||
value = desc.get("value", "") | ||
cwe_id_string_list = re.findall(cwe_regex, value) | ||
cwe_strings.extend(cwe_id_string_list) | ||
else: | ||
problemTypes = cve_data.get("containers", {}).get("cna", {}).get("problemTypes", []) | ||
descriptions = problemTypes[0].get("descriptions", []) if len(problemTypes) > 0 else [] | ||
for description in descriptions: | ||
cwe_id_string = description.get("cweId", "") | ||
cwe_strings.append(cwe_id_string) | ||
|
||
weaknesses = create_weaknesses_list(cwe_strings) | ||
return weaknesses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.