forked from OpenCTI-Platform/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencti_marking_definition.py
350 lines (324 loc) · 12.6 KB
/
opencti_marking_definition.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
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
# coding: utf-8
import json
import uuid
from stix2.canonicalization.Canonicalize import canonicalize
class MarkingDefinition:
def __init__(self, opencti):
self.opencti = opencti
self.properties = """
id
standard_id
entity_type
parent_types
definition_type
definition
x_opencti_order
x_opencti_color
created
modified
created_at
updated_at
"""
@staticmethod
def generate_id(definition, definition_type):
data = {"definition": definition, "definition_type": definition_type}
data = canonicalize(data, utf8=False)
id = str(uuid.uuid5(uuid.UUID("00abedb4-aa42-466c-9c01-fed23315a9b7"), data))
return "marking-definition--" + id
"""
List Marking-Definition objects
:param filters: the filters to apply
:param first: return the first n rows from the after ID (or the beginning if not set)
:param after: ID of the first row for pagination
:return List of Marking-Definition objects
"""
def list(self, **kwargs):
filters = kwargs.get("filters", None)
first = kwargs.get("first", 500)
after = kwargs.get("after", None)
order_by = kwargs.get("orderBy", None)
order_mode = kwargs.get("orderMode", None)
custom_attributes = kwargs.get("customAttributes", None)
get_all = kwargs.get("getAll", False)
with_pagination = kwargs.get("withPagination", False)
if get_all:
first = 500
self.opencti.app_logger.info(
"Listing Marking-Definitions with filters", {"filters": json.dumps(filters)}
)
query = (
"""
query MarkingDefinitions($filters: FilterGroup, $first: Int, $after: ID, $orderBy: MarkingDefinitionsOrdering, $orderMode: OrderingMode) {
markingDefinitions(filters: $filters, first: $first, after: $after, orderBy: $orderBy, orderMode: $orderMode) {
edges {
node {
"""
+ (custom_attributes if custom_attributes is not None else self.properties)
+ """
}
}
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
globalCount
}
}
}
"""
)
result = self.opencti.query(
query,
{
"filters": filters,
"first": first,
"after": after,
"orderBy": order_by,
"orderMode": order_mode,
},
)
return self.opencti.process_multiple(
result["data"]["markingDefinitions"], with_pagination
)
"""
Read a Marking-Definition object
:param id: the id of the Marking-Definition
:param filters: the filters to apply if no id provided
:return Marking-Definition object
"""
def read(self, **kwargs):
id = kwargs.get("id", None)
filters = kwargs.get("filters", None)
if id is not None:
self.opencti.app_logger.info("Reading Marking-Definition", {"id": id})
query = (
"""
query MarkingDefinition($id: String!) {
markingDefinition(id: $id) {
"""
+ self.properties
+ """
}
}
"""
)
result = self.opencti.query(query, {"id": id})
return self.opencti.process_multiple_fields(
result["data"]["markingDefinition"]
)
elif filters is not None:
result = self.list(filters=filters)
if len(result) > 0:
return result[0]
else:
return None
else:
self.opencti.app_logger.error(
"[opencti_marking_definition] Missing parameters: id or filters"
)
return None
"""
Create a Marking-Definition object
:param definition_type: the definition_type
:param definition: the definition
:return Marking-Definition object
"""
def create(self, **kwargs):
stix_id = kwargs.get("stix_id", None)
created = kwargs.get("created", None)
modified = kwargs.get("modified", None)
definition_type = kwargs.get("definition_type", None)
definition = kwargs.get("definition", None)
x_opencti_order = kwargs.get("x_opencti_order", 0)
x_opencti_color = kwargs.get("x_opencti_color", None)
x_opencti_stix_ids = kwargs.get("x_opencti_stix_ids", None)
update = kwargs.get("update", False)
if definition is not None and definition_type is not None:
query = (
"""
mutation MarkingDefinitionAdd($input: MarkingDefinitionAddInput!) {
markingDefinitionAdd(input: $input) {
"""
+ self.properties
+ """
}
}
"""
)
result = self.opencti.query(
query,
{
"input": {
"definition_type": definition_type,
"definition": definition,
"x_opencti_order": x_opencti_order,
"x_opencti_color": x_opencti_color,
"stix_id": stix_id,
"created": created,
"modified": modified,
"x_opencti_stix_ids": x_opencti_stix_ids,
"update": update,
}
},
)
return self.opencti.process_multiple_fields(
result["data"]["markingDefinitionAdd"]
)
else:
self.opencti.app_logger.error(
"[opencti_marking_definition] Missing parameters: definition and definition_type",
)
"""
Update a Marking definition object field
:param id: the Marking definition id
:param input: the input of the field
:return The updated Marking definition object
"""
def update_field(self, **kwargs):
id = kwargs.get("id", None)
input = kwargs.get("input", None)
if id is not None and input is not None:
self.opencti.app_logger.info("Updating Marking Definition", {"id": id})
query = """
mutation MarkingDefinitionEdit($id: ID!, $input: [EditInput]!) {
markingDefinitionEdit(id: $id) {
fieldPatch(input: $input) {
id
standard_id
entity_type
}
}
}
"""
result = self.opencti.query(
query,
{
"id": id,
"input": input,
},
)
return self.opencti.process_multiple_fields(
result["data"]["markingDefinitionEdit"]["fieldPatch"]
)
else:
self.opencti.app_logger.error(
"[opencti_marking_definition] Missing parameters: id and key and value"
)
return None
"""
Import an Marking Definition object from a STIX2 object
:param stixObject: the MarkingDefinition
:return MarkingDefinition object
"""
def import_from_stix2(self, **kwargs):
stix_object = kwargs.get("stixObject", None)
update = kwargs.get("update", False)
if stix_object is not None:
if (
"x_opencti_definition_type" in stix_object
and "x_opencti_definition" in stix_object
):
definition_type = stix_object["x_opencti_definition_type"]
definition = stix_object["x_opencti_definition"]
elif "definition_type" in stix_object:
definition_type = stix_object["definition_type"]
definition = None
if stix_object["definition_type"] == "tlp":
definition_type = definition_type.upper()
if "definition" in stix_object:
definition = (
definition_type
+ ":"
+ stix_object["definition"]["tlp"].upper()
)
elif "name" in stix_object:
definition = stix_object["name"]
else:
if "definition" in stix_object:
if isinstance(stix_object["definition"], str):
definition = stix_object["definition"]
elif (
isinstance(stix_object["definition"], dict)
and stix_object["definition_type"]
in stix_object["definition"]
):
definition = stix_object["definition"][
stix_object["definition_type"]
]
else:
definition = stix_object["name"]
elif "name" in stix_object:
definition = stix_object["name"]
elif "name" in stix_object:
if ":" in stix_object["name"]:
definition_type = stix_object["name"].split(":")[0]
else:
definition_type = "statement"
definition = stix_object["name"]
else:
return None
# Replace TLP:WHITE
if definition == "TLP:WHITE":
definition = "TLP:CLEAR"
# Search in extensions
if (
"x_opencti_order" not in stix_object
and self.opencti.get_attribute_in_extension("order", stix_object)
is not None
):
stix_object["x_opencti_order"] = (
self.opencti.get_attribute_in_extension("order", stix_object)
)
if "x_opencti_color" not in stix_object:
stix_object["x_opencti_color"] = (
self.opencti.get_attribute_in_extension("color", stix_object)
)
if "x_opencti_stix_ids" not in stix_object:
stix_object["x_opencti_stix_ids"] = (
self.opencti.get_attribute_in_extension("stix_ids", stix_object)
)
return self.opencti.marking_definition.create(
stix_id=stix_object["id"],
created=stix_object["created"] if "created" in stix_object else None,
modified=stix_object["modified"] if "modified" in stix_object else None,
definition_type=definition_type,
definition=definition,
x_opencti_order=(
stix_object["x_opencti_order"]
if "x_opencti_order" in stix_object
else 0
),
x_opencti_color=(
stix_object["x_opencti_color"]
if "x_opencti_color" in stix_object
else None
),
x_opencti_stix_ids=(
stix_object["x_opencti_stix_ids"]
if "x_opencti_stix_ids" in stix_object
else None
),
update=update,
)
else:
self.opencti.app_logger.error(
"[opencti_marking_definition] Missing parameters: stixObject"
)
def delete(self, **kwargs):
id = kwargs.get("id", None)
if id is not None:
self.opencti.app_logger.info("Deleting Marking-Definition", {"id": id})
query = """
mutation MarkingDefinitionEdit($id: ID!) {
markingDefinitionEdit(id: $id) {
delete
}
}
"""
self.opencti.query(query, {"id": id})
else:
self.opencti.app_logger.error(
"[opencti_marking_definition] Missing parameters: id"
)
return None