-
Notifications
You must be signed in to change notification settings - Fork 203
/
MS_Graph_for_Office_365_Search_and_Purge.py
452 lines (320 loc) · 20.2 KB
/
MS_Graph_for_Office_365_Search_and_Purge.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
"""
Accepts an Internet Message ID, searches for its presence in each mailbox, and then deletes the ones it finds. The Message Eviction playbook performs a "soft delete", which allows for messages to be recovered.
"""
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 'input_decision' block
input_decision(container=container)
return
@phantom.playbook_block()
def playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1() called")
filtered_artifact_0_data_input_filter = phantom.collect2(container=container, datapath=["filtered-data:input_filter:condition_1:artifact:*.cef.internet message id"])
filtered_artifact_0__cef_internet_message_id = [item[0] for item in filtered_artifact_0_data_input_filter]
inputs = {
"message_id": filtered_artifact_0__cef_internet_message_id,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
# call playbook "local/MS_Graph_for_Office_365_Message_Identifier_Activity_Analysis", returns the playbook_run_id
playbook_run_id = phantom.playbook("local/MS_Graph_for_Office_365_Message_Identifier_Activity_Analysis", container=container, name="playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1", callback=identifier_filter, inputs=inputs)
return
@phantom.playbook_block()
def artifact_does_not_exist(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("artifact_does_not_exist() called")
################################################################################
# In the event an artifact does not exist suitable for this usecase, inform the
# user.
################################################################################
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.comment(container=container, comment="\"No Internet Message ID artifacts found\"")
return
@phantom.playbook_block()
def identifier_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("identifier_filter() called")
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1:playbook_output:observables", "!=", None]
],
name="identifier_filter:condition_1",
delimiter=None)
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
format_prompt(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
# collect filtered artifact ids and results for 'if' condition 2
matched_artifacts_2, matched_results_2 = phantom.condition(
container=container,
conditions=[
["playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1:playbook_output:observables", "==", None]
],
name="identifier_filter:condition_2",
delimiter=None)
# call connected blocks if filtered artifacts or results
if matched_artifacts_2 or matched_results_2:
no_observable_found(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_2, filtered_results=matched_results_2)
return
@phantom.playbook_block()
def playbook_ms_graph_for_office_365_message_eviction_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("playbook_ms_graph_for_office_365_message_eviction_1() called")
filtered_output_0_playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1_output_observables = phantom.collect2(container=container, datapath=["filtered-data:identifier_filter:condition_1:playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1:playbook_output:observables.recipient","filtered-data:identifier_filter:condition_1:playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1:playbook_output:observables.value"])
filtered_output_0_playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1_output_observables_recipient = [item[0] for item in filtered_output_0_playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1_output_observables]
filtered_output_0_playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1_output_observables_value = [item[1] for item in filtered_output_0_playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1_output_observables]
inputs = {
"email": filtered_output_0_playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1_output_observables_recipient,
"message_id": filtered_output_0_playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1_output_observables_value,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
# call playbook "local/MS_Graph_for_Office_365_Message_Eviction", returns the playbook_run_id
playbook_run_id = phantom.playbook("local/MS_Graph_for_Office_365_Message_Eviction", container=container, name="playbook_ms_graph_for_office_365_message_eviction_1", callback=format_note, inputs=inputs)
return
@phantom.playbook_block()
def no_observable_found(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("no_observable_found() called")
################################################################################
# In the event the Message Identifier Activity Analysis playbook does not return
# an observable, inform the user.
################################################################################
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.comment(container=container, comment="No observable output found for dispatched playbook.")
return
@phantom.playbook_block()
def format_note(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("format_note() called")
################################################################################
# Format the output of the Message Eviction playbook to provide a table to the
# user showing the deleted messages.
################################################################################
template = """SOAR deleted messages in O365. The table below shows a summary of the messages.\n\n| Mailbox | Message ID | Status | Message |\n| --- | --- | --- | --- |\n%%\n| {0} | {1} | {2} | {3} |\n%%"""
# parameter list for template variable replacement
parameters = [
"playbook_ms_graph_for_office_365_message_eviction_1:playbook_output:observable.mailbox",
"playbook_ms_graph_for_office_365_message_eviction_1:playbook_output:observable.value",
"playbook_ms_graph_for_office_365_message_eviction_1:playbook_output:observable.status",
"playbook_ms_graph_for_office_365_message_eviction_1:playbook_output:observable.message"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_note")
update_workbook_task(container=container)
return
@phantom.playbook_block()
def update_workbook_task(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("update_workbook_task() called")
################################################################################
# Upon completion, update the workbook task with the formatted output and mark
# task as complete.
################################################################################
id_value = container.get("id", None)
format_note = phantom.get_format_data(name="format_note")
parameters = []
parameters.append({
"owner": None,
"status": "complete",
"container": id_value,
"task_name": "playbook",
"note_title": "Search and Purge Results",
"note_content": format_note,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/workbook_task_update", parameters=parameters, name="update_workbook_task", callback=format_artifact)
return
@phantom.playbook_block()
def format_artifact(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("format_artifact() called")
################################################################################
# These artifacts capture the message ID and the mailbox they were removed from
# so that they can be restored later.
################################################################################
template = """%%\n{{\"cef_data\": \n{{\"email\": \"{0}\", \"internet message id\": \"{1}\"}}}}\n%%"""
# parameter list for template variable replacement
parameters = [
"playbook_ms_graph_for_office_365_message_eviction_1:playbook_input:email",
"playbook_ms_graph_for_office_365_message_eviction_1:playbook_input:message_id"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_artifact")
create_artifacts_0(container=container)
return
@phantom.playbook_block()
def create_artifacts_0(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("create_artifacts_0() called")
################################################################################
# These artifacts capture the message ID and the mailbox they were removed from
# so that they can be restored later.
################################################################################
id_value = container.get("id", None)
format_artifact__as_list = phantom.get_format_data(name="format_artifact__as_list")
parameters = []
# build parameters list for 'create_artifacts_0' call
for format_artifact__item in format_artifact__as_list:
parameters.append({
"name": "Purged Email Results",
"tags": None,
"label": "office_365_delete",
"severity": None,
"cef_field": None,
"cef_value": None,
"container": id_value,
"input_json": format_artifact__item,
"cef_data_type": None,
"run_automation": None,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/artifact_create", parameters=parameters, name="create_artifacts_0")
return
@phantom.playbook_block()
def input_decision(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("input_decision() called")
# check for 'if' condition 1
found_match_1 = phantom.decision(
container=container,
conditions=[
["artifact:*.cef.internet message id", "!=", ""]
],
delimiter=None)
# call connected blocks if condition 1 matched
if found_match_1:
input_filter(action=action, success=success, container=container, results=results, handle=handle)
return
# check for 'else' condition 2
artifact_does_not_exist(action=action, success=success, container=container, results=results, handle=handle)
return
@phantom.playbook_block()
def input_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("input_filter() called")
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["artifact:*.cef.internet message id", "!=", ""]
],
name="input_filter:condition_1",
delimiter=None)
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1(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 format_prompt(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("format_prompt() called")
template = """| Mailbox | Subject | Email ID |\n| --- | --- | --- |\n%%\n| {0} | {1} | {2} |\n%%\n"""
# parameter list for template variable replacement
parameters = [
"filtered-data:identifier_filter:condition_2:playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1:playbook_output:observables.recipient",
"filtered-data:identifier_filter:condition_2:playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1:playbook_output:observables.subject",
"filtered-data:identifier_filter:condition_2:playbook_ms_graph_for_office_365_message_identifier_activity_analysis_1:playbook_output:observables.value"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_prompt")
delete_email_prompt_1(container=container)
return
@phantom.playbook_block()
def delete_email_prompt_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("delete_email_prompt_1() called")
# set user and message variables for phantom.prompt call
user = container.get('owner_name', None)
role = None
message = """The following emails will be deleted. These emails will be recoverable but will not be visible in the mailbox.\n\n{0} """
# parameter list for template variable replacement
parameters = [
"format_prompt:formatted_data"
]
# responses
response_types = [
{
"prompt": "Delete these messages?",
"options": {
"type": "list",
"choices": [
"Yes",
"No"
],
},
}
]
phantom.prompt2(container=container, user=user, role=role, message=message, respond_in_mins=30, name="delete_email_prompt_1", parameters=parameters, response_types=response_types, callback=prompt_decision)
return
@phantom.playbook_block()
def prompt_decision(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, loop_state_json=None, **kwargs):
phantom.debug("prompt_decision() called")
# check for 'if' condition 1
found_match_1 = phantom.decision(
container=container,
conditions=[
["delete_email_prompt_1:action_result.summary.responses.0", "==", "yes"]
],
delimiter=None)
# call connected blocks if condition 1 matched
if found_match_1:
playbook_ms_graph_for_office_365_message_eviction_1(action=action, success=success, container=container, results=results, handle=handle)
return
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