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,
    }