-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsatei.py
More file actions
410 lines (340 loc) · 11.8 KB
/
Copy pathsatei.py
File metadata and controls
410 lines (340 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
from __future__ import annotations
import re
from typing import Any, Optional
import requests
import typer
from rich import box
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
import json
import re
from typing import Any, Optional
app = typer.Typer(
help="SATEI - CVE prioritization CLI",
add_completion=False,
no_args_is_help=True,
)
console = Console()
VERSION = "0.1.0"
NVD_CVE_API_URL = "https://services.nvd.nist.gov/rest/json/cves/2.0"
EPSS_API_URL = "https://api.first.org/data/v1/epss"
CISA_KEV_JSON_URL = "https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json"
DEFAULT_HEADERS = {
"User-Agent": "SATEI/0.1 (+https://github.com/your-user/satei)",
"Accept": "application/json",
}
CVE_PATTERN = re.compile(r"^CVE-\d{4}-\d{4,}$", re.IGNORECASE)
def validate_cve(value: str) -> str:
cve_id = value.strip().upper()
if not CVE_PATTERN.match(cve_id):
raise typer.BadParameter("Introduce un CVE válido, por ejemplo: CVE-2024-3400")
return cve_id
def safe_get(url: str, *, params: Optional[dict[str, Any]] = None) -> requests.Response:
response = requests.get(
url,
params=params,
headers=DEFAULT_HEADERS,
timeout=20,
)
response.raise_for_status()
return response
def fetch_nvd_cve(cve_id: str) -> dict[str, Any]:
response = safe_get(NVD_CVE_API_URL, params={"cveId": cve_id})
payload = response.json()
vulnerabilities = payload.get("vulnerabilities", [])
if not vulnerabilities:
raise ValueError(f"No se encontró información en NVD para {cve_id}")
return vulnerabilities[0].get("cve", {})
def fetch_epss(cve_id: str) -> Optional[dict[str, Any]]:
response = safe_get(EPSS_API_URL, params={"cve": cve_id})
payload = response.json()
data = payload.get("data", [])
if not data:
return None
return data[0]
def fetch_kev_entry(cve_id: str) -> Optional[dict[str, Any]]:
response = safe_get(CISA_KEV_JSON_URL)
payload = response.json()
vulnerabilities = payload.get("vulnerabilities", [])
for item in vulnerabilities:
if str(item.get("cveID", "")).upper() == cve_id.upper():
return item
return None
def extract_description(cve: dict[str, Any]) -> str:
descriptions = cve.get("descriptions", [])
for item in descriptions:
if item.get("lang") == "en":
return item.get("value", "No description available.")
if descriptions:
return descriptions[0].get("value", "No description available.")
return "No description available."
def extract_cvss(cve: dict[str, Any]) -> dict[str, Any]:
metrics = cve.get("metrics", {})
candidates = [
("cvssMetricV40", "4.0"),
("cvssMetricV31", "3.1"),
("cvssMetricV30", "3.0"),
("cvssMetricV2", "2.0"),
]
for key, version in candidates:
entries = metrics.get(key)
if not entries:
continue
preferred = next((entry for entry in entries if entry.get("type") == "Primary"), entries[0])
cvss_data = preferred.get("cvssData", {})
return {
"version": version,
"base_score": cvss_data.get("baseScore"),
"severity": cvss_data.get("baseSeverity") or preferred.get("baseSeverity"),
"vector": cvss_data.get("vectorString"),
"source": preferred.get("source"),
}
return {
"version": None,
"base_score": None,
"severity": None,
"vector": None,
"source": None,
}
def calculate_priority(
*,
cvss_score: Optional[float],
epss_score: Optional[float],
in_kev: bool,
) -> tuple[str, str]:
if in_kev:
return "critical", "Incluida en CISA KEV (explotación conocida en el mundo real)"
if epss_score is not None and epss_score >= 0.70:
return "high", "EPSS alto"
if cvss_score is not None and cvss_score >= 9.0:
return "high", "CVSS muy alto"
if epss_score is not None and epss_score >= 0.30:
return "medium", "EPSS moderado"
if cvss_score is not None and cvss_score >= 7.0:
return "medium", "CVSS alto"
return "low", "Sin señales fuertes de priorización"
def build_output_payload(
*,
cve_id: str,
priority: str,
reason: str,
nvd_cve: dict[str, Any],
description: str,
cvss: dict[str, Any],
epss_data: Optional[dict[str, Any]],
kev_entry: Optional[dict[str, Any]],
) -> dict[str, Any]:
return {
"tool": "SATEI",
"version": VERSION,
"query": {
"type": "cve",
"value": cve_id,
},
"verdict": {
"priority": priority,
"reason": reason,
},
"nvd": {
"id": nvd_cve.get("id"),
"status": nvd_cve.get("vulnStatus"),
"published": nvd_cve.get("published"),
"last_modified": nvd_cve.get("lastModified"),
"description": description,
"cvss": {
"version": cvss.get("version"),
"base_score": cvss.get("base_score"),
"severity": cvss.get("severity"),
"vector": cvss.get("vector"),
"source": cvss.get("source"),
},
},
"epss": {
"score": epss_data.get("epss") if epss_data else None,
"percentile": epss_data.get("percentile") if epss_data else None,
"date": (
epss_data.get("date", epss_data.get("created"))
if epss_data
else None
),
},
"kev": {
"in_kev": bool(kev_entry),
"vendor": kev_entry.get("vendorProject") if kev_entry else None,
"product": kev_entry.get("product") if kev_entry else None,
"date_added": kev_entry.get("dateAdded") if kev_entry else None,
"due_date": kev_entry.get("dueDate") if kev_entry else None,
"known_ransomware_use": (
kev_entry.get("knownRansomwareCampaignUse")
if kev_entry
else None
),
"notes": kev_entry.get("notes") if kev_entry else None,
},
}
def make_summary_table(
cve_id: str,
priority: str,
reason: str,
vuln_status: Optional[str],
published: Optional[str],
last_modified: Optional[str],
) -> Table:
table = Table(title="SATEI Verdict", box=box.ROUNDED, show_lines=False)
table.add_column("Campo", style="cyan", no_wrap=True)
table.add_column("Valor", style="white")
table.add_row("CVE", cve_id)
table.add_row("Prioridad", priority.upper())
table.add_row("Motivo", reason)
table.add_row("Estado", str(vuln_status or "-"))
table.add_row("Publicado", str(published or "-"))
table.add_row("Última modificación", str(last_modified or "-"))
return table
def make_scoring_table(
cvss: dict[str, Any],
epss: Optional[dict[str, Any]],
kev: Optional[dict[str, Any]],
) -> Table:
table = Table(title="Risk Signals", box=box.ROUNDED, show_lines=False)
table.add_column("Campo", style="cyan", no_wrap=True)
table.add_column("Valor", style="white")
table.add_row("CVSS version", str(cvss.get("version") or "-"))
table.add_row("CVSS base score", str(cvss.get("base_score") or "-"))
table.add_row("CVSS severity", str(cvss.get("severity") or "-"))
table.add_row("CVSS vector", str(cvss.get("vector") or "-"))
if epss:
table.add_row("EPSS", str(epss.get("epss", "-")))
table.add_row("EPSS percentile", str(epss.get("percentile", "-")))
table.add_row("EPSS date", str(epss.get("date", epss.get("created", "-"))))
else:
table.add_row("EPSS", "No data")
table.add_row("In CISA KEV", "True" if kev else "False")
if kev:
table.add_row("KEV vendor", str(kev.get("vendorProject", "-")))
table.add_row("KEV product", str(kev.get("product", "-")))
table.add_row("KEV ransomware", str(kev.get("knownRansomwareCampaignUse", "-")))
table.add_row("KEV date added", str(kev.get("dateAdded", "-")))
table.add_row("KEV due date", str(kev.get("dueDate", "-")))
return table
def make_description_panel(description: str) -> Panel:
return Panel.fit(
description,
title="Description",
border_style="cyan",
)
@app.command()
def version() -> None:
"""Muestra la versión de SATEI."""
console.print(f"[bold cyan]SATEI[/bold cyan] v{VERSION}")
@app.command()
def cve(
value: str = typer.Argument(..., help="CVE a priorizar, por ejemplo CVE-2024-3400"),
json_output: bool = typer.Option(
False,
"--json",
help="Muestra la salida en formato JSON.",
),
) -> None:
"""Prioriza un CVE combinando NVD, EPSS y CISA KEV."""
cve_id = validate_cve(value)
if not json_output:
console.print(
Panel.fit(
f"[bold white]SATEI[/bold white] → analizando [cyan]{cve_id}[/cyan]",
border_style="cyan",
)
)
try:
nvd_cve = fetch_nvd_cve(cve_id)
except requests.RequestException as exc:
if json_output:
typer.echo(
json.dumps(
{
"tool": "SATEI",
"version": VERSION,
"query": {"type": "cve", "value": cve_id},
"error": f"Error consultando NVD: {exc}",
},
indent=2,
ensure_ascii=False,
)
)
else:
console.print(f"[red]Error consultando NVD:[/red] {exc}")
raise typer.Exit(code=1)
except ValueError as exc:
if json_output:
typer.echo(
json.dumps(
{
"tool": "SATEI",
"version": VERSION,
"query": {"type": "cve", "value": cve_id},
"error": str(exc),
},
indent=2,
ensure_ascii=False,
)
)
else:
console.print(f"[red]{exc}[/red]")
raise typer.Exit(code=1)
try:
epss_data = fetch_epss(cve_id)
except requests.RequestException:
epss_data = None
try:
kev_entry = fetch_kev_entry(cve_id)
except requests.RequestException:
kev_entry = None
description = extract_description(nvd_cve)
cvss = extract_cvss(nvd_cve)
cvss_score = cvss.get("base_score")
if cvss_score is not None:
try:
cvss_score = float(cvss_score)
except (TypeError, ValueError):
cvss_score = None
epss_score = None
if epss_data and epss_data.get("epss") is not None:
try:
epss_score = float(epss_data["epss"])
except (TypeError, ValueError):
epss_score = None
priority, reason = calculate_priority(
cvss_score=cvss_score,
epss_score=epss_score,
in_kev=bool(kev_entry),
)
payload = build_output_payload(
cve_id=cve_id,
priority=priority,
reason=reason,
nvd_cve=nvd_cve,
description=description,
cvss=cvss,
epss_data=epss_data,
kev_entry=kev_entry,
)
if json_output:
typer.echo(json.dumps(payload, indent=2, ensure_ascii=False))
return
console.print()
console.print(
make_summary_table(
cve_id=cve_id,
priority=priority,
reason=reason,
vuln_status=nvd_cve.get("vulnStatus"),
published=nvd_cve.get("published"),
last_modified=nvd_cve.get("lastModified"),
)
)
console.print()
console.print(make_scoring_table(cvss=cvss, epss=epss_data, kev=kev_entry))
console.print()
console.print(make_description_panel(description))
if __name__ == "__main__":
app()