Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CWE and CVSS raw data #313

Merged
merged 4 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CveXplore/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.36
0.3.37
1 change: 1 addition & 0 deletions CveXplore/core/database_indexer/db_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self, datasource: DatabaseConnectionBase):
MongoAddIndex(index=[("cvss", ASCENDING)], name="cvss"),
MongoAddIndex(index=[("cvss3", ASCENDING)], name="cvss3"),
MongoAddIndex(index=[("cvss4", ASCENDING)], name="cvss4"),
MongoAddIndex(index=[("cvss_data", ASCENDING)], name="cvss_data"),
MongoAddIndex(index=[("summary", TEXT)], name="summary"),
MongoAddIndex(index=[("vendors", ASCENDING)], name="vendors"),
MongoAddIndex(index=[("products", ASCENDING)], name="products"),
Expand Down
144 changes: 136 additions & 8 deletions CveXplore/core/database_maintenance/sources_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import glob
import hashlib
import json
import re
import shutil
import time
from typing import Any, Tuple
Expand Down Expand Up @@ -592,6 +593,123 @@ def process_the_item(self, item: dict = None):
else:
cve["cvss"] = None

cve["cvss_data"] = {"cvss2": {}, "cvss3": {}, "cvss4": {}}

for version in [
"cvssMetricV40",
"cvssMetricV31",
"cvssMetricV30",
"cvssMetricV2",
]:
if version in item["cve"]["metrics"]:
for metric in item["cve"]["metrics"][version]:
cvss_key = (
"cvss4"
if version == "cvssMetricV40"
else (
"cvss3"
if version in ["cvssMetricV31", "cvssMetricV30"]
else "cvss2"
)
)
source = metric["source"]

entry = {
"type": metric["type"],
"vectorString": metric["cvssData"]["vectorString"],
"baseScore": metric["cvssData"]["baseScore"],
}

if cvss_key == "cvss4":
entry.update(
{
"vulnerable_system_confidentiality": metric[
"cvssData"
].get("vulnerableSystemConfidentiality"),
"vulnerable_system_integrity": metric["cvssData"].get(
"vulnerableSystemIntegrity"
),
"vulnerable_system_availability": metric[
"cvssData"
].get("vulnerableSystemAvailability"),
"subsequent_system_confidentiality": metric[
"cvssData"
].get("subsequentSystemConfidentiality"),
"subsequent_system_integrity": metric["cvssData"].get(
"subsequentSystemIntegrity"
),
"subsequent_system_availability": metric[
"cvssData"
].get("subsequentSystemAvailability"),
"attackVector": metric["cvssData"].get("attackVector"),
"attackComplexity": metric["cvssData"].get(
"attackComplexity"
),
"attackRequirements": metric["cvssData"].get(
"attackRequirements"
),
"privilegesRequired": metric["cvssData"].get(
"privilegesRequired"
),
"userInteraction": metric["cvssData"].get(
"userInteraction"
),
"exploitMaturity": metric["cvssData"].get(
"exploitMaturity"
),
}
)
elif cvss_key == "cvss3":
entry.update(
{
"confidentialityImpact": metric["cvssData"].get(
"confidentialityImpact"
),
"integrityImpact": metric["cvssData"].get(
"integrityImpact"
),
"availabilityImpact": metric["cvssData"].get(
"availabilityImpact"
),
"attackVector": metric["cvssData"].get("attackVector"),
"attackComplexity": metric["cvssData"].get(
"attackComplexity"
),
"privilegesRequired": metric["cvssData"].get(
"privilegesRequired"
),
"userInteraction": metric["cvssData"].get(
"userInteraction"
),
"scope": metric["cvssData"].get("scope"),
}
)
elif cvss_key == "cvss2":
entry.update(
{
"authentication": metric["cvssData"].get(
"authentication"
),
"accessComplexity": metric["cvssData"].get(
"accessComplexity"
),
"accessVector": metric["cvssData"].get("accessVector"),
"confidentialityImpact": metric["cvssData"].get(
"confidentialityImpact"
),
"integrityImpact": metric["cvssData"].get(
"integrityImpact"
),
"availabilityImpact": metric["cvssData"].get(
"availabilityImpact"
),
}
)

if source not in cve["cvss_data"][cvss_key]:
cve["cvss_data"][cvss_key][source] = []
cve["cvss_data"][cvss_key][source].append(entry)

if "references" in item["cve"]:
cve["references"] = []
for ref in item["cve"]["references"]:
Expand Down Expand Up @@ -694,17 +812,27 @@ def process_the_item(self, item: dict = None):
self.stem(cpeuri["criteria"]),
)
if "weaknesses" in item["cve"]:
cwe_set = set()

for problem in item["cve"]["weaknesses"]:
for cwe in problem[
"description"
]: # NVD JSON not clear if we can get more than one CWE per CVE (until we take the last one) -
# NVD-CWE-Other??? list?
for cwe in problem.get("description", []):
if cwe["lang"] == "en":
cve["cwe"] = cwe["value"]
if not ("cwe" in cve):
cve["cwe"] = defaultvalue["cwe"]
cwe_set.add(cwe["value"])

cve["cwe"] = sorted(cwe_set)

# If at least one valid CWE exists, remove all "NVD-CWE-*" entries
if any(not re.match(r"^NVD-CWE-", cwe) for cwe in cve["cwe"]):
cve["cwe"] = [
cwe for cwe in cve["cwe"] if not re.match(r"^NVD-CWE-", cwe)
]

# If the list is empty after filtering, assign the default value
if not cve["cwe"]:
cve["cwe"] = [defaultvalue["cwe"]]
else:
cve["cwe"] = defaultvalue["cwe"]
# Assign the default value if "weaknesses" is not present
cve["cwe"] = [defaultvalue["cwe"]]

cve["vulnerable_configuration_cpe_2_2"] = []

Expand Down
1 change: 1 addition & 0 deletions CveXplore/database/helpers/generic_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, collection: str):
"cvss",
"cvss3",
"cvss4",
"cvss_data",
"summary",
"vendors",
"products",
Expand Down
1 change: 1 addition & 0 deletions CveXplore/database/helpers/specific_db.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class CvesDatabaseFunctions:
cvss: GenericDatabaseFieldsFunctions
cvss3: GenericDatabaseFieldsFunctions
cvss4: GenericDatabaseFieldsFunctions
cvss_data: GenericDatabaseFieldsFunctions
summary: GenericDatabaseFieldsFunctions
vendors: GenericDatabaseFieldsFunctions
products: GenericDatabaseFieldsFunctions
Expand Down
3 changes: 2 additions & 1 deletion CveXplore/database_models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class Cves(CveXploreBase):
cvssTime = Column(DateTime, doc="Time of the CVSS of the CVE")
cvssVector = Column(String(100), doc="Vector of the CVSS of the CVE")
configurations = Column(JSON, doc="Vulnerable configurations of the CVE")
cwe = Column(String(50), index=True, doc="Related CWEs to the CVE")
cvss_data = Column(JSON, doc="Raw cvss data")
cwe = Column(JSON, default=[], doc="Related CWEs to the CVE")
epss = Column(Float, index=True, doc="Epss of the CVE")
epssMetric = Column(JSON, doc="Epss metric of the CVE")
exploitabilityScore = Column(Float, doc="Exploitability Score of the CVE")
Expand Down