forked from corelight/phantom-playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorelight_investigate_dns_alert.py
773 lines (585 loc) · 33.1 KB
/
corelight_investigate_dns_alert.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
"""
This playbook automates analyst workflow when reviewing a Suricata event for a malicious DNS name, combined with Zeek metadata.
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
##############################
# Start - Global Code Block
import re
import time
# For filter 10: If you want to error check further for false positives, you can use the DNS UID to look at sourcetype=corelight_conn and see if resp_bytes=0. This speculates that a device inline has blocked the query and can be check against.
# End - Global Code block
##############################
def on_start(container):
phantom.debug('on_start() called')
# call 'timestamp_to_epoch' block
timestamp_to_epoch(container=container)
return
"""
Build a Splunk query to find the DNS log with the UID matching the Phantom event, which was triggered by a Suricata signature for a blacklisted DNS name.
"""
def format_DNS_alert_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('format_DNS_alert_query() called')
template = """index=corelight sourcetype=corelight_dns {0} earliest={1} latest=now() | table uid answer id_orig_h"""
# parameter list for template variable replacement
parameters = [
"artifact:*.cef.uid",
"timestamp_to_epoch:custom_function_result.data.epoch_time",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_DNS_alert_query")
run_DNS_alert_query(container=container)
return
"""
Run the Splunk query built in the previous block.
"""
def run_DNS_alert_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('run_DNS_alert_query() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'run_DNS_alert_query' call
formatted_data_1 = phantom.get_format_data(name='format_DNS_alert_query')
parameters = []
# build parameters list for 'run_DNS_alert_query' call
parameters.append({
'query': formatted_data_1,
'command': "search",
'display': "",
'parse_only': False,
})
phantom.act(action="run query", parameters=parameters, assets=['splunk-demo'], callback=IP_regex_and_format_source_dest_query, name="run_DNS_alert_query")
return
"""
Run the Splunk query built in the previous block.
"""
def run_source_dest_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('run_source_dest_query() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
IP_regex_and_format_source_dest_query__query = json.loads(phantom.get_run_data(key='IP_regex_and_format_source_dest_query:query'))
# collect data for 'run_source_dest_query' call
parameters = []
# build parameters list for 'run_source_dest_query' call
parameters.append({
'query': IP_regex_and_format_source_dest_query__query,
'command': "search",
'display': "",
'parse_only': False,
})
phantom.act(action="run query", parameters=parameters, assets=['splunk-demo'], callback=If_traffic_between_the_two_units, name="run_source_dest_query")
return
def If_traffic_between_the_two_units(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('If_traffic_between_the_two_units() called')
# collect filtered artifact ids for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["run_source_dest_query:action_result.data.*.count", "!=", 0],
["run_source_dest_query:action_result.data.*.count", "!=", ""],
],
logical_operator='and',
name="If_traffic_between_the_two_units:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
pin_DNS_alert(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
multi_connection_query_construct(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
"""
Loop through each matching DNS alert from the previous Splunk query, validate IPv4 and IPv6 addresses, and format another Splunk query to check for matching connection log entries between the same originating host and the host in the DNS answer.
"""
def IP_regex_and_format_source_dest_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('IP_regex_and_format_source_dest_query() called')
results_data_1 = phantom.collect2(container=container, datapath=['run_DNS_alert_query:action_result.data.*.answer', 'run_DNS_alert_query:action_result.data.*.id_orig_h'], action_results=results)
custom_function_results_data_1 = phantom.collect2(container=container, datapath=['timestamp_to_epoch:custom_function_result.data.epoch_time'], action_results=results)
results_item_1_0 = [item[0] for item in results_data_1]
results_item_1_1 = [item[1] for item in results_data_1]
custom_function_results_item_1_0 = [item[0] for item in custom_function_results_data_1]
IP_regex_and_format_source_dest_query__query = None
IP_regex_and_format_source_dest_query__id_resp_h = None
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here..
#phantom.debug(container_item_0)
#This query loops through the Corelight Answers and checks if a valid IP4/IP6 address
# it then creates a query to check if there was a conn log entry between the two IP address.
id_orig_h = str(results_item_1_1[0])
phantom.debug(id_orig_h)
query_base = "index=corelight sourcetype = corelight_conn earliest = " + str(custom_function_results_item_1_0[0]) + " id.orig_h = " + id_orig_h + " AND id.resp_h = "
IPregex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$'''
# Equivalent of our old filter checking for an empty string
if (results_item_1_0[0] is None):
phantom.debug("Caught a null, no answers found")
return
# If we got a single answer back, it'll be a string
# We deal with it directly vs untangling the loop
if (isinstance(results_item_1_0[0], str)):
id_resp_h=str(results_item_1_0[0])
if (re.search(IPregex, id_resp_h)):
phantom.debug("String was an IP address")
query = query_base + id_resp_h + "| stats count(id.orig_h) as count by id.resp_h uid"
phantom.debug(query)
IP_regex_and_format_source_dest_query__id_resp_h = id_resp_h
phantom.save_run_data(key='IP_regex_and_format_source_dest_query:query', value=json.dumps(query))
phantom.save_run_data(key='IP_regex_and_format_source_dest_query:id_resp_h', value=json.dumps(IP_regex_and_format_source_dest_query__id_resp_h))
run_source_dest_query(container=container)
return
# Assuming that if we were not null and not a string we're a list of results
for item in results_item_1_0[0]:
id_resp_h = str(item)
phantom.debug(id_resp_h)
if (re.search(IPregex, id_resp_h)):
query = query_base + id_resp_h + " | stats count(id.orig_h) as count by id.resp_h uid"
phantom.debug(query)
IP_regex_and_format_source_dest_query__id_resp_h = id_resp_h
phantom.save_run_data(key='IP_regex_and_format_source_dest_query:query', value=json.dumps(query))
phantom.save_run_data(key='IP_regex_and_format_source_dest_query:id_resp_h', value=json.dumps(IP_regex_and_format_source_dest_query__id_resp_h))
run_source_dest_query(container=container)
return
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key='IP_regex_and_format_source_dest_query:query', value=json.dumps(IP_regex_and_format_source_dest_query__query))
phantom.save_run_data(key='IP_regex_and_format_source_dest_query:id_resp_h', value=json.dumps(IP_regex_and_format_source_dest_query__id_resp_h))
run_source_dest_query(container=container)
return
"""
Run the Splunk query built in the previous block.
"""
def query_connections(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('query_connections() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
multi_connection_query_construct__query = json.loads(phantom.get_run_data(key='multi_connection_query_construct:query'))
# collect data for 'query_connections' call
parameters = []
# build parameters list for 'query_connections' call
parameters.append({
'query': multi_connection_query_construct__query,
'command': "search",
'display': "",
'parse_only': False,
})
phantom.act(action="run query", parameters=parameters, assets=['splunk-demo'], callback=filter_nonzero_bytes, name="query_connections")
return
"""
Filter out connections with zero bytes sent or received.
"""
def filter_nonzero_bytes(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('filter_nonzero_bytes() called')
# collect filtered artifact ids for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["query_connections:action_result.data.*.orig_bytes", "!=", 0],
["query_connections:action_result.data.*.resp_bytes", "!=", 0],
],
logical_operator='or',
name="filter_nonzero_bytes:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
filter_3(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
format_suricata_query(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
format_file_query(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
"""
Filter between plaintext HTTP and SSL/TLS.
"""
def filter_3(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('filter_3() called')
# collect filtered artifact ids for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["filtered-data:filter_nonzero_bytes:condition_1:query_connections:action_result.data.*.service", "==", "http"],
],
name="filter_3:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
format_HTTP_query(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
# collect filtered artifact ids for 'if' condition 2
matched_artifacts_2, matched_results_2 = phantom.condition(
container=container,
action_results=results,
conditions=[
["filtered-data:filter_nonzero_bytes:condition_1:query_connections:action_result.data.*.service", "==", "ssl"],
],
name="filter_3:condition_2")
# call connected blocks if filtered artifacts or results
if matched_artifacts_2 or matched_results_2:
format_SSL_query(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_2, filtered_results=matched_results_2)
return
"""
Format a detailed query to collect plaintext HTTP indicators to present to analysts.
"""
def format_HTTP_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('format_HTTP_query() called')
template = """index=corelight sourcetype=corelight_http {0} | spath host | table ts uid host uri method referrer user_agent"""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_3:condition_1:query_connections:action_result.data.*.uid",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_HTTP_query")
run_HTTP_query(container=container)
return
"""
Run the Splunk query built in the previous block.
"""
def run_HTTP_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('run_HTTP_query() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'run_HTTP_query' call
formatted_data_1 = phantom.get_format_data(name='format_HTTP_query')
parameters = []
# build parameters list for 'run_HTTP_query' call
parameters.append({
'query': formatted_data_1,
'command': "search",
'display': "",
'parse_only': False,
})
phantom.act(action="run query", parameters=parameters, assets=['splunk-demo'], callback=format_http_note, name="run_HTTP_query")
return
"""
Format a query for Suricata alerts generated on the connection resulting from the malicious DNS lookup (by way of the linked UID).
"""
def format_suricata_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('format_suricata_query() called')
template = """index=corelight sourcetype=corelight_suricata_corelight {0} | table uid alert.signature alert.signature_id alert.rev alert.category alert.severity alert.metadata metadata id.orig_h id.orig_p id.resp_h id.resp_p ts"""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_nonzero_bytes:condition_1:query_connections:action_result.data.*.uid",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_suricata_query")
run_suricata_query(container=container)
return
"""
Run the Splunk query built in the previous block.
"""
def run_suricata_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('run_suricata_query() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'run_suricata_query' call
formatted_data_1 = phantom.get_format_data(name='format_suricata_query')
parameters = []
# build parameters list for 'run_suricata_query' call
parameters.append({
'query': formatted_data_1,
'command': "search",
'display': "",
'parse_only': False,
})
phantom.act(action="run query", parameters=parameters, assets=['splunk-demo'], callback=filter_valid_suricata_alerts, name="run_suricata_query")
return
"""
Format a detailed query to collect SSL/TLS indicators to present to analysts.
"""
def format_SSL_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('format_SSL_query() called')
template = """index=corelight sourcetype=corelight_ssl {0} | table uid subject validation_status version ja3 ja3s"""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_3:condition_2:query_connections:action_result.data.*.uid",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_SSL_query")
run_SSL_query(container=container)
return
"""
Run the Splunk query built in the previous block.
"""
def run_file_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('run_file_query() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'run_file_query' call
formatted_data_1 = phantom.get_format_data(name='format_file_query')
parameters = []
# build parameters list for 'run_file_query' call
parameters.append({
'query': formatted_data_1,
'command': "search",
'display': "",
'parse_only': False,
})
phantom.act(action="run query", parameters=parameters, assets=['splunk-demo'], callback=filter_valid_files, name="run_file_query")
return
"""
Run the Splunk query built in the previous block.
"""
def run_SSL_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('run_SSL_query() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'run_SSL_query' call
formatted_data_1 = phantom.get_format_data(name='format_SSL_query')
parameters = []
# build parameters list for 'run_SSL_query' call
parameters.append({
'query': formatted_data_1,
'command': "search",
'display': "",
'parse_only': False,
})
phantom.act(action="run query", parameters=parameters, assets=['splunk-demo','splunk-demo'], callback=format_ssl_note, name="run_SSL_query")
return
"""
Query Virustotal for threat information about any SHA1 hashes found in the corelight_files query.
"""
def file_reputation_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('file_reputation_1() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'file_reputation_1' call
results_data_1 = phantom.collect2(container=container, datapath=['run_file_query:action_result.data.*.sha1', 'run_file_query:action_result.parameter.context.artifact_id'], action_results=results)
parameters = []
# build parameters list for 'file_reputation_1' call
for results_item_1 in results_data_1:
if results_item_1[0]:
parameters.append({
'hash': results_item_1[0],
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': results_item_1[1]},
})
phantom.act(action="file reputation", parameters=parameters, assets=['public-vt'], callback=filter_7, name="file_reputation_1")
return
"""
Update the heads-up display with the DNS query.
"""
def pin_DNS_alert(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('pin_DNS_alert() called')
results_data_1 = phantom.collect2(container=container, datapath=['run_DNS_alert_query:action_result.data.*.query'], action_results=results)
results_item_1_0 = [item[0] for item in results_data_1]
phantom.pin(container=container, data=results_item_1_0, message="Connection to Alerted DNS Address", pin_type="card", pin_style="red", name=None)
return
"""
Post the SHA1 hash to the heads-up display.
"""
def pin_virustotal_response(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('pin_virustotal_response() called')
filtered_results_data_1 = phantom.collect2(container=container, datapath=['filtered-data:filter_7:condition_1:file_reputation_1:action_result.data.*.sha1'])
filtered_results_item_1_0 = [item[0] for item in filtered_results_data_1]
phantom.pin(container=container, data=filtered_results_item_1_0, message="File Downloads VT Hit", pin_type="card", pin_style="", name=None)
return
"""
Add a heads-up display pin for any suricata alerts that were found.
"""
def pin_suricata_alert(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('pin_suricata_alert() called')
formatted_data_1 = phantom.get_format_data(name='format_suricata_pin')
phantom.pin(container=container, data=formatted_data_1, message="Corelight UID with Surcata Alerts", name=None)
return
"""
Create a note to display the HTTP URI.
"""
def add_HTTP_metadata_note(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('add_HTTP_metadata_note() called')
formatted_data_1 = phantom.get_format_data(name='format_http_note')
note_title = "URI the end point made requests to"
note_content = formatted_data_1
note_format = "html"
phantom.add_note(container=container, note_type="general", title=note_title, content=note_content, note_format=note_format)
return
"""
Add a note for the SSL metadata.
"""
def add_SSL_metadata_note(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('add_SSL_metadata_note() called')
formatted_data_1 = phantom.get_format_data(name='format_ssl_note')
note_title = "SSL Metadata"
note_content = formatted_data_1
note_format = "html"
phantom.add_note(container=container, note_type="general", title=note_title, content=note_content, note_format=note_format)
return
"""
Format a note for the SSL metadata.
"""
def format_ssl_note(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('format_ssl_note() called')
template = """UID = {0}
JA3 = {1}
ja3s = {2}
subject = {3}
validation_status = {4}
version = {5}"""
# parameter list for template variable replacement
parameters = [
"run_SSL_query:action_result.data.*.uid",
"run_SSL_query:action_result.data.*.ja3",
"run_SSL_query:action_result.data.*.ja3s",
"run_SSL_query:action_result.data.*.subject",
"run_SSL_query:action_result.data.*.validation_status",
"run_SSL_query:action_result.data.*.version",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_ssl_note")
add_SSL_metadata_note(container=container)
return
"""
Proceed if there are one or more positive Virustotal matches.
"""
def filter_7(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('filter_7() called')
# collect filtered artifact ids for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["file_reputation_1:action_result.data.*.positives", ">", 1],
],
name="filter_7:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
pin_virustotal_response(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
"""
Format a heads-up display pin for any Suricata alerts that were found.
"""
def format_suricata_pin(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('format_suricata_pin() called')
template = """URI = {1}
SURI_id = {0}
alert.signature ={2}"""
# parameter list for template variable replacement
parameters = [
"run_suricata_query:action_result.data.*.suri_id",
"run_suricata_query:action_result.data.*.uri",
"run_suricata_query:action_result.data.*.alert.signature",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_suricata_pin")
pin_suricata_alert(container=container)
return
"""
Only proceed with valid Suricata alerts.
"""
def filter_valid_suricata_alerts(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('filter_valid_suricata_alerts() called')
# collect filtered artifact ids for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["run_suricata_query:action_result.data.*.suri_id", "!=", ""],
],
name="filter_valid_suricata_alerts:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
format_suricata_pin(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
"""
Only proceed if files were found.
"""
def filter_valid_files(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('filter_valid_files() called')
# collect filtered artifact ids for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["run_file_query:action_result.data.*.sha1", "!=", ""],
],
name="filter_valid_files:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
file_reputation_1(action=action, success=success, container=container, results=results, handle=handle, custom_function=custom_function, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
"""
Convert the timestamp in the alert from ISO 8601 format to a unix epoch timestamp.
"""
def timestamp_to_epoch(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('timestamp_to_epoch() called')
container_data_0 = phantom.collect2(container=container, datapath=['artifact:*.cef.ts', 'artifact:*.id'])
literal_values_0 = [
[
-1,
],
]
parameters = []
for item0 in container_data_0:
for item1 in literal_values_0:
parameters.append({
'input_datetime': item0[0],
'amount_to_modify': item1[0],
'modification_unit': None,
'input_format_string': None,
'output_format_string': None,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
# call custom function "community/datetime_modify", returns the custom_function_run_id
phantom.custom_function(custom_function='community/datetime_modify', parameters=parameters, name='timestamp_to_epoch', callback=format_DNS_alert_query)
return
"""
Format a query to look for the metadata of files detected in the connection stream by Corelight.
"""
def format_file_query(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('format_file_query() called')
template = """index=corelight sourcetype=corelight_files {0} | spath source | table tx_hosts{{}} rx_hosts{{}} filename mime_type source sha1"""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_nonzero_bytes:condition_1:query_connections:action_result.data.*.uid",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_file_query")
run_file_query(container=container)
return
"""
Format a note for the HTTP metadata.
"""
def format_http_note(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('format_http_note() called')
template = """Timestamp = {0}
HTTP Host = {1}
URI = {2}
HTTP Method ={3}
HTTP Referrer = {4}
User-Agent = {5}"""
# parameter list for template variable replacement
parameters = [
"run_HTTP_query:action_result.data.*.ts",
"run_HTTP_query:action_result.data.*.host",
"run_HTTP_query:action_result.data.*.uri",
"run_HTTP_query:action_result.data.*.method",
"run_HTTP_query:action_result.data.*.referrer",
"run_HTTP_query:action_result.data.*.user_agent",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_http_note")
add_HTTP_metadata_note(container=container)
return
def multi_connection_query_construct(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug('multi_connection_query_construct() called')
results_data_1 = phantom.collect2(container=container, datapath=['run_source_dest_query:action_result.data.*.uid'], action_results=results)
results_item_1_0 = [item[0] for item in results_data_1]
multi_connection_query_construct__query = None
################################################################################
## Custom Code Start
################################################################################
for uid in results_data_1:
query = "index=corelight sourcetype=corelight_conn " + uid[0] + " | table *"
phantom.save_run_data(key='multi_connection_query_construct:query', value=json.dumps(query))
query_connections(container=container)
phantom.debug("Returning from format_connection_query")
return
##########
##########
##########
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key='multi_connection_query_construct:query', value=json.dumps(multi_connection_query_construct__query))
query_connections(container=container)
return
def on_finish(container, summary):
phantom.debug('on_finish() called')
# This function is called after all actions are completed.
# summary of all the action and/or all details of actions
# can be collected here.
# summary_json = phantom.get_summary()
# if 'result' in summary_json:
# for action_result in summary_json['result']:
# if 'action_run_id' in action_result:
# action_results = phantom.get_action_results(action_run_id=action_result['action_run_id'], result_data=False, flatten=False)
# phantom.debug(action_results)
return