This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_ace_api.py
792 lines (636 loc) · 32.2 KB
/
test_ace_api.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# vim: ts=4:sw=4:et:cc=120
import datetime
import hashlib
import io
import logging
import os.path
import pickle
import shutil
import tempfile
import uuid
import ace_api
import saq
from saq.analysis import RootAnalysis
from saq.constants import *
from saq.database import acquire_lock, use_db
from saq.test import *
from aceapi.cloudphish.test import CloudphishTestCase, TEST_URL
from saq.util import *
import pytz
import tzlocal
class TestCase(ACEEngineTestCase):
def setUp(self, *args, **kwargs):
super().setUp(*args, **kwargs)
self.start_api_server()
ace_api.set_default_remote_host(saq.API_PREFIX)
ace_api.set_default_ssl_ca_path(saq.CONFIG['SSL']['ca_chain_path'])
def test_ping(self):
result = ace_api.ping()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
self.assertEquals(result['result'], 'pong')
def test_get_supported_api_version(self):
result = ace_api.get_supported_api_version()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
self.assertEquals(result['result'], 1)
@use_db
def test_get_valid_companies(self, db, c):
result = ace_api.get_valid_companies()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
self.assertTrue(isinstance(result['result'], list))
lookup = {}
c.execute("SELECT id, name FROM company")
for _id, _name in c:
lookup[_id] = _name
self.assertEquals(len(lookup), len(result['result']))
for r in result['result']:
self.assertTrue(r['id'] in lookup and lookup[r['id']] == r['name'])
result = ace_api.get_valid_companies()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
self.assertTrue(isinstance(result['result'], list))
def test_get_valid_observables(self):
from saq.constants import VALID_OBSERVABLE_TYPES, OBSERVABLE_DESCRIPTIONS, DEPRECATED_OBSERVABLES
result = ace_api.get_valid_observables()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
self.assertTrue(isinstance(result['result'], list))
for r in result['result']:
self.assertTrue(r['name'] in VALID_OBSERVABLE_TYPES)
self.assertEquals(OBSERVABLE_DESCRIPTIONS[r['name']], r['description'])
active_observables = set(VALID_OBSERVABLE_TYPES) - set(DEPRECATED_OBSERVABLES)
self.assertEquals(len(active_observables), len(result['result']))
def test_get_valid_directives(self):
from saq.constants import VALID_DIRECTIVES, DIRECTIVE_DESCRIPTIONS
result = ace_api.get_valid_directives()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
self.assertTrue(isinstance(result['result'], list))
for r in result['result']:
self.assertTrue(r['name'] in VALID_DIRECTIVES)
self.assertEquals(DIRECTIVE_DESCRIPTIONS[r['name']], r['description'])
def _get_submit_time(self):
return datetime.datetime(2017, 11, 11, hour=7, minute=36, second=1)
def _get_localized_submit_time(self):
#return ace_api.LOCAL_TIMEZONE.localize(self._get_submit_time()).astimezone(pytz.UTC)
return ace_api.LOCAL_TIMEZONE.localize(self._get_submit_time()).astimezone(pytz.timezone('Etc/UTC'))
def _submit(self, analysis_mode=None,
tool=None,
tool_instance=None,
type=None,
description=None,
details=None,
event_time=None,
observables=None,
tags=None):
temp_path = os.path.join(saq.SAQ_HOME, saq.TEMP_DIR, 'submit_test.dat')
temp_data = os.urandom(1024)
with open(temp_path, 'wb') as fp:
fp.write(temp_data)
try:
with open(temp_path, 'rb') as fp:
return ace_api.submit(
analysis_mode='test_empty' if analysis_mode is None else analysis_mode,
tool='unittest_tool' if tool is None else tool,
tool_instance='unittest_tool_instance' if tool_instance is None else tool_instance,
type='unittest_type' if type is None else type,
description='testing' if description is None else description,
details={'hello': 'world'} if details is None else details,
event_time=self._get_submit_time() if event_time is None else event_time,
observables=[
{ 'type': 'ipv4', 'value': '1.2.3.4', 'time': self._get_submit_time(), 'tags': [ 'tag_1', 'tag_2' ], 'directives': [ 'no_scan' ], 'limited_analysis': ['basic_test'] },
{ 'type': 'user', 'value': 'test_user', 'time': self._get_submit_time() },
] if observables is None else observables,
tags=[ 'alert_tag_1', 'alert_tag_2' ] if tags is None else tags,
files=[('sample.dat', io.BytesIO(b'Hello, world!')),
('submit_test.dat', fp)])
finally:
os.remove(temp_path)
@use_db
def test_submit(self, db, c):
result = self._submit()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertIsNotNone(result['uuid'])
uuid = result['uuid']
# make sure this actually uploaded
root = RootAnalysis(storage_dir=workload_storage_dir(uuid))
root.load()
self.assertEquals(root.analysis_mode, 'test_empty')
self.assertEquals(root.tool, 'unittest_tool')
self.assertEquals(root.tool_instance, 'unittest_tool_instance')
self.assertEquals(root.alert_type, 'unittest_type')
self.assertEquals(root.description, 'testing')
self.assertEquals(root.details, {'hello': 'world'})
self.assertEquals(root.event_time, self._get_localized_submit_time())
self.assertEquals(root.tags[0].name, 'alert_tag_1')
self.assertEquals(root.tags[1].name, 'alert_tag_2')
# NOTE that this is 4 instead of 2 since adding a file adds a F_FILE observable type
self.assertEquals(len(root.all_observables), 4)
o = root.find_observable(lambda o: o.type == 'ipv4')
self.assertIsNotNone(o)
self.assertEquals(o.value, '1.2.3.4')
self.assertEquals(len(o.tags), 2)
self.assertTrue(o.has_directive('no_scan'))
self.assertTrue('basic_test' in o.limited_analysis)
o = root.find_observable(lambda o: o.type == 'file' and o.value == 'sample.dat')
self.assertIsNotNone(o)
with open(os.path.join(root.storage_dir, o.value), 'rb') as fp:
self.assertEquals(fp.read(), b'Hello, world!')
o = root.find_observable(lambda o: o.type == 'file' and o.value == 'submit_test.dat')
self.assertIsNotNone(o)
self.assertEquals(os.path.getsize(os.path.join(root.storage_dir, o.value)), 1024)
# we should see a single workload entry
c.execute("SELECT id, uuid, node_id, analysis_mode FROM workload WHERE uuid = %s", (uuid,))
row = c.fetchone()
self.assertIsNotNone(row)
self.assertIsNotNone(row[0])
self.assertEquals(row[1], uuid)
self.assertIsNotNone(row[2])
self.assertEquals(row[3], 'test_empty')
@use_db
def test_resubmit(self, db, c):
# submit something so we have something to resubmit
result = self._submit(analysis_mode=ANALYSIS_MODE_CORRELATION)
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertIsNotNone(result['uuid'])
uuid = result['uuid']
# make sure this actually uploaded
root = RootAnalysis(storage_dir=storage_dir_from_uuid(uuid))
root.load()
self.assertEquals(root.analysis_mode, ANALYSIS_MODE_CORRELATION)
self.assertEquals(root.tool, 'unittest_tool')
self.assertEquals(root.tool_instance, 'unittest_tool_instance')
self.assertEquals(root.alert_type, 'unittest_type')
self.assertEquals(root.description, 'testing')
self.assertEquals(root.details, {'hello': 'world'})
self.assertEquals(root.event_time, self._get_localized_submit_time())
self.assertEquals(root.tags[0].name, 'alert_tag_1')
self.assertEquals(root.tags[1].name, 'alert_tag_2')
# NOTE that this is 4 instead of 2 since adding a file adds a F_FILE observable type
self.assertEquals(len(root.all_observables), 4)
o = root.find_observable(lambda o: o.type == 'ipv4')
self.assertIsNotNone(o)
self.assertEquals(o.value, '1.2.3.4')
self.assertEquals(len(o.tags), 2)
self.assertTrue(o.has_directive('no_scan'))
self.assertTrue('basic_test' in o.limited_analysis)
o = root.find_observable(lambda o: o.type == 'file' and o.value == 'sample.dat')
self.assertIsNotNone(o)
with open(os.path.join(root.storage_dir, o.value), 'rb') as fp:
self.assertEquals(fp.read(), b'Hello, world!')
o = root.find_observable(lambda o: o.type == 'file' and o.value == 'submit_test.dat')
self.assertIsNotNone(o)
self.assertEquals(os.path.getsize(os.path.join(root.storage_dir, o.value)), 1024)
# we should see a single workload entry
c.execute("SELECT id, uuid, node_id, analysis_mode FROM workload WHERE uuid = %s", (uuid,))
row = c.fetchone()
self.assertIsNotNone(row)
self.assertIsNotNone(row[0])
self.assertEquals(row[1], uuid)
self.assertIsNotNone(row[2])
self.assertEquals(row[3], ANALYSIS_MODE_CORRELATION)
# now resubmit the alert
result = ace_api.resubmit_alert(uuid)
self.assertFalse('error' in result)
def test_submit_with_utc_timezone(self):
# make sure we can submit with a UTC timezone already set
result = self._submit(event_time=self._get_localized_submit_time())
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertIsNotNone(result['uuid'])
uuid = result['uuid']
root = RootAnalysis(storage_dir=workload_storage_dir(uuid))
root.load()
self.assertEquals(root.event_time, self._get_localized_submit_time())
def test_submit_with_other_timezone(self):
# make sure we can submit with another timezone already set
result = self._submit(event_time=self._get_localized_submit_time().astimezone(pytz.timezone('US/Eastern')))
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertIsNotNone(result['uuid'])
uuid = result['uuid']
root = RootAnalysis(storage_dir=workload_storage_dir(uuid))
root.load()
self.assertEquals(root.event_time, self._get_localized_submit_time())
def test_get_analysis(self):
result = self._submit()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertIsNotNone(result['uuid'])
uuid = result['uuid']
result = ace_api.get_analysis(uuid)
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertEquals(result['analysis_mode'], 'test_empty')
self.assertEquals(result['tool'], 'unittest_tool')
self.assertEquals(result['tool_instance'], 'unittest_tool_instance')
self.assertEquals(result['type'], 'unittest_type')
self.assertEquals(result['description'], 'testing')
self.assertEquals(result['event_time'], '2017-11-11T07:36:01.000000+0000')
self.assertEquals(result['tags'][0], 'alert_tag_1')
self.assertEquals(result['tags'][1], 'alert_tag_2')
self.assertEquals(len(result['observable_store']), 4)
# the details should be a file_path reference
self.assertTrue(isinstance(result['details'], dict))
self.assertTrue('file_path' in result['details'])
self.assertTrue(result['details']['file_path'].startswith('RootAnalysis_'))
def test_get_analysis_details(self):
result = self._submit()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertIsNotNone(result['uuid'])
uuid = result['uuid']
result = ace_api.get_analysis(uuid)
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
details_result = ace_api.get_analysis_details(uuid, result['details']['file_path'])
self.assertIsNotNone(details_result)
details_result = details_result['result']
self.assertTrue('hello' in details_result)
self.assertEquals(details_result['hello'], 'world')
def test_get_analysis_file(self):
result = self._submit()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertIsNotNone(result['uuid'])
uuid = result['uuid']
result = ace_api.get_analysis(uuid)
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
# first test getting a file by uuid
file_uuid = None
for o_uuid in result['observables']:
o = result['observable_store'][o_uuid]
if o['type'] == 'file' and o['value'] == 'sample.dat':
file_uuid = o_uuid
break
self.assertIsNotNone(file_uuid)
output_path = os.path.join(saq.SAQ_HOME, saq.TEMP_DIR, 'get_file_test.dat')
self.assertTrue(ace_api.get_analysis_file(uuid, file_uuid, output_file=output_path))
with open(output_path, 'rb') as fp:
self.assertEquals(fp.read(), b'Hello, world!')
# same thing but with passing a file pointer
with open(output_path, 'wb') as fp:
self.assertTrue(ace_api.get_analysis_file(uuid, file_uuid, output_fp=fp))
# now test by using the file name
self.assertTrue(ace_api.get_analysis_file(uuid, 'sample.dat', output_file=output_path))
with open(output_path, 'rb') as fp:
self.assertEquals(fp.read(), b'Hello, world!')
def test_get_analysis_status(self):
result = self._submit()
self.assertIsNotNone(result)
self.assertTrue('result' in result)
result = result['result']
self.assertIsNotNone(result['uuid'])
uuid = result['uuid']
result = ace_api.get_analysis_status(uuid)
self.assertIsNotNone(result)
result = result['result']
self.assertTrue('workload' in result)
self.assertTrue('delayed_analysis' in result)
self.assertTrue('locks' in result)
self.assertTrue('alert' in result)
self.assertEquals(result['alert'], None)
self.assertEquals(result['delayed_analysis'], [])
self.assertIsNone(result['locks'])
self.assertTrue(isinstance(result['workload']['id'], int))
self.assertEquals(result['workload']['uuid'], uuid)
self.assertIsNotNone(result['workload']['node_id'])
self.assertEquals(result['workload']['analysis_mode'], 'test_empty')
self.assertTrue(isinstance(parse_event_time(result['workload']['insert_date']), datetime.datetime))
def test_download(self):
root = create_root_analysis(uuid=str(uuid.uuid4()))
root.initialize_storage()
root.details = { 'hello': 'world' }
root.save()
temp_dir = tempfile.mkdtemp(dir=saq.TEMP_DIR)
try:
result = ace_api.download(root.uuid, temp_dir)
self.assertTrue(os.path.join(temp_dir, 'data.json'))
root = RootAnalysis(storage_dir=temp_dir)
root.load()
self.assertEquals(root.details, { 'hello': 'world' })
finally:
shutil.rmtree(temp_dir)
def test_upload(self):
root = create_root_analysis(uuid=str(uuid.uuid4()), storage_dir=os.path.join(saq.TEMP_DIR, 'unittest'))
root.initialize_storage()
root.details = { 'hello': 'world' }
root.save()
result = ace_api.upload(root.uuid, root.storage_dir)
self.assertTrue(result['result'])
# uploads go straight into saq.DATA_DIR
# XXX I don't think we need uploads at all
root = RootAnalysis(storage_dir=storage_dir_from_uuid(root.uuid))
root.load()
self.assertEquals(root.details, { 'hello': 'world' })
def test_clear(self):
root = create_root_analysis(uuid=str(uuid.uuid4()))
root.initialize_storage()
root.details = { 'hello': 'world' }
root.save()
self.assertTrue(os.path.exists(root.storage_dir))
lock_uuid = str(uuid.uuid4())
self.assertTrue(acquire_lock(root.uuid, lock_uuid))
result = ace_api.clear(root.uuid, lock_uuid)
self.assertFalse(os.path.exists(root.storage_dir))
def test_clear_invalid_lock_uuid(self):
root = create_root_analysis(uuid=str(uuid.uuid4()))
root.initialize_storage()
root.details = { 'hello': 'world' }
root.save()
self.assertTrue(os.path.exists(root.storage_dir))
lock_uuid = str(uuid.uuid4())
self.assertTrue(acquire_lock(root.uuid, lock_uuid))
lock_uuid = str(uuid.uuid4())
with self.assertRaises(Exception):
self.assertFalse(ace_api.clear(root.uuid, lock_uuid))
self.assertTrue(os.path.exists(root.storage_dir))
def test_legacy_submit(self):
alert = ace_api.Alert(description='Test Alert')
alert.add_observable(F_IPV4, '1.2.3.4', local_time(), directives=[DIRECTIVE_NO_SCAN])
alert.add_tag('test')
temp_path = os.path.join(saq.TEMP_DIR, 'test.txt')
with open(temp_path, 'w') as fp:
fp.write('test')
alert.add_attachment_link(temp_path, 'dest/test.txt')
alert.submit(f'https://{saq.API_PREFIX}', ssl_verification=saq.CONFIG['SSL']['ca_chain_path'])
self.assertTrue(validate_uuid(alert.uuid))
root = RootAnalysis(storage_dir=storage_dir_from_uuid(alert.uuid))
root.load()
self.assertEquals(root.description, 'Test Alert')
ipv4_observable = root.find_observable(lambda o: o.type == F_IPV4)
self.assertIsNotNone(ipv4_observable)
self.assertEquals(ipv4_observable.value, '1.2.3.4')
self.assertTrue(ipv4_observable.has_directive(DIRECTIVE_NO_SCAN))
file_observable = root.find_observable(lambda o: o.type == F_FILE)
self.assertIsNotNone(file_observable)
self.assertEquals(file_observable.value, 'dest/test.txt')
with open(os.path.join(root.storage_dir, file_observable.value), 'r') as fp:
self.assertEquals(fp.read(), 'test')
def test_legacy_import(self):
from ace_client_lib.client import Alert
from ace_api import Alert as Alert_2
# these two should be the same thing
self.assertTrue(Alert is Alert_2)
def test_legacy_failed_submit(self):
self.stop_api_server()
alert = ace_api.Alert(description='Test Alert')
alert.add_observable(F_IPV4, '1.2.3.4', local_time(), directives=[DIRECTIVE_NO_SCAN])
alert.add_tag('test')
temp_path = os.path.join(saq.TEMP_DIR, 'test.txt')
with open(temp_path, 'w') as fp:
fp.write('test')
alert.add_attachment_link(temp_path, 'dest/test.txt')
with self.assertRaises(Exception):
alert.submit(f'https://{saq.API_PREFIX}', ssl_verification=saq.CONFIG['SSL']['ca_chain_path'])
self.assertEquals(log_count('unable to submit '), 1)
# the .saq_alerts directory should have a single subdirectory
dir_list = os.listdir('.saq_alerts')
self.assertEquals(len(dir_list), 1)
# load the alert
target_path = os.path.join('.saq_alerts', dir_list[0], 'alert')
with open(target_path, 'rb') as fp:
new_alert = pickle.load(fp)
self.assertEquals(new_alert.submit_kwargs, alert.submit_kwargs)
def test_failed_submit(self):
self.stop_api_server()
analysis = ace_api.Analysis(description='Test Analysis submit')
analysis.add_observable(F_IPV4, '1.2.3.4', local_time(), directives=[DIRECTIVE_NO_SCAN])
analysis.add_tag('test')
analysis.add_user('test_user')
temp_path = os.path.join(saq.TEMP_DIR, 'test.txt')
with open(temp_path, 'w') as fp:
fp.write('test')
analysis.add_file(temp_path, relative_storage_path='dest/test.txt')
with self.assertRaises(Exception):
analysis.submit(f'https://{saq.API_PREFIX}', ssl_verification=saq.CONFIG['SSL']['ca_chain_path'])
self.assertEquals(log_count('unable to submit '), 1)
# the .saq_alerts directory should have a single subdirectory
dir_list = os.listdir('.saq_alerts')
self.assertEquals(len(dir_list), 1)
# load the analysis object
target_path = os.path.join('.saq_alerts', dir_list[0], 'alert')
with open(target_path, 'rb') as fp:
new_analysis = pickle.load(fp)
self.assertEquals(new_analysis.submit_kwargs, analysis.submit_kwargs)
def test_submit_failed_alerts(self):
self.stop_api_server()
alert = ace_api.Alert(description='Test Alert')
alert.add_observable(F_IPV4, '1.2.3.4', local_time(), directives=[DIRECTIVE_NO_SCAN])
alert.add_tag('test')
temp_path = os.path.join(saq.TEMP_DIR, 'test.txt')
with open(temp_path, 'w') as fp:
fp.write('test')
alert.add_attachment_link(temp_path, 'dest/test.txt')
with self.assertRaises(Exception):
uuid = alert.submit(f'https://{saq.API_PREFIX}', ssl_verification=saq.CONFIG['SSL']['ca_chain_path'])
self.assertEquals(log_count('unable to submit '), 1)
# the .saq_alerts directory should have a single subdirectory
dir_list = os.listdir('.saq_alerts')
self.assertEquals(len(dir_list), 1)
# load the alert
target_path = os.path.join('.saq_alerts', dir_list[0], 'alert')
with open(target_path, 'rb') as fp:
new_alert = pickle.load(fp)
self.assertEquals(new_alert.submit_kwargs, alert.submit_kwargs)
# try to submit it using submit_failed_alerts
self.start_api_server()
ace_api.submit_failed_alerts(delete_on_success=True)
# this directory should be cleared out
dir_list = os.listdir('.saq_alerts')
self.assertEquals(len(dir_list), 0)
def test_submit_failed_analysis(self):
self.stop_api_server()
analysis = ace_api.Analysis(description='Test Analysis')
analysis.add_observable(F_IPV4, '1.2.3.4', local_time(), directives=[DIRECTIVE_NO_SCAN])
analysis.add_tag('test')
temp_path = os.path.join(saq.TEMP_DIR, 'test.txt')
with open(temp_path, 'w') as fp:
fp.write('test')
analysis.add_file(temp_path, relative_storage_path='dest/test.txt')
with self.assertRaises(Exception):
uuid = analysis.submit(f'https://{saq.API_PREFIX}', ssl_verification=saq.CONFIG['SSL']['ca_chain_path'])
self.assertEquals(log_count('unable to submit '), 1)
# the .saq_alerts directory should have a single subdirectory
dir_list = os.listdir('.saq_alerts')
self.assertEquals(len(dir_list), 1)
# load the alert
target_path = os.path.join('.saq_alerts', dir_list[0], 'alert')
with open(target_path, 'rb') as fp:
new_analysis = pickle.load(fp)
self.assertEquals(new_analysis.submit_kwargs, analysis.submit_kwargs)
# did we actually write data to the file?
data_test = None
with open(os.path.join('.saq_alerts', new_analysis.uuid, 'dest/test.txt'), 'r') as fp:
data_test = fp.read()
self.assertEquals(data_test, 'test')
# try to submit it using submit_failed_alerts
self.start_api_server()
ace_api.submit_failed_alerts(delete_on_success=True)
# this directory should be cleared out
dir_list = os.listdir('.saq_alerts')
self.assertEquals(len(dir_list), 0)
def test_analysis_file_handling(self):
analysis = ace_api.Analysis(description='Test Analysis')
# add a normal file
normal_file_path = self.create_test_file('normal.txt')
analysis.add_file(normal_file_path)
# add a normal file, passing in the file pointer
fp_file_path = self.create_test_file('fp.txt')
fp = open(fp_file_path, 'rb')
analysis.add_file(fp_file_path, fp)
# add a normal file but tell it to go into a subdirectory
subdir_file_path = self.create_test_file('subdir.txt')
analysis.add_file(subdir_file_path, relative_storage_path='subdir/subdir.txt')
# add a file passing the contents as a string
analysis.add_file('str.txt', 'This is a string.')
# add a file passing the contents as a bytes
analysis.add_file('bytes.txt', b'This is a bytes.')
result = analysis.submit()
# make sure it got our files
io_buffer = io.BytesIO()
ace_api.get_analysis_file(result.uuid, 'normal.txt', output_fp=io_buffer)
with open(normal_file_path, 'rb') as fp:
self.assertEquals(fp.read(), io_buffer.getvalue())
io_buffer = io.BytesIO()
ace_api.get_analysis_file(result.uuid, 'fp.txt', output_fp=io_buffer)
with open(fp_file_path, 'rb') as fp:
self.assertEquals(fp.read(), io_buffer.getvalue())
#io_buffer = io.BytesIO()
#ace_api.get_analysis_file(result.uuid, 'subdir/subdir.txt', output_fp=io_buffer)
#with open(subdir_file_path, 'rb') as fp:
#self.assertEquals(fp.read(), io_buffer.getvalue())
io_buffer = io.BytesIO()
ace_api.get_analysis_file(result.uuid, 'str.txt', output_fp=io_buffer)
self.assertEquals(b'This is a string.', io_buffer.getvalue())
io_buffer = io.BytesIO()
ace_api.get_analysis_file(result.uuid, 'bytes.txt', output_fp=io_buffer)
self.assertEquals(b'This is a bytes.', io_buffer.getvalue())
class CloudphishAPITestCase(CloudphishTestCase, ACEEngineTestCase):
def setUp(self, *args, **kwargs):
super().setUp(*args, **kwargs)
self.start_api_server()
ace_api.set_default_remote_host(saq.API_PREFIX)
ace_api.set_default_ssl_ca_path(saq.CONFIG['SSL']['ca_chain_path'])
def test_cloudphish_api(self):
import saq.cloudphish
submission_result = ace_api.cloudphish_submit(TEST_URL)
for key in [ saq.cloudphish.KEY_RESULT,
saq.cloudphish.KEY_DETAILS,
saq.cloudphish.KEY_STATUS,
saq.cloudphish.KEY_ANALYSIS_RESULT,
saq.cloudphish.KEY_HTTP_RESULT,
saq.cloudphish.KEY_HTTP_MESSAGE,
saq.cloudphish.KEY_SHA256_CONTENT,
saq.cloudphish.KEY_SHA256_URL,
saq.cloudphish.KEY_LOCATION,
saq.cloudphish.KEY_FILE_NAME,
saq.cloudphish.KEY_UUID, ]:
self.assertTrue(key in submission_result)
self.assertEquals(submission_result[saq.cloudphish.KEY_RESULT], saq.cloudphish.RESULT_OK)
self.assertIsNotNone(submission_result[saq.cloudphish.KEY_DETAILS])
self.assertEquals(submission_result[saq.cloudphish.KEY_STATUS], saq.cloudphish.STATUS_NEW)
self.assertEquals(submission_result[saq.cloudphish.KEY_ANALYSIS_RESULT], saq.cloudphish.SCAN_RESULT_UNKNOWN)
self.assertIsNone(submission_result[saq.cloudphish.KEY_HTTP_RESULT])
self.assertIsNone(submission_result[saq.cloudphish.KEY_HTTP_MESSAGE])
self.assertIsNone(submission_result[saq.cloudphish.KEY_SHA256_CONTENT])
self.assertIsNotNone(submission_result[saq.cloudphish.KEY_SHA256_URL])
self.assertIsNone(submission_result[saq.cloudphish.KEY_LOCATION])
self.assertIsNone(submission_result[saq.cloudphish.KEY_FILE_NAME])
self.assertIsNotNone(submission_result[saq.cloudphish.KEY_UUID])
# now we start an engine to work on cloudphish analysis
engine = TestEngine(analysis_pools={ANALYSIS_MODE_CLOUDPHISH: 1},
local_analysis_modes=[ANALYSIS_MODE_CLOUDPHISH])
engine.enable_alerting()
engine.enable_module('analysis_module_crawlphish', ANALYSIS_MODE_CLOUDPHISH)
engine.enable_module('analysis_module_cloudphish_request_analyzer', ANALYSIS_MODE_CLOUDPHISH)
# force this analysis to become an alert
engine.enable_module('analysis_module_forced_detection', ANALYSIS_MODE_CLOUDPHISH)
#engine.enable_module('analysis_module_detection', ANALYSIS_MODE_CLOUDPHISH)
engine.controlled_stop()
engine.start()
engine.wait()
submission_result = ace_api.cloudphish_submit(TEST_URL)
self.assertEquals(submission_result[saq.cloudphish.KEY_RESULT], saq.cloudphish.RESULT_OK)
self.assertIsNotNone(submission_result[saq.cloudphish.KEY_DETAILS])
self.assertEquals(submission_result[saq.cloudphish.KEY_STATUS], saq.cloudphish.STATUS_ANALYZED)
self.assertEquals(submission_result[saq.cloudphish.KEY_ANALYSIS_RESULT], saq.cloudphish.SCAN_RESULT_ALERT)
self.assertEquals(submission_result[saq.cloudphish.KEY_HTTP_RESULT], 200)
self.assertEquals(submission_result[saq.cloudphish.KEY_HTTP_MESSAGE], 'OK')
self.assertIsNotNone(submission_result[saq.cloudphish.KEY_SHA256_CONTENT])
self.assertIsNotNone(submission_result[saq.cloudphish.KEY_SHA256_URL])
self.assertIsNotNone(submission_result[saq.cloudphish.KEY_LOCATION])
self.assertEquals(submission_result[saq.cloudphish.KEY_FILE_NAME], 'Payment_Advice.pdf')
self.assertIsNotNone(submission_result[saq.cloudphish.KEY_UUID])
# attempt to download the contents of the url
output_path = os.path.join(saq.TEMP_DIR, 'cloudphish.download')
download_result = ace_api.cloudphish_download(sha256=submission_result[saq.cloudphish.KEY_SHA256_URL],
output_path=output_path)
# make sure we can download the file
with open(output_path, 'rb') as fp:
data = fp.read()
hasher = hashlib.sha256()
hasher.update(data)
self.assertEquals(hasher.hexdigest().lower(), submission_result[saq.cloudphish.KEY_SHA256_CONTENT].lower())
# make sure we can clear the alert for this url
self.assertTrue(ace_api.cloudphish_clear_alert(sha256=submission_result[saq.cloudphish.KEY_SHA256_URL]))
# and verify that
submission_result = ace_api.cloudphish_submit(TEST_URL)
self.assertEquals(submission_result[saq.cloudphish.KEY_ANALYSIS_RESULT], saq.cloudphish.SCAN_RESULT_CLEAR)
class EventsAPITestCase(ACEEngineTestCase):
def setUp(self, *args, **kwargs):
super().setUp(*args, **kwargs)
self.start_api_server()
ace_api.set_default_remote_host(saq.API_PREFIX)
ace_api.set_default_ssl_ca_path(saq.CONFIG['SSL']['ca_chain_path'])
def test_get_open_events(self):
result = ace_api.get_open_events()
self.assertIsNotNone(result)
self.assertEquals(result, [])
@use_db
def test_update_event_status(self, db, c):
# Create an event
c.execute("""
INSERT INTO `events`
(`creation_date`,
`name`,
`type`,
`vector`,
`prevention_tool`,
`remediation`,
`status`,
`comment`,
`campaign_id`,
`uuid`)
VALUES
('2019-03-06',
'test event',
'phish',
'corporate email',
'response team',
'not remediated',
'OPEN',
'blah blah blah',
1,
'12345678-1234-1234-1234-123456789ab');""")
db.commit()
c.execute("SELECT id FROM events WHERE name='test event'")
event_id = c.fetchone()[0]
result = ace_api.update_event_status(event_id, 'CLOSED')
self.assertIsNotNone(result)
self.assertEqual(result['status'], 'CLOSED')