#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Zero-Day Intelligence Module - NCRC Threat Register (Tier-1, OEM & Priority Hardware)

Main fixes vs. previous version:
  * WORD-BOUNDARY matching. Before, "ecu" matched "s-ecu-re", "ivi" matched "pr-ivi-lege",
    "lear" matched "c-lear", "mini" matched "administrator", "ota" matched "quota",
    "ble" (Bluetooth surface) matched "enable/table/possible", "api" matched "capital" ...
  * Matching only on description + CPE vendor/product (not json.dumps of the whole record,
    which included reference URLs, hashes, etc.)
  * Ambiguous terms (bosch, gateway, diagnostic, uds, ford, mini ...) need vehicle context
  * CPE vendor is used as strong signal (cpe:2.3:o:tesla:...)
  * Date filter was silently broken: NVD dates are naive, comparison with an aware datetime
    raised TypeError, which the bare except swallowed -> filter never applied
  * CVE-ID year filter removed (CVE-2023-xxxx published in 2024 was dropped); published date is used
  * CVSS v4.0 supported; Primary (NVD) metric preferred
  * Rejected CVEs skipped; CVEs still awaiting NVD scoring are kept (flagged), not dropped
  * Surfaces detected from the description only (not from the search keyword)
"""

import re
from datetime import datetime, timezone, timedelta

from feeds_automotive import collect_all_feeds

LOOKBACK_DAYS = 730            # published within last 2 years
PENDING_WINDOW_DAYS = 120      # unscored CVEs younger than this are kept
MIN_SCORE_PRIORITY = 4.0       # hardware / keyless
MIN_SCORE_DEFAULT = 7.0


def _rx(terms):
    parts = sorted({t.lower() for t in terms}, key=len, reverse=True)
    return re.compile(r"(?<![a-z0-9])(?:" + "|".join(re.escape(p) for p in parts) + r")(?![a-z0-9])",
                      re.I)


def _hits(rx, text):
    return sorted({m.group(0).lower() for m in rx.finditer(text)})


# ---------------------------------------------------------
# TERM LISTS
# ---------------------------------------------------------
EXCLUDE_PRODUCTS = ["fleetdm", "kibana", "elastic agent", "suse rancher",
                    "windows mdm", "apple mdm"]

OEM_STRONG = [
    "audi", "volkswagen", "vw", "porsche", "skoda", "tesla", "bmw", "mercedes",
    "mercedes-benz", "daimler", "toyota", "lexus", "chevrolet", "cadillac", "hyundai",
    "kia", "honda", "nissan", "volvo", "polestar", "renault", "stellantis", "jeep",
    "rivian", "byd", "nio", "xpeng", "suzuki", "indian motorcycle", "harley-davidson",
    "land rover", "mazda", "subaru", "general motors", "ford motor", "scania", "Yutong",
    "vdl", "iveco",
]
OEM_AMBIGUOUS = ["ford", "mini", "seat", "fiat", "lucid", "polaris", "dodge", "ram", "chrysler"]

SUPPLIER_STRONG = [
    "harman", "denso", "visteon", "panasonic automotive", "clarion", "aptiv", "valeo",
    "zf group", "zf friedrichshafen", "vector informatik", "qnx", "autosar", "elektrobit",
]
SUPPLIER_AMBIGUOUS = ["bosch", "continental", "alpine", "pioneer", "magna", "lear",
                      "infineon", "nxp", "renesas", "panasonic"]

TECH_STRONG = [
    "can bus", "controller area network", "can frame", "can frames", "iso-tp", "isotp",
    "j1939", "j1708", "j1587", "some/ip", "someip", "doip", "diagnostics over ip",
    "unified diagnostic services", "evse", "ocpp", "dcfc",
    "iso 15118", "iso15118", "plug and charge", "plug & charge",
    "vehicle-to-grid", "vehicle to grid", "v2g",
    "ecu", "ecus",
    "electronic control unit", "tcu", "telematics", "telematics control unit",
    "infotainment", "vcsec", "tpms", "in-vehicle", "obd", "obd-ii", "obd2",
    "keyless", "immobilizer", "immobiliser", "rolling-code", "rolling code",
    "key fob", "key fobs", "smart key", "remote key", "charging station", "vehicle cloud",
]
TECH_WEAK = ["uds", "ivi", "gateway", "diagnostic", "diagnostics", "wcm",
             "fleet management", "ota update", "over-the-air", "ota"]

# vehicle context required for ambiguous terms
CONTEXT_TERMS = [
    "vehicle", "vehicles", "automotive", "car", "cars", "truck", "trucks", "motorcycle",
    "head unit", "in-vehicle", "electric vehicle", "ev charger", "infotainment",
    "telematics", "can bus", "ecu", "obd", "evse", "ocpp", "charging station",
    "iso 15118", "iso15118", "plug and charge", "plug & charge",
    "vehicle-to-grid", "vehicle to grid", "v2g", "keyless",
]

PRIORITY_HARDWARE = ["keyless", "rolling-code", "rolling code", "immobilizer", "immobiliser",
                     "wcm", "vcsec", "tpms", "can bus", "remote key", "smart key", "key fob"]

EXCLUDE_RX = _rx(EXCLUDE_PRODUCTS)

# Keep the evidence classes separate. A strong automotive technology term can
# independently establish automotive relevance. An OEM or supplier name cannot,
# because names such as Tesla, Alpine, Lucid, Continental and Bosch also occur
# in unrelated software products and organisations.
TECH_STRONG_RX = _rx(TECH_STRONG)
OEM_STRONG_RX = _rx(OEM_STRONG)
OEM_AMBIGUOUS_RX = _rx(OEM_AMBIGUOUS)
SUPPLIER_STRONG_RX = _rx(SUPPLIER_STRONG)
SUPPLIER_AMBIGUOUS_RX = _rx(SUPPLIER_AMBIGUOUS)
TECH_WEAK_RX = _rx(TECH_WEAK)

CONTEXT_RX = _rx(CONTEXT_TERMS)
PRIORITY_RX = _rx(PRIORITY_HARDWARE)

# ---------------------------------------------------------
# TEXT EXTRACTION
# ---------------------------------------------------------
def record_text(rec):
    """Returns (description_text, cpe_vendor_product_text) - lowercase."""
    cve = rec.get("cve", {})
    desc = " ".join(d.get("value", "") for d in cve.get("descriptions", []))
    cpe_parts = []
    for cfg in cve.get("configurations", []):
        for node in cfg.get("nodes", []):
            for m in node.get("cpeMatch", []):
                bits = m.get("criteria", "").split(":")
                if len(bits) > 5:
                    cpe_parts.append(bits[3].replace("_", " "))
                    cpe_parts.append(bits[4].replace("_", " "))
    return desc.lower(), " ".join(cpe_parts).lower()


# ---------------------------------------------------------
# AUTOMOTIVE RELEVANCE
# ---------------------------------------------------------
def classify_automotive(rec):
    """
    Return (is_automotive, matched_terms).

    Classification rules:
    1. Explicitly excluded products are rejected unless independent automotive
       technology evidence is present.
    2. Strong automotive technology terms can independently establish
       automotive relevance.
    3. OEM and supplier names require automotive or vehicle context.
    4. Ambiguous OEM, supplier and technology terms also require context.

    This prevents package or project names such as the Erlang package "tesla"
    from being classified as automotive solely because their name matches an
    automotive OEM.
    """
    desc, cpe = record_text(rec)
    full = f"{desc} {cpe}".strip()

    tech_strong_hits = set(_hits(TECH_STRONG_RX, full))
    context_hits = set(_hits(CONTEXT_RX, full))

    oem_strong_hits = set(_hits(OEM_STRONG_RX, full))
    oem_ambiguous_hits = set(_hits(OEM_AMBIGUOUS_RX, full))

    supplier_strong_hits = set(_hits(SUPPLIER_STRONG_RX, full))
    supplier_ambiguous_hits = set(_hits(SUPPLIER_AMBIGUOUS_RX, full))

    tech_weak_hits = set(_hits(TECH_WEAK_RX, full))

    # Explicitly excluded general IT products stay excluded unless the record
    # contains independent strong automotive technology evidence.
    if EXCLUDE_RX.search(full) and not tech_strong_hits:
        return False, []

    matched = set()

    # Strong automotive technology is sufficient on its own.
    matched |= tech_strong_hits

    # Names and weak technologies are accepted only when vehicle or automotive
    # context is present somewhere in the description or CPE vendor/product.
    if context_hits:
        matched |= oem_strong_hits
        matched |= oem_ambiguous_hits
        matched |= supplier_strong_hits
        matched |= supplier_ambiguous_hits
        matched |= tech_weak_hits

    return bool(matched), sorted(matched)

def is_valid_automotive_record(rec):          # kept for backwards compatibility
    return classify_automotive(rec)[0]


# ---------------------------------------------------------
# SEVERITY
# ---------------------------------------------------------
def passes_severity_filter(rec, cvss_score):
    if cvss_score is None:
        return False
    desc, cpe = record_text(rec)
    if PRIORITY_RX.search(desc + " " + cpe) and cvss_score >= MIN_SCORE_PRIORITY:
        return True
    return cvss_score >= MIN_SCORE_DEFAULT


def cvss_to_score(cvss):
    if cvss is None:
        return "Unknown"
    try:
        v = float(cvss)
    except Exception:
        return "Unknown"
    if v >= 9.0:
        return "Critical"
    if v >= 7.0:
        return "High"
    if v >= 4.0:
        return "Medium"
    return "Low"


def extract_cvss(metrics):
    for key, ver in (("cvssMetricV31", "3.1"), ("cvssMetricV30", "3.0"),
                     ("cvssMetricV40", "4.0"), ("cvssMetricV2", "2.0")):
        lst = metrics.get(key)
        if isinstance(lst, list) and lst:
            pick = next((m for m in lst if m.get("type") == "Primary"), lst[0])
            score = pick.get("cvssData", {}).get("baseScore")
            if score is not None:
                try:
                    return float(score), ver
                except (TypeError, ValueError):
                    pass
    return None, None


# ---------------------------------------------------------
# SURFACE DETECTION
# ---------------------------------------------------------
SURFACE_RULES = {
    "CAN Bus": ["can bus", "controller area network", "can frame", "can frames"],
    "UDS Diagnostics": ["uds", "unified diagnostic services", "security access"],
    "ISO-TP": ["iso-tp", "isotp"],
    "DOIP": ["doip", "diagnostics over ip"],
    "SOME/IP": ["someip", "some/ip"],
    "J1939": ["j1939"],
    "OTA": ["ota", "over-the-air", "firmware update"],
    "Telematics": ["telematics", "tcu", "vehicle cloud"],
    "Fleet": ["fleet", "fleet management"],
    "Backend": ["backend", "cloud server"],
    "Identity": ["certificate", "authentication"],
    "Bluetooth": ["bluetooth", "ble"],
    "WiFi": ["wifi", "wi-fi", "wlan"],
    "GNSS": ["gps", "gnss"],
    "EV Charging": [
        "evse",
        "charger",
        "charging station",
        "ocpp",
        "dcfc",
        "iso 15118",
        "iso15118",
        "plug and charge",
        "plug & charge",
        "vehicle-to-grid",
        "vehicle to grid",
        "v2g",
    ],
    "Infotainment": ["infotainment", "ivi", "head unit"],
    "Gateway": ["gateway"],
    "ECU": ["ecu", "ecus", "electronic control unit"],
    "Keyless / Hardware": ["keyless", "immobilizer", "immobiliser", "rolling-code",
                           "rolling code", "wcm", "key fob", "smart key", "tpms"],
}
SURFACE_RX = {s: _rx(k) for s, k in SURFACE_RULES.items()}


def detect_surfaces(text):
    found = {s for s, rx in SURFACE_RX.items() if rx.search(text)}
    return sorted(found) or ["General Automotive Surface"]


# ---------------------------------------------------------
# NVD -> RECORD
# ---------------------------------------------------------
def parse_dt(s):
    if not s:
        return None
    try:
        dt = datetime.fromisoformat(s.replace("Z", "+00:00"))
    except ValueError:
        return None
    return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)


def convert_nvd_record(rec, keyword, matched):
    cve = rec.get("cve", {})
    name = cve.get("id", "unknown")
    descriptions = cve.get("descriptions", [])
    cvss, ver = extract_cvss(cve.get("metrics", {}))
    desc, cpe = record_text(rec)

    vulns = [{"text": d["value"]} for d in descriptions
             if d.get("lang") == "en" and d.get("value")]

    return {
        "name": name,
        "published": cve.get("published", ""),
        "score": cvss_to_score(cvss),
        "cvss": cvss,
        "cvss_version": ver,
        "pending_analysis": cvss is None,
        "keyword": keyword,
        "keywords": [keyword],
        "matched_terms": matched,
        "surfaces": detect_surfaces(desc + " " + cpe),
        "vulnerabilities": vulns,
        "url": f"https://nvd.nist.gov/vuln/detail/{name}",
        "fuzzing": [],
    }


# ---------------------------------------------------------
# MAIN PIPELINE
# ---------------------------------------------------------
def run_zero_day():
    print("[ZERO-DAY] Collecting automotive intelligence feeds...")
    feeds = collect_all_feeds()
    nvd_feeds = [f for f in feeds if f.get("source") == "NVD"]
    print(f"[ZERO-DAY] Processing {len(nvd_feeds)} NVD feeds")

    now = datetime.now(timezone.utc)
    cutoff = now - timedelta(days=LOOKBACK_DAYS)
    pending_cutoff = now - timedelta(days=PENDING_WINDOW_DAYS)

    unique, rejected = {}, set()
    stats = dict(seen=0, rejected_status=0, not_automotive=0, too_old=0, low_severity=0, kept=0)

    for feed in nvd_feeds:
        keyword = feed.get("keyword", "")
        for rec in feed.get("data", {}).get("vulnerabilities", []):
            cve = rec.get("cve", {})
            cid = cve.get("id", "unknown")

            if cid in unique:                       # same CVE found via another query
                unique[cid]["keywords"] = sorted(set(unique[cid]["keywords"]) | {keyword})
                continue
            if cid in rejected:
                continue
            stats["seen"] += 1

            # rejected CVEs
            first_desc = (cve.get("descriptions") or [{}])[0].get("value", "")
            if cve.get("vulnStatus") == "Rejected" or first_desc.startswith("** REJECT **"):
                stats["rejected_status"] += 1; rejected.add(cid); continue

            # automotive relevance
            ok, matched = classify_automotive(rec)
            if not ok:
                stats["not_automotive"] += 1; rejected.add(cid); continue

            # date
            pub = parse_dt(cve.get("published"))
            if pub and pub < cutoff:
                stats["too_old"] += 1; rejected.add(cid); continue

            record = convert_nvd_record(rec, keyword, matched)

            # severity (unscored & recent -> keep, flagged pending_analysis)
            if record["cvss"] is None:
                if pub and pub < pending_cutoff:
                    stats["low_severity"] += 1; rejected.add(cid); continue
            elif not passes_severity_filter(rec, record["cvss"]):
                stats["low_severity"] += 1; rejected.add(cid); continue

            unique[cid] = record
            stats["kept"] += 1

    final = sorted(unique.values(),
                   key=lambda r: (r["cvss"] is None, -(r["cvss"] or 0), r["published"]),
                   reverse=False)
    print(f"[ZERO-DAY] Stats: {stats}")
    print(f"[ZERO-DAY] Produced {len(final)} prioritized automotive records.")
    return final
