File size: 29,618 Bytes
27f6252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
import json
import pandas as pd
from datetime import datetime
import random
import re
from typing import List, Dict, Any

def load_json_file(file_path: str) -> Any:
    """Load JSON file with proper UTF-8 encoding."""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except Exception as e:
        print(f"Error loading {file_path}: {e}")
        return None

def load_mitre_techniques() -> Dict[str, Dict[str, Any]]:
    """Load MITRE ATT&CK techniques with detection and data source information."""
    print("Loading MITRE ATT&CK techniques...")
    try:
        df = pd.read_excel('data/CTI/raw/enterprise-attack-v17.1-techniques.xlsx')
        techniques = {}
        
        for _, row in df.iterrows():
            if pd.notna(row['ID']) and str(row['ID']).startswith('T'):
                technique_id = str(row['ID'])
                techniques[technique_id] = {
                    'name': row['name'] if pd.notna(row['name']) else '',
                    'description': row['description'] if pd.notna(row['description']) else '',
                    'detection': row['detection'] if pd.notna(row['detection']) else '',
                    'data_sources': row['data sources'] if pd.notna(row['data sources']) else '',
                    'tactics': row['tactics'] if pd.notna(row['tactics']) else '',
                    'platforms': row['platforms'] if pd.notna(row['platforms']) else ''
                }
        
        print(f"Loaded {len(techniques)} MITRE ATT&CK techniques")
        return techniques
    except Exception as e:
        print(f"Error loading MITRE techniques: {e}")
        return {}

def create_enhanced_dataset_with_mitigations():
    """Create an enhanced dataset with CVE, CWE, MITRE, CAPEC, and mitigation data."""
    
    print("Loading CVE nodes...")
    cve_nodes = load_json_file('data/knowledge_base/knowledge_graph/cves_nodes.json')
    if not cve_nodes:
        return
    
    print("Loading CWE relationships...")
    cwe_relationships = load_json_file('data/knowledge_base/knowledge_graph/cve_cwe_relationships.json')
    if not cwe_relationships:
        return
    
    print("Loading MITRE technique relationships...")
    mitre_relationships = load_json_file('data/knowledge_base/knowledge_graph/cve_mitre_technique_relationships.json')
    if not mitre_relationships:
        return
    
    print("Loading MITRE tactics relationships...")
    mitre_tactics_relationships = load_json_file('data/knowledge_base/knowledge_graph/cve_mitre_tactic_relationships.json')
    if not mitre_tactics_relationships:
        return
    
    print("Loading CAPEC relationships...")
    capec_relationships = load_json_file('data/knowledge_base/knowledge_graph/cve_capec_relationships.json')
    if not capec_relationships:
        return
    
    print("Loading known exploited vulnerabilities...")
    kev_df = pd.read_csv('data/CTI/raw/known_exploited_vulnerabilities.csv')
    
    # Load MITRE techniques for mitigation data
    mitre_techniques = load_mitre_techniques()
    
    # Create relationship mappings
    print("Creating relationship mappings...")
    cve_to_cwe = {}
    for rel in cwe_relationships:
        cve_id = rel['cve_id']
        if cve_id not in cve_to_cwe:
            cve_to_cwe[cve_id] = []
        cve_to_cwe[cve_id].append(rel['cwe_id'])
    
    cve_to_mitre_techniques = {}
    for rel in mitre_relationships:
        cve_id = rel['cve_id']
        if cve_id not in cve_to_mitre_techniques:
            cve_to_mitre_techniques[cve_id] = []
        cve_to_mitre_techniques[cve_id].append(rel['technique_id'])
    
    cve_to_mitre_tactics = {}
    for rel in mitre_tactics_relationships:
        cve_id = rel['cve_id']
        if cve_id not in cve_to_mitre_tactics:
            cve_to_mitre_tactics[cve_id] = []
        cve_to_mitre_tactics[cve_id].append(rel['tactic_id'])
    
    cve_to_capec = {}
    for rel in capec_relationships:
        cve_id = rel['cve_id']
        if cve_id not in cve_to_capec:
            cve_to_capec[cve_id] = []
        cve_to_capec[cve_id].append(rel['capec_id'])
    
    # Create KEV mapping
    kev_cves = set(kev_df['cveID'].tolist())
    
    # Create enhanced dataset
    print("Creating enhanced dataset with mitigations...")
    dataset = []
    
    for cve_id, cve_data in cve_nodes.items():
        # Get relationships
        cwes = cve_to_cwe.get(cve_id, [])
        mitre_techniques_list = cve_to_mitre_techniques.get(cve_id, [])
        mitre_tactics = cve_to_mitre_tactics.get(cve_id, [])
        capecs = cve_to_capec.get(cve_id, [])
        is_kev = cve_id in kev_cves
        
        # Get MITRE technique details for mitigations
        mitre_technique_details = []
        for tech_id in mitre_techniques_list:
            if tech_id in mitre_techniques:
                mitre_technique_details.append({
                    'id': tech_id,
                    'name': mitre_techniques[tech_id]['name'],
                    'description': mitre_techniques[tech_id]['description'],
                    'detection': mitre_techniques[tech_id]['detection'],
                    'data_sources': mitre_techniques[tech_id]['data_sources']
                })
        
        # Create enhanced CVE data
        enhanced_cve = {
            'id': cve_id,
            'title': cve_data.get('title', ''),
            'description': cve_data.get('description', ''),
            'cvss_v3_score': cve_data.get('cvss_v3', {}).get('base_score', 0),
            'cvss_v3_severity': cve_data.get('cvss_v3', {}).get('base_severity', 'UNKNOWN'),
            'affected_products': cve_data.get('affected_products', []),
            'is_in_kev': is_kev,
            'cwes': cwes,
            'mitre_techniques': mitre_techniques_list,
            'mitre_technique_details': mitre_technique_details,
            'mitre_tactics': mitre_tactics,
            'capecs': capecs
        }
        
        dataset.append(enhanced_cve)
    
    print(f"Created enhanced dataset with {len(dataset)} CVEs")
    
    # Count CVEs with different types of mappings
    cves_with_cwe = sum(1 for cve in dataset if cve['cwes'])
    cves_with_mitre_techniques = sum(1 for cve in dataset if cve['mitre_techniques'])
    cves_with_mitre_tactics = sum(1 for cve in dataset if cve['mitre_tactics'])
    cves_with_capec = sum(1 for cve in dataset if cve['capecs'])
    cves_in_kev = sum(1 for cve in dataset if cve['is_in_kev'])
    
    print(f"CVEs with CWE mappings: {cves_with_cwe}")
    print(f"CVEs with MITRE technique mappings: {cves_with_mitre_techniques}")
    print(f"CVEs with MITRE tactic mappings: {cves_with_mitre_tactics}")
    print(f"CVEs with CAPEC mappings: {cves_with_capec}")
    print(f"CVEs in KEV database: {cves_in_kev}")
    
    # Save enhanced dataset
    output_file = 'data/training_datasets/enhanced_cve_dataset_with_mitigations.json'
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(dataset, f, indent=2, ensure_ascii=False)
    
    print(f"Enhanced dataset with mitigations saved to {output_file}")
    
    # Create training examples
    create_training_examples_with_mitigations(dataset)

