-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapper.py
More file actions
253 lines (195 loc) · 10.5 KB
/
Copy pathmapper.py
File metadata and controls
253 lines (195 loc) · 10.5 KB
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
from typing import Dict, Any, List
import logging
import json
from loader import load_d3fend_data, load_d3fend_technique_details
logger = logging.getLogger(__name__)
def clean_group_data(group_data):
"""
Merges and cleans up MITRE ATT&CK group data structure.
Added debug logging to trace data transformation.
"""
if not isinstance(group_data, dict):
logger.warning(f"Group data is not a dict: {type(group_data)}") # Added debug log
return None
# Extract main object properties and relationships
cleaned_data = group_data.get('object', group_data) # Modified to handle both formats
return cleaned_data
def clean_mitigation_data(mitigation_data):
"""
Merges and cleans up MITRE ATT&CK mitigation data structure by flattening the nested structure.
Args:
mitigation_data (dict): Raw mitigation data with 'object' and 'relationships' properties
Returns:
dict: Cleaned data with merged properties
"""
if not isinstance(mitigation_data, dict):
return None
# Extract main object properties and relationships
cleaned_data = mitigation_data.get('object', {})
return cleaned_data
def make_json_serializable(obj):
"""Convert STIX objects to dictionaries"""
if hasattr(obj, 'serialize'):
return json.loads(obj.serialize())
elif isinstance(obj, dict):
return {k: make_json_serializable(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [make_json_serializable(i) for i in obj]
return obj
class TechniqueMapper:
def __init__(self, attack):
self.attack = attack
self.d3fend_cache = {}
def map_groups_to_technique(self, technique_id: str) -> List[Dict[str, Any]]:
"""Maps groups to a specific technique with debug logging"""
try:
if not technique_id:
return []
tech_obj = self.attack.get_object_by_attack_id(technique_id, "attack-pattern")
if not tech_obj:
logger.warning(f"Could not find technique object for {technique_id}")
return []
# Get groups using this technique
groups = self.attack.get_groups_using_technique(tech_obj.id)
if groups:
serialized_groups = [make_json_serializable(group) for group in groups]
cleaned_groups = [clean_group_data(group) for group in serialized_groups]
return cleaned_groups
return []
except Exception as e:
logger.error(f"Error mapping groups for technique {technique_id}: {str(e)}", exc_info=True)
return []
def map_mitigations_to_technique(self, technique_id: str) -> List[Dict[str, Any]]:
"""Maps mitigations to a specific technique"""
try:
if not technique_id:
return []
tech_obj = self.attack.get_object_by_attack_id(technique_id, "attack-pattern")
if not tech_obj:
return []
# Get mitigations for this technique
mitigations = self.attack.get_mitigations_mitigating_technique(tech_obj.id)
if mitigations:
return [clean_mitigation_data(make_json_serializable(mitigation)) for mitigation in mitigations]
return []
except Exception as e:
logger.error(f"Error mapping mitigations for technique {technique_id}: {str(e)}", exc_info=True)
return []
def map_all_techniques(self, techniques: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Maps groups and mitigations to all techniques"""
logger.info("Mapping techniques to groups and mitigations...")
total = len(techniques)
for i, technique in enumerate(techniques, 1):
if i % 50 == 0:
logger.info(f"Processed {i}/{total} techniques...")
technique_id = technique.get('technique_id')
groups = self.map_groups_to_technique(technique_id)
if groups:
technique['groups'] = groups
mitigations = self.map_mitigations_to_technique(technique_id)
if mitigations:
technique['mitigations'] = mitigations
logger.info("Finished mapping all techniques")
return techniques
def map_d3fend_to_technique(self, technique_id: str, use_cache: bool = True) -> List[Dict[str, Any]]:
"""Maps D3FEND defensive techniques to an ATT&CK technique with full references"""
try:
if not technique_id:
return []
d3fend_data = load_d3fend_data(technique_id, use_cache)
if not d3fend_data or 'off_to_def' not in d3fend_data:
return []
d3fend_techniques = []
bindings = d3fend_data['off_to_def']['results']['bindings']
for binding in bindings:
if 'def_tech_label' not in binding:
continue
def_tech_id = binding['def_tech']['value'].split('#')[-1]
if def_tech_id not in self.d3fend_cache:
self.d3fend_cache[def_tech_id] = load_d3fend_technique_details(def_tech_id, use_cache)
def_tech_details = self.d3fend_cache[def_tech_id]
if not def_tech_details:
continue
description = None
if 'description' in def_tech_details and '@graph' in def_tech_details['description']:
graph = def_tech_details['description']['@graph']
if graph:
def_text = graph[0].get('d3f:definition', '')
if isinstance(def_text, str):
description = def_text
elif isinstance(def_text, list):
description = def_text[0] if def_text else None
# Create technique info
technique_info = {
"id": def_tech_id,
"title": binding['def_tech_label']['value'],
"url": f"https://d3fend.mitre.org/technique/d3f:{def_tech_id}"
}
if description:
technique_info["description"] = description
# Add full references and authors information
if 'references' in def_tech_details and def_tech_details['references'].get('@graph'):
ref_graph = def_tech_details['references']['@graph'][0]
# Get all reference information
if '@graph' in def_tech_details['references']:
references = []
for ref in def_tech_details['references']['@graph']:
if 'd3f:has-link' in ref:
ref_url = ref.get('d3f:has-link', {}).get('@value')
ref_desc = ref.get('d3f:description', '')
if ref_url:
references.append({
'url': ref_url,
'description': ref_desc
})
if references:
technique_info["references"] = references
# Get author information
if 'd3f:kb-author' in ref_graph:
author_value = ref_graph['d3f:kb-author']
if isinstance(author_value, str):
# Split by comma and clean up each author name
authors = [a.strip() for a in author_value.split(',') if a.strip()]
if authors:
technique_info["authors"] = authors
elif isinstance(author_value, list):
technique_info["authors"] = author_value
# Log for debugging
logger.debug(f"D3FEND technique for {def_tech_id}:")
if "references" in technique_info:
logger.debug(f"References: {technique_info['references']}")
if "authors" in technique_info:
logger.debug(f"Authors: {technique_info['authors']}")
d3fend_techniques.append(technique_info)
return d3fend_techniques
except Exception as e:
logger.error(f"Error mapping D3FEND data for technique {technique_id}: {str(e)}", exc_info=True)
return []
def map_all_techniques(self, techniques: List[Dict[str, Any]], use_cache: bool = True) -> List[Dict[str, Any]]:
"""Maps all relationships (groups, mitigations, and D3FEND) to techniques"""
logger.info("Mapping techniques to groups, mitigations, and D3FEND...")
total = len(techniques)
for i, technique in enumerate(techniques, 1):
if i % 50 == 0:
logger.info(f"Processed {i}/{total} techniques...")
technique_id = technique.get('technique_id')
logger.debug(f"Processing technique {technique_id}")
# Map all relationships
groups = self.map_groups_to_technique(technique_id)
if groups:
technique['groups'] = groups
mitigations = self.map_mitigations_to_technique(technique_id)
if mitigations:
technique['mitigations'] = mitigations
d3fend = self.map_d3fend_to_technique(technique_id, use_cache)
if d3fend:
technique['d3fend'] = d3fend
logger.info("Finished mapping all techniques")
return techniques
def map_all_data(data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Main function to map all relationships"""
attack = data['attack']
mapper = TechniqueMapper(attack)
mapped_data = mapper.map_all_techniques(data['techniques'])
# Ensure everything is JSON serializable
return make_json_serializable(mapped_data)