File size: 15,202 Bytes
51b23af
 
 
 
 
 
 
 
 
 
 
5d65b68
 
51b23af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d65b68
51b23af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d65b68
 
 
 
 
 
51b23af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""CVE Explainer / Summarizer.

Enter a CVE ID (e.g. CVE-2024-3094) or paste a raw vulnerability description.
Fetches public CVE metadata (CIRCL / NVD-style JSON) and produces a plain-English
summary, risk level, remediation steps and suggested MITRE ATT&CK techniques.
Uses Hugging Face Inference Providers when a hub token is available; otherwise a
structured fallback built from CVSS/CWE fields.
"""

from __future__ import annotations

import spaces  # ZeroGPU Space: required before other heavy imports

import json
import os
import re
from typing import Any

import gradio as gr
import requests

CVE_RE = re.compile(r"CVE-\d{4}-\d{4,7}", re.I)
CIRCL = "https://cve.circl.lu/api/cve/{cve_id}"
NVD = "https://services.nvd.nist.gov/rest/json/cvds/2.0?cveId={cve_id}"

LLM = os.environ.get("CVE_LLM", "Qwen/Qwen2.5-7B-Instruct")
PROVIDER = os.environ.get("CVE_INFERENCE_PROVIDER", "featherless-ai")


def _token() -> str | None:
    return os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")


def parse_cve_id(text: str) -> str | None:
    m = CVE_RE.search(text.strip())
    return m.group(0).upper() if m else None


def fetch_circl(cve_id: str) -> dict[str, Any]:
    r = requests.get(CIRCL.format(cve_id=cve_id), timeout=30)
    r.raise_for_status()
    return r.json()


def fetch_nvd(cve_id: str) -> dict[str, Any] | None:
    try:
        r = requests.get(NVD.format(cve_id=cve_id), timeout=45, headers={"Accept": "application/json"})
        if r.status_code != 200:
            return None
        data = r.json()
        if not data.get("vulnerabilities"):
            return None
        return data["vulnerabilities"][0]["cve"]
    except Exception:
        return None


def cvss_from_record(rec: dict[str, Any]) -> tuple[float | None, str, dict[str, Any]]:
    """Return (score, severity_label, metric_dict)."""
    best_score, best_sev, best = None, "UNKNOWN", {}
    containers = rec.get("containers") or {}
    for block in (containers.get("cna"), *(containers.get("adp") or [])):
        if not block:
            continue
        for m in block.get("metrics") or []:
            for key in ("cvssV3_1", "cvssV3_0", "cvssV4_0"):
                if key in m:
                    d = m[key]
                    sc = float(d.get("baseScore", 0))
                    sev = str(d.get("baseSeverity", "UNKNOWN"))
                    if best_score is None or sc > best_score:
                        best_score, best_sev, best = sc, sev, d
    return best_score, best_sev, best


def descriptions_from_record(rec: dict[str, Any]) -> list[str]:
    out: list[str] = []
    cna = (rec.get("containers") or {}).get("cna") or {}
    for d in cna.get("descriptions") or []:
        if d.get("lang", "en") == "en" and d.get("value"):
            out.append(d["value"])
    nvd = rec.get("_nvd") or {}
    for d in nvd.get("descriptions") or []:
        if d.get("lang") == "en" and d.get("value"):
            out.append(d["value"])
    return out


def cwes_from_record(rec: dict[str, Any]) -> list[str]:
    ids: list[str] = []
    cna = (rec.get("containers") or {}).get("cna") or {}
    for p in cna.get("problemTypes") or []:
        for d in p.get("descriptions") or []:
            if d.get("cweId"):
                ids.append(d["cweId"])
            elif d.get("description"):
                ids.append(d["description"])
    nvd = rec.get("_nvd") or {}
    for w in nvd.get("weaknesses") or []:
        for d in w.get("description") or []:
            if d.get("value", "").startswith("CWE-"):
                ids.append(d["value"])
    return list(dict.fromkeys(ids))


def references_from_record(rec: dict[str, Any]) -> list[str]:
    refs: list[str] = []
    cna = (rec.get("containers") or {}).get("cna") or {}
    for ref in cna.get("references") or []:
        u = ref.get("url")
        if u:
            refs.append(u)
    nvd = rec.get("_nvd") or {}
    for ref in nvd.get("references") or []:
        u = ref.get("url")
        if u:
            refs.append(u)
    return list(dict.fromkeys(refs))[:12]


def risk_band(score: float | None, severity: str) -> str:
    if score is not None:
        if score >= 9.0:
            return "Critical"
        if score >= 7.0:
            return "High"
        if score >= 4.0:
            return "Medium"
        return "Low"
    sev = severity.upper()
    if "CRIT" in sev:
        return "Critical"
    if "HIGH" in sev:
        return "High"
    if "MED" in sev:
        return "Medium"
    if "LOW" in sev:
        return "Low"
    return "Unknown"


def cwe_mitre_hints(cwes: list[str], description: str) -> list[str]:
    text = " ".join(cwes) + " " + description.lower()
    hints: list[tuple[str, str]] = [
        (r"remote code execution|\brce\b|arbitrary code", "T1190 – Exploit Public-Facing Application"),
        (r"privilege escalation|elevation of privilege", "T1068 – Exploitation for Privilege Escalation"),
        (r"sql injection", "T1190 – Exploit Public-Facing Application (SQLi)"),
        (r"cross[- ]site scripting|\bxss\b", "T1189 – Drive-by Compromise (XSS delivery)"),
        (r"phishing|social engineering", "T1566 – Phishing"),
        (r"credential|password|authentication bypass", "T1078 – Valid Accounts"),
        (r"lateral movement", "T1021 – Remote Services"),
        (r"command injection|shell", "T1059 – Command and Scripting Interpreter"),
        (r"backdoor|supply chain|malicious code", "T1195 – Supply Chain Compromise"),
        (r"denial of service|\bdos\b", "T1499 – Endpoint Denial of Service"),
        (r"memory corruption|buffer overflow|use after free", "T1203 – Exploitation for Client Execution"),
    ]
    found = []
    for pat, tid in hints:
        if re.search(pat, text, re.I):
            found.append(tid)
    return list(dict.fromkeys(found))[:6]


def build_context(cve_id: str | None, raw: str, rec: dict[str, Any] | None) -> dict[str, Any]:
    if rec:
        score, sev, metrics = cvss_from_record(rec)
        descs = descriptions_from_record(rec)
        desc = descs[0] if descs else raw or "(no description in feed)"
        cwes = cwes_from_record(rec)
        refs = references_from_record(rec)
        title = ((rec.get("containers") or {}).get("cna") or {}).get("title") or cve_id
        published = (rec.get("cveMetadata") or {}).get("datePublished", "")
    else:
        score, sev, metrics, cwes, refs, title, published = None, "UNKNOWN", {}, [], [], cve_id or "Custom text", ""
        desc = raw.strip()
    band = risk_band(score, sev)
    mitre = cwe_mitre_hints(cwes, desc)
    return {
        "cve_id": cve_id or "N/A",
        "title": title,
        "published": published,
        "description": desc,
        "cvss_score": score,
        "cvss_severity": sev,
        "cvss_vector": metrics.get("vectorString"),
        "risk_band": band,
        "cwes": cwes,
        "references": refs,
        "mitre_hints": mitre,
        "attack_vector": metrics.get("attackVector"),
        "attack_complexity": metrics.get("attackComplexity"),
        "privileges_required": metrics.get("privilegesRequired"),
        "user_interaction": metrics.get("userInteraction"),
    }


def llm_explain(ctx: dict[str, Any]) -> str | None:
    token = _token()
    if not token:
        return None
    try:
        from huggingface_hub import InferenceClient
    except ImportError:
        return None
    prompt = f"""You are a senior vulnerability analyst writing for IT and security teams.