def create_training_examples_with_mitigations(enhanced_cves: List[Dict[str, Any]]):
    """Create training examples from enhanced CVE data with MITRE mitigations."""
    
    print("Creating training examples with mitigations...")
    training_examples = []
    
    for cve in enhanced_cves:
        # Skip CVEs without sufficient data
        if not cve['description'] or not cve['title']:
            continue
        
        # Create different types of training examples
        examples = create_vulnerability_analysis_examples_with_mitigations(cve)
        training_examples.extend(examples)
        
        examples = create_remediation_examples_with_mitigations(cve)
        training_examples.extend(examples)
        
        examples = create_technical_analysis_examples_with_mitigations(cve)
        training_examples.extend(examples)
        
        examples = create_detection_examples(cve)
        training_examples.extend(examples)
        
        examples = create_mitigation_strategy_examples(cve)
        training_examples.extend(examples)
    
    # Apply data quality improvements directly
    print("Applying data quality improvements...")
    training_examples = improve_data_quality(training_examples)
    
    # Shuffle and limit to 1500 examples (increased for more variety)
    random.shuffle(training_examples)
    training_examples = training_examples[:1500]
    
    print(f"Created {len(training_examples)} high-quality training examples with mitigations")
    
    # Save training dataset
    output_file = 'data/training_datasets/enhanced_training_dataset_with_mitigations.json'
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(training_examples, f, indent=2, ensure_ascii=False)
    
    print(f"Enhanced training dataset with mitigations saved to {output_file}")

