forked from demisto/content
/
ACTIIndicatorQuery_test.py
714 lines (545 loc) · 32.6 KB
/
ACTIIndicatorQuery_test.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
import requests_mock
from ACTIIndicatorQuery import IDEFENSE_URL_TEMPLATE, Client, domain_command, url_command, ip_command, uuid_command, _calculate_dbot_score, getThreatReport_command, fix_markdown, addBaseUrlToPartialPaths, convert_inline_image_to_encoded, fundamental_uuid_command # noqa: E501
from CommonServerPython import DemistoException, DBotScoreReliability
from test_data.response_constants import URL_RES_JSON, URL_INTEL_JSON, IP_RES_JSON, IP_INTEL_JSON, DOMAIN_RES_JSON, \
DOMAIN_INTEL_JSON, UUID_RES_JSON, RES_JSON_IA, RES_JSON_IR, expected_output_ia, expected_output_ir, MALWARE_FAMILY_RES_JSON, \
expected_output_malware_family, RAW_MALWARE_FAMILY_RES_JSON
import demistomock as demisto
import pytest
API_URL = "https://test.com"
DBOT_KEY = 'DBotScore(val.Indicator && val.Indicator == obj.Indicator && ' \
'val.Vendor == obj.Vendor && val.Type == obj.Type)'
INTEGRATION_NAME = 'iDefense'
ENDPOINTS = {
'threatindicator': '/rest/threatindicator',
'document': '/rest/document',
'fundamental': '/rest/fundamental'
}
@pytest.fixture(autouse=True)
def handle_calling_context(mocker):
mocker.patch.object(demisto, 'callingContext', {'context': {'IntegrationBrand': INTEGRATION_NAME}})
def test_ip_command():
"""
Given:
- an IP
When:
- running ip command and validate whether the ip is malicious
Then:
- return command results containing indicator, dbotscore and associated intelligence alerts, reports
"""
url = 'https://test.com/rest/threatindicator/v0/ip?key.values=0.0.0.0'
doc_url = 'https://test.com/rest/document/v0?links.display_text.values=0.0.0.0&type.values=intelligence_alert&type.values=intelligence_report&links.display_text.match_all=true' # noqa: E501
fund_url = 'https://test.com/rest/fundamental/v0/malware_family?key.values=Hive'
status_code = 200
json_data = IP_RES_JSON
intel_json_data = IP_INTEL_JSON
malware_json_data = RAW_MALWARE_FAMILY_RES_JSON
expected_output = {
'IP': [{'Address': '0.0.0.0'}],
'DBOTSCORE': [{'Indicator': '0.0.0.0', 'Type': 'ip', 'Vendor': 'iDefense', 'Score': 2,
'Reliability': 'B - Usually reliable'}]}
ip_to_check = {'ip': '0.0.0.0'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
m.get(doc_url, status_code=status_code, json=intel_json_data)
m.get(fund_url, status_code=status_code, json=malware_json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = ip_command(client, ip_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
context_result = results[0].to_context()
output = results[0].to_context().get('EntryContext', {})
assert output.get('IP(val.Address && val.Address == obj.Address)', []) == expected_output.get('IP')
assert output.get(DBOT_KEY, []) == expected_output.get('DBOTSCORE')
assert _is_intelligence_data_present_in_command_result(context_result, intel_json_data) is True
def test_ip_command_when_api_key_not_authorised_for_document_search():
"""
Given:
- a ip and api key not authorized for doc search
When:
- running ip command and validate whether the ip is malicious
Then:
- return command results containing indicator, dbotscore and NO associated intelligence alerts, reports
"""
url = 'https://test.com/rest/threatindicator/v0/ip?key.values=0.0.0.0'
doc_url = 'https://test.com/rest/document/v0?links.display_text.values=0.0.0.0&type.values=intelligence_alert&type.values=intelligence_report&links.display_text.match_all=true' # noqa: E501
fund_url = 'https://test.com/rest/fundamental/v0/malware_family?key.values=Hive'
status_code = 200
error_status_code = 403
json_data = IP_RES_JSON
malware_family_data = RAW_MALWARE_FAMILY_RES_JSON
doc_search_exception_response = {'timestamp': '2021-11-12T09:09:27.983Z', 'status': 403,
'error': 'Forbidden', 'message': 'Forbidden', 'path': '/rest/document/v0'}
expected_output = {
'IP': [{'Address': '0.0.0.0'}],
'DBOTSCORE': [{'Indicator': '0.0.0.0', 'Type': 'ip', 'Vendor': 'iDefense', 'Score': 2,
'Reliability': 'B - Usually reliable'}]}
ip_to_check = {'ip': '0.0.0.0'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
m.get(doc_url, status_code=error_status_code, json=doc_search_exception_response)
m.get(fund_url, status_code=status_code, json=malware_family_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = ip_command(client, ip_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
context_result = results[0].to_context()
content = context_result['HumanReadable']
output = context_result.get('EntryContext', {})
assert output.get('IP(val.Address && val.Address == obj.Address)', []) == expected_output.get('IP')
assert output.get(DBOT_KEY, []) == expected_output.get('DBOTSCORE')
assert 'Intelligence Alerts' not in content
assert 'Intelligence Reports' not in content
def test_domain_command():
"""
Given:
- a domain
When:
- running domain command and validate whether the domain is malicious
Then:
- return command results containing indicator, dbotscore and associated intelligence alerts, reports
"""
url = 'https://test.com/rest/threatindicator/v0/domain?key.values=mydomain.com'
doc_url = 'https://test.com/rest/document/v0?links.display_text.values=mydomain.com&type.values=intelligence_alert&type.values=intelligence_report&links.display_text.match_all=true' # noqa: E501
status_code = 200
json_data = DOMAIN_RES_JSON
intel_json_data = DOMAIN_INTEL_JSON
expected_output = {
'domain': [{'Name': 'mydomain.com'}],
'DBOTSCORE': [{'Indicator': 'mydomain.com', 'Type': 'domain', 'Vendor': 'iDefense', 'Score': 2, 'Reliability': 'B - Usually reliable'}] # noqa: E501
}
domain_to_check = {'domain': 'mydomain.com'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
m.get(doc_url, status_code=status_code, json=intel_json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = domain_command(client, domain_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
context_result = results[0].to_context()
output = results[0].to_context().get('EntryContext', {})
assert output.get('Domain(val.Name && val.Name == obj.Name)', []) == expected_output.get('domain')
assert output.get(DBOT_KEY, []) == expected_output.get('DBOTSCORE')
assert _is_intelligence_data_present_in_command_result(context_result, intel_json_data) is True
def test_domain_command_when_api_key_not_authorized_for_document_search():
"""
Given:
- a domain and api key not authorized for doc search
When:
- running domain command and validate whether the domain is malicious
Then:
- return command results containing indicator, dbotscore and NO associated intelligence alerts, reports
"""
url = 'https://test.com/rest/threatindicator/v0/domain?key.values=mydomain.com'
doc_url = 'https://test.com/rest/document/v0?links.display_text.values=mydomain.com&type.values=intelligence_alert&type.values=intelligence_report&links.display_text.match_all=true' # noqa: E501
status_code = 200
error_status_code = 403
json_data = DOMAIN_RES_JSON
doc_search_exception_response = {'timestamp': '2021-11-12T09:09:27.983Z', 'status': 403,
'error': 'Forbidden', 'message': 'Forbidden', 'path': '/rest/document/v0'}
expected_output = {
'domain': [{'Name': 'mydomain.com'}],
'DBOTSCORE': [{'Indicator': 'mydomain.com', 'Type': 'domain', 'Vendor': 'iDefense', 'Score': 2, 'Reliability': 'B - Usually reliable'}] # noqa: E501
}
domain_to_check = {'domain': 'mydomain.com'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
m.get(doc_url, status_code=error_status_code, json=doc_search_exception_response)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = domain_command(client, domain_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
context_result = results[0].to_context()
content = context_result['HumanReadable']
output = context_result.get('EntryContext', {})
assert output.get('Domain(val.Name && val.Name == obj.Name)', []) == expected_output.get('domain')
assert output.get(DBOT_KEY, []) == expected_output.get('DBOTSCORE')
assert 'Intelligence Alerts' not in content
assert 'Intelligence Reports' not in content
def _is_intelligence_data_present_in_command_result(context_result, test_intel_json_data) -> bool:
"""
Function to verify whether context result includes intelligence alert and report information
Args:
context_result from demisto command
test_intel_json_data mock response data used for document search api
Returns:
True if intelligence alert and report are present else False
"""
test_data = test_intel_json_data.get('results')
alerts, reports = {}, {}
for result in test_data:
if result['type'] == 'intelligence_alert':
alerts[result['title']] = IDEFENSE_URL_TEMPLATE.format(result['type'], result['uuid'])
if result['type'] == 'intelligence_report':
reports[result['title']] = IDEFENSE_URL_TEMPLATE.format(result['type'], result['uuid'])
content = context_result['HumanReadable']
for title, url in alerts.items():
if url not in content[content.find(title):content.find('|', content.find(title))]:
return False
return all(url in content[content.find(title):content.find("|", content.find(title))] for title, url in reports.items())
def test_uuid_command():
"""
Given:
- a domain uuid
When:
- running uuid command and validate whether the domain is malicious
Then:
- return command results containing indicator, dbotscore and associated intelligence alerts, reports
"""
url = 'https://test.com/rest/threatindicator/v0/461b5ba2-d4fe-4b5c-ac68-35b6636c6edf'
doc_url = 'https://test.com/rest/document/v0?links.display_text.values=mydomain.com&type.values=intelligence_alert&type.values=intelligence_report&links.display_text.match_all=true' # noqa: E501
malware_family_url = 'https://test.com/rest/fundamental/v0/malware_family?key.values=Hive'
status_code = 200
json_data = UUID_RES_JSON
intel_json_data = DOMAIN_INTEL_JSON
malware_json_data = RAW_MALWARE_FAMILY_RES_JSON
expected_output = {
'domain': [{'Name': 'mydomain.com'}],
'DBOTSCORE': [{'Indicator': 'mydomain.com', 'Type': 'domain', 'Vendor': 'iDefense', 'Score': 2, 'Reliability': 'B - Usually reliable'}] # noqa: E501
}
uuid_to_check = {'uuid': '461b5ba2-d4fe-4b5c-ac68-35b6636c6edf'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
m.get(doc_url, status_code=status_code, json=intel_json_data)
m.get(malware_family_url, status_code=status_code, json=malware_json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = uuid_command(client, uuid_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
context_result = results.to_context()
output = results.to_context().get('EntryContext', {})
assert output.get('Domain(val.Name && val.Name == obj.Name)', []) == expected_output.get('domain')
assert output.get(DBOT_KEY, []) == expected_output.get('DBOTSCORE')
assert _is_intelligence_data_present_in_command_result(context_result, intel_json_data) is True
def test_uuid_command_when_api_key_not_authorized_for_document_search():
"""
Given:
- a domain uuid and api key not authorized for doc search
When:
- running uuid command and validate whether the domain is malicious
Then:
- return command results containing indicator, dbotscore and NO associated intelligence alerts, reports
"""
url = 'https://test.com/rest/threatindicator/v0/461b5ba2-d4fe-4b5c-ac68-35b6636c6edf'
doc_url = 'https://test.com/rest/document/v0?links.display_text.values=mydomain.com&type.values=intelligence_alert&type.values=intelligence_report&links.display_text.match_all=true' # noqa: E501
malware_family_url = 'https://test.com/rest/fundamental/v0/malware_family?key.values=Hive'
status_code = 200
error_status_code = 403
json_data = UUID_RES_JSON
malware_json_data = RAW_MALWARE_FAMILY_RES_JSON
doc_search_exception_response = {'timestamp': '2021-11-12T09:09:27.983Z', 'status': 403,
'error': 'Forbidden', 'message': 'Forbidden', 'path': '/rest/document/v0'}
expected_output = {
'domain': [{'Name': 'mydomain.com'}],
'DBOTSCORE': [{'Indicator': 'mydomain.com', 'Type': 'domain', 'Vendor': 'iDefense', 'Score': 2, 'Reliability': 'B - Usually reliable'}] # noqa: E501
}
uuid_to_check = {'uuid': '461b5ba2-d4fe-4b5c-ac68-35b6636c6edf'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
m.get(doc_url, status_code=error_status_code, json=doc_search_exception_response)
m.get(malware_family_url, status_code=status_code, json=malware_json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = uuid_command(client, uuid_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
context_result = results.to_context()
content = context_result['HumanReadable']
output = context_result.get('EntryContext', {})
assert output.get('Domain(val.Name && val.Name == obj.Name)', []) == expected_output.get('domain')
assert output.get(DBOT_KEY, []) == expected_output.get('DBOTSCORE')
assert 'Intelligence Alerts' not in content
assert 'Intelligence Reports' not in content
def test_ip_not_found():
"""
Given:
- an IP
When:
- running ip command and validate whether the ip is malicious
Then:
- return command results with context indicate that no results were found
"""
url = 'https://test.com/rest/threatindicator/v0/ip?key.values=1.1.1.1'
status_code = 200
json_data = {'total_size': 0, 'page': 1, 'page_size': 25, 'more': False}
expected_output = "No results were found for ip 1.1.1.1"
ip_to_check = {'ip': '1.1.1.1'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = ip_command(client, ip_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
output = results[0].to_context().get('HumanReadable')
assert expected_output in output
def test_url_not_found():
"""
Given:
- an URL
When:
- running url command and validate whether the url is malicious
Then:
- return command results with context indicate that no results were found
"""
url = 'https://test.com/rest/threatindicator/v0/url?key.values=http://www.malware.com/path'
status_code = 200
json_data = {'total_size': 0, 'page': 1, 'page_size': 25, 'more': False}
expected_output = "No results were found for url http://www.malware.com/path"
url_to_check = {'url': 'http://www.malware.com/path'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = url_command(client, url_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
output = results[0].to_context().get('HumanReadable')
assert expected_output in output
def test_domain_not_found():
"""
Given:
- an Domain
When:
- running domain command and validate whether the domain is malicious
Then:
- return command results with context indicate that no results were found
"""
url = 'https://test.com/rest/threatindicator/v0/domain?key.values=mydomain.com'
status_code = 200
json_data = {'total_size': 0, 'page': 1, 'page_size': 25, 'more': False}
expected_output = "No results were found for Domain mydomain.com"
domain_to_check = {'domain': 'mydomain.com'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = domain_command(client, domain_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
output = results[0].to_context().get('HumanReadable')
assert expected_output in output
def test_wrong_ip():
"""
Given:
- an IP
When:
- running ip command validate at first to check if the given ip is a valid ip
Then:
- raise error before calling http request that indicates that the given argument is not valid
"""
ip_to_check = {'ip': '1'}
client = Client(API_URL, 'api_token', True, False)
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
try:
ip_command(client, ip_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
except DemistoException as err:
assert "Received wrong IP value" in str(err)
def test_wrong_connection():
"""
Given:
- an api token
When:
- checking api access
Then:
- raise error if there is no access because of wrong api token
"""
from ACTIIndicatorQuery import test_module
with requests_mock.Mocker() as m:
mock_address = 'https://test.com/rest/threatindicator/v0/'
m.get(mock_address, status_code=401, json={})
client = Client('bad_api_key', 'wrong_token', True, False)
try:
test_module(client)
except DemistoException as err:
assert 'Error in API call - check the input parameters' in str(err)
def test_url_command():
"""
Given:
- url
When:
- running url command and validate whether the url is malicious
Then:
- return command results containing indicator, dbotscore and associated intelligence alerts, reports
"""
url = 'https://test.com/rest/threatindicator/v0/url?key.values=http://www.malware.com/path'
doc_url = 'https://test.com/rest/document/v0?links.display_text.values=http://www.malware.com/path&type.values=intelligence_alert&type.values=intelligence_report&links.display_text.match_all=true' # noqa: E501
status_code = 200
json_data = URL_RES_JSON
intel_json_data = URL_INTEL_JSON
expected_output = {
'URL': [{'Data': 'http://www.malware.com/path'}],
'DBOTSCORE': [{'Indicator': 'http://www.malware.com/path', 'Type': 'url', 'Vendor': 'iDefense',
'Score': 2, 'Reliability': 'B - Usually reliable'}]}
url_to_check = {'url': 'http://www.malware.com/path'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
m.get(doc_url, status_code=status_code, json=intel_json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = url_command(client, url_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
context_result = results[0].to_context()
output = results[0].to_context().get('EntryContext', {})
assert output.get('URL(val.Data && val.Data == obj.Data)', []) == expected_output.get('URL')
assert output.get(DBOT_KEY, []) == expected_output.get('DBOTSCORE')
assert _is_intelligence_data_present_in_command_result(context_result, intel_json_data) is True
def test_url_command_when_api_key_not_authorized_for_document_search():
"""
Given:
- a url and api key not authorized for doc search
When:
- running url command and validate whether the url is malicious
Then:
- return command results containing indicator, dbotscore and NO associated intelligence alerts, reports
"""
url = 'https://test.com/rest/threatindicator/v0/url?key.values=http://www.malware.com/path'
doc_url = 'https://test.com/rest/document/v0?links.display_text.values=http://www.malware.com/path&type.values=intelligence_alert&type.values=intelligence_report&links.display_text.match_all=true' # noqa: E501
status_code = 200
error_status_code = 403
json_data = URL_RES_JSON
doc_search_exception_response = {'timestamp': '2021-11-12T09:09:27.983Z', 'status': 403,
'error': 'Forbidden', 'message': 'Forbidden', 'path': '/rest/document/v0'}
expected_output = {
'URL': [{'Data': 'http://www.malware.com/path'}],
'DBOTSCORE': [{'Indicator': 'http://www.malware.com/path', 'Type': 'url', 'Vendor': 'iDefense',
'Score': 2, 'Reliability': 'B - Usually reliable'}]}
url_to_check = {'url': 'http://www.malware.com/path'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
m.get(doc_url, status_code=error_status_code, json=doc_search_exception_response)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['threatindicator'])
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
fundamental_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = url_command(client, url_to_check, DBotScoreReliability.B, doc_search_client, fundamental_client)
context_result = results[0].to_context()
content = context_result['HumanReadable']
output = context_result.get('EntryContext', {})
assert output.get('URL(val.Data && val.Data == obj.Data)', []) == expected_output.get('URL')
assert output.get(DBOT_KEY, []) == expected_output.get('DBOTSCORE')
assert 'Intelligence Alerts' not in content
assert 'Intelligence Reports' not in content
def test_calculate_dbot_score():
"""
Given:
- number represents severity
When:
- api call with indicator returns response that includes them severity score
Then:
- returns dbotscore according to internal conversion
"""
assert _calculate_dbot_score(0) == 0
assert _calculate_dbot_score(1) == 1
assert _calculate_dbot_score(2) == 1
assert _calculate_dbot_score(3) == 2
assert _calculate_dbot_score(4) == 2
assert _calculate_dbot_score(5) == 3
assert _calculate_dbot_score(6) == 3
assert _calculate_dbot_score(7) == 3
def test_fix_markdown():
text_to_update = "##Key Findings and Judgements\n\n* The sophisticated [cyber espionage operation](/#/node/intelligence"\
"_alert/view/a655306d-bd95-426d-8c93-ebeef57406e4) was selective; it used supply-chain attack techniques,"\
" mainly a malicious update of a widely used product from IT monitoring firm SolarWinds,"\
" but its final target list appears to number only in the hundreds. Known targets include public-"\
" and private-sector entities, mostly in the US, including IT vendors, US government entities, and think"\
" tanks (#/node/intelligence_alert/view/a655306d-bd95-426d-8c93-ebeef57406e4)."
expected_output = "## Key Findings and Judgements\n\n* The sophisticated [cyber espionage operation](/#/node/"\
"intelligence_alert/view/a655306d-bd95-426d-8c93-ebeef57406e4) was selective; it used supply-chain attack "\
"techniques, mainly a malicious update of a widely used product from IT monitoring firm SolarWinds, but its"\
" final target list appears to number only in the hundreds. Known targets include public- and private-sector "\
"entities, mostly in the US, including IT vendors, US government entities, and think tanks (#/node/"\
"intelligence_alert/view/a655306d-bd95-426d-8c93-ebeef57406e4)."
output = fix_markdown(text_to_update)
assert expected_output == output
def _test_getThreatReport_ia_command():
"""
Given:
- an URL
When:
- running ThreatReport command and fetch IA/IR
Then:
- return command results containing UUID, dbotscore
"""
url = 'https://test.com/rest/document/v0/a487dfdc-08b4-4909-82ea-2d934c27d901'
status_code = 200
json_res = RES_JSON_IA
expected_output = expected_output_ia
uuid_to_check = {'uuid': 'a487dfdc-08b4-4909-82ea-2d934c27d901'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_res)
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
results = getThreatReport_command(doc_search_client, uuid_to_check, DBotScoreReliability.B)
output = results.to_context().get('EntryContext', {})
assert output.get('IAIR(val.value && val.value == obj.value)', []) == expected_output.get('IA')
assert output.get(DBOT_KEY, []) == expected_output.get('DBot')
def _test_getThreatReport_ir_command():
"""
Given:
- an URL
When:
- running ThreatReport command and fetch IA/IR
Then:
- return command results containing UUID, dbotscore
"""
url = 'https://test.com/rest/document/v0/bdc9d16f-6040-4894-8544-9c98986a41fd'
status_code = 200
json_res = RES_JSON_IR
expected_output = expected_output_ir
uuid_to_check = {'uuid': 'bdc9d16f-6040-4894-8544-9c98986a41fd'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_res)
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
results = getThreatReport_command(doc_search_client, uuid_to_check, DBotScoreReliability.B)
output = results.to_context().get('EntryContext', {})
assert output.get('IAIR(val.value && val.value == obj.value)', []) == expected_output.get('IR')
assert output.get(DBOT_KEY, []) == expected_output.get('DBot')
def test_getThreatReport_not_found():
url = 'https://test.com/rest/document/v0/a487dfdc-08b4-49a09-82ea-2d934c27d901'
status_code = 200
json_res = None
expected_output = 'No report was found for UUID: a487dfdc-08b4-49a09-82ea-2d934c27d901 !!'
uuid_to_check = {'uuid': 'a487dfdc-08b4-49a09-82ea-2d934c27d901'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_res)
doc_search_client = Client(API_URL, 'api_token', True, False, ENDPOINTS['document'])
results = getThreatReport_command(doc_search_client, uuid_to_check, DBotScoreReliability.B)
output = results.to_context().get('HumanReadable')
assert expected_output in output
def test_addBaseUrlToPartialPaths():
IA_link_in_content = " BELUGASTURGEON activity, including a [2020 campaign against the Cypriot"\
" government](#/node/intelligence_alert/view/6cc805d7-cb77-443d-afea-d052916fa602)"
image_link_in_content = "as China has.\n\n ![Arctic Map](/rest/files/download/0f/6c"\
"/6f/91de9ef8d8d38345dc270f8915d9faa496a00b5babe2bff231dd195cd0/ArcticMapUWNews28288859157_5f54b9c446_c.jpg)"
IA_link_expected_output = " BELUGASTURGEON activity, including a [2020 campaign against the Cypriot"\
" government](https://intelgraph.idefense.com/#/node/intelligence_alert/view/6cc805d7-cb77-443d-afea-d052916fa602)"
image_link_expected_output = "as China has.\n\n ![Arctic Map](https://intelgraph.idefense.com/rest/files/download/0f/6c"\
"/6f/91de9ef8d8d38345dc270f8915d9faa496a00b5babe2bff231dd195cd0/ArcticMapUWNews28288859157_5f54b9c446_c.jpg)"
ialink_output = addBaseUrlToPartialPaths(IA_link_in_content)
imagelink_output = addBaseUrlToPartialPaths(image_link_in_content)
assert ialink_output == IA_link_expected_output
assert imagelink_output == image_link_expected_output
def test_convert_inline_image_to_encoded():
md_text = "China has.\n\n ![Arctic Map](https://test.com/rest/files/download/0f/6c/6f/"\
"91de9ef8d8d38345dc270f8915d9faa496a00b5babe2bff231dd195cd0/ArcticMapUWNews28288859157_5f54b9c446_c.jpg)"
url = "https://test.com/rest/files/download/0f/6c/6f/91de9ef8d8d38345dc270f8915d9faa496a00b5ba"\
"be2bff231dd195cd0/ArcticMapUWNews28288859157_5f54b9c446_c.jpg"
status_code = 200
expected_res = "China has.\n\n ![Arctic Map](data:image/jpg;base64,eyJjb250ZW50IjogIkNoaW"\
"5hIGhhcy5cblxuICFbQXJjdGljIE1hcF0oZGF0YTppbWFnZS9qcGc7YmFzZTY0LCkifQ==)"
raw_res = {
'content': 'China has.\n\n ![Arctic Map](data:image/jpg;base64,)'
}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=raw_res)
res = convert_inline_image_to_encoded(md_text)
assert res == expected_res
def test_fundamental_uuid_command():
url = 'https://test.com/rest/fundamental/v0/c1b3216e-8b2e-4a9f-b0a9-2e184b7182f7'
status_code = 200
json_data = MALWARE_FAMILY_RES_JSON
expected_output = expected_output_malware_family
uuid_to_check = {'uuid': 'c1b3216e-8b2e-4a9f-b0a9-2e184b7182f7'}
with requests_mock.Mocker() as m:
m.get(url, status_code=status_code, json=json_data)
client = Client(API_URL, 'api_token', True, False, ENDPOINTS['fundamental'])
results = fundamental_uuid_command(client, uuid_to_check, DBotScoreReliability.B)
output = results.to_context().get('EntryContext', {})
assert output.get('ACTI_MalwareFamily(val.value && val.value == obj.value)', []) == expected_output.get('malware_family')
assert output.get(DBOT_KEY, []) == expected_output.get('dbot')