forked from phantomcyber/playbooks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalert_deescalation_for_test_machines.py
204 lines (149 loc) · 8.13 KB
/
alert_deescalation_for_test_machines.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
"""
This playbook de-escalates the severity level of a particular event based upon whether it's cef.sourceAddress is found in the custom list "test_machine_ips." If the source address is not found within "test machine ips", and is not found in the custom list "non_test_machine_ips", a role is prompted as to whether the source address is a test machine. If it is, it is added to the "test_machine_ips" custom list, and the severity of the event is set to LOW with a sensitivity of TLP:WHITE. Otherwise, the source address is added to "non_test_machine_ips", and the playbook completes.
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
def on_start(container):
phantom.debug('on_start() called')
# call 'decision_1' block
decision_1(container=container)
return
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,
conditions=[
["artifact:*.cef.sourceAddress", "!=", ""],
])
# call connected blocks if condition 1 matched
if matched_artifacts_1 or matched_results_1:
decision_3(action=action, success=success, container=container, results=results, handle=handle)
return
# call connected blocks for 'else' condition 2
return
def decision_3(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('decision_3() called')
# check for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["artifact:*.cef.sourceAddress", "in", "custom_list:test_machine_ips"],
])
# call connected blocks if condition 1 matched
if matched_artifacts_1 or matched_results_1:
join_deescalate_alert(action=action, success=success, container=container, results=results, handle=handle)
return
# call connected blocks for 'else' condition 2
decision_5(action=action, success=success, container=container, results=results, handle=handle)
return
def decision_5(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('decision_5() called')
# check for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["artifact:*.cef.sourceAddress", "not in", "custom_list:non_test_machine_ips"],
])
# call connected blocks if condition 1 matched
if matched_artifacts_1 or matched_results_1:
prompt_1(action=action, success=success, container=container, results=results, handle=handle)
return
# call connected blocks for 'else' condition 2
return
def Add_to_test_machine_list(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('Add_to_test_machine_list() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'Add_to_test_machine_list' call
container_data = phantom.collect2(container=container, datapath=['artifact:*.cef.sourceAddress', 'artifact:*.id'])
parameters = []
# build parameters list for 'Add_to_test_machine_list' call
for container_item in container_data:
if container_item[0]:
parameters.append({
'list': "custom_list:test_machine_ips",
'create': True,
'new_row': container_item[0],
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': container_item[1]},
})
phantom.act("add listitem", parameters=parameters, assets=['helper'], callback=join_deescalate_alert, name="Add_to_test_machine_list")
return
def Add_to_non_test_machine_list(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('Add_to_non_test_machine_list() called')
#phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
# collect data for 'Add_to_non_test_machine_list' call
container_data = phantom.collect2(container=container, datapath=['artifact:*.cef.sourceAddress', 'artifact:*.id'])
parameters = []
# build parameters list for 'Add_to_non_test_machine_list' call
for container_item in container_data:
if container_item[0]:
parameters.append({
'list': "custom_list:non_test_machine_ips",
'create': True,
'new_row': container_item[0],
# context (artifact id) is added to associate results with the artifact
'context': {'artifact_id': container_item[1]},
})
phantom.act("add listitem", parameters=parameters, assets=['helper'], name="Add_to_non_test_machine_list")
return
def decision_4(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('decision_4() called')
# check for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
action_results=results,
conditions=[
["prompt_1:action_result.summary.response", "!=", "Yes"],
])
# call connected blocks if condition 1 matched
if matched_artifacts_1 or matched_results_1:
Add_to_test_machine_list(action=action, success=success, container=container, results=results, handle=handle)
return
# call connected blocks for 'else' condition 2
Add_to_non_test_machine_list(action=action, success=success, container=container, results=results, handle=handle)
return
def deescalate_alert(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('deescalate_alert() called')
phantom.set_sensitivity(container, "white")
phantom.set_severity(container, "low")
return
def join_deescalate_alert(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('join_deescalate_alert() called')
# check if all connected incoming actions are done i.e. have succeeded or failed
if phantom.actions_done([ 'Add_to_test_machine_list' ]):
# call connected block "deescalate_alert"
deescalate_alert(container=container, handle=handle)
return
def prompt_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None):
phantom.debug('prompt_1() called')
# set user and message variables for phantom.prompt call
user = "Automation Engineer"
message = """sourceAddress \"{0}\" has been compromised - is this a test machine?"""
# parameter list for template variable replacement
parameters = [
"artifact:*.cef.sourceAddress",
]
# response options
options = {
"type": "list",
"choices": [
"Yes",
"No",
]
}
phantom.prompt(container=container, user=user, message=message, respond_in_mins=30, name="prompt_1", parameters=parameters, options=options, callback=decision_4)
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