-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwmi_connector.py
338 lines (244 loc) · 11.5 KB
/
wmi_connector.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
# File: wmi_connector.py
#
# Copyright (c) 2016-2024 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions
# and limitations under the License.
#
#
import re
import phantom.app as phantom
from phantom.action_result import ActionResult
from phantom.base_connector import BaseConnector
import wmi_client_wrapper as wmi
from wmi_consts import *
class WmiConnector(BaseConnector):
# Actions supported by this script
ACTION_ID_GET_PROCESSES = "get_processes"
ACTION_ID_GET_SERVICES = "get_services"
ACTION_ID_GET_RUNNING_SERVICES = "get_running_services"
ACTION_ID_GET_USERS = "get_users"
ACTION_ID_RUN_QUERY = "run_query"
ACTION_ID_GET_SYSINFO = "get_sysinfo"
def __init__(self):
# Call the BaseConnectors init first
super(WmiConnector, self).__init__()
def _get_error_msg_from_exception(self, e):
"""
This method is used to get appropriate error messages from the exception.
:param e: Exception object
:return: Error message
"""
error_code = WMI_ERROR_CODE_MSG
error_msg = WMI_ERROR_MSG_UNAVAILABLE
try:
if e.args:
if len(e.args) > 1:
error_code = e.args[0]
error_msg = e.args[1]
elif len(e.args) == 1:
error_code = WMI_ERROR_CODE_MSG
error_msg = e.args[0]
except:
pass
try:
if error_code in WMI_ERROR_CODE_MSG:
error_text = "Error Message: {0}".format(error_msg)
else:
error_text = "Error Code: {0}. Error Message: {1}".format(error_code, error_msg)
except:
self.debug_print("Error occurred while parsing error message")
error_text = WMI_PARSE_ERROR_MSG
return error_text
def _modify_exception_message(self, e):
mod_msg = re.sub('%.* ', '%<password> ', self._get_error_msg_from_exception(e))
self.debug_print("Modified Exception Message:", mod_msg)
return mod_msg
def _run_query(self, query, wmic, action_result):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
try:
ret_data = wmic.query(query)
except Exception as e:
action_result.set_status(phantom.APP_ERROR, WMI_ERROR_QUERY_EXECUTION_FAILED)
action_result.append_to_message(self._modify_exception_message(e))
return None
# print "In _run_query::Query Returned"
self.debug_print("query_results", ret_data)
if not ret_data:
action_result.set_status(phantom.APP_ERROR, "Data retrieved was empty")
return None
if not isinstance(ret_data, list):
action_result.set_status(phantom.APP_ERROR, "Invalid Data received")
return None
action_result.set_status(phantom.APP_SUCCESS)
# We still need to validate that the data we sent out is sane
return ret_data
def _get_sysinfo(self, wmic, action_result):
cumulative_data = {}
query = "select * from Win32_ComputerSystem"
ret_data = self._run_query(query, wmic, action_result)
if phantom.is_fail(action_result.get_status()):
return action_result.get_status()
cumulative_data[WMI_JSON_SYSTEM_DETAILS] = ret_data[0]
query = "select * from Win32_OperatingSystem"
ret_data = self._run_query(query, wmic, action_result)
if phantom.is_fail(action_result.get_status()):
return action_result.get_status()
cumulative_data[WMI_JSON_OS_DETAILS] = ret_data[0]
query = "select * from Win32_BootConfiguration"
ret_data = self._run_query(query, wmic, action_result)
if phantom.is_fail(action_result.get_status()):
return action_result.get_status()
cumulative_data[WMI_JSON_BOOT_CONFIG_DETAILS] = ret_data[0]
action_result.add_data(cumulative_data)
# Create summary
data = action_result.get_data()
data = data[0]
summary = {}
try:
summary[WMI_JSON_DNSHOSTNAME] = data[WMI_JSON_SYSTEM_DETAILS].get('DNSHostName', '')
except:
pass
try:
summary[WMI_JSON_PHYSICAL_MEM] = data[WMI_JSON_SYSTEM_DETAILS]['TotalPhysicalMemory']
except:
pass
try:
summary[WMI_JSON_WORKGROUP] = data[WMI_JSON_SYSTEM_DETAILS]['Workgroup']
except:
pass
try:
summary[WMI_JSON_DOMAIN] = data[WMI_JSON_SYSTEM_DETAILS]['Domain']
except:
pass
try:
summary[WMI_JSON_VERSION] = '{0} [{1}] {2} {3}'.format(
data[WMI_JSON_OS_DETAILS]['Caption'],
data[WMI_JSON_OS_DETAILS]['Version'],
data[WMI_JSON_OS_DETAILS].get('OSArchitecture', 'Unknown'),
data[WMI_JSON_OS_DETAILS]['CSDVersion'])
except:
pass
action_result.update_summary(summary)
return action_result.set_status(phantom.APP_SUCCESS)
def _get_processes(self, wmic, action_result):
query = "select * from Win32_Process"
ret_data = self._run_query(query, wmic, action_result)
if phantom.is_success(action_result.get_status()) and len(ret_data):
action_result.update_summary({WMI_JSON_TOTAL_PROCESSES: len(ret_data)})
for curr_process in ret_data:
action_result.add_data(curr_process)
return action_result.get_status()
def _get_services(self, wmic, action, action_result):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
query = "select * from Win32_Service"
if action == self.ACTION_ID_GET_RUNNING_SERVICES:
query = "select * from Win32_Service where State = 'Running'"
ret_data = self._run_query(query, wmic, action_result)
self.debug_print("query_results", ret_data)
if phantom.is_success(action_result.get_status()) and len(ret_data):
action_result.update_summary({WMI_JSON_TOTAL_SERVICES: len(ret_data)})
total_running = 0
for curr_service in ret_data:
action_result.add_data(curr_service)
if curr_service.get('State', 'Unknown') == 'Running':
total_running += 1
action_result.update_summary({WMI_JSON_RUNNING_SERVICES: total_running})
else:
action_result.update_summary({WMI_JSON_TOTAL_SERVICES: 0})
return action_result.get_status()
def _get_users(self, wmic, action_result):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
query = "select * from Win32_Account where SIDType = 1"
ret_data = self._run_query(query, wmic, action_result)
self.debug_print("query_results", ret_data)
if phantom.is_success(action_result.get_status()) and len(ret_data):
action_result.update_summary({WMI_JSON_TOTAL_USERS: len(ret_data)})
total_disabled = 0
for curr_user in ret_data:
# print "Service: \n %s" % curr_user
action_result.add_data(curr_user)
if curr_user.get('Disabled'):
total_disabled += 1
action_result.update_summary({WMI_JSON_DISABLED_USERS: total_disabled})
return action_result.get_status()
def _test_connectivity(self, wmic, action_result):
self.save_progress("Connecting to server")
query = "select * from Win32_ComputerSystem"
self.save_progress("Fetching System Details")
_ = self._run_query(query, wmic, action_result)
if phantom.is_fail(action_result.get_status()):
self.save_progress("Test Connectivity Failed")
return action_result.get_status()
self.save_progress("Test Connectivity Passed")
return action_result.set_status(phantom.APP_SUCCESS)
def handle_action(self, param):
"""
Function that handles all the actions.
:param param: A dictionary which contains information about the actions to be executed
:return: Status success/failure
"""
# First get the config
config = self.get_config()
user = config[phantom.APP_JSON_USERNAME]
passw = config[phantom.APP_JSON_PASSWORD]
# Get the action
action = self.get_action_identifier()
curr_machine = config[phantom.APP_JSON_SERVER]
if action != phantom.ACTION_ID_TEST_ASSET_CONNECTIVITY:
curr_machine = param[phantom.APP_JSON_IP_HOSTNAME]
# default to same as default in WmiClientWrapper::__init__()
namespace = param.get('namespace', '//./root/cimv2')
action_result = self.add_action_result(ActionResult(dict(param)))
action_result.update_param({phantom.APP_JSON_IP_HOSTNAME: curr_machine})
wmic = None
self.save_progress(phantom.APP_PROG_CONNECTING_TO_ELLIPSES, curr_machine)
force_ntlm_v2 = config.get('force_ntlmv2', False)
try:
wmic = wmi.WmiClientWrapper(username=user, password=passw, host=curr_machine, namespace=namespace, force_ntlm_v2=force_ntlm_v2)
except Exception as e:
self.save_progress(WMI_MSG_CONNECTION_FAILED, machine=curr_machine)
action_result.set_status(phantom.APP_ERROR, WMI_MSG_CONNECTION_FAILED, machine=curr_machine)
action_result.append_to_message(self._modify_exception_message(e))
return action_result.get_status()
if action == self.ACTION_ID_GET_PROCESSES:
return self._get_processes(wmic, action_result)
elif action == self.ACTION_ID_GET_SERVICES or action == self.ACTION_ID_GET_RUNNING_SERVICES:
return self._get_services(wmic, action, action_result)
elif action == self.ACTION_ID_GET_USERS:
return self._get_users(wmic, action_result)
elif action == self.ACTION_ID_GET_SYSINFO:
return self._get_sysinfo(wmic, action_result)
elif action == self.ACTION_ID_RUN_QUERY:
query = param[WMI_JSON_QUERY]
action_result.update_param({WMI_JSON_QUERY: query})
query_results = self._run_query(query, wmic, action_result)
if phantom.is_success(action_result.get_status()):
action_result.add_data(query_results)
return action_result.set_status(phantom.APP_SUCCESS, WMI_SUCC_QUERY_EXECUTED)
return action_result.get_status()
elif action == phantom.ACTION_ID_TEST_ASSET_CONNECTIVITY:
return self._test_connectivity(wmic, action_result)
return phantom.APP_SUCCESS
if __name__ == '__main__':
import json
import sys
import pudb
pudb.set_trace()
with open(sys.argv[1]) as f:
in_json = f.read()
in_json = json.loads(in_json)
print(json.dumps(in_json, indent=4))
connector = WmiConnector()
connector.print_progress_message = True
result = connector._handle_action(json.dumps(in_json), None)
print(result)
sys.exit(0)