Debian Codegraph @

src/OVAL/probes/windows/registry_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 <windows.h>
29 #include "debug_priv.h"
30 #include "_seap.h"
31 #include "probe-api.h"
32 #include "probe/entcmp.h"
33 #include "probe/probe.h"
34 #include "registry_probe.h"
35 #include "oscap_helpers.h"
36
37 #define UNLIMITED_DEPTH -1
38
39 struct ent_info {
40 SEXP_t *behaviors_ent;
41 SEXP_t *hive_ent;
42 SEXP_t *key_ent;
43 SEXP_t *name_ent;
44 };
45
46 struct registry_key_info {
47 const char *hive_str;
48 const char *key_str;
49 DWORD max_value_len;
50 DWORD max_name_len;
51 HKEY opened_key_handle;
52 FILETIME last_write_time;
53 int windows_view;
54 };
55
56 enum registry_windows_view {
57 REGISTRY_WINDOWS_VIEW_64_BIT,
58 REGISTRY_WINDOWS_VIEW_32_BIT
59 };
60
61 enum registry_recurse_direction {
62 REGISTRY_RECURSE_DIRECTION_NONE,
63 REGISTRY_RECURSE_DIRECTION_DOWN,
64 REGISTRY_RECURSE_DIRECTION_UP
65 };
66
67 static const char *windows_view_to_str(int windows_view)
68 {
69 if (windows_view == REGISTRY_WINDOWS_VIEW_64_BIT) {
70 return "64_bit";
71 }
72 if (windows_view == REGISTRY_WINDOWS_VIEW_32_BIT) {
73 return "32_bit";
74 }
75 return NULL;
76 }
77
78 static const char *registry_value_type_to_str(DWORD type)
79 {
80 const char *type_str = NULL;
81 switch (type) {
82 case REG_BINARY:
83 type_str = "reg_binary";
84 break;
85 case REG_DWORD:
86 type_str = "reg_dword";
87 break;
88 case REG_DWORD_BIG_ENDIAN:
89 type_str = "reg_dword_big_endian";
90 break;
91 case REG_EXPAND_SZ:
92 type_str = "reg_expand_sz";
93 break;
94 case REG_LINK:
95 type_str = "reg_link";
96 break;
97 case REG_MULTI_SZ:
98 type_str = "reg_multi_sz";
99 break;
100 case REG_NONE:
101 type_str = "reg_none";
102 break;
103 case REG_QWORD:
104 type_str = "reg_qword";
105 break;
106 case REG_SZ:
107 type_str = "reg_sz";
108 break;
109 case REG_RESOURCE_LIST: /* new in 5.11.1 */
110 type_str = "reg_resource_list";
111 break;
112 case REG_FULL_RESOURCE_DESCRIPTOR: /* new in 5.11.1 */
113 type_str = "reg_full_resource_descriptor";
114 break;
115 case REG_RESOURCE_REQUIREMENTS_LIST: /* new in 5.11.1 */
116 type_str = "reg_resource_requirements_list";
117 break;
118 default:
119 /* The empty string is also allowed as a valid value to support empty elements associated with error conditions. */
120 type_str = "";
121 }
122 return type_str;
123 }
124
125 static void report_finding(probe_ctx *ctx, const char *hive, const char *key, const char *name, FILETIME last_write_time, DWORD type, SEXP_t *value, int windows_view)
126 {
127 SEXP_t *item = probe_item_create(OVAL_WINDOWS_REGISTRY, NULL,
128 "hive", OVAL_DATATYPE_STRING, hive, NULL);
129
130 if (key != NULL) {
131 SEXP_t *key_sexp = SEXP_string_new(key, strlen(key));
132 probe_item_ent_add(item, "key", NULL, key_sexp);
133 SEXP_free(key_sexp);
134 }
135
136 if (name != NULL) {
137 SEXP_t *name_sexp = SEXP_string_new(name, strlen(name));
138 probe_item_ent_add(item, "name", NULL, name_sexp);
139 SEXP_free(name_sexp);
140 }
141
142 /* convert FILETIME structure to a 64-bit unsigned integer */
143 uint64_t last_write_time_uint64 = (((uint64_t) last_write_time.dwHighDateTime) << 32) + last_write_time.dwLowDateTime;
144 SEXP_t *last_write_time_sexp = SEXP_number_newu_64(last_write_time_uint64);
145 probe_item_ent_add(item, "last_write_time", NULL, last_write_time_sexp);
146 SEXP_free(last_write_time_sexp);
147
148 if (name != NULL) {
149 const char *type_str = registry_value_type_to_str(type);
150 SEXP_t *type_sexp = SEXP_string_new(type_str, strlen(type_str));
151 probe_item_ent_add(item, "type", NULL, type_sexp);
152 SEXP_free(type_sexp);
153
154 probe_item_ent_add(item, "value", NULL, value);
155 }
156
157 const char *windows_view_str = windows_view_to_str(windows_view);
158 SEXP_t *windows_view_sexp = SEXP_string_new(windows_view_str, strlen(windows_view_str));
159 probe_item_ent_add(item, "windows_view", NULL, windows_view_sexp);
160 SEXP_free(windows_view_sexp);
161
162 probe_item_collect(ctx, item);
163 }
164
165 static HKEY get_hive_from_str(const char *hive_str)
166 {
167 /* OVAL specification limits hive to HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG,
168 HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, and HKEY_USERS. */
169 if (strcmp("HKEY_CLASSES_ROOT", hive_str) == 0) {
170 return HKEY_CLASSES_ROOT;
171 }
172 if (strcmp("HKEY_CURRENT_CONFIG", hive_str) == 0) {
173 return HKEY_CURRENT_CONFIG;
174 }
175 if (strcmp("HKEY_CURRENT_USER", hive_str) == 0) {
176 return HKEY_CURRENT_USER;
177 }
178 if (strcmp("HKEY_LOCAL_MACHINE", hive_str) == 0) {
179 return HKEY_LOCAL_MACHINE;
180 }
181 if (strcmp("HKEY_USERS", hive_str) == 0) {
182 return HKEY_USERS;
183 }
184 return NULL;
185 }
186
187 static char *bytes_to_hex(BYTE *bytes, DWORD length)
188 {
189 /* Each byte will be repesented by 2 characters. */
190 const size_t hex_str_len = 2 * length + 1; // +1 for null byte
191 char *hex_str = malloc(hex_str_len);
192 char *ptr = hex_str;
193 for (DWORD i = 0; i < length; i++) {
194 ptr += snprintf(ptr, hex_str_len, "%02X", bytes[i]);
195 }
196 *ptr = '\0';
197 return hex_str;
198 }
199
200 static char *build_subkey_path(const char *key_path, const char *subkey_name)
201 {
202 if (key_path == NULL) {
203 return oscap_strdup(subkey_name);
204 } else {
205 return oscap_sprintf("%s\\%s", key_path, subkey_name);
206 }
207 }
208
209 static REGSAM get_access_rights(int windows_view)
210 {
211 REGSAM access_rights = KEY_READ;
212 /* Set alternate registry view according to windows_view attribute of behaviors element */
213 if (windows_view == REGISTRY_WINDOWS_VIEW_64_BIT) {
214 access_rights |= KEY_WOW64_64KEY;
215 }
216 else {
217 access_rights |= KEY_WOW64_32KEY;
218 }
219 return access_rights;
220 }
221
222 static struct ent_info *ent_info_new(SEXP_t *probe_in)
223 {
224 struct ent_info *ei = malloc(sizeof(struct ent_info));
225 ei->behaviors_ent = probe_obj_getent(probe_in, "behaviors", 1);
226 ei->hive_ent = probe_obj_getent(probe_in, "hive", 1);
227 ei->key_ent = probe_obj_getent(probe_in, "key", 1);
228 ei->name_ent = probe_obj_getent(probe_in, "name", 1);
229 return ei;
230 }
231
232 static void ent_info_free(struct ent_info *ei)
233 {
234 SEXP_free(ei->behaviors_ent);
235 SEXP_free(ei->hive_ent);
236 SEXP_free(ei->key_ent);
237 SEXP_free(ei->name_ent);
238 free(ei);
239 }
240
241 static int registry_behaviors_get_max_depth(SEXP_t *behaviors_ent)
242 {
243 int max_depth = UNLIMITED_DEPTH; // default behavior value
244 if (behaviors_ent != NULL && probe_ent_attrexists(behaviors_ent, "max_depth")) {
245 SEXP_t *max_depth_attrval = probe_ent_getattrval(behaviors_ent, "max_depth");
246 char *max_depth_str = SEXP_string_cstr(max_depth_attrval);
247 max_depth = strtol(max_depth_str, NULL, 10);
248 free(max_depth_str);
249 SEXP_free(max_depth_attrval);
250 }
251 return max_depth;
252 }
253
254 static int registry_behaviors_get_recurse_direction(SEXP_t *behaviors_ent)
255 {
256 int recurse_direction = REGISTRY_RECURSE_DIRECTION_NONE; // default behavior value
257 if (behaviors_ent != NULL && probe_ent_attrexists(behaviors_ent, "recurse_direction")) {
258 SEXP_t *recurse_direction_attrval = probe_ent_getattrval(behaviors_ent, "recurse_direction");
259 char *recurse_direction_str = SEXP_string_cstr(recurse_direction_attrval);
260 if (strcmp(recurse_direction_str, "none") == 0) {
261 recurse_direction = REGISTRY_RECURSE_DIRECTION_NONE;
262 } else if (strcmp(recurse_direction_str, "down") == 0) {
263 recurse_direction = REGISTRY_RECURSE_DIRECTION_DOWN;
264 } else if (strcmp(recurse_direction_str, "up") == 0) {
265 recurse_direction = REGISTRY_RECURSE_DIRECTION_UP;
266 } else {
267 dE("Invalid direction attribute value: %s", recurse_direction_str);
268 }
269 free(recurse_direction_str);
270 SEXP_free(recurse_direction_attrval);
271 }
272 return recurse_direction;
273 }
274
275 static int registry_behaviors_get_windows_view(SEXP_t *behaviors_ent)
276 {
277 int windows_view = REGISTRY_WINDOWS_VIEW_64_BIT; // default behavior value
278 if (behaviors_ent != NULL && probe_ent_attrexists(behaviors_ent, "windows_view")) {
279 SEXP_t *windows_view_attrval = probe_ent_getattrval(behaviors_ent, "windows_view");
280 char *windows_view_str = SEXP_string_cstr(windows_view_attrval);
281 if (strcmp(windows_view_str, "64_bit") == 0) {
282 windows_view = REGISTRY_WINDOWS_VIEW_64_BIT;
283 } else if (strcmp(windows_view_str, "32_bit") == 0) {
284 windows_view = REGISTRY_WINDOWS_VIEW_32_BIT;
285 } else {
286 dE("Invalid windows_view attribute value: %s", windows_view_str);
287 }
288 free(windows_view_str);
289 SEXP_free(windows_view_attrval);
290 }
291 return windows_view;
292 }
293
294
295 static void collect_registry_value(probe_ctx *ctx, struct registry_key_info *ki, DWORD index, SEXP_t *name_ent)
296 {
297 DWORD name_buffer_len = ki->max_name_len + 1; // +1 for terminating null byte
298 WCHAR *name_wstr = malloc(name_buffer_len * sizeof(WCHAR));
299 name_wstr[0] = '\0';
300 DWORD name_len = name_buffer_len;
301 DWORD type;
302 BYTE *value = malloc(ki->max_value_len);
303 DWORD value_len = ki->max_value_len;
304
305 int ret = RegEnumValueW(ki->opened_key_handle, index, name_wstr, &name_len, NULL, &type, value, &value_len);
306 if (ret != ERROR_SUCCESS) {
307 free(value);
308 free(name_wstr);
309 return;
310 }
311
312 char *name_str = oscap_windows_wstr_to_str(name_wstr);
313 if (name_str != NULL && strlen(name_str) == 0) {
314 free(name_str);
315 name_str = NULL;
316 }
317 if (name_str == NULL) {
318 name_str = strdup("(Default)");
319 }
320
321 SEXP_t *tmp = SEXP_string_new(name_str, strlen(name_str));
322 if (probe_entobj_cmp(name_ent, tmp) != OVAL_RESULT_TRUE) {
323 SEXP_free(tmp);
324 free(value);
325 free(name_wstr);
326 free(name_str);
327 return;
328 }
329
330 SEXP_t *value_sexp;
331 char *str_buf;
332 WCHAR *wstr_buf;
333
334 switch (type) {
335 case REG_DWORD:
336 value_sexp = SEXP_number_newi_32(*(DWORD *)value);
337 report_finding(ctx, ki->hive_str, ki->key_str, name_str, ki->last_write_time, type, value_sexp, ki->windows_view);
338 SEXP_free(value_sexp);
339 break;
340 case REG_QWORD:
341 value_sexp = SEXP_number_newi_64(*(DWORD64 *)value);
342 report_finding(ctx, ki->hive_str, ki->key_str, name_str, ki->last_write_time, type, value_sexp, ki->windows_view);
343 SEXP_free(value_sexp);
344 break;
345 case REG_SZ:
346 case REG_EXPAND_SZ:
347 str_buf = oscap_windows_wstr_to_str((WCHAR *)value);
348 value_sexp = SEXP_string_new(str_buf, strlen(str_buf));
349 free(str_buf);
350 report_finding(ctx, ki->hive_str, ki->key_str, name_str, ki->last_write_time, type, value_sexp, ki->windows_view);
351 SEXP_free(value_sexp);
352 break;
353 case REG_MULTI_SZ:
354 /* A sequence of null-terminated strings, terminated by an empty string (\0). */
355 /* Example: String1\0String2\0String3\0LastString\0\0 */
356 wstr_buf = (WCHAR *)value;
357 while (*wstr_buf != L'\0') {
358 str_buf = oscap_windows_wstr_to_str(wstr_buf);
359 value_sexp = SEXP_string_new(str_buf, strlen(str_buf));
360 free(str_buf);
361 report_finding(ctx, ki->hive_str, ki->key_str, name_str, ki->last_write_time, type, value_sexp, ki->windows_view);
362 SEXP_free(value_sexp);
363 wstr_buf += (wcslen(wstr_buf) + 1);
364 }
365 break;
366 case REG_BINARY:
367 default: /* treat unsupported types as binary blobs */
368 str_buf = bytes_to_hex(value, value_len);
369 value_sexp = SEXP_string_new(str_buf, strlen(str_buf));
370 free(str_buf);
371 report_finding(ctx, ki->hive_str, ki->key_str, name_str, ki->last_write_time, type, value_sexp, ki->windows_view);
372 SEXP_free(value_sexp);
373 break;
374 }
375 SEXP_free(tmp);
376 free(name_str);
377 free(value);
378 free(name_wstr);
379 }
380
381 static int collect_registry_key(probe_ctx *ctx, const char *hive_str, const char *key_str, SEXP_t *name_ent, int windows_view)
382 {
383 HKEY hive = get_hive_from_str(hive_str);
384 HKEY opened_key_handle;
385 REGSAM access_rights = get_access_rights(windows_view);
386 WCHAR *key_wstr = oscap_windows_str_to_wstr(key_str);
387 LONG err = RegOpenKeyExW(hive, key_wstr, 0, access_rights, &opened_key_handle);
388 free(key_wstr);
389 if (err != ERROR_SUCCESS) {
390 char *error_message = oscap_windows_error_message(err);
391 dD("Can't open registry key '%s': RegOpenKeyExW error: %s", key_str, error_message);
392 free(error_message);
393 return 1;
394 }
395
396 DWORD subkeys_count, max_subkey_len, values_count, max_name_len, max_value_len;
397 FILETIME last_write_time;
398 DWORD ret = RegQueryInfoKeyW(opened_key_handle, NULL, NULL, NULL,
399 &subkeys_count, &max_subkey_len, NULL, &values_count, &max_name_len,
400 &max_value_len, NULL, &last_write_time);
401
402 SEXP_t *name_sexp_val = probe_ent_getval(name_ent);
403 if (key_str == NULL) {
404 report_finding(ctx, hive_str, NULL, NULL, last_write_time, REG_NONE, NULL, windows_view);
405 RegCloseKey(opened_key_handle);
406 return 0;
407 }
408
409 if (name_sexp_val == NULL) {
410 /* Collect the key itself */
411 report_finding(ctx, hive_str, key_str, NULL, last_write_time, REG_NONE, NULL, windows_view);
412 RegCloseKey(opened_key_handle);
413 return 0;
414 }
415
416 struct registry_key_info *ki = malloc(sizeof(struct registry_key_info));
417 ki->hive_str = hive_str;
418 ki->key_str = key_str;
419 ki->max_value_len = max_value_len;
420 ki->max_name_len = max_name_len;
421 ki->opened_key_handle = opened_key_handle;
422 ki->last_write_time = last_write_time;
423 ki->windows_view = windows_view;
424
425 /* Enumerate the key values */
426 for (DWORD i = 0; i < values_count; i++) {
427 collect_registry_value(ctx, ki, i, name_ent);
428 }
429
430 free(ki);
431
432 RegCloseKey(opened_key_handle);
433 return 0;
434 }
435
436 static int registry_recurse_down(probe_ctx *ctx, struct ent_info *ei, char *hive_str, char *key_str, int depth, int max_depth, oval_operation_t operation, int windows_view)
437 {
438 HKEY opened_key_handle;
439 HKEY hive = get_hive_from_str(hive_str);
440 REGSAM access_rights = get_access_rights(windows_view);
441 WCHAR *key_wstr = oscap_windows_str_to_wstr(key_str);
442 LONG err = RegOpenKeyExW(hive, key_wstr, 0, access_rights, &opened_key_handle);
443 free(key_wstr);
444 if (err != ERROR_SUCCESS) {
445 char *error_message = oscap_windows_error_message(err);
446 dD("Can't open registry key '%s': RegOpenKeyExW error: %s", key_str, error_message);
447 free(error_message);
448 return 0;
449 }
450
451 DWORD subkeys_count, max_subkey_len;
452 DWORD ret = RegQueryInfoKeyW(opened_key_handle, NULL, NULL, NULL,
453 &subkeys_count, &max_subkey_len, NULL, NULL, NULL,
454 NULL, NULL, NULL);
455
456 if ((max_depth == UNLIMITED_DEPTH || depth < max_depth) && subkeys_count > 0) {
457 DWORD subkey_buffer_len = max_subkey_len + 1; // +1 for terminating null character
458 WCHAR *subkey = malloc(subkey_buffer_len * sizeof(WCHAR));
459 for (DWORD i = 0; i < subkeys_count; i++) {
460 DWORD subkey_len = subkey_buffer_len;
461 ret = RegEnumKeyExW(opened_key_handle, i, subkey, &subkey_len, NULL, NULL, NULL, NULL);
462 if (ret == ERROR_SUCCESS) {
463 char *subkey_str = oscap_windows_wstr_to_str(subkey);
464 char *subkey_path = build_subkey_path(key_str, subkey_str);
465 registry_recurse_down(ctx, ei, hive_str, subkey_path, depth + 1, max_depth, operation, windows_view);
466 free(subkey_path);
467 free(subkey_str);
468 }
469 }
470 free(subkey);
471 }
472
473 if (key_str != NULL) {
474 SEXP_t *tmp = SEXP_string_new(key_str, strlen(key_str));
475 if (operation == OVAL_OPERATION_EQUALS || operation == OVAL_OPERATION_CASE_INSENSITIVE_EQUALS || probe_entobj_cmp(ei->key_ent, tmp) == OVAL_RESULT_TRUE) {
476 collect_registry_key(ctx, hive_str, key_str, ei->name_ent, windows_view);
477 }
478 SEXP_free(tmp);
479 }
480
481 RegCloseKey(opened_key_handle);
482 return 0;
483
484 }
485
486 static int registry_recurse_up(probe_ctx *ctx, struct ent_info *ei, char *hive_str, char *key_str, int max_depth, int windows_view)
487 {
488 int depth = 0;
489 do {
490 int ret = collect_registry_key(ctx, hive_str, key_str, ei->name_ent, windows_view);
491 if (ret != 0) {
492 break;
493 }
494 char *delimiter = strrchr(key_str, '\\');
495 if (delimiter == NULL) {
496 collect_registry_key(ctx, hive_str, NULL, ei->name_ent, windows_view);
497 break;
498 }
499 *delimiter = '\0';
500 depth++;
501 } while (max_depth == UNLIMITED_DEPTH || depth < max_depth);
502 return 0;
503 }
504
505 int registry_probe_main(probe_ctx *ctx, void *arg)
506 {
507 SEXP_t *probe_in = probe_ctx_getobject(ctx);
508 struct ent_info *ei = ent_info_new(probe_in);
509
510 SEXP_t *hive_val = probe_ent_getval(ei->hive_ent);
511 char *hive_str = SEXP_string_cstr(hive_val);
512 SEXP_free(hive_val);
513
514 /* Get behaviors or their default values */
515 int max_depth = registry_behaviors_get_max_depth(ei->behaviors_ent);
516 int recurse_direction = registry_behaviors_get_recurse_direction(ei->behaviors_ent);
517 int windows_view = registry_behaviors_get_windows_view(ei->behaviors_ent);
518
519 oval_operation_t key_operation = probe_ent_getoperation(ei->key_ent, OVAL_OPERATION_EQUALS);
520 if (key_operation == OVAL_OPERATION_EQUALS || key_operation == OVAL_OPERATION_CASE_INSENSITIVE_EQUALS) {
521 SEXP_t *key_val = probe_ent_getval(ei->key_ent);
522 char *key_str = SEXP_string_cstr(key_val);
523 SEXP_free(key_val);
524 if (recurse_direction == REGISTRY_RECURSE_DIRECTION_NONE || max_depth == 0) {
525 /* max_depth == 0 means no recursion no matter what recurse_direction was set in behaviors */
526 collect_registry_key(ctx, hive_str, key_str, ei->name_ent, windows_view);
527 } else if (recurse_direction == REGISTRY_RECURSE_DIRECTION_DOWN) {
528 HKEY hive = get_hive_from_str(hive_str);
529 registry_recurse_down(ctx, ei, hive_str, key_str, 0, max_depth, key_operation, windows_view);
530 } else if (recurse_direction == REGISTRY_RECURSE_DIRECTION_UP) {
531 registry_recurse_up(ctx, ei, hive_str, key_str, max_depth, windows_view);
532 }
533 free(key_str);
534 } else { /* pattern match, not equal, ... */
535 HKEY hive = get_hive_from_str(hive_str);
536 registry_recurse_down(ctx, ei, hive_str, NULL, 0, UNLIMITED_DEPTH, key_operation, windows_view);
537 }
538
539 free(hive_str);
540 ent_info_free(ei);
541
542 return 0;
543 }