Files
2026-07-13 12:12:34 +02:00

111 lines
3.0 KiB
Python

import json, os, sys, ssl, datetime, urllib.parse, urllib.request, urllib.error
BASE = "https://services.nvd.nist.gov/rest/json/cves/2.0"
def _cfg():
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
def _inputs():
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
def _ctx(cfg):
if cfg.get("insecure"):
c = ssl.create_default_context()
c.check_hostname = False
c.verify_mode = ssl.CERT_NONE
return c
return None
def _headers(cfg):
h = {"User-Agent": "Riposte-SOAR", "Accept": "application/json"}
k = cfg.get("api_key")
if k:
h["apiKey"] = str(k)
return h
def _get(url, cfg):
req = urllib.request.Request(url, headers=_headers(cfg))
with urllib.request.urlopen(req, timeout=120, context=_ctx(cfg)) as r:
return r.read()
def _run(fn):
try:
print(json.dumps(fn(_cfg(), _inputs())))
except urllib.error.HTTPError as e:
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
sys.exit(1)
except Exception as e:
print(json.dumps({"error": str(e)}))
sys.exit(1)
def _cvss(metrics):
for key in ("cvssMetricV31", "cvssMetricV30", "cvssMetricV2"):
arr = metrics.get(key)
if arr and isinstance(arr, list) and isinstance(arr[0], dict):
d = arr[0].get("cvssData", {})
return d.get("baseScore"), d.get("baseSeverity") or arr[0].get("baseSeverity")
return None, None
def main(cfg, inputs):
days = int(inputs.get("days") or cfg.get("days") or 7)
days = max(1, min(days, 120))
end = datetime.datetime.now(datetime.timezone.utc)
start = end - datetime.timedelta(days=days)
fmt = "%Y-%m-%dT%H:%M:%S.000"
per = int(inputs.get("max_indicators") or 0) or 2000
per = max(1, min(per, 2000))
params = {
"pubStartDate": start.strftime(fmt),
"pubEndDate": end.strftime(fmt),
"resultsPerPage": per,
}
url = BASE + "?" + urllib.parse.urlencode(params)
raw = _get(url, cfg)
data = json.loads(raw) if raw else {}
maxn = int(inputs.get("max_indicators") or 0)
out = []
for v in data.get("vulnerabilities", []):
cve = v.get("cve", {}) if isinstance(v, dict) else {}
cid = cve.get("id")
if not cid:
continue
desc = ""
for d in cve.get("descriptions", []):
if d.get("lang") == "en":
desc = d.get("value")
break
score, severity = _cvss(cve.get("metrics", {}))
out.append({
"value": cid,
"type": "cve",
"description": desc,
"cvss": score,
"severity": severity,
"published": cve.get("published"),
"status": cve.get("vulnStatus"),
})
if maxn and len(out) >= maxn:
break
return {
"source": "nvd:recent",
"count": len(out),
"indicators": out,
"total_results": data.get("totalResults"),
}
_run(main)