Debian Codegraph @

src/OVAL/probes/windows/wmi57_probe.c

raw symbols SCIP
1 /*
2 * Copyright 2018 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Jan Černý <[email protected]m>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <wbemidl.h>
29 #include <WMIUtils.h>
30 #include <windows.h>
31 #include "debug_priv.h"
32 #include "_seap.h"
33 #include "probe-api.h"
34 #include "probe/entcmp.h"
35 #include "probe/probe.h"
36 #include "list.h"
37 #include "util.h"
38 #include "oval_definitions.h"
39 #include "oval_sexp.h"
40 #include "wmi57_probe.h"
41 #include "oscap_helpers.h"
42
43 static struct oscap_list *get_wql_fields(WCHAR *wql)
44 {
45 HRESULT hr = 0;
46 IWbemQuery *query = NULL;
47
48 hr = CoCreateInstance(&CLSID_WbemQuery, 0, CLSCTX_INPROC_SERVER, &IID_IWbemQuery, (LPVOID *)&query);
49 if (FAILED(hr)) {
50 dE("Failed to create IWbemQuery object.");
51 return NULL;
52 }
53
54 hr = query->lpVtbl->Parse(query, L"WQL", wql, 0);
55 if (FAILED(hr)) {
56 dE("Failed to parse IWbemQuery object.");
57 return NULL;
58 }
59
60 /* Perform an analysis of the WQL statement */
61 SWbemRpnEncodedQuery *wql_analysis = NULL;
62 hr = query->lpVtbl->GetAnalysis(query, WMIQ_ANALYSIS_RPN_SEQUENCE, 0, (LPVOID *)&wql_analysis);
63 if (FAILED(hr)) {
64 dE("Failed to analyse IWbemQuery object.");
65 return NULL;
66 }
67
68 ULONG fields_count = wql_analysis->m_uSelectListSize;
69 struct oscap_list *fields_list = oscap_list_new();
70
71 for (ULONG i = 0; i < fields_count; i++) {
72 for (ULONG j = 0; j < wql_analysis->m_ppSelectList[i]->m_uNameListSize; j++) {
73 WCHAR *field = wql_analysis->m_ppSelectList[i]->m_ppszNameList[j];
74 oscap_list_add(fields_list, wcsdup(field));
75 if (wcscmp(field, L"*") == 0) {
76 dE("The WQL query '%S' contains * in SELECT statement, which is not allowed by OVAL Definitions Schema.", wql);
77 /* free the memory that the parser returns in a previous call to GetAnalysis */
78 query->lpVtbl->FreeMemory(query, wql_analysis);
79 /* free the memory that the parser is holding */
80 query->lpVtbl->Empty(query);
81 oscap_list_free(fields_list, free);
82 return NULL;
83 }
84 }
85 }
86
87 /* free the memory that the parser returns in a previous call to GetAnalysis */
88 query->lpVtbl->FreeMemory(query, wql_analysis);
89 /* free the memory that the parser is holding */
90 query->lpVtbl->Empty(query);
91
92 return fields_list;
93 }
94
95 static int collect_value(probe_ctx *ctx, WCHAR *wmi_namespace, WCHAR *wql_query, WCHAR *name, VARIANT *variant, CIMTYPE type)
96 {
97 SEXP_t *value = NULL;
98 WCHAR *wstrval;
99 char *strval;
100 SYSTEMTIME systime;
101 oval_datatype_t oval_data_type = OVAL_DATATYPE_UNKNOWN;
102 switch (type) {
103 case CIM_EMPTY:
104 value = SEXP_string_new("", 0);
105 oval_data_type = OVAL_DATATYPE_STRING;
106 break;
107 case CIM_SINT8:
108 value = SEXP_number_newi_8(V_I1(variant));
109 oval_data_type = OVAL_DATATYPE_INTEGER;
110 break;
111 case CIM_UINT8:
112 value = SEXP_number_newu_8(V_UI1(variant));
113 oval_data_type = OVAL_DATATYPE_INTEGER;
114 break;
115 case CIM_SINT16:
116 value = SEXP_number_newi_16(V_I2(variant));
117 oval_data_type = OVAL_DATATYPE_INTEGER;
118 break;
119 case CIM_UINT16:
120 value = SEXP_number_newu_16(V_UI2(variant));
121 oval_data_type = OVAL_DATATYPE_INTEGER;
122 break;
123 case CIM_SINT32:
124 value = SEXP_number_newi_32(V_I4(variant));
125 oval_data_type = OVAL_DATATYPE_INTEGER;
126 break;
127 case CIM_UINT32:
128 value = SEXP_number_newu_32(V_UI4(variant));
129 oval_data_type = OVAL_DATATYPE_INTEGER;
130 break;
131 case CIM_SINT64:
132 value = SEXP_number_newi_64(V_I8(variant));
133 oval_data_type = OVAL_DATATYPE_INTEGER;
134 break;
135 case CIM_UINT64:
136 value = SEXP_number_newu_64(V_UI8(variant));
137 oval_data_type = OVAL_DATATYPE_INTEGER;
138 break;
139 case CIM_REAL32:
140 value = SEXP_number_newf(V_R4(variant));
141 oval_data_type = OVAL_DATATYPE_FLOAT;
142 break;
143 case CIM_REAL64:
144 value = SEXP_number_newf(V_R8(variant));
145 oval_data_type = OVAL_DATATYPE_FLOAT;
146 break;
147 case CIM_BOOLEAN:
148 value = SEXP_number_newb(V_BOOL(variant));
149 oval_data_type = OVAL_DATATYPE_BOOLEAN;
150 break;
151 case CIM_STRING:
152 wstrval = variant->bstrVal;
153 strval = oscap_windows_wstr_to_str(wstrval);
154 value = SEXP_string_new(strval, strlen(strval));
155 oval_data_type = OVAL_DATATYPE_STRING;
156 break;
157 case CIM_DATETIME:
158 VariantTimeToSystemTime(V_DATE(variant), &systime);
159 strval = oscap_sprintf("%02u/%02u/%04u %02d.%02d.%02d", systime.wDay, systime.wMonth, systime.wYear, systime.wHour, systime.wMinute, systime.wSecond);
160 value = SEXP_string_new(strval, strlen(strval));
161 oval_data_type = OVAL_DATATYPE_STRING;
162 break;
163 case CIM_REFERENCE:
164 case CIM_CHAR16:
165 /* TODO */
166 case CIM_OBJECT:
167 case CIM_FLAG_ARRAY:
168 case CIM_ILLEGAL:
169 default:
170 break;
171 }
172
173 /* prepare the result element - record entity */
174 SEXP_t *result_sexp = probe_ent_creat1("result", NULL, NULL);
175 probe_ent_setdatatype(result_sexp, OVAL_DATATYPE_RECORD);
176
177 /* prepare the field element */
178 SEXP_t *field_ent = probe_ent_creat1("field", NULL, value);
179 probe_ent_setdatatype(field_ent, oval_data_type);
180
181 char *name_str = oscap_windows_wstr_to_str(name);
182 /*
183 * Convert the value of the 'name' attribute to lowercase letters, because
184 * it is not allowed to have uppercase letters according to OVAL specification.
185 * See EntityItemFieldType in
186 * https://oval.mitre.org/language/version5.11/ovalsc/documentation/oval-system-characteristics-schema.html#EntityItemFieldType
187 */
188 for (char *p = name_str; *p; p++) {
189 *p = tolower(*p);
190 }
191 SEXP_t *name_sexp = SEXP_string_new(name_str, strlen(name_str));
192 probe_ent_attr_add(field_ent, "name", name_sexp);
193 SEXP_free(name_sexp);
194
195 /* put the field in the record */
196 SEXP_list_add(result_sexp, field_ent);
197 SEXP_free(field_ent);
198
199 /* Create item and put the record in item */
200 SEXP_t *item = probe_item_create(OVAL_WINDOWS_WMI_57, NULL,
201 "namespace", OVAL_DATATYPE_STRING, oscap_windows_wstr_to_str(wmi_namespace),
202 "wql", OVAL_DATATYPE_STRING, oscap_windows_wstr_to_str(wql_query),
203 NULL);
204 SEXP_list_add(item, result_sexp);
205 SEXP_free(result_sexp);
206
207 probe_item_collect(ctx, item);
208
209 return 0;
210 }
211
212 static int query_wmi(probe_ctx *ctx, WCHAR *wmi_namespace, WCHAR *wql_query)
213 {
214 HRESULT ret = 0;
215 IWbemLocator *locator = NULL;
216 IWbemServices *services = NULL;
217 IEnumWbemClassObject *results = NULL;
218
219 BSTR resource = SysAllocString(wmi_namespace);
220 BSTR language = SysAllocString(L"WQL");
221
222 BSTR query = SysAllocString(wql_query);
223
224 ret = CoInitializeEx(0, COINIT_MULTITHREADED);
225 if (FAILED(ret)) {
226 dE("Cannot initialize the COM library.");
227 goto cleanup;
228 }
229 ret = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
230 if (FAILED(ret)) {
231 dE("Cannot initialize security for the COM library.");
232 goto cleanup;
233 }
234
235 ret = CoCreateInstance(&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (LPVOID *)&locator);
236 if (FAILED(ret)) {
237 dE("Cannot create any instance of IWbemLocator object.");
238 goto cleanup;
239 }
240 ret = locator->lpVtbl->ConnectServer(locator, resource, NULL, NULL, NULL, 0, NULL, NULL, &services);
241 if (FAILED(ret)) {
242 locator->lpVtbl->Release(locator);
243 dE("Cannot connect to WMI namespace '%S'.", wmi_namespace);
244 goto cleanup;
245 }
246
247 ret = services->lpVtbl->ExecQuery(services, language, query, WBEM_FLAG_BIDIRECTIONAL, NULL, &results);
248 if (FAILED(ret)) {
249 locator->lpVtbl->Release(locator);
250 services->lpVtbl->Release(services);
251 dE("Cannot execute WQL query '%S'.", wql_query);
252 goto cleanup;
253 }
254
255 struct oscap_list *wql_fields = get_wql_fields(wql_query);
256 if (wql_fields == NULL) {
257 locator->lpVtbl->Release(locator);
258 services->lpVtbl->Release(services);
259 results->lpVtbl->Release(results);
260 goto cleanup;
261 }
262
263 if (results != NULL) {
264 IWbemClassObject *result = NULL;
265 ULONG returnedCount = 0;
266
267 while ((ret = results->lpVtbl->Next(results, WBEM_INFINITE, 1, &result, &returnedCount)) == S_OK) {
268 VARIANT variant;
269 CIMTYPE type;
270
271 struct oscap_iterator *it = oscap_iterator_new(wql_fields);
272 while (oscap_iterator_has_more(it)) {
273 WCHAR *field_name = oscap_iterator_next(it);
274
275 ret = result->lpVtbl->Get(result, field_name, 0, &variant, &type, 0);
276 collect_value(ctx, wmi_namespace, wql_query, field_name, &variant, type);
277 }
278
279 oscap_iterator_free(it);
280 result->lpVtbl->Release(result);
281 }
282 }
283
284 oscap_list_free(wql_fields, free);
285
286 results->lpVtbl->Release(results);
287 services->lpVtbl->Release(services);
288 locator->lpVtbl->Release(locator);
289
290 cleanup:
291 CoUninitialize(); // must be called even if CoInitializeEx fails
292 SysFreeString(query);
293 SysFreeString(language);
294 SysFreeString(resource);
295
296 return 0;
297 }
298
299 int wmi57_probe_main(probe_ctx *ctx, void *arg)
300 {
301 SEXP_t *probe_in = probe_ctx_getobject(ctx);
302 SEXP_t *namespace_ent = probe_obj_getent(probe_in, "namespace", 1);
303 SEXP_t *wql_ent = probe_obj_getent(probe_in, "wql", 1);
304 SEXP_t *namespace_val = probe_ent_getval(namespace_ent);
305 SEXP_t *wql_val = probe_ent_getval(wql_ent);
306 char *namespace_str = SEXP_string_cstr(namespace_val);
307 char *wql_str = SEXP_string_cstr(wql_val);
308 WCHAR *namespace_wstr = oscap_windows_str_to_wstr(namespace_str);
309 WCHAR *wql_wstr = oscap_windows_str_to_wstr(wql_str);
310
311 query_wmi(ctx, namespace_wstr, wql_wstr);
312
313 free(wql_wstr);
314 free(namespace_wstr);
315 free(wql_str);
316 free(namespace_str);
317 SEXP_free(wql_val);
318 SEXP_free(namespace_val);
319 SEXP_free(wql_ent);
320 SEXP_free(namespace_ent);
321 return 0;
322 }