forked from phantomcyber/playbooks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compromised_email_containment.py
509 lines (383 loc) · 22.2 KB
/
compromised_email_containment.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
"""
This playbook responds automatically when a threat intelligence service detects that one or more of our internal email addresses have been compromised and are sending malicious outbound email. First we will run a query to find the LDAP accounts associated with the identified email addresses. Then if the Phantom admin approves, those LDAP accounts will have their passwords reset to prevent further misuse. All investigation results and actions taken will be documented as "Work notes" in a newly created ServiceNow ticket.
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
def on_start(container):
phantom.debug('on_start() called')
# call 'filter_1' block
filter_1(container=container)
return
"""
Query LDAP for the user account with the given email address.
"""
def query_ldap(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('query_ldap() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'query_ldap' call
filtered_artifacts_data_1 = phantom.collect2(container=container, datapath=['filtered-data:filter_1:condition_1:artifact:*.cef.fromEmail', 'filtered-data:filter_1:condition_1:artifact:*.id'])
parameters = []
# build parameters list for 'query_ldap' call
for filtered_artifacts_item_1 in filtered_artifacts_data_1:
if filtered_artifacts_item_1[0]:
parameters.append({
'username': filtered_artifacts_item_1[0],
'fields': "",
'attribute': "",
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': filtered_artifacts_item_1[1]},
})
phantom.act("get user attributes", parameters=parameters, assets=['ldap'], callback=filter_2, name="query_ldap", parent_action=action)
return
"""
Split based on whether or not the given email address is associated with an LDAP account.
"""
def filter_2(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('filter_2() called')
# collect filtered artifact ids for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["query_ldap:action_result.data.*.samaccountname", "!=", ""],
],
name="filter_2:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
format_2(action=action, success=success, container=container, results=results, handle=handle, 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=[
["query_ldap:action_result.data.*.samaccountname", "==", ""],
],
name="filter_2:condition_2")
# call connected blocks if filtered artifacts or results
if matched_artifacts_2 or matched_results_2:
format_6(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_2, filtered_results=matched_results_2)
return
"""
Enrich the ticket with the list of email addresses that are not associated with LDAP accounts and explain why that might be.
"""
def format_6(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('format_6() called')
template = """{{ \"work_notes\": \"Missing LDAP Users: Phantom automatically ran an LDAP query and no LDAP users were found with the following email addresses:\\n{0}\\n\\nThe threat intelligence alert may have been a false positive or an adversary may be spoofing a non-existent company email address. No further automated action will be taken by Phantom at this time, but this should be investigated further.\"}}"""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_2:condition_2:query_ldap:action_result.parameter.username",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_6")
update_ticket_5(container=container)
return
"""
Enrich the ticket with the list of email addresses that are not associated with LDAP accounts and explain why that might be.
"""
def update_ticket_5(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('update_ticket_5() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'update_ticket_5' call
results_data_1 = phantom.collect2(container=container, datapath=['create_ticket_1:action_result.summary.created_ticket_id', 'create_ticket_1:action_result.parameter.context.artifact_id'], action_results=results)
formatted_data_1 = phantom.get_format_data(name='format_6')
parameters = []
# build parameters list for 'update_ticket_5' call
for results_item_1 in results_data_1:
if results_item_1[0]:
parameters.append({
'table': "",
'vault_id': "",
'id': results_item_1[0],
'fields': formatted_data_1,
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': results_item_1[1]},
})
phantom.act("update ticket", parameters=parameters, assets=['servicenow'], name="update_ticket_5")
return
"""
Enrich the ticket with list of email addresses that are associated with LDAP accounts.
"""
def update_ticket_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('update_ticket_1() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'update_ticket_1' call
results_data_1 = phantom.collect2(container=container, datapath=['create_ticket_1:action_result.summary.created_ticket_id', 'create_ticket_1:action_result.parameter.context.artifact_id'], action_results=results)
formatted_data_1 = phantom.get_format_data(name='format_2')
parameters = []
# build parameters list for 'update_ticket_1' call
for results_item_1 in results_data_1:
if results_item_1[0]:
parameters.append({
'table': "",
'vault_id': "",
'id': results_item_1[0],
'fields': formatted_data_1,
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': results_item_1[1]},
})
phantom.act("update ticket", parameters=parameters, assets=['servicenow'], callback=compromised_email_password_reset, name="update_ticket_1")
return
"""
Prompt the admin user before resetting passwords because it could affect operations.
"""
def compromised_email_password_reset(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('compromised_email_password_reset() called')
# set user and message variables for phantom.prompt call
user = "admin"
message = """A threat intelligence service has detected that one or more of our internal email accounts has been compromised and is being used to send malicious outbound email. The following email addresses have been identified and found to be associated with LDAP accounts:
{0}
Select Yes to reset the passwords on the associated LDAP accounts."""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_2:condition_1:query_ldap:action_result.parameter.username",
]
# response options
options = {
"type": "list",
"choices": [
"Yes",
"No",
]
}
phantom.prompt(container=container, user=user, message=message, respond_in_mins=30, name="compromised_email_password_reset", parameters=parameters, options=options, callback=decision_1)
return
"""
Split on whether or not the admin approved the password resets.
"""
def decision_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('decision_1() called')
# check for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["compromised_email_password_reset:action_result.summary.response", "==", "Yes"],
])
# call connected blocks if condition 1 matched
if matched_artifacts_1 or matched_results_1:
reset_password_2(action=action, success=success, container=container, results=results, handle=handle)
return
# call connected blocks for 'else' condition 2
format_5(action=action, success=success, container=container, results=results, handle=handle)
return
"""
Use LDAP to reset passwords on the identified accounts. This will require users to login manually to choose new passwords.
"""
def reset_password_2(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('reset_password_2() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'reset_password_2' call
filtered_results_data_1 = phantom.collect2(container=container, datapath=["filtered-data:filter_2:condition_1:query_ldap:action_result.parameter.username", "filtered-data:filter_2:condition_1:query_ldap:action_result.parameter.context.artifact_id"])
parameters = []
# build parameters list for 'reset_password_2' call
for filtered_results_item_1 in filtered_results_data_1:
if filtered_results_item_1[0]:
parameters.append({
'username': filtered_results_item_1[0],
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': filtered_results_item_1[1]},
})
phantom.act("reset password", parameters=parameters, assets=['ldap'], callback=decision_2, name="reset_password_2")
return
"""
Split on whether or not the password resets succeeded.
"""
def decision_2(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('decision_2() called')
# check for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["reset_password_2:action_result.status", "==", "success"],
])
# call connected blocks if condition 1 matched
if matched_artifacts_1 or matched_results_1:
format_3(action=action, success=success, container=container, results=results, handle=handle)
return
# call connected blocks for 'else' condition 2
format_4(action=action, success=success, container=container, results=results, handle=handle)
return
"""
Enrich the ticket with the list of accounts that were successfully reset.
"""
def format_3(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('format_3() called')
template = """{{ \"work_notes\": \"LDAP Password Reset Succeeded: The Phantom admin accepted the prompt and LDAP passwords were automatically reset for the following email accounts:\\n{0}\\n\\nNo further automated action will be taken by Phantom at this time.\"}}"""
# parameter list for template variable replacement
parameters = [
"reset_password_2:action_result.parameter.username",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_3")
update_ticket_2(container=container)
return
"""
Enrich the ticket with the list of accounts that were successfully reset.
"""
def update_ticket_2(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('update_ticket_2() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'update_ticket_2' call
results_data_1 = phantom.collect2(container=container, datapath=['create_ticket_1:action_result.summary.created_ticket_id', 'create_ticket_1:action_result.parameter.context.artifact_id'], action_results=results)
formatted_data_1 = phantom.get_format_data(name='format_3')
parameters = []
# build parameters list for 'update_ticket_2' call
for results_item_1 in results_data_1:
if results_item_1[0]:
parameters.append({
'table': "",
'vault_id': "",
'id': results_item_1[0],
'fields': formatted_data_1,
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': results_item_1[1]},
})
phantom.act("update ticket", parameters=parameters, assets=['servicenow'], name="update_ticket_2")
return
"""
Enrich the ticket with the list of accounts where password reset failed and show the error message.
"""
def format_4(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('format_4() called')
template = """{{ \"work_notes\": \"LDAP Password Reset Failed: The Phantom admin accepted the prompt but the LDAP password reset failed for the following email accounts:\\n{0}\\n\\nThe failure message was:\\n{1}\\n\\nNo further automated action will be taken by Phantom at this time.\"}}"""
# parameter list for template variable replacement
parameters = [
"reset_password_2:action_result.parameter.username",
"reset_password_2:action_result.message",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_4")
update_ticket_3(container=container)
return
"""
Enrich the ticket with the list of accounts where password reset failed and show the error message.
"""
def update_ticket_3(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('update_ticket_3() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'update_ticket_3' call
results_data_1 = phantom.collect2(container=container, datapath=['create_ticket_1:action_result.summary.created_ticket_id', 'create_ticket_1:action_result.parameter.context.artifact_id'], action_results=results)
formatted_data_1 = phantom.get_format_data(name='format_4')
parameters = []
# build parameters list for 'update_ticket_3' call
for results_item_1 in results_data_1:
if results_item_1[0]:
parameters.append({
'table': "",
'vault_id': "",
'id': results_item_1[0],
'fields': formatted_data_1,
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': results_item_1[1]},
})
phantom.act("update ticket", parameters=parameters, assets=['servicenow'], name="update_ticket_3")
return
"""
Enrich the ticket with the list of accounts for which the Phantom admin denied the password reset.
"""
def format_5(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('format_5() called')
template = """{{ \"work_notes\": \"Admin Denied Password Reset: The Phantom admin denied the prompt asking whether or not LDAP passwords should be automatically reset for the following email accounts:\\n{0}\\n\\nNo further automated action will be taken by Phantom at this time, but this should be investigated further.\"}}"""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_2:condition_1:query_ldap:action_result.parameter.username",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_5")
update_ticket_4(container=container)
return
"""
Enrich the ticket with the list of accounts for which the Phantom admin denied the password reset.
"""
def update_ticket_4(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('update_ticket_4() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'update_ticket_4' call
results_data_1 = phantom.collect2(container=container, datapath=['create_ticket_1:action_result.summary.created_ticket_id', 'create_ticket_1:action_result.parameter.context.artifact_id'], action_results=results)
formatted_data_1 = phantom.get_format_data(name='format_5')
parameters = []
# build parameters list for 'update_ticket_4' call
for results_item_1 in results_data_1:
if results_item_1[0]:
parameters.append({
'table': "",
'vault_id': "",
'id': results_item_1[0],
'fields': formatted_data_1,
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': results_item_1[1]},
})
phantom.act("update ticket", parameters=parameters, assets=['servicenow'], name="update_ticket_4")
return
"""
Only process artifacts with internal email addresses.
"""
def filter_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('filter_1() called')
# collect filtered artifact ids for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["@corp.contoso.com", "in", "artifact:*.cef.fromEmail"],
],
name="filter_1:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
format_1(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)
return
"""
This uses the default ServiceNow ticket type: "Incident". The returned ticket ID will be used to update this ticket once more is known.
"""
def create_ticket_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('create_ticket_1() called')
# collect data for 'create_ticket_1' call
formatted_data_1 = phantom.get_format_data(name='format_1')
parameters = []
# build parameters list for 'create_ticket_1' call
parameters.append({
'short_description': "compromised internal email account",
'table': "",
'vault_id': "",
'description': formatted_data_1,
'fields': "",
})
phantom.act("create ticket", parameters=parameters, assets=['servicenow'], callback=query_ldap, name="create_ticket_1")
return
"""
Start the ticket with the list of email addresses and an overview of what the playbook is going to do.
"""
def format_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('format_1() called')
template = """A threat intelligence service has detected that one or more of our internal email accounts has been compromised and is being used to send malicious outbound email. The following email addresses have been identified:
{0}
Now Phantom will run a query to find the LDAP accounts associated with the identified email addresses. Then if the Phantom admin approves, those LDAP accounts will have their passwords reset to prevent further misuse. All investigation results and containment actions taken will be documented as \"Work notes\" in this ticket."""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_1:condition_1:artifact:*.cef.fromEmail",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_1")
create_ticket_1(container=container)
return
"""
Enrich the ticket with list of email addresses that are associated with LDAP accounts.
"""
def format_2(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('format_2() called')
template = """{{ \"work_notes\": \"Found LDAP Users: Phantom automatically ran an LDAP query and LDAP users were found with the following email addresses:\\n{0}\\n\\nNext the Phantom admin will be prompted about whether or not to reset those user's passwords.\"}}"""
# parameter list for template variable replacement
parameters = [
"filtered-data:filter_2:condition_1:query_ldap:action_result.parameter.username",
]
phantom.format(container=container, template=template, parameters=parameters, name="format_2")
update_ticket_1(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 detals 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