def improve_data_quality(examples: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Apply data quality improvements to prevent overfitting"""
    print("🔄 Improving data quality...")
    
    # Step 1: Diversify instructions
    examples = diversify_instructions(examples)
    
    # Step 2: Diversify inputs
    examples = diversify_inputs(examples)
    
    # Step 3: Diversify outputs
    examples = diversify_outputs(examples)
    
    # Step 4: Remove duplicates
    examples = remove_duplicates(examples)
    
    print(f"✅ Data quality improvements applied. Final count: {len(examples)} examples")
    return examples

def diversify_instructions(examples: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Diversify instructions to increase variety"""
    print("🔄 Diversifying instructions...")
    
    # Define instruction templates with variations
    instruction_templates = [
        "Analyze this CVE vulnerability and provide a comprehensive security assessment.",
        "Examine the following CVE and explain its technical details and potential impact.",
        "Provide a detailed analysis of this cybersecurity vulnerability with mitigation strategies.",
        "Investigate this CVE and describe its attack vectors and security implications.",
        "Conduct a thorough security analysis of this vulnerability, including risk assessment.",
        "Review this CVE and provide technical insights along with recommended countermeasures.",
        "Analyze the security implications of this vulnerability and suggest protective measures.",
        "Examine this CVE for potential threats and provide detailed security recommendations.",
        "Provide a comprehensive vulnerability analysis with technical details and remediation steps.",
        "Investigate this cybersecurity issue and explain its severity and potential exploits.",
        "Conduct a detailed security review of this CVE with attack scenario analysis.",
        "Analyze this vulnerability's technical aspects and provide security best practices.",
        "Review this CVE for security risks and provide detailed mitigation guidance.",
        "Examine this vulnerability and explain its technical characteristics and security impact.",
        "Provide a thorough analysis of this CVE with technical details and security recommendations."
    ]
    
    # Additional instruction variations for specific aspects
    aspect_instructions = [
        "Focus on the technical implementation details and code-level analysis.",
        "Emphasize the business impact and risk assessment aspects.",
        "Include MITRE ATT&CK framework mapping and attack patterns.",
        "Provide detailed remediation steps and security controls.",
        "Analyze the vulnerability from an attacker's perspective.",
        "Focus on detection methods and monitoring strategies.",
        "Include compliance implications and regulatory considerations.",
        "Provide detailed patch analysis and update recommendations."
    ]
    
    all_instructions = instruction_templates + aspect_instructions
    
    # Apply instruction diversification
    for i, example in enumerate(examples):
        # Use different instructions based on index to ensure diversity
        instruction_idx = i % len(all_instructions)
        example['instruction'] = all_instructions[instruction_idx]
        
        # Add some randomness to further diversify
        if random.random() < 0.3:  # 30% chance to add aspect-specific instruction
            aspect_idx = random.randint(0, len(aspect_instructions) - 1)
            example['instruction'] += f" {aspect_instructions[aspect_idx]}"
    
    print(f"✅ Diversified instructions for {len(examples)} examples")
    return examples

def diversify_inputs(examples: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Diversify input formats to increase variety"""
    print("🔄 Diversifying inputs...")
    
    # Define different input formats
    input_formats = [
        "CVE Information:\n{input}",
        "Vulnerability Details:\n{input}",
        "Security Issue:\n{input}",
        "CVE Analysis Request:\n{input}",
        "Vulnerability Assessment:\n{input}",
        "Security Analysis:\n{input}",
        "CVE Details:\n{input}",
        "Vulnerability Report:\n{input}"
    ]
    
    # Apply input diversification
    for i, example in enumerate(examples):
        format_idx = i % len(input_formats)
        original_input = example['input']
        example['input'] = input_formats[format_idx].format(input=original_input)
    
    print(f"✅ Diversified inputs for {len(examples)} examples")
    return examples

def diversify_outputs(examples: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Diversify output patterns to prevent repetitive responses"""
    print("🔄 Diversifying outputs...")
    
    # Define different output structure variations
    output_variations = [
        "## Analysis\n{content}",
        "**Security Assessment:**\n{content}",
        "### Vulnerability Analysis\n{content}",
        "**CVE Analysis:**\n{content}",
        "## Security Review\n{content}",
        "**Technical Assessment:**\n{content}",
        "### Security Analysis\n{content}",
        "**Vulnerability Assessment:**\n{content}"
    ]
    
    # Apply output diversification
    for i, example in enumerate(examples):
        # Extract the main content (remove existing formatting)
        content = example['output']
        
        # Remove common headers if present
        content = re.sub(r'^#+\s*.*?\n', '', content, flags=re.MULTILINE)
        content = re.sub(r'^\*\*.*?\*\*:\n', '', content, flags=re.MULTILINE)
        
        # Apply new format
        format_idx = i % len(output_variations)
        example['output'] = output_variations[format_idx].format(content=content.strip())
    
    print(f"✅ Diversified outputs for {len(examples)} examples")
    return examples

def remove_duplicates(examples: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    """Remove duplicate examples based on content similarity"""
    print("🔄 Removing duplicates...")
    
    seen = set()
    unique_examples = []
    duplicates_removed = 0
    
    for example in examples:
        # Create a hash of the example content
        content_hash = hash(f"{example['instruction']}_{example['input']}_{example['output']}")
        
        if content_hash not in seen:
            seen.add(content_hash)
            unique_examples.append(example)
        else:
            duplicates_removed += 1
    
    print(f"✅ Removed {duplicates_removed} duplicate examples")
    print(f"✅ Final unique examples: {len(unique_examples)}")
    return unique_examples

def create_vulnerability_analysis_examples_with_mitigations(cve: Dict[str, Any]) -> List[Dict[str, str]]:
    """Create vulnerability analysis training examples with MITRE context."""
    examples = []
    
    # Enhanced vulnerability analysis with MITRE context
    prompt = f"""Analyze the following CVE vulnerability with MITRE ATT&CK context:



CVE ID: {cve['id']}

Title: {cve['title']}

Description: {cve['description']}

CVSS v3 Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})

Affected Products: {', '.join(cve['affected_products']) if cve['affected_products'] else 'Not specified'}



Provide a comprehensive analysis including MITRE ATT&CK techniques, attack patterns, and potential mitigations."""

    response = f"""## Vulnerability Analysis for {cve['id']}



**Severity Assessment:**

- CVSS v3 Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})

- This is a {cve['cvss_v3_severity'].lower()} severity vulnerability



**Technical Details:**

{cve['description']}



**Affected Systems:**

{', '.join(cve['affected_products']) if cve['affected_products'] else 'Not specified'}



**Attack Vectors:**

Based on the CVSS metrics, this vulnerability can be exploited remotely without authentication."""

    if cve['cwes']:
        response += f"\n\n**Related Weaknesses:**\n- CWE IDs: {', '.join(cve['cwes'])}"

    if cve['mitre_technique_details']:
        response += f"\n\n**MITRE ATT&CK Techniques:**"
        for tech in cve['mitre_technique_details'][:3]:  # Limit to top 3
            response += f"\n- **{tech['id']} ({tech['name']}):** {tech['description'][:150]}..."

    if cve['capecs']:
        response += f"\n\n**Attack Patterns:**\n- CAPEC IDs: {', '.join(cve['capecs'])}"

    if cve['is_in_kev']:
        response += "\n\n**Exploitation Status:**\n- This vulnerability is listed in the Known Exploited Vulnerabilities (KEV) database, indicating active exploitation in the wild."

    examples.append({
        "instruction": "Analyze the following CVE vulnerability with MITRE ATT&CK context and provide a comprehensive security assessment.",
        "input": prompt,
        "output": response
    })
    
    return examples

def create_remediation_examples_with_mitigations(cve: Dict[str, Any]) -> List[Dict[str, str]]:
    """Create remediation training examples with MITRE-based mitigations."""
    examples = []
    
    prompt = f"""Provide comprehensive remediation guidance with MITRE ATT&CK mitigations for the following CVE vulnerability:



CVE ID: {cve['id']}

Title: {cve['title']}

Description: {cve['description']}

CVSS v3 Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})

Affected Products: {', '.join(cve['affected_products']) if cve['affected_products'] else 'Not specified'}



What are the recommended remediation steps and MITRE ATT&CK mitigations for this vulnerability?"""

    response = f"""## Remediation Guidance for {cve['id']}



**Immediate Actions:**

1. **Assess Impact:** Determine if your systems are affected by this vulnerability

2. **Prioritize:** Given the {cve['cvss_v3_severity'].lower()} severity, this should be addressed promptly

3. **Inventory:** Identify all affected systems and applications



**Recommended Remediation Steps:**



1. **Patch Management:**

   - Apply vendor-provided security patches as soon as they become available

   - Test patches in a non-production environment first

   - Implement automated patch management where possible



2. **Compensating Controls:**

   - Implement network segmentation to limit exposure

   - Use firewalls and intrusion detection systems

   - Apply the principle of least privilege



3. **Monitoring:**

   - Monitor affected systems for suspicious activity

   - Implement logging and alerting for potential exploitation attempts

   - Regular vulnerability scanning and assessment"""

    if cve['mitre_technique_details']:
        response += f"\n\n**MITRE ATT&CK Mitigations:**"
        for tech in cve['mitre_technique_details'][:3]:
            response += f"\n\n**For {tech['id']} ({tech['name']}):**"
            if tech['detection']:
                response += f"\n- **Detection:** {tech['detection'][:200]}..."
            if tech['data_sources']:
                response += f"\n- **Data Sources:** {tech['data_sources']}"
            response += f"\n- **Mitigation:** Implement controls to prevent {tech['name'].lower()}"

    if cve['is_in_kev']:
        response += "\n\n**Critical Note:** This vulnerability is actively exploited in the wild. Immediate action is required."

    if cve['cwes']:
        response += f"\n\n**Addressing Root Causes:**\n- Focus on the underlying weaknesses: {', '.join(cve['cwes'])}\n- Implement secure coding practices to prevent similar vulnerabilities"

    response += f"""



**Affected Products:**

{', '.join(cve['affected_products']) if cve['affected_products'] else 'Contact vendor for specific guidance'}



**Follow-up:**

- Verify remediation effectiveness through testing

- Document all changes and maintain security baselines

- Consider implementing additional security controls"""

    examples.append({
        "instruction": "Provide comprehensive remediation guidance with MITRE ATT&CK mitigations for this CVE vulnerability.",
        "input": prompt,
        "output": response
    })
    
    return examples

def create_technical_analysis_examples_with_mitigations(cve: Dict[str, Any]) -> List[Dict[str, str]]:
    """Create technical analysis training examples with MITRE context."""
    examples = []
    
    prompt = f"""Provide a detailed technical analysis with MITRE ATT&CK context for the following CVE vulnerability:



CVE ID: {cve['id']}

Title: {cve['title']}

Description: {cve['description']}

CVSS v3 Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})



Analyze the technical aspects including attack vectors, MITRE techniques, and technical impact."""

    response = f"""## Technical Analysis: {cve['id']}



**Vulnerability Overview:**

{cve['title']}



**Technical Description:**

{cve['description']}



**CVSS Analysis:**

- Base Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})

- The high CVSS score indicates significant security impact



**Attack Vector Analysis:**

Based on the CVSS metrics, this vulnerability:

- Can be exploited over the network (AV:N)

- Requires low attack complexity (AC:L)

- Does not require authentication (PR:N)

- Does not require user interaction (UI:N)

- Has high impact on confidentiality, integrity, and availability (C:H/I:H/A:H)"""

    if cve['cwes']:
        response += f"\n\n**Underlying Weaknesses:**\n- CWE IDs: {', '.join(cve['cwes'])}\n- These represent the root causes that should be addressed in future development"

    if cve['mitre_technique_details']:
        response += f"\n\n**MITRE ATT&CK Analysis:**"
        for tech in cve['mitre_technique_details'][:3]:
            response += f"\n\n**{tech['id']} ({tech['name']}):**"
            response += f"\n- Description: {tech['description'][:150]}..."
            if tech['detection']:
                response += f"\n- Detection: {tech['detection'][:150]}..."

    if cve['capecs']:
        response += f"\n\n**Attack Patterns:**\n- CAPEC IDs: {', '.join(cve['capecs'])}\n- These represent common attack patterns associated with this vulnerability"

    response += f"""



**Technical Impact:**

- **Confidentiality:** High - sensitive data may be exposed

- **Integrity:** High - data or system integrity may be compromised

- **Availability:** High - system availability may be affected



**Exploitation Complexity:**

The low attack complexity suggests this vulnerability can be exploited with minimal effort and resources."""

    examples.append({
        "instruction": "Provide a detailed technical analysis with MITRE ATT&CK context for this CVE vulnerability.",
        "input": prompt,
        "output": response
    })
    
    return examples

def create_detection_examples(cve: Dict[str, Any]) -> List[Dict[str, str]]:
    """Create detection strategy training examples."""
    examples = []
    
    if not cve['mitre_technique_details']:
        return examples
    
    prompt = f"""Create detection strategies for the following CVE vulnerability:



CVE ID: {cve['id']}

Title: {cve['title']}

Description: {cve['description']}

CVSS v3 Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})



What detection methods and monitoring strategies should be implemented for this vulnerability?"""

    response = f"""## Detection Strategies for {cve['id']}



**Vulnerability Context:**

{cve['title']}

- CVSS Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})

- Severity: {cve['cvss_v3_severity']}



**Detection Approach:**



1. **Network Monitoring:**

   - Monitor for unusual network traffic patterns

   - Implement IDS/IPS rules for known attack signatures

   - Monitor for failed authentication attempts



2. **System Monitoring:**

   - Monitor system logs for suspicious activities

   - Track file system changes and unusual processes

   - Monitor for privilege escalation attempts"""

    if cve['mitre_technique_details']:
        response += f"\n\n**MITRE ATT&CK Detection Strategies:**"
        for tech in cve['mitre_technique_details'][:3]:
            response += f"\n\n**For {tech['id']} ({tech['name']}):**"
            if tech['detection']:
                response += f"\n- **Detection Method:** {tech['detection'][:200]}..."
            if tech['data_sources']:
                response += f"\n- **Data Sources:** {tech['data_sources']}"
            response += f"\n- **Monitoring Focus:** Look for indicators of {tech['name'].lower()}"

    response += f"""



**SIEM Correlation Rules:**

- Create alerts for multiple failed authentication attempts

- Monitor for unusual process execution patterns

- Track network connections to suspicious destinations



**Log Analysis:**

- Review authentication logs for unusual patterns

- Monitor system logs for privilege escalation

- Track application logs for error conditions



**Threat Hunting:**

- Search for indicators of compromise (IOCs)

- Look for persistence mechanisms

- Investigate unusual network connections"""

    examples.append({
        "instruction": "Create comprehensive detection strategies for this CVE vulnerability.",
        "input": prompt,
        "output": response
    })
    
    return examples

def create_mitigation_strategy_examples(cve: Dict[str, Any]) -> List[Dict[str, str]]:
    """Create mitigation strategy training examples."""
    examples = []
    
    if not cve['mitre_technique_details']:
        return examples
    
    prompt = f"""Develop a comprehensive mitigation strategy for the following CVE vulnerability:



CVE ID: {cve['id']}

Title: {cve['title']}

Description: {cve['description']}

CVSS v3 Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})



What mitigation strategies and security controls should be implemented?"""

    response = f"""## Mitigation Strategy for {cve['id']}



**Vulnerability Overview:**

{cve['title']}

- CVSS Score: {cve['cvss_v3_score']} ({cve['cvss_v3_severity']})

- Priority: {'Critical' if cve['cvss_v3_score'] >= 9.0 else 'High' if cve['cvss_v3_score'] >= 7.0 else 'Medium'}



**Defense-in-Depth Strategy:**



1. **Preventive Controls:**

   - Implement network segmentation

   - Use application whitelisting

   - Apply principle of least privilege

   - Implement strong authentication mechanisms



2. **Detective Controls:**

   - Deploy intrusion detection systems

   - Implement comprehensive logging

   - Use security information and event management (SIEM)

   - Regular vulnerability assessments"""

    if cve['mitre_technique_details']:
        response += f"\n\n**MITRE ATT&CK Mitigation Strategies:**"
        for tech in cve['mitre_technique_details'][:3]:
            response += f"\n\n**Mitigating {tech['id']} ({tech['name']}):**"
            response += f"\n- **Technique Description:** {tech['description'][:150]}..."
            response += f"\n- **Mitigation Approach:**"
            response += f"\n  - Implement access controls to prevent {tech['name'].lower()}"
            response += f"\n  - Use monitoring and alerting for detection"
            response += f"\n  - Apply security hardening measures"

    if cve['cwes']:
        response += f"\n\n**Addressing Root Causes:**\n- **CWE Focus:** {', '.join(cve['cwes'])}\n- Implement secure coding practices\n- Use static and dynamic code analysis\n- Provide developer security training"

    response += f"""



**Implementation Timeline:**

1. **Immediate (0-24 hours):** Apply emergency patches and workarounds

2. **Short-term (1-7 days):** Implement compensating controls

3. **Medium-term (1-4 weeks):** Deploy comprehensive security controls

4. **Long-term (1-6 months):** Implement preventive measures and training



**Success Metrics:**

- Reduced attack surface

- Improved detection capabilities

- Enhanced incident response readiness

- Decreased vulnerability exposure time"""

    examples.append({
        "instruction": "Develop a comprehensive mitigation strategy for this CVE vulnerability.",
        "input": prompt,
        "output": response
    })
    
    return examples

if __name__ == "__main__":
    create_enhanced_dataset_with_mitigations()