-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathCrowdStrike_OAuth_API_File_Collection.py
More file actions
606 lines (441 loc) · 30.5 KB
/
CrowdStrike_OAuth_API_File_Collection.py
File metadata and controls
606 lines (441 loc) · 30.5 KB
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
"""
Accepts a hostname or device id as well as a file path as input and collects the file to the event File Vault from a device in Crowdstrike. An artifact is created from the collected file. We then generate an observable report as well as a Markdown formatted report. Both reports can be customized based on user preference.
"""
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, loop_state_json=None, **kwargs):
phantom.debug("input_filter() called")
################################################################################
# Determines if the provided inputs are present in the dataset.
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
logical_operator="and",
conditions=[
["playbook_input:device", "!=", ""],
["playbook_input:path", "!=", ""]
],
name="input_filter:condition_1",
delimiter=None)
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
format_fql(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 host_observables(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("host_observables() called")
################################################################################
# Format a normalized output for each host
################################################################################
create_session_result_data = phantom.collect2(container=container, datapath=["create_session:action_result.parameter.device_id"], action_results=results)
query_device_result_data = phantom.collect2(container=container, datapath=["query_device:action_result.data.*.hostname"], action_results=results)
list_session_files_result_data = phantom.collect2(container=container, datapath=["list_session_files:action_result.data.*.resources.*.sha256","list_session_files:action_result.data.*.resources.*.size"], action_results=results)
get_session_file_result_data = phantom.collect2(container=container, datapath=["get_session_file:action_result.data.*.vault_document","get_session_file:action_result.summary.vault_id"], action_results=results)
determine_get_status__file_name = json.loads(_ if (_ := phantom.get_run_data(key="determine_get_status:file_name")) != "" else "null") # pylint: disable=used-before-assignment
determine_get_status__status = json.loads(_ if (_ := phantom.get_run_data(key="determine_get_status:status")) != "" else "null") # pylint: disable=used-before-assignment
determine_get_status__status_detail = json.loads(_ if (_ := phantom.get_run_data(key="determine_get_status:status_detail")) != "" else "null") # pylint: disable=used-before-assignment
create_session_parameter_device_id = [item[0] for item in create_session_result_data]
query_device_result_item_0 = [item[0] for item in query_device_result_data]
list_session_files_result_item_0 = [item[0] for item in list_session_files_result_data]
list_session_files_result_item_1 = [item[1] for item in list_session_files_result_data]
get_session_file_result_item_0 = [item[0] for item in get_session_file_result_data]
get_session_file_summary_vault_id = [item[1] for item in get_session_file_result_data]
host_observables__observable_array = None
################################################################################
## Custom Code Start
################################################################################
host_observables__observable_array = []
for device_id, hostname, file_hash, file_size, vault_document, vault_id in zip(create_session_parameter_device_id, query_device_result_item_0, list_session_files_result_item_0, list_session_files_result_item_1, get_session_file_result_item_0, get_session_file_summary_vault_id):
file_artifacts = []
for file_name, status, status_message in zip(determine_get_status__file_name, determine_get_status__status, determine_get_status__status_detail):
# Initialize the observable dictionary
if status == "success":
file_artifacts.append({
"name": file_name,
"status": status,
"status_detail": status_message,
"hashes": [
{
"value": file_hash,
"algorithm": "SHA-256",
"algorithm_id": 3
}
],
"size": file_size,
"vault_document": vault_document,
"vault_id": vault_id
})
else:
file_artifacts.append({
"name": file_name,
"status": status,
"status_detail": status_message
})
observable = {
"source": "Crowdstrike OAuth API",
"type": "Endpoint",
"activity_name": "File Collection",
"uid": device_id,
"hostname": hostname,
"file_artifacts": file_artifacts
}
# Add the observable to the array
host_observables__observable_array.append(observable)
# Debug output for verification
#phantom.debug(host_observables__observable_array)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="host_observables:observable_array", value=json.dumps(host_observables__observable_array))
return
@phantom.playbook_block()
def format_fql(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_fql() called")
################################################################################
# Format the FQL query to get the input device information using its ID or hostname.
################################################################################
template = """%%\nhostname:['{0}'],device_id:['{0}']\n%%"""
# parameter list for template variable replacement
parameters = [
"playbook_input:device"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_fql")
query_device(container=container)
return
@phantom.playbook_block()
def query_device(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("query_device() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Get information about the device using its hostname or device id.
################################################################################
format_fql__as_list = phantom.get_format_data(name="format_fql__as_list")
parameters = []
# build parameters list for 'query_device' call
for format_fql__item in format_fql__as_list:
parameters.append({
"limit": 50,
"filter": format_fql__item,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("query device", parameters=parameters, name="query_device", assets=["crowdstrike_oauth_api"], callback=create_session)
return
@phantom.playbook_block()
def format_file_collection_report(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_file_collection_report() called")
################################################################################
# Format a summary table with the information gathered from the playbook.
################################################################################
template = """Endpoint Files were collected by Splunk SOAR and an artifact was created. The table below summarizes the information gathered.\n\n| Device ID | Hostname | File Path | File Hash | Vault ID | Artifact ID | Status | Message |\n| --- | --- | --- | --- | --- | --- | --- | --- |\n%%\n| {0} | {1} | `{2}` | {3} | {4} | {5} | {6} | `{7}` |\n%%"""
# parameter list for template variable replacement
parameters = [
"create_session:action_result.parameter.device_id",
"query_device:action_result.data.*.hostname",
"list_session_files:action_result.data.*.resources.*.name",
"list_session_files:action_result.data.*.resources.*.sha256",
"get_session_file:action_result.summary.vault_id",
"artifact_create:custom_function_result.data.artifact_id",
"determine_get_status:custom_function:status",
"determine_get_status:custom_function:status_detail"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_file_collection_report")
host_observables(container=container)
return
@phantom.playbook_block()
def create_session(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_session() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Creates a Real Time Response (RTR) session to interact with the endpoint
################################################################################
query_device_result_data = phantom.collect2(container=container, datapath=["query_device:action_result.data.*.device_id","query_device:action_result.parameter.context.artifact_id"], action_results=results)
parameters = []
# build parameters list for 'create_session' call
for query_device_result_item in query_device_result_data:
if query_device_result_item[0] is not None:
parameters.append({
"device_id": query_device_result_item[0],
"context": {'artifact_id': query_device_result_item[1]},
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("create session", parameters=parameters, name="create_session", assets=["crowdstrike_oauth_api"], callback=run_admin_command_get)
return
@phantom.playbook_block()
def run_admin_command_get(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("run_admin_command_get() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Gets the specified file based on the playbook input path
################################################################################
playbook_input_path = phantom.collect2(container=container, datapath=["playbook_input:path"])
query_device_result_data = phantom.collect2(container=container, datapath=["query_device:action_result.data.*.device_id","query_device:action_result.parameter.context.artifact_id"], action_results=results)
create_session_result_data = phantom.collect2(container=container, datapath=["create_session:action_result.summary.session_id","create_session:action_result.parameter.context.artifact_id"], action_results=results)
parameters = []
# build parameters list for 'run_admin_command_get' call
for playbook_input_path_item in playbook_input_path:
for query_device_result_item in query_device_result_data:
for create_session_result_item in create_session_result_data:
if query_device_result_item[0] is not None and create_session_result_item[0] is not None:
parameters.append({
"data": playbook_input_path_item[0],
"command": "get",
"device_id": query_device_result_item[0],
"session_id": create_session_result_item[0],
"context": {'artifact_id': create_session_result_item[1]},
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("run admin command", parameters=parameters, name="run_admin_command_get", assets=["crowdstrike_oauth_api"], callback=determine_get_status)
return
@phantom.playbook_block()
def list_session_files(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("list_session_files() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Lists the previous file we ran a get command on, which returns its SHA256 hash
################################################################################
create_session_result_data = phantom.collect2(container=container, datapath=["create_session:action_result.summary.session_id","create_session:action_result.parameter.context.artifact_id"], action_results=results)
parameters = []
# build parameters list for 'list_session_files' call
for create_session_result_item in create_session_result_data:
if create_session_result_item[0] is not None:
parameters.append({
"session_id": create_session_result_item[0],
"context": {'artifact_id': create_session_result_item[1]},
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("list session files", parameters=parameters, name="list_session_files", assets=["crowdstrike_oauth_api"], callback=get_session_file)
return
@phantom.playbook_block()
def get_session_file(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("get_session_file() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Downloads the RTR session file from the endpoint to the SOAR File Vault
################################################################################
list_session_files_result_data = phantom.collect2(container=container, datapath=["list_session_files:action_result.data.*.resources.*.sha256","list_session_files:action_result.parameter.context.artifact_id"], action_results=results)
playbook_input_path = phantom.collect2(container=container, datapath=["playbook_input:path"])
create_session_result_data = phantom.collect2(container=container, datapath=["create_session:action_result.summary.session_id","create_session:action_result.parameter.context.artifact_id"], action_results=results)
parameters = []
# build parameters list for 'get_session_file' call
for list_session_files_result_item in list_session_files_result_data:
for playbook_input_path_item in playbook_input_path:
for create_session_result_item in create_session_result_data:
if list_session_files_result_item[0] is not None and create_session_result_item[0] is not None:
parameters.append({
"file_hash": list_session_files_result_item[0],
"file_name": playbook_input_path_item[0],
"session_id": create_session_result_item[0],
"context": {'artifact_id': create_session_result_item[1]},
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("get session file", parameters=parameters, name="get_session_file", assets=["crowdstrike_oauth_api"], callback=create_file_artifact)
return
@phantom.playbook_block()
def delete_session(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_session() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Closes a Real Time Response (RTR) session with the endpoint
################################################################################
create_session_result_data = phantom.collect2(container=container, datapath=["create_session:action_result.summary.session_id","create_session:action_result.parameter.context.artifact_id"], action_results=results)
parameters = []
# build parameters list for 'delete_session' call
for create_session_result_item in create_session_result_data:
if create_session_result_item[0] is not None:
parameters.append({
"session_id": create_session_result_item[0],
"context": {'artifact_id': create_session_result_item[1]},
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("delete session", parameters=parameters, name="delete_session", assets=["crowdstrike_oauth_api"], callback=format_file_collection_report)
return
@phantom.playbook_block()
def create_file_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("create_file_artifact() called")
################################################################################
# Create the JSON structure from the collected file metadata to create a SOAR
# artifact.
################################################################################
query_device_result_data = phantom.collect2(container=container, datapath=["query_device:action_result.data.*.device_id","query_device:action_result.data.*.hostname"], action_results=results)
list_session_files_result_data = phantom.collect2(container=container, datapath=["list_session_files:action_result.data.*.resources.*.name","list_session_files:action_result.data.*.resources.*.sha256"], action_results=results)
get_session_file_result_data = phantom.collect2(container=container, datapath=["get_session_file:action_result.data.*.vault_id"], action_results=results)
run_admin_command_get_result_data = phantom.collect2(container=container, datapath=["run_admin_command_get:action_result.parameter.data"], action_results=results)
query_device_result_item_0 = [item[0] for item in query_device_result_data]
query_device_result_item_1 = [item[1] for item in query_device_result_data]
list_session_files_result_item_0 = [item[0] for item in list_session_files_result_data]
list_session_files_result_item_1 = [item[1] for item in list_session_files_result_data]
get_session_file_result_item_0 = [item[0] for item in get_session_file_result_data]
run_admin_command_get_parameter_data = [item[0] for item in run_admin_command_get_result_data]
create_file_artifact__data = None
################################################################################
## Custom Code Start
################################################################################
import os
create_file_artifact__data = []
for device_id, hostname, origFilepath, filehash, vault_id, fullpath in zip(query_device_result_item_0, query_device_result_item_1, list_session_files_result_item_0, list_session_files_result_item_1, get_session_file_result_item_0, run_admin_command_get_parameter_data):
filename = os.path.basename(fullpath.replace('\\','/'))
filepath = os.path.dirname(fullpath.replace('\\','/'))
create_file_artifact__data = {
"cef": {
"deviceExternalId": device_id,
"hostname": hostname,
"origFilePath": origFilepath,
"fileName": filename,
"filePath": os.path.join(filepath.replace('/','\\'), ''),
"fileHashSha256": filehash,
"vaultId": vault_id
}
}
#phantom.debug(f"File artifact data: {create_file_artifact__data}")
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="create_file_artifact:data", value=json.dumps(create_file_artifact__data))
artifact_create(container=container)
return
@phantom.playbook_block()
def artifact_create(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_create() called")
################################################################################
# Creates a new artifact to store information about the file collected from Crowdstrike.
################################################################################
id_value = container.get("id", None)
create_file_artifact__data = json.loads(_ if (_ := phantom.get_run_data(key="create_file_artifact:data")) != "" else "null") # pylint: disable=used-before-assignment
parameters = []
parameters.append({
"name": "Collected File Artifact",
"tags": None,
"label": "collected_file",
"severity": None,
"cef_field": None,
"cef_value": None,
"container": id_value,
"input_json": create_file_artifact__data,
"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="artifact_create", callback=delete_session)
return
@phantom.playbook_block()
def determine_get_status(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("determine_get_status() called")
################################################################################
# Check the results of the RTR get command to ensure it was indeed successful.
# RTR commands are notably unreliable in how they report status, instead proving
# stdout and stderr information.
################################################################################
run_admin_command_get_result_data = phantom.collect2(container=container, datapath=["run_admin_command_get:action_result.status","run_admin_command_get:action_result.data.*.resources.*.stdout","run_admin_command_get:action_result.data.*.resources.*.stderr","run_admin_command_get:action_result.parameter.data","run_admin_command_get:action_result.message"], action_results=results)
run_admin_command_get_result_item_0 = [item[0] for item in run_admin_command_get_result_data]
run_admin_command_get_result_item_1 = [item[1] for item in run_admin_command_get_result_data]
run_admin_command_get_result_item_2 = [item[2] for item in run_admin_command_get_result_data]
run_admin_command_get_parameter_data = [item[3] for item in run_admin_command_get_result_data]
run_admin_command_get_result_message = [item[4] for item in run_admin_command_get_result_data]
determine_get_status__file_name = None
determine_get_status__status = None
determine_get_status__status_detail = None
################################################################################
## Custom Code Start
################################################################################
determine_get_status__file_name = []
determine_get_status__status = []
determine_get_status__status_detail = []
for file_name, status, stdout, stderr, message in zip(run_admin_command_get_parameter_data, run_admin_command_get_result_item_0, run_admin_command_get_result_item_1, run_admin_command_get_result_item_2,run_admin_command_get_result_message):
#phantom.debug(f"file_name: {file_name}, status: {status}, stdout: {stdout}, stderr: {stderr}")
determine_get_status__file_name.append(file_name)
determine_get_status__status.append(status)
if status == "success" and stdout.strip() and not stderr.strip():
determine_get_status__status_detail.append(f"File collected: {stdout.strip()}")
else:
determine_get_status__status_detail.append(stderr.strip() if stderr else message)
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="determine_get_status:file_name", value=json.dumps(determine_get_status__file_name))
phantom.save_run_data(key="determine_get_status:status", value=json.dumps(determine_get_status__status))
phantom.save_run_data(key="determine_get_status:status_detail", value=json.dumps(determine_get_status__status_detail))
list_session_files(container=container)
return
@phantom.playbook_block()
def on_finish(container, summary):
phantom.debug("on_finish() called")
format_file_collection_report = phantom.get_format_data(name="format_file_collection_report")
host_observables__observable_array = json.loads(_ if (_ := phantom.get_run_data(key="host_observables:observable_array")) != "" else "null") # pylint: disable=used-before-assignment
output = {
"observable": host_observables__observable_array,
"markdown_report": format_file_collection_report,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.save_playbook_output_data(output=output)
return