forked from phantomcyber/playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mission_Control_Attribute_Lookup.py
494 lines (350 loc) · 23.1 KB
/
Mission_Control_Attribute_Lookup.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
"""
Launches "attribute lookup" input playbooks, creates events, and then concludes the Response Tasks where this playbook appears.
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
@phantom.playbook_block()
def on_start(container):
phantom.debug('on_start() called')
# call 'azure_ad_graph_entity_attribute_lookup' block
azure_ad_graph_entity_attribute_lookup(container=container)
# call 'crowdstrike_device_attribute_lookup' block
crowdstrike_device_attribute_lookup(container=container)
return
@phantom.playbook_block()
def observable_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("observable_filter() called")
################################################################################
# Exclude Null playbook outputs
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["observable_merge:custom_function_result.data.item", "!=", None]
],
name="observable_filter:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
create_events(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
@phantom.playbook_block()
def azure_ad_graph_entity_attribute_lookup(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("azure_ad_graph_entity_attribute_lookup() called")
data_summary_user_value = container.get("data", {}).get("summary", {}).get("user", None)
container_artifact_data = phantom.collect2(container=container, datapath=["artifact:*.cef.user"])
container_artifact_fields_item_0 = [item[0] for item in container_artifact_data]
user_combined_value = phantom.concatenate(container_artifact_fields_item_0, data_summary_user_value)
inputs = {
"user": user_combined_value,
"device": [],
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
# call playbook "local/Azure_AD_Graph_User_Attribute_Lookup", returns the playbook_run_id
playbook_run_id = phantom.playbook("local/Azure_AD_Graph_User_Attribute_Lookup", container=container, name="azure_ad_graph_entity_attribute_lookup", callback=join_observable_merge, inputs=inputs)
return
@phantom.playbook_block()
def crowdstrike_device_attribute_lookup(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("crowdstrike_device_attribute_lookup() called")
data_summary_dest_value = container.get("data", {}).get("summary", {}).get("dest", None)
data_summary_src_value = container.get("data", {}).get("summary", {}).get("src", None)
data_summary_dest_ip_value = container.get("data", {}).get("summary", {}).get("dest_ip", None)
data_summary_src_ip_value = container.get("data", {}).get("summary", {}).get("src_ip", None)
container_artifact_data = phantom.collect2(container=container, datapath=["artifact:*.cef.dest","artifact:*.cef.dest_ip","artifact:*.cef.src","artifact:*.cef.src_ip"])
container_artifact_fields_item_0 = [item[0] for item in container_artifact_data]
container_artifact_fields_item_1 = [item[1] for item in container_artifact_data]
container_artifact_fields_item_2 = [item[2] for item in container_artifact_data]
container_artifact_fields_item_3 = [item[3] for item in container_artifact_data]
device_combined_value = phantom.concatenate(container_artifact_fields_item_0, container_artifact_fields_item_1, container_artifact_fields_item_2, container_artifact_fields_item_3, data_summary_dest_value, data_summary_src_value, data_summary_dest_ip_value, data_summary_src_ip_value)
inputs = {
"device": device_combined_value,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
# call playbook "local/CrowdStrike_OAuth_API_Device_Attribute_Lookup", returns the playbook_run_id
playbook_run_id = phantom.playbook("local/CrowdStrike_OAuth_API_Device_Attribute_Lookup", container=container, name="crowdstrike_device_attribute_lookup", callback=join_observable_merge, inputs=inputs)
return
@phantom.playbook_block()
def join_observable_merge(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("join_observable_merge() called")
if phantom.completed(playbook_names=["azure_ad_graph_entity_attribute_lookup", "crowdstrike_device_attribute_lookup"]):
# call connected block "observable_merge"
observable_merge(container=container, handle=handle)
return
@phantom.playbook_block()
def observable_merge(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("observable_merge() called")
################################################################################
# Merges outputs from previous playbooks. Add or take away datapaths based on
# which input playbooks are present.
################################################################################
azure_ad_graph_entity_attribute_lookup_output_observable = phantom.collect2(container=container, datapath=["azure_ad_graph_entity_attribute_lookup:playbook_output:observable"])
crowdstrike_device_attribute_lookup_output_observable = phantom.collect2(container=container, datapath=["crowdstrike_device_attribute_lookup:playbook_output:observable"])
azure_ad_graph_entity_attribute_lookup_output_observable_values = [item[0] for item in azure_ad_graph_entity_attribute_lookup_output_observable]
crowdstrike_device_attribute_lookup_output_observable_values = [item[0] for item in crowdstrike_device_attribute_lookup_output_observable]
parameters = []
parameters.append({
"input_1": azure_ad_graph_entity_attribute_lookup_output_observable_values,
"input_2": crowdstrike_device_attribute_lookup_output_observable_values,
"input_3": None,
"input_4": None,
"input_5": None,
"input_6": None,
"input_7": None,
"input_8": None,
"input_9": None,
"input_10": None,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/list_merge", parameters=parameters, name="observable_merge", callback=observable_filter)
return
@phantom.playbook_block()
def create_events(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("create_events() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Create new events with the outputs of the dispatch lookup (Contains custom code)
################################################################################
external_id_value = container.get("external_id", None)
filtered_cf_result_0 = phantom.collect2(container=container, datapath=["filtered-data:observable_filter:condition_1:observable_merge:custom_function_result.data.item"])
parameters = []
# build parameters list for 'create_events' call
for filtered_cf_result_0_item in filtered_cf_result_0:
if external_id_value is not None:
parameters.append({
"pairs": [
{ "name": "Placeholder", "value": filtered_cf_result_0_item[0] },
],
"incident_id": external_id_value,
})
################################################################################
## Custom Code Start
################################################################################
parameters = []
for filtered_cf_result_0_item in filtered_cf_result_0:
pairs = []
for k,v in filtered_cf_result_0_item[0]['attributes'].items():
pairs.append(
{"name": k, "value": v}
)
parameters.append({
"incident_id": external_id_value,
"pairs": pairs
})
################################################################################
## Custom Code End
################################################################################
phantom.act("create events", parameters=parameters, name="create_events", assets=["builtin_mc_connector"], callback=get_tasks)
return
@phantom.playbook_block()
def filter_response_tasks(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("filter_response_tasks() called")
################################################################################
# Locate the response task that contains this playbook in the suggestions
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["get_tasks:action_result.data.*.suggestions.playbooks.*.name", "==", "get_playbook_name:custom_function:output"]
],
name="filter_response_tasks:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
add_task_note(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
@phantom.playbook_block()
def get_tasks(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_tasks() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Get all tasks from response template
################################################################################
external_id_value = container.get("external_id", None)
parameters = []
if external_id_value is not None:
parameters.append({
"id": external_id_value,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("get tasks", parameters=parameters, name="get_tasks", assets=["builtin_mc_connector"], callback=get_playbook_name)
return
@phantom.playbook_block()
def get_playbook_name(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_playbook_name() called")
################################################################################
# Get the current playbook name for the downstream block in case the playbook
# is used in a custom repo or the playbook name changes.
################################################################################
get_playbook_name__output = None
################################################################################
## Custom Code Start
################################################################################
get_playbook_name__output = phantom.get_playbook_info()[0]['name']
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="get_playbook_name:output", value=json.dumps(get_playbook_name__output))
filter_response_tasks(container=container)
return
@phantom.playbook_block()
def add_task_note(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("add_task_note() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Leave a closing note for the task associated with this playbook.
################################################################################
external_id_value = container.get("external_id", None)
filtered_result_0_data_filter_response_tasks = phantom.collect2(container=container, datapath=["filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.id","filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.phase_id","filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.response_plan_id"])
parameters = []
# build parameters list for 'add_task_note' call
for filtered_result_0_item_filter_response_tasks in filtered_result_0_data_filter_response_tasks:
if external_id_value is not None and filtered_result_0_item_filter_response_tasks[0] is not None and filtered_result_0_item_filter_response_tasks[1] is not None and filtered_result_0_item_filter_response_tasks[2] is not None:
parameters.append({
"id": external_id_value,
"title": "Task Complete",
"content": "MC Attribute Lookup created new events",
"task_id": filtered_result_0_item_filter_response_tasks[0],
"phase_id": filtered_result_0_item_filter_response_tasks[1],
"response_plan_id": filtered_result_0_item_filter_response_tasks[2],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("add task note", parameters=parameters, name="add_task_note", assets=["builtin_mc_connector"], callback=task_status_decision)
return
@phantom.playbook_block()
def join_close_task_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("join_close_task_1() called")
# if the joined function has already been called, do nothing
if phantom.get_run_data(key="join_close_task_1_called"):
return
if phantom.completed(action_names=["add_task_note"]):
# save the state that the joined function has now been called
phantom.save_run_data(key="join_close_task_1_called", value="close_task_1")
# call connected block "close_task_1"
close_task_1(container=container, handle=handle)
return
@phantom.playbook_block()
def close_task_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("close_task_1() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Close the task associated with this playbook. (Contains custom code)
################################################################################
external_id_value = container.get("external_id", None)
filtered_result_0_data_filter_response_tasks = phantom.collect2(container=container, datapath=["filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.name","filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.order","filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.id"])
parameters = []
# build parameters list for 'close_task_1' call
for filtered_result_0_item_filter_response_tasks in filtered_result_0_data_filter_response_tasks:
if external_id_value is not None and filtered_result_0_item_filter_response_tasks[0] is not None and filtered_result_0_item_filter_response_tasks[1] is not None and filtered_result_0_item_filter_response_tasks[2] is not None:
parameters.append({
"id": external_id_value,
"name": filtered_result_0_item_filter_response_tasks[0],
"order": filtered_result_0_item_filter_response_tasks[1],
"task_id": filtered_result_0_item_filter_response_tasks[2],
})
################################################################################
## Custom Code Start
################################################################################
new_param = []
for item in parameters:
item['status'] = "Ended"
new_param.append(item)
parameters = new_param
################################################################################
## Custom Code End
################################################################################
phantom.act("update task", parameters=parameters, name="close_task_1", assets=["builtin_mc_connector"])
return
@phantom.playbook_block()
def task_status_decision(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("task_status_decision() called")
################################################################################
# Determines if the task has been started before progressing to closed.
################################################################################
# check for 'if' condition 1
found_match_1 = phantom.decision(
container=container,
conditions=[
["filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.status", "==", "Pending"]
])
# call connected blocks if condition 1 matched
if found_match_1:
start_task(action=action, success=success, container=container, results=results, handle=handle)
return
# check for 'else' condition 2
join_close_task_1(action=action, success=success, container=container, results=results, handle=handle)
return
@phantom.playbook_block()
def start_task(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("start_task() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Start the task associated with this playbook. (Contains custom code)
################################################################################
external_id_value = container.get("external_id", None)
filtered_result_0_data_filter_response_tasks = phantom.collect2(container=container, datapath=["filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.name","filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.order","filtered-data:filter_response_tasks:condition_1:get_tasks:action_result.data.*.id"])
parameters = []
# build parameters list for 'start_task' call
for filtered_result_0_item_filter_response_tasks in filtered_result_0_data_filter_response_tasks:
if external_id_value is not None and filtered_result_0_item_filter_response_tasks[0] is not None and filtered_result_0_item_filter_response_tasks[1] is not None and filtered_result_0_item_filter_response_tasks[2] is not None:
parameters.append({
"id": external_id_value,
"name": filtered_result_0_item_filter_response_tasks[0],
"order": filtered_result_0_item_filter_response_tasks[1],
"owner": "soar_automation_user",
"task_id": filtered_result_0_item_filter_response_tasks[2],
})
################################################################################
## Custom Code Start
################################################################################
new_param = []
for item in parameters:
item['status'] = "Started"
new_param.append(item)
parameters = new_param
################################################################################
## Custom Code End
################################################################################
phantom.act("update task", parameters=parameters, name="start_task", assets=["builtin_mc_connector"], callback=join_close_task_1)
return
@phantom.playbook_block()
def on_finish(container, summary):
phantom.debug("on_finish() called")
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
return