File size: 15,319 Bytes
c8d30bc | 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 | # tools/attck_tool.py
# ๅ่ฝ๏ผCWE โ CAPEC โ MITRE ATT&CK Technique ๆ ๅฐ
# ๆถๆงๅฎไฝ๏ผ่ฃๅ
จ Intel Fusion ๅ
ญ็ถญๅๆ็ ATT&CK ็ถญๅบฆ๏ผๆฌ้ 10%๏ผ
#
# ็บไป้บผ็จ้ๆ
ๆ ๅฐ๏ผไธ็จ API๏ผ๏ผ
# - MITRE ATT&CK ๅฎๆน TAXII ไผบๆๅจๆฒๆ CVEโTechnique ็ดๆฅๆฅ่ฉข็ซฏ้ป
# - ็ ็ฉถ๏ผCAPEC ๆฏ CWE ๅ ATT&CK ไน้ๆ็ตๆงๅ็ๆฉๆฅ
# - ่ทฏๅพ๏ผCVE description โ CWE โ CAPEC โ ATT&CK Technique
# - ไพๆบ๏ผNIST NVD ๆไพ CWE๏ผMITRE ๅฎๆนๆไพ CAPECโATT&CK ๅฐๆ
#
# ไฝ่ญ๏ผ
# - Center for Threat-Informed Defense (CTID) Mappings Explorer
# - https://mappings-explorer.mitre.org/
# - NopSec (2024): "CWEโCAPECโATT&CK is the most structured mapping path"
# - GitHub: threatsurfer/cve-attack-mapper๏ผๅ่ๅฏฆไฝ๏ผ
#
# ไฝฟ็จๆนๅผ๏ผ
# from tools.attck_tool import lookup_attck_technique
import json
import logging
import re
logger = logging.getLogger("ThreatHunter.attck_tool")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# CWE โ ATT&CK Technique ๅ CAPEC ๆ ๅฐ่กจ
# ไพๆบ๏ผMITRE ATT&CK v14 + CAPEC 3.9
# ๆถต่ๆๅธธ่ฆ็ Web/็ณป็ตฑๆผๆด CWE
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
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",
},
# โโ XSS / ๅฎขๆถ็ซฏๆณจๅ
ฅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ SSRF / ่ซๆฑๅฝ้ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ Buffer Overflow / ่จๆถ้ซๅฎๅ
จ โโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ Use-After-Free / ่จๆถ้ซ็ฎก็ โโโโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ LDAP ๆณจๅ
ฅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ XXE โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ Prototype Pollution (Node.js) โโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ ReDoS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ DoS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
"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",
},
# โโ Open Redirect โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
"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 โ CWE ๅฟซ้ๆ ๅฐ๏ผๅพ CVE ๆ่ฟฐไธญๆๅ๏ผ
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()
# 1. ๅพๆ่ฟฐไธญๆๅๆ็ขบ็ CWE ็ทจ่๏ผๅฆ "CWE-79"๏ผ
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
# 2. ้้ตๅญๅน้
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,
}
"""
# ๅชๅ
ไฝฟ็จๆ็ขบ็ CWE ๅ่กจ
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}"}
# ็กๆณๅฐๆ๏ผ่ฟๅ้็จ T1190๏ผๆๅธธ่ฆ็ๆผๆดๅฉ็จ Technique๏ผ
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,
}
|