forked from phantomcyber/playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrowdStrike_OAuth_API_Identifier_Activity_Analysis.py
457 lines (325 loc) · 20.3 KB
/
CrowdStrike_OAuth_API_Identifier_Activity_Analysis.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
"""
Accepts a file_hash or domain name, and asks CrowdStrike for a list of device IDs that have interacted with each. The list of IDs is then sent back to Crowdstrike to get more information, and then produces a normalized output and summary table.
"""
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_filter' block
input_filter(container=container)
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, **kwargs):
phantom.debug("input_filter() called")
################################################################################
# Routing by indicator type
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["playbook_input:file", "!=", None]
],
name="input_filter:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
hunt_file(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_input:domain", "!=", None]
],
name="input_filter:condition_2")
# call connected blocks if filtered artifacts or results
if matched_artifacts_2 or matched_results_2:
hunt_domain(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 hunt_file(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("hunt_file() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
filtered_input_0_file = phantom.collect2(container=container, datapath=["filtered-data:input_filter:condition_1:playbook_input:file"])
parameters = []
# build parameters list for 'hunt_file' call
for filtered_input_0_file_item in filtered_input_0_file:
if filtered_input_0_file_item[0] is not None:
parameters.append({
"hash": filtered_input_0_file_item[0],
"limit": 1000,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("hunt file", parameters=parameters, name="hunt_file", assets=["crowdstrike_oauth_api"], callback=file_results_filter)
return
@phantom.playbook_block()
def hunt_domain(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("hunt_domain() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
filtered_input_0_domain = phantom.collect2(container=container, datapath=["filtered-data:input_filter:condition_2:playbook_input:domain"])
parameters = []
# build parameters list for 'hunt_domain' call
for filtered_input_0_domain_item in filtered_input_0_domain:
if filtered_input_0_domain_item[0] is not None:
parameters.append({
"limit": 1000,
"domain": filtered_input_0_domain_item[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("hunt domain", parameters=parameters, name="hunt_domain", assets=["crowdstrike_oauth_api"], callback=domain_results_filter)
return
@phantom.playbook_block()
def file_results_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("file_results_filter() called")
################################################################################
# Only proceed if there are results
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["hunt_file:action_result.summary.device_count", ">", 0]
],
name="file_results_filter:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
get_systems_from_file(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 domain_results_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("domain_results_filter() called")
################################################################################
# Only proceed if there are results
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["hunt_domain:action_result.summary.device_count", ">", 0]
],
name="domain_results_filter:condition_1")
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
get_systems_from_domain(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_report_file(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_report_file() called")
template = """SOAR searched for occurrences of `{0}` within your environment using CrowdStrike. The table below shows a summary of the information gathered.\n\n| File | Computer | Last IP Address | OS | CrowdStrike AID | Source |\n| --- | --- | --- | --- | --- | --- |\n%%\n| `{0}` | {1} | {2} | {3} | {4} | CrowdStrike OAuth API |\n%%\n\n"""
# parameter list for template variable replacement
parameters = [
"hunt_file:action_result.parameter.hash",
"get_systems_from_file:action_result.data.*.hostname",
"get_systems_from_file:action_result.data.*.local_ip",
"get_systems_from_file:action_result.data.*.os_version",
"get_systems_from_file:action_result.parameter.id"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_report_file")
build_file_output(container=container)
return
@phantom.playbook_block()
def format_report_domain(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_report_domain() called")
template = """SOAR searched for occurrences of `{0}` within your environment using CrowdStrike. The table below shows a summary of the information gathered.\n\n| Domain | Computer | Last IP Address | OS | CrowdStrike AID | Source |\n| --- | --- | --- | --- | --- | --- |\n%%\n| `{0}` | {1} | {2} | {3} | {4} | CrowdStrike OAuth API |\n%%\n"""
# parameter list for template variable replacement
parameters = [
"hunt_domain:action_result.parameter.domain",
"get_systems_from_domain:action_result.data.*.hostname",
"get_systems_from_domain:action_result.data.*.local_ip",
"get_systems_from_domain:action_result.data.*.os_version",
"get_systems_from_domain:action_result.parameter.id"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_report_domain")
build_domain_output(container=container)
return
@phantom.playbook_block()
def build_file_output(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("build_file_output() called")
################################################################################
# First constructs a list of devices based upon details returned in previous actions,
# and then appends that list to the observable object which includes the context
# in which that list makes any sense
################################################################################
hunt_file_result_data = phantom.collect2(container=container, datapath=["hunt_file:action_result.parameter.hash","hunt_file:action_result.summary.device_count"], action_results=results)
get_systems_from_file_result_data = phantom.collect2(container=container, datapath=["get_systems_from_file:action_result.data"], action_results=results)
hunt_file_parameter_hash = [item[0] for item in hunt_file_result_data]
hunt_file_summary_device_count = [item[1] for item in hunt_file_result_data]
get_systems_from_file_result_item_0 = [item[0] for item in get_systems_from_file_result_data]
build_file_output__observable_array = None
################################################################################
## Custom Code Start
################################################################################
build_file_output__observable_array = []
device_list = []
indicator = hunt_file_parameter_hash[0]
count = hunt_file_summary_device_count[0]
# if AIDs that no longer exist saw the artifact, they can still
# be included in the middle of the hunt response, but will return
# empty-ish objects from the `get system info` action, so we need
# to length check them
# Build devices list
for item in get_systems_from_file_result_item_0:
if len(item) > 0:
device = item[0]
device_filtered = {
"name": device['hostname'],
"id": device['device_id'],
"ip_address": device['local_ip'],
"operating_system": device['os_version']
}
device_list.append(device_filtered)
# Build observable object
observable_array = {
"value": indicator,
"type": "file",
"total_count": len(device_list),
"source": "Crowdstrike OAuth",
"identifier_activity": device_list
}
build_file_output__observable_array.append(observable_array)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="build_file_output:observable_array", value=json.dumps(build_file_output__observable_array))
return
@phantom.playbook_block()
def build_domain_output(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("build_domain_output() called")
################################################################################
# First constructs a list of devices based upon details returned in previous actions,
# and then appends that list to the observable object which includes the context
# in which that list makes any sense
################################################################################
hunt_domain_result_data = phantom.collect2(container=container, datapath=["hunt_domain:action_result.parameter.domain","hunt_domain:action_result.summary.device_count"], action_results=results)
get_systems_from_domain_result_data = phantom.collect2(container=container, datapath=["get_systems_from_domain:action_result.data"], action_results=results)
hunt_domain_parameter_domain = [item[0] for item in hunt_domain_result_data]
hunt_domain_summary_device_count = [item[1] for item in hunt_domain_result_data]
get_systems_from_domain_result_item_0 = [item[0] for item in get_systems_from_domain_result_data]
build_domain_output__observable_array = None
################################################################################
## Custom Code Start
################################################################################
build_domain_output__observable_array = []
device_list = []
indicator = hunt_domain_parameter_domain[0]
count = hunt_domain_summary_device_count[0]
# Build devices list
for item in get_systems_from_domain_result_item_0:
# if AIDs that no longer exist saw the artifact, they can still
# be included in the middle of the hunt response, but will return
# empty-ish objects from the `get system info` action, so we need
# to length check them
if len(item) > 0:
device = item[0]
device_filtered = {
"name": device['hostname'],
"id": device['device_id'],
"ip_address": device['local_ip'],
"operating_system": device['os_version']
}
device_list.append(device_filtered)
# Build observable object
observable_array = {
"value": indicator,
"type": "domain",
"total_count": len(device_list),
"source": "Crowdstrike OAuth",
"identifier_activity": device_list
}
build_domain_output__observable_array.append(observable_array)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="build_domain_output:observable_array", value=json.dumps(build_domain_output__observable_array))
return
@phantom.playbook_block()
def get_systems_from_file(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_systems_from_file() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
filtered_result_0_data_file_results_filter = phantom.collect2(container=container, datapath=["filtered-data:file_results_filter:condition_1:hunt_file:action_result.data.*.device_id"])
parameters = []
# build parameters list for 'get_systems_from_file' call
for filtered_result_0_item_file_results_filter in filtered_result_0_data_file_results_filter:
if filtered_result_0_item_file_results_filter[0] is not None:
parameters.append({
"id": filtered_result_0_item_file_results_filter[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("get system info", parameters=parameters, name="get_systems_from_file", assets=["crowdstrike_oauth_api"], callback=format_report_file)
return
@phantom.playbook_block()
def get_systems_from_domain(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_systems_from_domain() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
filtered_result_0_data_domain_results_filter = phantom.collect2(container=container, datapath=["filtered-data:domain_results_filter:condition_1:hunt_domain:action_result.data.*.device_id"])
parameters = []
# build parameters list for 'get_systems_from_domain' call
for filtered_result_0_item_domain_results_filter in filtered_result_0_data_domain_results_filter:
if filtered_result_0_item_domain_results_filter[0] is not None:
parameters.append({
"id": filtered_result_0_item_domain_results_filter[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("get system info", parameters=parameters, name="get_systems_from_domain", assets=["crowdstrike_oauth_api"], callback=format_report_domain)
return
@phantom.playbook_block()
def on_finish(container, summary):
phantom.debug("on_finish() called")
format_report_file = phantom.get_format_data(name="format_report_file")
format_report_domain = phantom.get_format_data(name="format_report_domain")
build_file_output__observable_array = json.loads(_ if (_ := phantom.get_run_data(key="build_file_output:observable_array")) != "" else "null") # pylint: disable=used-before-assignment
build_domain_output__observable_array = json.loads(_ if (_ := phantom.get_run_data(key="build_domain_output:observable_array")) != "" else "null") # pylint: disable=used-before-assignment
observable_combined_value = phantom.concatenate(build_file_output__observable_array, build_domain_output__observable_array)
markdown_report_combined_value = phantom.concatenate(format_report_file, format_report_domain)
output = {
"observable": observable_combined_value,
"markdown_report": markdown_report_combined_value,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.save_playbook_output_data(output=output)
return