File size: 4,444 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 | #!/usr/bin/env python3
import json
import sys
from pathlib import Path
# Add parent directory to path
sys.path.append(str(Path(__file__).parent.parent.parent))
from src.mapper.mapper_config import MapperConfig
from src.mapper.cve_cwe_mapper import CVEtoCWEtoTTPMapper
from src.mapper.simple_tie_inference import SimpleTIEInference
def test_cwe_mapping():
"""Test CWE-based mapping."""
print("=== Testing CWE-based Mapping ===")
config = MapperConfig()
if not config.cwe_capec_mitre_mapping_path.exists():
print(f"ERROR: CWE mapping file not found at {config.cwe_capec_mitre_mapping_path}")
print("Please run the data collection pipeline first")
return False
cwe_mapper = CVEtoCWEtoTTPMapper(config.cwe_capec_mitre_mapping_path)
# Test CVE with known CWEs
test_cve = {
"id": "CVE-2021-44228",
"cwe_ids": ["CWE-502", "CWE-400", "CWE-20"]
}
ttps, details = cwe_mapper.map_cve_to_ttps(test_cve)
print(f"CVE: {test_cve['id']}")
print(f"CWEs: {', '.join(test_cve['cwe_ids'])}")
print(f"TTPs found: {len(ttps)}")
if ttps:
print(f"TTPs: {', '.join(ttps[:5])}")
return len(ttps) > 0
def test_simple_tie():
"""Test simple TIE inference."""
print("\n=== Testing Simple TIE Inference ===")
config = MapperConfig()
if not config.tie_model_path.exists():
print(f"TIE model not found at {config.tie_model_path}")
print("TIE inference will be skipped")
return False
try:
tie = SimpleTIEInference(
model_path=config.tie_model_path,
enrichment_path=config.tie_enrichment_path
)
if not tie.model_loaded:
print("TIE model failed to load")
return False
print(f"TIE model loaded: {tie.n} techniques, {tie.k}-dim embeddings")
# Test inference
test_description = """
A remote code execution vulnerability exists that allows an attacker
to execute arbitrary commands via SQL injection attacks.
"""
ttps, details = tie.infer_ttps_from_description(test_description)
print(f"Test description inference:")
print(f"TTPs found: {len(ttps)}")
if ttps:
print(f"TTPs: {', '.join(ttps[:5])}")
print(f"Method: {details.get('method', 'unknown')}")
return len(ttps) > 0
except ImportError as e:
print(f"TIE inference requires NumPy: {e}")
return False
except Exception as e:
print(f"TIE test failed: {e}")
return False
def test_combined():
"""Test combined mapping."""
print("\n=== Testing Combined Mapping ===")
try:
from src.mapper.cve_ttp_mapper import CVEtoTTPMapper
config = MapperConfig()
mapper = CVEtoTTPMapper(config)
test_cve = {
"id": "CVE-2024-TEST",
"description": "A buffer overflow vulnerability allows remote code execution via command injection",
"cwe_ids": ["CWE-119"]
}
result = mapper.map_cve_to_ttps(test_cve)
print(f"CVE: {result['cve_id']}")
print(f"Methods used: {', '.join(result['methods_used'])}")
print(f"Total TTPs: {result['total_ttps_found']}")
if result['ttps']:
print(f"TTPs: {', '.join(result['ttps'][:5])}")
return result['total_ttps_found'] > 0
except Exception as e:
print(f"Combined test failed: {e}")
return False
def main():
"""Run all tests."""
print("CVE-to-TTP Mapper Test Suite (Simplified)")
print("=" * 50)
results = []
results.append(test_cwe_mapping())
results.append(test_simple_tie())
results.append(test_combined())
print(f"\n=== Test Results ===")
print(f"CWE Mapping: {'✓' if results[0] else '✗'}")
print(f"TIE Inference: {'✓' if results[1] else '✗'}")
print(f"Combined: {'✓' if results[2] else '✗'}")
if any(results):
print("\nAt least one method is working! ✓")
else:
print("\nNo methods are working. Check your setup.")
if __name__ == "__main__":
main() |