| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import json |
| import logging |
| import re |
|
|
| logger = logging.getLogger("ThreatHunter.attck_tool") |
|
|
| |
| |
| |
| |
| |
|
|
| CWE_TO_ATTCK: dict[str, dict] = { |
| |
| "CWE-89": { |
| "technique_id": "T1190", |
| "technique_name": "Exploit Public-Facing Application", |
| "tactic": "Initial Access", |
| "capec": "CAPEC-66", |
| "capec_name": "SQL Injection", |
| "description": "SQL injection allows attackers to manipulate DB queries", |
| }, |
| "CWE-78": { |
| "technique_id": "T1059", |
| "technique_name": "Command and Scripting Interpreter", |
| "tactic": "Execution", |
| "capec": "CAPEC-88", |
| "capec_name": "OS Command Injection", |
| "description": "OS command injection via improper input neutralization", |
| }, |
| "CWE-77": { |
| "technique_id": "T1059", |
| "technique_name": "Command and Scripting Interpreter", |
| "tactic": "Execution", |
| "capec": "CAPEC-88", |
| "capec_name": "Command Injection", |
| "description": "Command injection in shell-invoked functions", |
| }, |
| |
| "CWE-79": { |
| "technique_id": "T1059.007", |
| "technique_name": "JavaScript", |
| "tactic": "Execution", |
| "capec": "CAPEC-86", |
| "capec_name": "XSS via HTTP Query Strings", |
| "description": "Cross-site scripting enables malicious script injection", |
| }, |
| "CWE-80": { |
| "technique_id": "T1059.007", |
| "technique_name": "JavaScript", |
| "tactic": "Execution", |
| "capec": "CAPEC-198", |
| "capec_name": "XSS via HTTP Headers", |
| "description": "Basic XSS through unescaped HTML", |
| }, |
| |
| "CWE-22": { |
| "technique_id": "T1083", |
| "technique_name": "File and Directory Discovery", |
| "tactic": "Discovery", |
| "capec": "CAPEC-126", |
| "capec_name": "Path Traversal", |
| "description": "Path traversal grants unauthorized file access", |
| }, |
| "CWE-73": { |
| "technique_id": "T1083", |
| "technique_name": "File and Directory Discovery", |
| "tactic": "Discovery", |
| "capec": "CAPEC-126", |
| "capec_name": "External Control of File Name", |
| "description": "Externally controlled file name leads to traversal", |
| }, |
| |
| "CWE-287": { |
| "technique_id": "T1078", |
| "technique_name": "Valid Accounts", |
| "tactic": "Initial Access", |
| "capec": "CAPEC-115", |
| "capec_name": "Authentication Bypass", |
| "description": "Authentication bypass allows unauthorized access", |
| }, |
| "CWE-306": { |
| "technique_id": "T1078", |
| "technique_name": "Valid Accounts", |
| "tactic": "Initial Access", |
| "capec": "CAPEC-115", |
| "capec_name": "Missing Authentication", |
| "description": "Missing authentication for critical function", |
| }, |
| "CWE-798": { |
| "technique_id": "T1552.001", |
| "technique_name": "Credentials In Files", |
| "tactic": "Credential Access", |
| "capec": "CAPEC-191", |
| "capec_name": "Hardcoded Credentials", |
| "description": "Hardcoded credentials expose authentication secrets", |
| }, |
| |
| "CWE-200": { |
| "technique_id": "T1530", |
| "technique_name": "Data from Cloud Storage", |
| "tactic": "Collection", |
| "capec": "CAPEC-118", |
| "capec_name": "Collect and Analyze Information", |
| "description": "Exposure of sensitive information to unauthorized actors", |
| }, |
| "CWE-312": { |
| "technique_id": "T1552.004", |
| "technique_name": "Private Keys", |
| "tactic": "Credential Access", |
| "capec": "CAPEC-37", |
| "capec_name": "Unencrypted Storage", |
| "description": "Cleartext storage of sensitive information", |
| }, |
| |
| "CWE-502": { |
| "technique_id": "T1059", |
| "technique_name": "Command and Scripting Interpreter", |
| "tactic": "Execution", |
| "capec": "CAPEC-586", |
| "capec_name": "Object Injection", |
| "description": "Deserialization of untrusted data enables code execution", |
| }, |
| |
| "CWE-918": { |
| "technique_id": "T1090", |
| "technique_name": "Proxy", |
| "tactic": "Command and Control", |
| "capec": "CAPEC-664", |
| "capec_name": "Server-Side Request Forgery", |
| "description": "SSRF allows reaching internal services via the server", |
| }, |
| |
| "CWE-295": { |
| "technique_id": "T1557", |
| "technique_name": "Adversary-in-the-Middle", |
| "tactic": "Collection", |
| "capec": "CAPEC-94", |
| "capec_name": "Man-in-the-Browser", |
| "description": "Improper certificate validation enables MITM", |
| }, |
| |
| "CWE-120": { |
| "technique_id": "T1203", |
| "technique_name": "Exploitation for Client Execution", |
| "tactic": "Execution", |
| "capec": "CAPEC-100", |
| "capec_name": "Overflow Buffers", |
| "description": "Buffer overflow can lead to arbitrary code execution", |
| }, |
| "CWE-119": { |
| "technique_id": "T1203", |
| "technique_name": "Exploitation for Client Execution", |
| "tactic": "Execution", |
| "capec": "CAPEC-100", |
| "capec_name": "Memory Buffer Overflow", |
| "description": "Improper restriction of buffer operations", |
| }, |
| |
| "CWE-416": { |
| "technique_id": "T1203", |
| "technique_name": "Exploitation for Client Execution", |
| "tactic": "Execution", |
| "capec": "CAPEC-46", |
| "capec_name": "Overflow Variables and Tags", |
| "description": "Use-after-free enables heap manipulation attacks", |
| }, |
| |
| "CWE-494": { |
| "technique_id": "T1195.002", |
| "technique_name": "Compromise Software Supply Chain", |
| "tactic": "Initial Access", |
| "capec": "CAPEC-538", |
| "capec_name": "Open-Source Library Manipulation", |
| "description": "Download of code without integrity check", |
| }, |
| |
| "CWE-90": { |
| "technique_id": "T1190", |
| "technique_name": "Exploit Public-Facing Application", |
| "tactic": "Initial Access", |
| "capec": "CAPEC-136", |
| "capec_name": "LDAP Injection", |
| "description": "LDAP injection via improper LDAP query neutralization", |
| }, |
| |
| "CWE-611": { |
| "technique_id": "T1190", |
| "technique_name": "Exploit Public-Facing Application", |
| "tactic": "Initial Access", |
| "capec": "CAPEC-221", |
| "capec_name": "DTD Injection", |
| "description": "XML External Entity injection enables file disclosure", |
| }, |
| |
| "CWE-1321": { |
| "technique_id": "T1059.007", |
| "technique_name": "JavaScript", |
| "tactic": "Execution", |
| "capec": "CAPEC-1", |
| "capec_name": "Accessing Functionality Not Properly Constrained", |
| "description": "Prototype pollution via __proto__ manipulation", |
| }, |
| |
| "CWE-1333": { |
| "technique_id": "T1499", |
| "technique_name": "Endpoint Denial of Service", |
| "tactic": "Impact", |
| "capec": "CAPEC-492", |
| "capec_name": "Regular Expression Exponential Blowup", |
| "description": "Inefficient regular expression causes ReDoS", |
| }, |
| |
| "CWE-400": { |
| "technique_id": "T1499", |
| "technique_name": "Endpoint Denial of Service", |
| "tactic": "Impact", |
| "capec": "CAPEC-147", |
| "capec_name": "XML Routing Detour Attacks", |
| "description": "Uncontrolled resource consumption leads to DoS", |
| }, |
| |
| "CWE-601": { |
| "technique_id": "T1204.001", |
| "technique_name": "Malicious Link", |
| "tactic": "Execution", |
| "capec": "CAPEC-194", |
| "capec_name": "Fake the Source of Data", |
| "description": "URL redirection to untrusted sites", |
| }, |
| } |
|
|
| |
| KEYWORD_TO_CWE: dict[str, str] = { |
| "sql injection": "CWE-89", |
| "sqli": "CWE-89", |
| "command injection": "CWE-78", |
| "os command": "CWE-78", |
| "xss": "CWE-79", |
| "cross-site scripting": "CWE-79", |
| "cross site scripting": "CWE-79", |
| "path traversal": "CWE-22", |
| "directory traversal": "CWE-22", |
| "authentication bypass": "CWE-287", |
| "hardcoded": "CWE-798", |
| "hard-coded": "CWE-798", |
| "ssrf": "CWE-918", |
| "server-side request forgery": "CWE-918", |
| "deserialization": "CWE-502", |
| "prototype pollution": "CWE-1321", |
| "redos": "CWE-1333", |
| "denial of service": "CWE-400", |
| "open redirect": "CWE-601", |
| "xxe": "CWE-611", |
| "xml external entity": "CWE-611", |
| "buffer overflow": "CWE-120", |
| "use after free": "CWE-416", |
| "use-after-free": "CWE-416", |
| "supply chain": "CWE-494", |
| "ldap injection": "CWE-90", |
| } |
|
|
|
|
| def lookup_attck_by_cwe(cwe_id: str) -> dict | None: |
| """ |
| 根據 CWE ID 查詢對應的 ATT&CK Technique。 |
| |
| Args: |
| cwe_id: 格式 "CWE-79" 或 "79" |
| |
| Returns: |
| {technique_id, technique_name, tactic, capec, ...} 或 None |
| """ |
| norm = cwe_id.strip().upper() |
| if not norm.startswith("CWE-"): |
| norm = f"CWE-{norm}" |
| result = CWE_TO_ATTCK.get(norm) |
| if result: |
| logger.info("[ATTCK] CWE %s -> %s (%s)", norm, result["technique_id"], result["technique_name"]) |
| return result |
|
|
|
|
| def lookup_attck_by_description(description: str) -> dict | None: |
| """ |
| 從 CVE 描述文字中提取可能的 CWE,再查詢 ATT&CK。 |
| |
| 優先精確 CWE,其次關鍵字匹配。 |
| """ |
| text = description.lower() |
|
|
| |
| cwe_match = re.search(r"cwe-(\d+)", text) |
| if cwe_match: |
| result = lookup_attck_by_cwe(f"CWE-{cwe_match.group(1)}") |
| if result: |
| return result |
|
|
| |
| for keyword, cwe in KEYWORD_TO_CWE.items(): |
| if keyword in text: |
| result = lookup_attck_by_cwe(cwe) |
| if result: |
| return result |
|
|
| return None |
|
|
|
|
| def get_attck_for_cve(cve_id: str, description: str = "", cwe_ids: list[str] | None = None) -> dict: |
| """ |
| 程式碼層呼叫:給定 CVE ID + 描述 + CWE 列表,返回最匹配的 ATT&CK Technique。 |
| |
| 供 Intel Fusion _verify_and_recalculate 使用。 |
| |
| Returns: |
| { |
| "technique_id": "T1059.007", |
| "technique_name": "JavaScript", |
| "tactic": "Execution", |
| "capec": "CAPEC-86", |
| "source": "CWE->ATTCK_MAP", |
| "matched_by": "CWE-79" | "keyword:xss" | None, |
| } |
| """ |
| |
| if cwe_ids: |
| for cwe in cwe_ids: |
| result = lookup_attck_by_cwe(cwe) |
| if result: |
| return {**result, "source": "CWE->ATTCK_MAP", "matched_by": cwe} |
|
|
| |
| if description: |
| result = lookup_attck_by_description(description) |
| if result: |
| |
| text = description.lower() |
| matched_kw = next((kw for kw in KEYWORD_TO_CWE if kw in text), "keyword") |
| return {**result, "source": "CWE->ATTCK_MAP", "matched_by": f"keyword:{matched_kw}"} |
|
|
| |
| return { |
| "technique_id": "T1190", |
| "technique_name": "Exploit Public-Facing Application", |
| "tactic": "Initial Access", |
| "capec": "CAPEC-1", |
| "capec_name": "Unknown", |
| "description": "No specific ATT&CK mapping found; defaulting to general exploitation", |
| "source": "CWE->ATTCK_MAP", |
| "matched_by": None, |
| } |
|
|