forked from OpenCTI-Platform/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencti_stix2_update.py
305 lines (286 loc) · 14.2 KB
/
opencti_stix2_update.py
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
# coding: utf-8
import traceback
from pycti.api import LOGGER as API_LOGGER
from pycti.utils.constants import StixCyberObservableTypes
class OpenCTIStix2Update:
"""Python API for Stix2 Update in OpenCTI
:param opencti: OpenCTI instance
"""
def __init__(self, opencti):
self.opencti = opencti
self.mapping_cache = {}
def add_object_marking_refs(self, entity_type, id, object_marking_refs, version=2):
for object_marking_ref in object_marking_refs:
if version == 2:
object_marking_ref = object_marking_ref["value"]
if entity_type == "relationship":
self.opencti.stix_core_relationship.add_marking_definition(
id=id, marking_definition_id=object_marking_ref
)
elif entity_type == "sighting":
self.opencti.stix_sighting_relationship.add_marking_definition(
id=id, marking_definition_id=object_marking_ref
)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.add_marking_definition(
id=id, marking_definition_id=object_marking_ref
)
else:
self.opencti.stix_domain_object.add_marking_definition(
id=id, marking_definition_id=object_marking_ref
)
def remove_object_marking_refs(
self, entity_type, id, object_marking_refs, version=2
):
for object_marking_ref in object_marking_refs:
if version == 2:
object_marking_ref = object_marking_ref["value"]
if entity_type == "relationship":
self.opencti.stix_core_relationship.remove_marking_definition(
id=id, marking_definition_id=object_marking_ref
)
elif entity_type == "sighting":
self.opencti.stix_sighting_relationship.remove_marking_definition(
id=id, marking_definition_id=object_marking_ref
)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.remove_marking_definition(
id=id, marking_definition_id=object_marking_ref
)
else:
self.opencti.stix_domain_object.remove_marking_definition(
id=id, marking_definition_id=object_marking_ref
)
def add_external_references(self, entity_type, id, external_references, version=2):
for external_reference in external_references:
if version == 2:
external_reference = external_reference["value"]
if "url" in external_reference and "source_name" in external_reference:
url = external_reference["url"]
source_name = external_reference["source_name"]
else:
continue
external_reference_id = self.opencti.external_reference.create(
source_name=source_name,
url=url,
external_id=external_reference["external_id"]
if "external_id" in external_reference
else None,
description=external_reference["description"]
if "description" in external_reference
else None,
)["id"]
if entity_type == "relationship":
self.opencti.stix_core_relationship.add_external_reference(
id=id, external_reference_id=external_reference_id
)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.add_external_reference(
id=id, external_reference_id=external_reference_id
)
else:
self.opencti.stix_domain_object.add_external_reference(
id=id, external_reference_id=external_reference_id
)
def remove_external_references(self, entity_type, id, external_references):
for external_reference in external_references:
if entity_type == "relationship":
self.opencti.stix_core_relationship.remove_external_reference(
id=id, external_reference_id=external_reference["id"]
)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.remove_external_reference(
id=id, external_reference_id=external_reference["id"]
)
else:
self.opencti.stix_domain_object.remove_external_reference(
id=id, external_reference_id=external_reference["id"]
)
def add_kill_chain_phases(self, entity_type, id, kill_chain_phases, version=2):
for kill_chain_phase in kill_chain_phases:
if version == 2:
kill_chain_phase = kill_chain_phase["value"]
kill_chain_phase_id = self.opencti.kill_chain_phase.create(
kill_chain_name=kill_chain_phase["kill_chain_name"],
phase_name=kill_chain_phase["phase_name"],
x_opencti_order=kill_chain_phase["x_opencti_order"]
if "x_opencti_order" in kill_chain_phase
else 0,
stix_id=kill_chain_phase["id"] if "id" in kill_chain_phase else None,
)["id"]
if entity_type == "relationship":
self.opencti.stix_core_relationship.add_kill_chain_phase(
id=id, kill_chain_phase_id=kill_chain_phase_id
)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.add_kill_chain_phase(
id=id, kill_chain_phase_id=kill_chain_phase_id
)
else:
self.opencti.stix_domain_object.add_kill_chain_phase(
id=id, kill_chain_phase_id=kill_chain_phase_id
)
def remove_kill_chain_phases(self, entity_type, id, kill_chain_phases):
for kill_chain_phase in kill_chain_phases:
if entity_type == "relationship":
self.opencti.stix_core_relationship.remove_kill_chain_phase(
id=id, kill_chain_phase_id=kill_chain_phase["id"]
)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.remove_kill_chain_phase(
id=id, kill_chain_phase_id=kill_chain_phase["id"]
)
else:
self.opencti.stix_domain_object.remove_kill_chain_phase(
id=id, kill_chain_phase_id=kill_chain_phase["id"]
)
def add_object_refs(self, entity_type, id, object_refs, version=2):
for object_ref in object_refs:
if version == 2:
object_ref = object_ref["value"]
if entity_type == "report":
self.opencti.report.add_stix_object_or_stix_relationship(
id=id, stixObjectOrStixRelationshipId=object_ref
)
elif entity_type == "note":
self.opencti.note.add_stix_object_or_stix_relationship(
id=id, stixObjectOrStixRelationshipId=object_ref
)
elif entity_type == "observed-data":
self.opencti.observed_data.add_stix_object_or_stix_relationship(
id=id, stixObjectOrStixRelationshipId=object_ref
)
elif entity_type == "opinion":
self.opencti.opinion.add_stix_object_or_stix_relationship(
id=id, stixObjectOrStixRelationshipId=object_ref
)
def remove_object_refs(self, entity_type, id, object_refs, version=2):
for object_ref in object_refs:
if version == 2:
object_ref = object_ref["value"]
if entity_type == "report":
self.opencti.report.remove_stix_object_or_stix_relationship(
id=id, stixObjectOrStixRelationshipId=object_ref
)
elif entity_type == "note":
self.opencti.note.remove_stix_object_or_stix_relationship(
id=id, stixObjectOrStixRelationshipId=object_ref
)
elif entity_type == "observed-data":
self.opencti.observed_data.remove_stix_object_or_stix_relationship(
id=id, stixObjectOrStixRelationshipId=object_ref
)
elif entity_type == "opinion":
self.opencti.opinion.remove_stix_object_or_stix_relationship(
id=id, stixObjectOrStixRelationshipId=object_ref
)
def add_labels(self, entity_type, id, labels, version=2):
for label in labels:
if version == 2:
label = label["value"]
if entity_type == "relationship":
self.opencti.stix_core_relationship.add_label(id=id, label_name=label)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.add_label(id=id, label_name=label)
else:
self.opencti.stix_domain_object.add_label(id=id, label_name=label)
def remove_labels(self, entity_type, id, labels, version=2):
for label in labels:
if version == 2:
label = label["value"]
if entity_type == "relationship":
self.opencti.stix_core_relationship.remove_label(
id=id, label_name=label
)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.remove_label(id=id, label_name=label)
else:
self.opencti.stix_domain_object.remove_label(id=id, label_name=label)
def replace_created_by_ref(self, entity_type, id, created_by_ref, version=2):
if version == 2:
created_by_ref = (
created_by_ref[0]["value"] if created_by_ref is not None else None
)
if entity_type == "relationship":
self.opencti.stix_core_relationship.update_created_by(
id=id, identity_id=created_by_ref
)
elif entity_type == "sighting":
self.opencti.stix_sighting_relationship.update_created_by(
id=id, identity_id=created_by_ref
)
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.update_created_by(
id=id, identity_id=created_by_ref
)
else:
self.opencti.stix_domain_object.update_created_by(
id=id, identity_id=created_by_ref
)
def update_attribute(self, entity_type, id, input):
# Relations
if entity_type == "relationship":
self.opencti.stix_core_relationship.update_field(id=id, input=input)
elif entity_type == "sighting":
self.opencti.stix_sighting_relationship.update_field(id=id, input=input)
# Observables
elif StixCyberObservableTypes.has_value(entity_type):
self.opencti.stix_cyber_observable.update_field(id=id, input=input)
# Meta
elif entity_type == "marking-definition":
self.opencti.marking_definition.update_field(id=id, input=input)
elif entity_type == "label":
self.opencti.label.update_field(id=id, input=input)
elif entity_type == "vocabulary":
self.opencti.vocabulary.update_field(id=id, input=input)
elif entity_type == "kill-chain-phase":
self.opencti.kill_chain_phase.update_field(id=id, input=input)
elif entity_type == "external-reference":
self.opencti.external_reference.update_field(id=id, input=input)
# Remaining stix domain
else:
self.opencti.stix_domain_object.update_field(id=id, input=input)
def process_update(self, data):
try:
# Build the inputs fo update api
inputs = []
if "add" in data["x_opencti_patch"]:
for key in data["x_opencti_patch"]["add"].keys():
val = data["x_opencti_patch"]["add"][key]
values = list(map(lambda x: x["value"] if "value" in x else x, val))
inputs.append({"key": key, "value": values, "operation": "add"})
if "remove" in data["x_opencti_patch"]:
for key in data["x_opencti_patch"]["remove"].keys():
val = data["x_opencti_patch"]["remove"][key]
values = list(map(lambda x: x["value"] if "value" in x else x, val))
inputs.append({"key": key, "value": values, "operation": "remove"})
if "replace" in data["x_opencti_patch"]:
for key in data["x_opencti_patch"]["replace"].keys():
if (
key != "id"
): # ID replace is a side effect handled by the platform
val = data["x_opencti_patch"]["replace"][key]
current_val = val["current"]
if type(current_val) is list:
values = list(
map(
lambda x: x["value"]
if (type(current_val) is dict and "value" in x)
else x,
str(current_val),
)
)
inputs.append({"key": key, "value": values})
else:
values = (
current_val["value"]
if (
type(current_val) is dict and "value" in current_val
)
else str(current_val)
)
inputs.append({"key": key, "value": values})
self.update_attribute(data["type"], data["id"], inputs)
except Exception:
error_msg = traceback.format_exc()
API_LOGGER.error(error_msg)