forked from splunk/security_content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrisk_notable_block_indicators.py
314 lines (220 loc) · 13.3 KB
/
risk_notable_block_indicators.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
"""
This playbook handles locating indicators marked for blocking and determining if any blocking playbooks exist. If there is a match to the appropriate tags in the playbook, a filter block routes the name of the playbook to launch to a code block.
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
################################################################################
## Global Custom Code Start
################################################################################
################################################################################
## Global Custom Code End
################################################################################
def on_start(container):
phantom.debug('on_start() called')
# call 'get_marked_indicators' block
get_marked_indicators(container=container)
return
def get_marked_indicators(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_marked_indicators() called")
id_value = container.get("id", None)
parameters = []
parameters.append({
"tags_or": "marked_for_block",
"tags_and": None,
"container": id_value,
"tags_exclude": "blocked, safe",
"indicator_timerange": None,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/indicator_get_by_tag", parameters=parameters, name="get_marked_indicators", callback=list_block_playbooks)
return
def list_block_playbooks(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("list_block_playbooks() called")
parameters = []
parameters.append({
"name": None,
"repo": "local",
"tags": "block, risk_notable",
"category": None,
"playbook_type": "input",
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/playbooks_list", parameters=parameters, name="list_block_playbooks", callback=indicator_and_playbook_decision)
return
def decide_and_launch_playbooks(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("decide_and_launch_playbooks() called")
################################################################################
# This block matches indicators with the available playbooks designed to act upon
# them. It then dedupes the playbook list, so each child playbook is only launched
# once. After that, it is up to the child playbook to fetch the indicators it
# knows how to block.
################################################################################
get_marked_indicators_data = phantom.collect2(container=container, datapath=["get_marked_indicators:custom_function_result.data.*.indicator_value","get_marked_indicators:custom_function_result.data.*.indicator_cef_type"])
list_block_playbooks_data = phantom.collect2(container=container, datapath=["list_block_playbooks:custom_function_result.data.*.full_name","list_block_playbooks:custom_function_result.data.*.input_spec"])
get_marked_indicators_data___indicator_value = [item[0] for item in get_marked_indicators_data]
get_marked_indicators_data___indicator_cef_type = [item[1] for item in get_marked_indicators_data]
list_block_playbooks_data___full_name = [item[0] for item in list_block_playbooks_data]
list_block_playbooks_data___input_spec = [item[1] for item in list_block_playbooks_data]
decide_and_launch_playbooks__names = None
################################################################################
## Custom Code Start
################################################################################
indicator_value_list = get_marked_indicators_data___indicator_value
indicator_cef_type_list = get_marked_indicators_data___indicator_cef_type
playbook_name = list_block_playbooks_data___full_name
playbook_spec = list_block_playbooks_data___input_spec
decide_and_launch_playbooks__names = []
playbook_launch_list = {}
for pb_name, spec_item in zip(playbook_name, playbook_spec):
pb_inputs = {}
for cef_value, cef_type in zip(indicator_value_list, indicator_cef_type_list):
for type_item in cef_type:
# check if any of the investigate type playbooks have inputs that accept this data type
for spec in spec_item:
for contains_type in spec['contains']:
if type_item and type_item in contains_type:
phantom.debug(f"Match found for '{cef_value}' of type '{type_item}' for playbook '{pb_name}' at input '{spec['name']}'")
if not pb_inputs:
pb_inputs[spec['name']] = [cef_value]
else:
if cef_value not in pb_inputs[spec['name']]:
pb_inputs[spec['name']].append(cef_value)
if pb_inputs:
playbook_launch_list[pb_name] = pb_inputs
if playbook_launch_list:
for k,v in playbook_launch_list.items():
name = 'playbook_{}'.format(pb_name.split('/')[1].replace(' ','_').lower())
decide_and_launch_playbooks__names.append(name)
phantom.playbook(playbook=k, container=container, inputs=v, name=name, callback=playbook_wait)
else:
raise RuntimeError("Unable to find match between indicator types and playbook input types")
################################################################################
## Custom Code End
################################################################################
phantom.save_run_data(key="decide_and_launch_playbooks:names", value=json.dumps(decide_and_launch_playbooks__names))
playbook_wait(container=container)
return
def indicator_and_playbook_decision(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("indicator_and_playbook_decision() called")
# check for 'if' condition 1
found_match_1 = phantom.decision(
container=container,
logical_operator="and",
conditions=[
["list_block_playbooks:custom_function_result.data.*.full_name", "!=", ""],
["get_marked_indicators:custom_function_result.data.*.indicator_value", "!=", ""]
])
# call connected blocks if condition 1 matched
if found_match_1:
decide_and_launch_playbooks(action=action, success=success, container=container, results=results, handle=handle)
return
return
def get_indicators_status(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("get_indicators_status() called")
id_value = container.get("id", None)
parameters = []
parameters.append({
"tags_or": "marked_for_block, blocked",
"tags_and": None,
"container": id_value,
"tags_exclude": None,
"indicator_timerange": None,
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.custom_function(custom_function="community/indicator_get_by_tag", parameters=parameters, name="get_indicators_status", callback=decision_3)
return
def format_note(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("format_note() called")
template = """The following indicators were marked for block. Their current status is shown in the table below. Any indicators that are still listed as \"marked_for_block,\" may require manual remediation.\n\n| Value | Type | Current Tags |\n| --- | --- | --- |\n%%\n| `{0}` | `{1}` | `{2}` |\n%%\n"""
# parameter list for template variable replacement
parameters = [
"get_indicators_status:custom_function_result.data.*.indicator_value",
"get_indicators_status:custom_function_result.data.*.indicator_cef_type",
"get_indicators_status:custom_function_result.data.*.indicator_tags"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_note")
return
def playbook_wait(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("playbook_wait() called")
decide_and_launch_playbooks__names = json.loads(phantom.get_run_data(key="decide_and_launch_playbooks:names"))
################################################################################
## Custom Code Start
################################################################################
if phantom.completed(playbook_names=decide_and_launch_playbooks__names):
# call connected block "indicators_not_blocked"
get_indicators_status(container=container)
# return early to avoid calling connected block too soon
return
################################################################################
## Custom Code End
################################################################################
get_indicators_status(container=container)
return
def decision_3(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("decision_3() called")
# check for 'if' condition 1
found_match_1 = phantom.decision(
container=container,
conditions=[
["get_indicators_status:custom_function_result.data.*.indicator_value", "!=", ""]
])
# call connected blocks if condition 1 matched
if found_match_1:
format_note(action=action, success=success, container=container, results=results, handle=handle)
return
return
def on_finish(container, summary):
phantom.debug("on_finish() called")
format_note = phantom.get_format_data(name="format_note")
output = {
"note_title": "[Auto-Generated] Block Indicator Summary",
"note_content": format_note,
}
################################################################################
## Custom Code Start
################################################################################
decide_and_launch_playbooks__names = phantom.get_run_data(key="decide_and_launch_playbooks:names")
if not decide_and_launch_playbooks__names:
raise RuntimeError("Unable to launch block playbooks due to missing indicators or missing playbooks")
elif not json.loads(decide_and_launch_playbooks__names):
raise RuntimeError("Unable to launch block playbooks due to no matching indicators for playbook inputs")
# This function is called after all actions are completed.
# summary of all the action and/or all details 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)
################################################################################
## Custom Code End
################################################################################
phantom.save_playbook_output_data(output=output)
return