cloning over all the old files
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import os
|
||||
import requests
|
||||
import pandas as pd
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
def get_latest_cves(days=3):
|
||||
try:
|
||||
end_date = datetime.now()
|
||||
start_date = end_date - timedelta(days=days)
|
||||
start_date_str = start_date.strftime("%Y-%m-%dT%H:%M:%S")
|
||||
end_date_str = end_date.strftime("%Y-%m-%dT%H:%M:%S")
|
||||
nvd_url = f"https://services.nvd.nist.gov/rest/json/cves/1.0?pubStartDate={start_date_str}&pubEndDate={end_date_str}"
|
||||
response = requests.get(nvd_url)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
print(f"Error: Unable to fetch CVE data. Status code: {response.status_code}")
|
||||
print("Response content:", response.content)
|
||||
return None
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error: Unable to fetch CVE data. {e}")
|
||||
return None
|
||||
|
||||
def extract_cvss_data(cve_entry):
|
||||
cvss_data = cve_entry.get("impact", {}).get("baseMetricV2", {}).get("cvssV2", {})
|
||||
if cvss_data:
|
||||
return {
|
||||
"CVSS Version": "2.0",
|
||||
"Vector String": cvss_data.get("vectorString", ""),
|
||||
"Base Score": cvss_data.get("baseScore", ""),
|
||||
"Base Severity": cvss_data.get("severity", ""),
|
||||
"Access Vector": cvss_data.get("accessVector", ""),
|
||||
"Access Complexity": cvss_data.get("accessComplexity", ""),
|
||||
"Authentication": cvss_data.get("authentication", ""),
|
||||
"Confidentiality Impact": cvss_data.get("confidentialityImpact", ""),
|
||||
"Integrity Impact": cvss_data.get("integrityImpact", ""),
|
||||
"Availability Impact": cvss_data.get("availabilityImpact", ""),
|
||||
"Exploitability Score": cve_entry.get("exploitabilityScore", ""),
|
||||
"Impact Score": cve_entry.get("impactScore", "")
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"CVSS Version": "",
|
||||
"Vector String": "",
|
||||
"Base Score": "",
|
||||
"Base Severity": "",
|
||||
"Access Vector": "",
|
||||
"Access Complexity": "",
|
||||
"Authentication": "",
|
||||
"Confidentiality Impact": "",
|
||||
"Integrity Impact": "",
|
||||
"Availability Impact": "",
|
||||
"Exploitability Score": "",
|
||||
"Impact Score": ""
|
||||
}
|
||||
|
||||
def generate_excel_report(cves, excel_filename):
|
||||
try:
|
||||
data = []
|
||||
for vulnerability_entry in cves.get("result", {}).get("CVE_Items", []):
|
||||
cve_entry = vulnerability_entry.get("cve", {})
|
||||
cve_id = cve_entry.get("CVE_data_meta", {}).get("ID", "")
|
||||
descriptions = cve_entry.get("description", {}).get("description_data", [])
|
||||
english_descriptions = [desc.get("value", "") for desc in descriptions if desc.get("lang") == "en"]
|
||||
description = " ".join(english_descriptions)
|
||||
references = cve_entry.get("references", {}).get("reference_data", [])
|
||||
reference_urls = [ref.get("url", "") for ref in references]
|
||||
reference_sources = [ref.get("name", "") for ref in references]
|
||||
reference_url = reference_urls[0] if reference_urls else ""
|
||||
reference_source = reference_sources[0] if reference_sources else ""
|
||||
cvss_data = extract_cvss_data(vulnerability_entry)
|
||||
data.append({
|
||||
"CVE ID": cve_id,
|
||||
"Description": description,
|
||||
"Reference URL": reference_url,
|
||||
"Reference Source": reference_source,
|
||||
**cvss_data
|
||||
})
|
||||
df = pd.DataFrame(data)
|
||||
df.to_excel(excel_filename, index=False)
|
||||
print(f"Excel report generated successfully: {excel_filename}")
|
||||
except Exception as e:
|
||||
print(f"Error: Unable to generate Excel report. {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
latest_cves = get_latest_cves()
|
||||
if latest_cves:
|
||||
excel_timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
excel_filename = f"latest_cves_{excel_timestamp}.xlsx"
|
||||
generate_excel_report(latest_cves, excel_filename)
|
||||
else:
|
||||
print("No CVEs found.")
|
||||
@@ -0,0 +1,121 @@
|
||||
import requests
|
||||
import json
|
||||
import csv
|
||||
import datetime
|
||||
import os
|
||||
import time
|
||||
|
||||
def get_nist_vulnerabilities(days=3, limit=5000): # Added limit parameter
|
||||
"""Retrieves the latest vulnerabilities from the NIST NVD API v2.0 with a limit."""
|
||||
|
||||
end_date = datetime.date.today()
|
||||
start_date = end_date - datetime.timedelta(days=days)
|
||||
|
||||
all_vulnerabilities = []
|
||||
startIndex = 0
|
||||
resultsPerPage = 2000
|
||||
|
||||
while True:
|
||||
url = f"https://services.nvd.nist.gov/rest/json/cves/2.0?startIndex={startIndex}&resultsPerPage={resultsPerPage}"
|
||||
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
vulnerabilities = data.get("vulnerabilities", [])
|
||||
|
||||
if not vulnerabilities:
|
||||
break
|
||||
|
||||
all_vulnerabilities.extend(vulnerabilities)
|
||||
startIndex += resultsPerPage
|
||||
time.sleep(0.5) # Reduced sleep time slightly
|
||||
|
||||
if len(all_vulnerabilities) >= limit: #Enforce limit
|
||||
print(f"Reached CVE limit of {limit}. Stopping retrieval.")
|
||||
break
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching data from NIST API: {e}")
|
||||
return []
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Error decoding JSON response: {e}")
|
||||
return []
|
||||
|
||||
filtered_vulnerabilities = [
|
||||
cve for cve in all_vulnerabilities
|
||||
if datetime.datetime.fromisoformat(cve.get('cve', {}).get('published', '').replace('Z', '+00:00')).date() >= start_date
|
||||
]
|
||||
return filtered_vulnerabilities
|
||||
|
||||
|
||||
|
||||
def write_to_csv(vulnerabilities, filename):
|
||||
# ... (This function is exactly the same as in my previous responses)
|
||||
if not vulnerabilities:
|
||||
print("No vulnerabilities found for the specified period.")
|
||||
return
|
||||
|
||||
fieldnames = ["Product", "Vendor", "CVE ID", "Date Added", "Date Updated", "Description", "Severity"]
|
||||
|
||||
try:
|
||||
with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
|
||||
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
||||
writer.writeheader()
|
||||
for cve in vulnerabilities:
|
||||
cve_data = cve.get('cve', {})
|
||||
|
||||
products = []
|
||||
vendors = []
|
||||
|
||||
if 'cpeMatch' in cve_data.get('configurations', {}):
|
||||
for match in cve_data['configurations']['cpeMatch']:
|
||||
cpe_string = match.get('cpe23Uri', '')
|
||||
parts = cpe_string.split(':')
|
||||
if len(parts) >= 5:
|
||||
vendors.append(parts[3])
|
||||
products.append(parts[4])
|
||||
else:
|
||||
vendors.append("N/A")
|
||||
products.append("N/A")
|
||||
|
||||
product_str = ", ".join(products)
|
||||
vendor_str = ", ".join(vendors)
|
||||
|
||||
description = cve_data.get('descriptions', [{}])[0].get('value', 'N/A')
|
||||
published_date = cve_data.get('published', 'N/A')
|
||||
last_modified_date = cve_data.get('lastModified', 'N/A')
|
||||
cve_id = cve_data.get('id', 'N/A')
|
||||
|
||||
severity = 'N/A'
|
||||
metrics = cve_data.get('metrics', {})
|
||||
if 'cvssMetricV31' in metrics:
|
||||
severity = metrics['cvssMetricV31'][0].get('cvssData', {}).get('baseSeverity', 'N/A')
|
||||
elif 'cvssMetricV30' in metrics:
|
||||
severity = metrics['cvssMetricV30'][0].get('cvssData', {}).get('baseSeverity', 'N/A')
|
||||
elif 'cvssMetricV2' in metrics:
|
||||
severity = metrics['cvssMetricV2'][0].get('cvssData', {}).get('baseSeverity', 'N/A')
|
||||
|
||||
writer.writerow({
|
||||
"Product": product_str,
|
||||
"Vendor": vendor_str,
|
||||
"CVE ID": cve_id,
|
||||
"Date Added": published_date,
|
||||
"Date Updated": last_modified_date,
|
||||
"Description": description,
|
||||
"Severity": severity
|
||||
})
|
||||
print(f"Vulnerabilities written to {filename}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error writing to CSV: {e}")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
vulnerabilities = get_nist_vulnerabilities()
|
||||
|
||||
if vulnerabilities:
|
||||
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
filename = f"nist_vulnerabilities_{timestamp}.csv"
|
||||
write_to_csv(vulnerabilities, filename)
|
||||
Reference in New Issue
Block a user