/
utils.py
502 lines (460 loc) · 15.8 KB
/
utils.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
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
import base64
import datetime
from typing import Dict, List, Optional
from uuid import uuid4
from opentaxii.server import ServerMapping
from opentaxii.taxii2.entities import (ApiRoot, Collection, Job, JobDetail,
ManifestRecord, STIXObject,
VersionRecord)
from opentaxii.taxii2.utils import DATETIMEFORMAT, taxii2_datetimeformat
API_ROOTS_WITH_DEFAULT = (
ApiRoot(str(uuid4()), True, "first title", "first description", False),
ApiRoot(str(uuid4()), False, "second title", "second description", True),
)
API_ROOTS_WITHOUT_DEFAULT = (
ApiRoot(str(uuid4()), False, "first title", "first description", False),
ApiRoot(str(uuid4()), False, "second title", "second description", True),
ApiRoot(str(uuid4()), False, "third title", None, False),
)
API_ROOTS = API_ROOTS_WITHOUT_DEFAULT
NOW = datetime.datetime.now(datetime.timezone.utc)
JOBS = tuple()
for api_root in API_ROOTS:
JOBS = JOBS + (
Job(
str(uuid4()),
api_root.id,
"complete",
NOW,
NOW - datetime.timedelta(hours=24, minutes=1),
),
Job(str(uuid4()), api_root.id, "pending", NOW, None),
)
JOB_DETAILS_KWARGS = sorted(
(
{
"stix_id": "indicator--c410e480-e42b-47d1-9476-85307c12bcbf",
"version": datetime.datetime.strptime(
"2018-05-27T12:02:41.312Z", DATETIMEFORMAT
).replace(tzinfo=datetime.timezone.utc),
"message": "",
"status": "success",
},
{
"stix_id": "malware--664fa29d-bf65-4f28-a667-bdb76f29ec98",
"version": datetime.datetime.strptime(
"2018-05-28T14:03:42.543Z", DATETIMEFORMAT
).replace(tzinfo=datetime.timezone.utc),
"message": "Unable to process object",
"status": "failure",
},
{
"stix_id": "indicator--252c7c11-daf2-42bd-843b-be65edca9f61",
"version": datetime.datetime.strptime(
"2018-05-18T20:16:21.148Z", DATETIMEFORMAT
).replace(tzinfo=datetime.timezone.utc),
"message": "",
"status": "pending",
},
{
"stix_id": "relationship--045585ad-a22f-4333-af33-bfd503a683b5",
"version": datetime.datetime.strptime(
"2018-05-15T10:13:32.579Z", DATETIMEFORMAT
).replace(tzinfo=datetime.timezone.utc),
"message": "",
"status": "pending",
},
),
key=lambda item: item["stix_id"],
)
JOB_DETAILS = tuple(
JobDetail(id=str(uuid4()), job_id=JOBS[0].id, **kwargs)
for kwargs in JOB_DETAILS_KWARGS
)
COLLECTIONS = (
Collection(
str(uuid4()),
API_ROOTS[0].id,
"0Read only",
"Read only description",
None,
False,
),
Collection(
str(uuid4()),
API_ROOTS[0].id,
"1Write only",
"Write only description",
None,
False,
),
Collection(
str(uuid4()),
API_ROOTS[0].id,
"2Read/Write",
"Read/Write description",
None,
False,
),
Collection(
str(uuid4()),
API_ROOTS[0].id,
"3No permissions",
"No permissions description",
None,
False,
),
Collection(str(uuid4()), API_ROOTS[0].id, "4No description", "", None, False),
Collection(
str(uuid4()),
API_ROOTS[0].id,
"5With alias",
"With alias description",
"this-is-an-alias",
False,
),
Collection(
str(uuid4()),
API_ROOTS[0].id,
"6Public",
"public description",
"",
True,
),
)
STIX_OBJECTS = (
STIXObject(
f"indicator--{str(uuid4())}",
COLLECTIONS[5].id,
"indicator",
"2.0",
NOW,
NOW + datetime.timedelta(seconds=1),
{
"created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff",
"created": "2016-04-06T20:03:48.000Z",
"modified": taxii2_datetimeformat(NOW + datetime.timedelta(seconds=1)),
"indicator_types": ["malicious-activity"],
"name": "Poison Ivy Malware",
"description": "This file is part of Poison Ivy",
"pattern": "[ file:hashes.'SHA-256' = "
"'4bac27393bdd9777ce02453256c5577cd02275510b2227f473d03f533924f877' ]",
"pattern_type": "stix",
"valid_from": "2016-01-01T00:00:00Z",
},
),
STIXObject(
f"relationship--{str(uuid4())}",
COLLECTIONS[5].id,
"relationship",
"2.1",
NOW + datetime.timedelta(seconds=2),
NOW + datetime.timedelta(seconds=3),
{
"created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff",
"created": "2016-04-06T20:06:37.000Z",
"modified": taxii2_datetimeformat(NOW + datetime.timedelta(seconds=3)),
"relationship_type": "indicates",
"source_ref": "indicator--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
"target_ref": "malware--31b940d4-6f7f-459a-80ea-9c1f17b5891b",
},
),
)
STIX_OBJECTS = STIX_OBJECTS + (
STIXObject(
STIX_OBJECTS[0].id,
COLLECTIONS[5].id,
"indicator",
"2.1",
NOW + datetime.timedelta(seconds=3),
NOW + datetime.timedelta(seconds=-1),
{
"created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff",
"created": "2016-04-06T20:03:48.000Z",
"modified": taxii2_datetimeformat(NOW + datetime.timedelta(seconds=-1)),
"indicator_types": ["malicious-activity"],
"name": "Poison Ivy Malware",
"description": "This file is part of Poison Ivy",
"pattern": "[ file:hashes.'SHA-256' = "
"'4bac27393bdd9777ce02453256c5577cd02275510b2227f473d03f533924f877' ]",
"pattern_type": "stix",
"valid_from": "2016-01-01T00:00:00Z",
},
),
STIXObject(
f"indicator--{str(uuid4())}",
COLLECTIONS[1].id,
"indicator",
"2.1",
NOW,
NOW + datetime.timedelta(seconds=1),
{
"created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff",
"created": "2016-04-06T20:03:48.000Z",
"modified": taxii2_datetimeformat(NOW + datetime.timedelta(seconds=1)),
"indicator_types": ["malicious-activity"],
"name": "Poison Ivy Malware",
"description": "This file is part of Poison Ivy",
"pattern": "[ file:hashes.'SHA-256' = "
"'4bac27393bdd9777ce02453256c5577cd02275510b2227f473d03f533924f877' ]",
"pattern_type": "stix",
"valid_from": "2016-01-01T00:00:00Z",
},
),
STIXObject(
f"indicator--{str(uuid4())}",
COLLECTIONS[6].id,
"indicator",
"2.0",
NOW,
NOW + datetime.timedelta(seconds=1),
{
"created_by_ref": "identity--f431f809-377b-45e0-aa1c-6a4751cae5ff",
"created": "2016-04-06T20:03:48.000Z",
"modified": taxii2_datetimeformat(NOW + datetime.timedelta(seconds=1)),
"indicator_types": ["malicious-activity"],
"name": "Poison Ivy Malware",
"description": "This file is part of Poison Ivy",
"pattern": "[ file:hashes.'SHA-256' = "
"'4bac27393bdd9777ce02453256c5577cd02275510b2227f473d03f533924f877' ]",
"pattern_type": "stix",
"valid_from": "2016-01-01T00:00:00Z",
},
),
)
def process_match_version(match_version):
if match_version is None:
match_version = ["last"]
versions_per_id = {}
for stix_obj in STIX_OBJECTS:
if stix_obj.id not in versions_per_id:
versions_per_id[stix_obj.id] = []
versions_per_id[stix_obj.id].append(stix_obj.version)
id_version_combos = []
for value in match_version:
if value == "last":
for obj_id, versions in versions_per_id.items():
id_version_combos.append((obj_id, max(versions)))
elif value == "first":
for obj_id, versions in versions_per_id.items():
id_version_combos.append((obj_id, min(versions)))
elif value == "all":
for obj_id, versions in versions_per_id.items():
for version in versions:
id_version_combos.append((obj_id, version))
else:
for obj_id in versions_per_id:
id_version_combos.append((obj_id, value))
return id_version_combos
def GET_API_ROOT_MOCK(api_root_id):
for api_root in API_ROOTS:
if api_root.id == api_root_id:
return api_root
return None
def GET_JOB_AND_DETAILS_MOCK(api_root_id, job_id):
job_response = None
details_response = []
for job in JOBS:
if job.api_root_id == api_root_id and job.id == job_id:
job_response = job
break
if job_response is None:
return None, []
for job_detail in JOB_DETAILS:
if job_detail.job_id == job_id:
details_response.append(job_detail)
return job_response, details_response
def GET_COLLECTIONS_MOCK(api_root_id):
response = []
for collection in COLLECTIONS:
if collection.api_root_id == api_root_id:
response.append(collection)
return response
def GET_COLLECTION_MOCK(api_root_id, collection_id_or_alias):
for collection in COLLECTIONS:
if collection.api_root_id == api_root_id and (
collection.id == collection_id_or_alias
or collection.alias == collection_id_or_alias
):
return collection
return None
def STIX_OBJECT_FROM_MANIFEST(stix_id):
for obj in STIX_OBJECTS:
if obj.id == stix_id:
return obj
def GET_MANIFEST_MOCK(
collection_id: str,
limit: Optional[int] = None,
added_after: Optional[datetime.datetime] = None,
next_kwargs: Optional[Dict] = None,
match_id: Optional[List[str]] = None,
match_type: Optional[List[str]] = None,
match_version: Optional[List[str]] = None,
match_spec_version: Optional[List[str]] = None,
):
stix_objects, more, _ = GET_OBJECTS_MOCK(
collection_id=collection_id,
limit=limit,
added_after=added_after,
next_kwargs=next_kwargs,
match_id=match_id,
match_type=match_type,
match_version=match_version,
match_spec_version=match_spec_version,
)
response = [
ManifestRecord(obj.id, obj.date_added, obj.version, obj.spec_version)
for obj in stix_objects
]
return response, more
def GET_NEXT_PARAM(kwargs: Dict) -> str:
return base64.b64encode(
f"{kwargs['date_added'].isoformat()}|{kwargs['id']}".encode("utf-8")
).decode()
def GET_OBJECTS_MOCK(
collection_id: str,
limit: Optional[int] = None,
added_after: Optional[datetime.datetime] = None,
next_kwargs: Optional[Dict] = None,
match_id: Optional[List[str]] = None,
match_type: Optional[List[str]] = None,
match_version: Optional[List[str]] = None,
match_spec_version: Optional[List[str]] = None,
):
id_version_combos = process_match_version(match_version)
response = []
more = False
for stix_object in STIX_OBJECTS:
if (
stix_object.collection_id == collection_id
and (stix_object.id, stix_object.version) in id_version_combos
):
if limit is not None and limit == len(response):
more = True
break
if added_after is not None:
if stix_object.date_added <= added_after:
continue
if next_kwargs is not None:
if stix_object.date_added < next_kwargs["date_added"] or (
stix_object.date_added == next_kwargs["date_added"]
and stix_object.id == next_kwargs["id"]
):
continue
if match_id is not None and stix_object.id not in match_id:
continue
if match_type is not None and stix_object.type not in match_type:
continue
if (
match_spec_version is not None
and stix_object.spec_version not in match_spec_version
):
continue
response.append(stix_object)
if more:
next_param = GET_NEXT_PARAM(
{"id": response[-1].id, "date_added": response[-1].date_added}
)
else:
next_param = None
return response, more, next_param
def GET_OBJECT_MOCK(
collection_id: str,
object_id: str,
limit: Optional[int] = None,
added_after: Optional[datetime.datetime] = None,
next_kwargs: Optional[Dict] = None,
match_version: Optional[List[str]] = None,
match_spec_version: Optional[List[str]] = None,
):
id_version_combos = process_match_version(match_version)
response = []
more = False
at_least_one = False
for stix_object in STIX_OBJECTS:
if stix_object.collection_id == collection_id and stix_object.id == object_id:
at_least_one = True
if (
stix_object.id,
stix_object.version,
) not in id_version_combos:
continue
if limit is not None and limit == len(response):
more = True
break
if added_after is not None:
if stix_object.date_added <= added_after:
continue
if next_kwargs is not None:
if stix_object.date_added < next_kwargs["date_added"] or (
stix_object.date_added == next_kwargs["date_added"]
and stix_object.id == next_kwargs["id"]
):
continue
if (
match_spec_version is not None
and stix_object.spec_version not in match_spec_version
):
continue
response.append(stix_object)
if more:
next_param = GET_NEXT_PARAM(
{"id": response[-1].id, "date_added": response[-1].date_added}
)
else:
next_param = None
if not at_least_one:
response = None
return response, more, next_param
def ADD_OBJECTS_MOCK(api_root_id: str, collection_id: str, objects: List[Dict]):
return (JOBS[0], JOB_DETAILS)
def DELETE_OBJECT_MOCK(
collection_id: str,
object_id: str,
match_version: Optional[List[str]] = None,
match_spec_version: Optional[List[str]] = None,
):
return
def GET_VERSIONS_MOCK(
collection_id: str,
object_id: str,
limit: Optional[int] = None,
added_after: Optional[datetime.datetime] = None,
next_kwargs: Optional[Dict] = None,
match_spec_version: Optional[List[str]] = None,
):
versions, more, _ = GET_OBJECT_MOCK(
collection_id=collection_id,
object_id=object_id,
limit=limit,
added_after=added_after,
next_kwargs=next_kwargs,
match_spec_version=match_spec_version,
match_version=["all"],
)
return (
[VersionRecord(obj.date_added, obj.version) for obj in versions]
if versions is not None
else None,
more,
)
def config_noop(config):
return config
def config_remove_fields(*fields):
def inner(config):
for field in fields:
del config[field]
return config
return inner
def config_override(override):
def inner(config):
return {**config, **override}
return inner
def server_mapping_remove_fields(*fields):
def inner(original):
override = {field: None for field in fields}
kwargs = {**dict(original._asdict()), **override}
return ServerMapping(**kwargs)
return inner
def server_mapping_noop(original):
return original