CVE: {ctx['cve_id']}
Title: {ctx['title']}
CVSS: {ctx['cvss_score']} ({ctx['cvss_severity']}) β€” overall risk band: {ctx['risk_band']}
CWEs: {', '.join(ctx['cwes']) or 'none listed'}
Official description:
{ctx['description'][:3500]}

Write a concise report with these sections (use markdown headings):
## Plain-English summary
(2-4 sentences, no jargon)
## Risk level
(why it is {ctx['risk_band']}; mention exploitability if known)
## Remediation steps
(numbered, practical: patch, config, detection, workarounds)
## MITRE ATT&CK mapping
(list 2-5 relevant technique IDs like T1190 with one-line justification each; include these hints if relevant: {', '.join(ctx['mitre_hints']) or 'infer from description'})

Be accurate. If something is unknown, say so. Do not invent CVE details not present above."""
    client = InferenceClient(token=token, provider=PROVIDER)
    try:
        out = client.chat_completion(
            model=LLM,
            messages=[{"role": "user", "content": prompt}],
            max_tokens=900,
            temperature=0.2,
        )
        return out.choices[0].message.content
    except Exception:
        try:
            out = client.chat_completion(
                model=LLM,
                messages=[{"role": "user", "content": prompt}],
                max_tokens=900,
                temperature=0.2,
            )
            return out.choices[0].message.content
        except Exception:
            return None


def fallback_report(ctx: dict[str, Any]) -> str:
    lines = [
        f"## Plain-English summary\n{ctx['description'][:1200]}",
        f"\n## Risk level\n**{ctx['risk_band']}**",
    ]
    if ctx["cvss_score"] is not None:
        lines.append(f"- CVSS base score: **{ctx['cvss_score']}** ({ctx['cvss_severity']})")
    if ctx.get("cvss_vector"):
        lines.append(f"- Vector: `{ctx['cvss_vector']}`")
    if ctx.get("attack_vector"):
        lines.append(f"- Attack vector: {ctx['attack_vector']} Β· Complexity: {ctx.get('attack_complexity')} Β· Privileges: {ctx.get('privileges_required')} Β· User interaction: {ctx.get('user_interaction')}")
    lines.append("\n## Remediation steps")
    steps = [
        "1. Confirm affected products/versions in your environment against vendor advisories.",
        "2. Apply the vendor patch or upgrade to a fixed release as soon as possible.",
        "3. If no patch exists, implement compensating controls (WAF rules, disable vulnerable feature, network segmentation).",
        "4. Hunt for exploitation indicators in logs (unexpected child processes, suspicious outbound connections, auth anomalies).",
        "5. Document exceptions and track residual risk until fully remediated.",
    ]
    if "supply chain" in ctx["description"].lower() or "backdoor" in ctx["description"].lower():
        steps.insert(2, "2b. For supply-chain issues: rebuild from trusted sources, rotate secrets, verify package signatures and SBOMs.")
    lines.extend(steps)
    lines.append("\n## MITRE ATT&CK mapping (heuristic)")
    if ctx["mitre_hints"]:
        lines.extend(f"- {h}" for h in ctx["mitre_hints"])
    else:
        lines.append("- No strong keyword match β€” review the description with your CTI team.")
    if ctx["cwes"]:
        lines.append(f"\n**CWE references:** {', '.join(ctx['cwes'])}")
    if ctx["references"]:
        lines.append("\n**References:**")
        lines.extend(f"- {u}" for u in ctx["references"][:6])
    lines.append("\n<sub>Generated from public CVE metadata (no LLM). Add `HF_TOKEN` Space secret for AI-expanded remediation.</sub>")
    return "\n".join(lines)


def _explain(cve_input: str, raw_description: str, use_llm: bool):
    cve_input = (cve_input or "").strip()
    raw_description = (raw_description or "").strip()
    cve_id = parse_cve_id(cve_input) or parse_cve_id(raw_description)
    if not cve_id and not raw_description and not cve_input:
        raise gr.Error("Enter a CVE ID (e.g. CVE-2024-3094) and/or paste a description.")

    rec: dict[str, Any] | None = None
    meta_line = ""
    if cve_id:
        try:
            rec = fetch_circl(cve_id)
            nvd = fetch_nvd(cve_id)
            if nvd:
                rec["_nvd"] = nvd
            meta_line = f"Loaded **{cve_id}** from public CVE feeds."
        except requests.HTTPError as e:
            if e.response is not None and e.response.status_code == 404:
                raise gr.Error(f"{cve_id} was not found in the CIRCL feed.")
            raise gr.Error(f"Could not fetch {cve_id}: {e}") from e
        except requests.RequestException as e:
            raise gr.Error(f"Network error fetching {cve_id}: {e}") from e
    elif cve_input and not raw_description:
        raw_description = cve_input

    ctx = build_context(cve_id, raw_description or cve_input, rec)
    report = None
    if use_llm:
        report = llm_explain(ctx)
    if not report:
        report = fallback_report(ctx)
        if use_llm and not _token():
            meta_line += " (LLM disabled: set Space secret `HF_TOKEN` with inference scope.)"
        elif use_llm:
            meta_line += " (LLM call failed β€” showing structured fallback.)"

    header = f"# {ctx['cve_id']}: {ctx['title']}\n"
    if ctx["published"]:
        header += f"*Published: {ctx['published'][:10]}*\n\n"
    header += meta_line + "\n\n"
    risk_md = f"**{ctx['risk_band']}**" + (f" (CVSS {ctx['cvss_score']})" if ctx["cvss_score"] is not None else "")
    json_out = json.dumps({k: v for k, v in ctx.items() if k != "description" or len(str(v)) < 500}, indent=2)
    return risk_md, header + report, json_out


@spaces.GPU(duration=90)
def explain(cve_input: str, raw_description: str, use_llm: bool):
    """Gradio entrypoint (ZeroGPU worker slot; work is mostly HTTP + optional Inference API)."""
    return _explain(cve_input, raw_description, use_llm)


EXAMPLES = [
    ["CVE-2024-3094", "", True],
    ["CVE-2021-44228", "", True],
    ["", "A buffer overflow in the XYZ VPN client allows remote attackers to execute arbitrary code via a crafted UDP packet. CVSS 9.8.", False],
]

with gr.Blocks(title="CVE Explainer") as demo:
    with gr.Column(elem_id="col"):
        gr.Markdown(
            "# πŸ›‘οΈ CVE Explainer / Summarizer\n"
            "Enter a **CVE ID** or paste a **raw vulnerability description** β†’ plain-English summary, "
            "**risk level**, **remediation steps**, and **MITRE ATT&CK** mapping.\n\n"
            "Data: [CIRCL CVE API](https://cve.circl.lu/) (+ NVD when available). "
            f"Optional AI summary via Inference Providers (`{LLM}`)."
        )
        with gr.Row():
            with gr.Column():
                cve = gr.Textbox(label="CVE ID", placeholder="CVE-2024-3094")
                raw = gr.Textbox(label="Or paste description / advisory text", lines=6, placeholder="Optional if CVE ID is set…")
                use_llm = gr.Checkbox(value=True, label="Expand with AI (requires HF_TOKEN secret on the Space)")
                btn = gr.Button("Explain", variant="primary")
            with gr.Column():
                risk = gr.Textbox(label="Risk level", interactive=False)
                out = gr.Markdown(label="Report")
        meta = gr.Code(label="Structured metadata (JSON)", language="json")
        gr.Examples(EXAMPLES, inputs=[cve, raw, use_llm], outputs=[risk, out, meta], fn=explain, cache_examples=False)
        gr.Markdown("⚠️ Advisory only β€” validate patches and scope with vendor bulletins and your asset inventory.")
    btn.click(explain, [cve, raw, use_llm], [risk, out, meta], api_name="explain")

if __name__ == "__main__":
    demo.queue(max_size=16).launch(theme=gr.themes.Soft(), css="#col {max-width: 960px; margin: 0 auto;}")