-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathMicrosoft_Defender_For_Endpoint_Network_Isolation.py
220 lines (156 loc) · 9.94 KB
/
Microsoft_Defender_For_Endpoint_Network_Isolation.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
"""
Accepts a uid (device_id or hostname) as input and quarantines the device in MS Defender for Endpoint. We then generate an observable report. The report can be customized based on user preference.\n\nRef:\nhttps://d3fend.mitre.org/technique/d3f:NetworkIsolation/
"""
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 quarantine_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("quarantine_device() called")
# phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))
################################################################################
# Quarantines device in MS Defender for Endpoint given a uid (device_id or hostname).
################################################################################
filtered_input_0_uid = phantom.collect2(container=container, datapath=["filtered-data:input_filter:condition_1:playbook_input:uid"])
parameters = []
# build parameters list for 'quarantine_device' call
for filtered_input_0_uid_item in filtered_input_0_uid:
if filtered_input_0_uid_item[0] is not None:
parameters.append({
"type": "Full",
"comment": "Device isolated via Splunk SOAR.",
"timeout": 30,
"device_id": filtered_input_0_uid_item[0],
})
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.act("quarantine device", parameters=parameters, name="quarantine_device", assets=["defender-atp"], callback=quarantine_device_filter)
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,
conditions=[
["playbook_input:uid", "!=", ""]
],
name="input_filter:condition_1",
delimiter=None)
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
quarantine_device(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 quarantine_device_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("quarantine_device_filter() called")
################################################################################
# Filters successful quarantine actions
################################################################################
# collect filtered artifact ids and results for 'if' condition 1
matched_artifacts_1, matched_results_1 = phantom.condition(
container=container,
conditions=[
["quarantine_device:action_result.status", "==", "success"]
],
name="quarantine_device_filter:condition_1",
delimiter=None)
# call connected blocks if filtered artifacts or results
if matched_artifacts_1 or matched_results_1:
format_report_device(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
################################################################################
filtered_result_0_data_quarantine_device_filter = phantom.collect2(container=container, datapath=["filtered-data:quarantine_device_filter:condition_1:quarantine_device:action_result.parameter.device_id","filtered-data:quarantine_device_filter:condition_1:quarantine_device:action_result.data.*.computerDnsName"])
quarantine_device_result_data = phantom.collect2(container=container, datapath=["quarantine_device:action_result.data.*.type"], action_results=results)
filtered_result_0_parameter_device_id = [item[0] for item in filtered_result_0_data_quarantine_device_filter]
filtered_result_0_data___computerdnsname = [item[1] for item in filtered_result_0_data_quarantine_device_filter]
quarantine_device_result_item_0 = [item[0] for item in quarantine_device_result_data]
host_observables__observable_array = None
################################################################################
## Custom Code Start
################################################################################
code_1__observable_array = []
for device_id, dns, isolationtype in zip(filtered_result_0_parameter_device_id, filtered_result_0_data___computerdnsname, quarantine_device_result_item_0):
# Initialize the observable dictionary
observable = {
"source": "Microsoft Defender for Endpoint",
"type": "defender atp device id",
"value": device_id,
"attributes": {
"computer_dns_name": dns,
"isolation_type": isolationtype
},
"status": "isolated",
"message": "Device isolated successfully"
}
# Add the observable to the array
code_1__observable_array.append(observable)
# Debug output for verification
phantom.debug(code_1__observable_array)
# Write your custom code here...
################################################################################
## 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_report_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("format_report_device() called")
################################################################################
# Format a summary table with the information gathered from the playbook.
################################################################################
template = """Devices were isolated via Splunk SOAR. The table below summarizes the information gathered.\n\n| Device ID | DNS Name | Isolation Type | Quarantine Status |\n| --- | --- | --- | --- |\n%%\n| {0} | {1} | {2} | {3} |\n%%"""
# parameter list for template variable replacement
parameters = [
"filtered-data:quarantine_device_filter:condition_1:quarantine_device:action_result.data.*.machineId",
"filtered-data:quarantine_device_filter:condition_1:quarantine_device:action_result.data.*.computerDnsName",
"filtered-data:quarantine_device_filter:condition_1:quarantine_device:action_result.data.*.type",
"filtered-data:quarantine_device_filter:condition_1:quarantine_device:action_result.status"
]
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.format(container=container, template=template, parameters=parameters, name="format_report_device")
host_observables(container=container)
return
@phantom.playbook_block()
def on_finish(container, summary):
phantom.debug("on_finish() called")
format_report_device = phantom.get_format_data(name="format_report_device")
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_report_device,
}
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
phantom.save_playbook_output_data(output=output)
return