Debian Codegraph @

src/OVAL/probes/windows/accesstoken_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 <stdio.h>
28 #include <string.h>
29 #include <Windows.h>
30 #include <Ntsecapi.h>
31 #include <ntstatus.h>
32 #include <LM.h>
33 #include <winnt.h>
34 #include <stdbool.h>
35 #include "debug_priv.h"
36 #include "_seap.h"
37 #include "probe-api.h"
38 #include "probe/entcmp.h"
39 #include "probe/probe.h"
40 #include "accesstoken_probe.h"
41 #include "list.h"
42
43 #define LAST_WELL_KNOWN_SID WinAuthenticationFreshKeyAuthSid
44 #define OVAL_PRIVILEGES_COUNT 44
45
46 WCHAR *privileges_texts[OVAL_PRIVILEGES_COUNT] = {
47 L"SeAssignPrimaryTokenPrivilege",
48 L"SeAuditPrivilege",
49 L"SeBackupPrivilege",
50 L"SeChangeNotifyPrivilege",
51 L"SeCreateGlobalPrivilege",
52 L"SeCreatePagefilePrivilege",
53 L"SeCreatePermanentPrivilege",
54 L"SeCreateSymbolicLinkPrivilege",
55 L"SeCreateTokenPrivilege",
56 L"SeDebugPrivilege",
57 L"SeEnableDelegationPrivilege",
58 L"SeImpersonatePrivilege",
59 L"SeIncreaseBasePriorityPrivilege",
60 L"SeIncreaseQuotaPrivilege",
61 L"SeIncreaseWorkingSetPrivilege",
62 L"SeLoadDriverPrivilege",
63 L"SeLockMemoryPrivilege",
64 L"SeMachineAccountPrivilege",
65 L"SeManageVolumePrivilege",
66 L"SeProfileSingleProcessPrivilege",
67 L"SeRelabelPrivilege",
68 L"SeRemoteShutdownPrivilege",
69 L"SeRestorePrivilege",
70 L"SeSecurityPrivilege",
71 L"SeShutdownPrivilege",
72 L"SeSyncAgentPrivilege",
73 L"SeSystemEnvironmentPrivilege",
74 L"SeSystemProfilePrivilege",
75 L"SeSystemtimePrivilege",
76 L"SeTakeOwnershipPrivilege",
77 L"SeTcbPrivilege",
78 L"SeTimeZonePrivilege",
79 L"SeUndockPrivilege",
80 L"SeUnsolicitedInputPrivilege",
81 L"SeBatchLogonRight",
82 L"SeInteractiveLogonRight"
83 L"SeNetworkLogonRight",
84 L"SeRemoteInteractiveLogonRight",
85 L"SeServiceLogonRight",
86 L"SeDenyBatchLogonRight",
87 L"SeDenyInteractiveLogonRight",
88 L"SeDenyNetworkLogonRight",
89 L"SeDenyRemoteInteractiveLogonRight",
90 L"SeDenyServiceLogonRight",
91 L"SeTrustedCredManAccessPrivilege"
92 };
93
94 static int get_all_local_users(struct oscap_list *list)
95 {
96 NET_API_STATUS status;
97
98 USER_INFO_0 *buffer = NULL;
99 DWORD preffered_max_len = MAX_PREFERRED_LENGTH;
100 DWORD entries_read = 0;
101 DWORD total_entries = 0;
102 DWORD resume_handle = 0;
103 status = NetUserEnum(NULL, 0, 0, (LPBYTE *)&buffer, preffered_max_len, &entries_read, &total_entries, &resume_handle);
104 if (status != NERR_Success) {
105 dD("NetUserEnum failed: %d", status);
106 return 1;
107 }
108 for (DWORD i = 0; i < entries_read; i++) {
109 WCHAR *user_name = buffer[i].usri0_name;
110 oscap_list_add(list, wcsdup(user_name));
111 }
112 NetApiBufferFree(buffer);
113 return 0;
114 }
115
116
117 static int get_user_local_groups(WCHAR *user, struct oscap_list *list)
118 {
119 NET_API_STATUS status;
120
121 LOCALGROUP_USERS_INFO_0 *buffer = NULL;
122 DWORD preffered_max_len = MAX_PREFERRED_LENGTH;
123 DWORD entries_read = 0;
124 DWORD total_entries = 0;
125 /*
126 * LG_INCLUDE_INDIRECT means the function also returns the names of
127 * the local groups in which the user is indirectly a member (that is,
128 * the user has membership in a global group that is itself a member
129 * of one or more local groups).
130 */
131 status = NetUserGetLocalGroups(NULL, user, 0, LG_INCLUDE_INDIRECT, (LPBYTE *)&buffer, preffered_max_len, &entries_read, &total_entries);
132 if (status != NERR_Success) {
133 dD("NetUserGetLocalGroups failed: %d", status);
134 return 1;
135 }
136 for (DWORD i = 0; i < entries_read; i++) {
137 WCHAR *group_name = buffer[i].lgrui0_name;
138 oscap_list_add(list, wcsdup(group_name));
139 }
140 NetApiBufferFree(buffer);
141 return 0;
142 }
143
144 static int get_user_global_groups(WCHAR *user, struct oscap_list *list)
145 {
146 NET_API_STATUS status;
147
148 GROUP_USERS_INFO_0 *buffer = NULL;
149 DWORD preffered_max_len = MAX_PREFERRED_LENGTH;
150 DWORD entries_read = 0;
151 DWORD total_entries = 0;
152 status = NetUserGetGroups(NULL, user, 0, (LPBYTE *)&buffer, preffered_max_len, &entries_read, &total_entries);
153 if (status != NERR_Success) {
154 dD("NetUserGetGroups failed: %d", status);
155 return 1;
156 }
157 for (DWORD i = 0; i < entries_read; i++) {
158 WCHAR *group_name = buffer[i].grui0_name;
159 oscap_list_add(list, wcsdup(group_name));
160 }
161 NetApiBufferFree(buffer);
162 return 0;
163 }
164
165 static int get_all_local_groups(struct oscap_list *list)
166 {
167 NET_API_STATUS status;
168 LOCALGROUP_INFO_0 *buffer = NULL;
169 DWORD preffered_max_len = MAX_PREFERRED_LENGTH;
170 DWORD entries_read = 0;
171 DWORD total_entries = 0;
172 DWORD resume_handle = 0;
173 status = NetLocalGroupEnum(NULL, 0, (LPBYTE *)&buffer, preffered_max_len, &entries_read, &total_entries, &resume_handle);
174 if (status != NERR_Success) {
175 dD("NetLocalGroupEnum failed: %d", status);
176 return 1;
177 }
178 for (DWORD i = 0; i < entries_read; i++) {
179 WCHAR *group_name = buffer[i].lgrpi0_name;
180 oscap_list_add(list, wcsdup(group_name));
181 }
182 NetApiBufferFree(buffer);
183 return 0;
184 }
185
186 static int get_all_global_groups(struct oscap_list *list)
187 {
188 NET_API_STATUS status;
189 GROUP_INFO_0 *buffer = NULL;
190 DWORD preffered_max_len = MAX_PREFERRED_LENGTH;
191 DWORD entries_read = 0;
192 DWORD total_entries = 0;
193 DWORD resume_handle = 0;
194 status = NetGroupEnum(NULL, 0, (LPBYTE *)&buffer, preffered_max_len, &entries_read, &total_entries, &resume_handle);
195 if (status != NERR_Success) {
196 dD("NetGroupEnum failed: %d", status);
197 return 1;
198 }
199 for (DWORD i = 0; i < entries_read; i++) {
200 WCHAR *group_name = buffer[i].grpi0_name;
201 oscap_list_add(list, wcsdup(group_name));
202 }
203 NetApiBufferFree(buffer);
204 return 0;
205 }
206
207 static int get_local_group_members(WCHAR *group_name, struct oscap_list *list)
208 {
209 NET_API_STATUS status;
210 LOCALGROUP_MEMBERS_INFO_1 *buffer = NULL;
211 DWORD preffered_max_len = MAX_PREFERRED_LENGTH;
212 DWORD entries_read = 0;
213 DWORD total_entries = 0;
214 DWORD resume_handle = 0;
215 status = NetLocalGroupGetMembers(NULL, group_name, 1, (LPBYTE *) &buffer, preffered_max_len, &entries_read, &total_entries, &resume_handle);
216 if (status != NERR_Success) {
217 dD("NetLocalGroupGetMembers failed: %d", status);
218 return 1;
219 }
220 for (DWORD i = 0; i < entries_read; i++) {
221 WCHAR *member_name = buffer[i].lgrmi1_name;
222 oscap_list_add(list, wcsdup(member_name));
223 }
224 NetApiBufferFree(buffer);
225 return 0;
226 }
227
228 static int get_global_group_members(WCHAR *group_name, struct oscap_list *list)
229 {
230 NET_API_STATUS status;
231 GROUP_USERS_INFO_0 *buffer = NULL;
232 DWORD preffered_max_len = MAX_PREFERRED_LENGTH;
233 DWORD entries_read = 0;
234 DWORD total_entries = 0;
235 DWORD resume_handle = 0;
236 status = NetGroupGetUsers(NULL, group_name, 0, (LPBYTE *)&buffer, preffered_max_len, &entries_read, &total_entries, &resume_handle);
237 if (status != NERR_Success) {
238 dD("NetGroupGetUsers failed: %d", status);
239 return 1;
240 }
241 for (DWORD i = 0; i < entries_read; i++) {
242 WCHAR *member_name = buffer[i].grui0_name;
243 oscap_list_add(list, wcsdup(member_name));
244 }
245 NetApiBufferFree(buffer);
246 return 0;
247 }
248
249 static WCHAR *sid_to_trustee_name(SID *sid)
250 {
251 DWORD name_len = 0;
252 DWORD domain_name_len = 0;
253 SID_NAME_USE sid_type;
254 LookupAccountSidW(NULL, sid, NULL, &name_len, NULL, &domain_name_len, &sid_type);
255 WCHAR *name = malloc(name_len * sizeof(WCHAR));
256 WCHAR *domain_name = malloc(domain_name_len * sizeof(WCHAR));
257 if (!LookupAccountSidW(NULL, sid, name, &name_len, domain_name, &domain_name_len, &sid_type)) {
258 free(name);
259 free(domain_name);
260 DWORD err = GetLastError();
261 char *error_message = oscap_windows_error_message(err);
262 dD("LookupAccountSidW failed: %s", error_message);
263 free(error_message);
264 return NULL;
265 }
266
267 if (*domain_name == L'\0') {
268 free(domain_name);
269 return name;
270 } else {
271 DWORD result_len = name_len + domain_name_len + 2;
272 WCHAR *result = malloc(result_len * sizeof(WCHAR));
273 swprintf(result, result_len, L"%s\\%s", domain_name, name);
274 free(domain_name);
275 free(name);
276 return result;
277 }
278 return NULL;
279 }
280
281 static int get_all_well_known_sids(struct oscap_list *trustees_list)
282 {
283 for (int well_known_sid = 0; well_known_sid < LAST_WELL_KNOWN_SID; well_known_sid++) {
284 DWORD sid_len = SECURITY_MAX_SID_SIZE;
285 SID *sid = malloc(sid_len);
286 if (!CreateWellKnownSid(well_known_sid, NULL, sid, &sid_len)) {
287 free(sid);
288 continue;
289 }
290 WCHAR *well_known_trustee_name = sid_to_trustee_name(sid);
291 if (well_known_trustee_name != NULL) {
292 oscap_list_add(trustees_list, well_known_trustee_name);
293 }
294 free(sid);
295 }
296 return 0;
297 }
298
299 static bool _members_cmp_func(WCHAR *s1, WCHAR *s2)
300 {
301 return wcscmp(s1, s2) == 0;
302 }
303
304 static void expand_group(WCHAR *group, struct oscap_list *output_list, int (*get_group_members)(WCHAR *group_name, struct oscap_list *list))
305 {
306 struct oscap_list *members_list = oscap_list_new();
307 get_group_members(group, members_list);
308 struct oscap_iterator *members_it = oscap_iterator_new(members_list);
309 while (oscap_iterator_has_more(members_it)) {
310 WCHAR *member = oscap_iterator_next(members_it);
311 /* TODO: implement this using a HashSet instead of linked list */
312 if (!oscap_list_contains(output_list, member, _members_cmp_func)) {
313 oscap_list_add(output_list, wcsdup(member));
314 }
315 }
316 oscap_iterator_free(members_it);
317 oscap_list_free(members_list, free);
318 }
319
320 static int get_all_trustee_names(struct oscap_list *trustees_list)
321 {
322 get_all_well_known_sids(trustees_list);
323 get_all_local_users(trustees_list);
324
325 struct oscap_list *local_groups_list = oscap_list_new();
326 get_all_local_groups(local_groups_list);
327
328 /* Find members of each local group */
329 struct oscap_iterator *local_groups_it = oscap_iterator_new(local_groups_list);
330 while (oscap_iterator_has_more(local_groups_it)) {
331 WCHAR *group = oscap_iterator_next(local_groups_it);
332 oscap_list_add(trustees_list, wcsdup(group));
333 expand_group(group, trustees_list, get_local_group_members);
334 }
335 oscap_iterator_free(local_groups_it);
336 oscap_list_free(local_groups_list, free);
337
338 struct oscap_list *global_groups_list = oscap_list_new();
339 get_all_global_groups(global_groups_list);
340
341 /* Find members of each global group */
342 struct oscap_iterator *global_groups_it = oscap_iterator_new(global_groups_list);
343 while (oscap_iterator_has_more(global_groups_it)) {
344 WCHAR *group = oscap_iterator_next(global_groups_it);
345 oscap_list_add(trustees_list, wcsdup(group));
346 expand_group(group, trustees_list, get_global_group_members);
347 }
348 oscap_iterator_free(global_groups_it);
349 oscap_list_free(global_groups_list, free);
350
351 return 0;
352 }
353
354 static bool accesstoken_behaviors_get_include_group(SEXP_t *behaviors_ent)
355 {
356 bool include_group = true; // Default value of include_group behaviors
357 if (behaviors_ent != NULL && probe_ent_attrexists(behaviors_ent, "include_group")) {
358 SEXP_t *include_group_attrval = probe_ent_getattrval(behaviors_ent, "include_group");
359 include_group = SEXP_string_getb(include_group_attrval);
360 SEXP_free(include_group_attrval);
361 }
362 return include_group;
363 }
364
365 static bool accesstoken_behaviors_get_resolve_group(SEXP_t *behaviors_ent)
366 {
367 bool resolve_group = false; // Default value of resolve_group behaviors
368 if (behaviors_ent != NULL && probe_ent_attrexists(behaviors_ent, "resolve_group")) {
369 SEXP_t *resolve_group_attrval = probe_ent_getattrval(behaviors_ent, "resolve_group");
370 resolve_group = SEXP_string_getb(resolve_group_attrval);
371 SEXP_free(resolve_group_attrval);
372 }
373 return resolve_group;
374 }
375
376 static int collect_access_rights(probe_ctx *ctx, WCHAR *security_principle, bool include_group, bool resolve_group)
377 {
378 char *security_principle_str = oscap_windows_wstr_to_str(security_principle);
379
380 LSA_OBJECT_ATTRIBUTES object_attributes;
381 ZeroMemory(&object_attributes, sizeof(LSA_OBJECT_ATTRIBUTES));
382 LSA_HANDLE lsa_policy_handle;
383
384 NTSTATUS status = LsaOpenPolicy(NULL, &object_attributes, POLICY_LOOKUP_NAMES, &lsa_policy_handle);
385 if (status != STATUS_SUCCESS) {
386 DWORD err = LsaNtStatusToWinError(status);
387 char *error_message = oscap_windows_error_message(err);
388 dD("LsaOpenPolicy failed for principle '%s': %s", security_principle_str, error_message);
389 free(error_message);
390 free(security_principle_str);
391 return 1;
392 }
393
394 /* Convert the value of the security_principle element to a SID. */
395 DWORD sid_len = 0;
396 DWORD domain_name_len = 0;
397 SID_NAME_USE sid_type;
398 LookupAccountNameW(NULL, security_principle, NULL, &sid_len, NULL, &domain_name_len, &sid_type);
399 SID *sid = malloc(sid_len);
400 WCHAR *domain_name = malloc(domain_name_len * sizeof(WCHAR));
401 if (!LookupAccountNameW(NULL, security_principle, sid, &sid_len, domain_name, &domain_name_len, &sid_type)) {
402 DWORD err = GetLastError();
403 char *error_message = oscap_windows_error_message(err);
404 dD("LookupAccountNameW failed for '%s': %s", security_principle_str, error_message);
405 free(error_message);
406 free(security_principle_str);
407 free(sid);
408 free(domain_name);
409 return 1;
410 }
411
412 /* Is it a group? */
413 if (sid_type == SidTypeGroup || sid_type == SidTypeWellKnownGroup || sid_type == SidTypeAlias) {
414 if (resolve_group) {
415 struct oscap_list *group_members_list = oscap_list_new();
416 get_local_group_members(security_principle, group_members_list);
417 get_global_group_members(security_principle, group_members_list);
418 struct oscap_iterator *group_members_it = oscap_iterator_new(group_members_list);
419 while (oscap_iterator_has_more(group_members_it)) {
420 WCHAR *group_member = oscap_iterator_next(group_members_it);
421 collect_access_rights(ctx, group_member, include_group, resolve_group);
422 }
423 oscap_iterator_free(group_members_it);
424 oscap_list_free(group_members_list, free);
425 }
426 if (!include_group) {
427 free(sid);
428 free(domain_name);
429 free(security_principle_str);
430 return 0;
431 }
432 }
433
434 /* Users and groups can inherit their privileges from their parents */
435 struct oscap_list *every_rights_sources = oscap_list_new();
436 oscap_list_add(every_rights_sources, wcsdup(security_principle));
437 get_user_local_groups(security_principle, every_rights_sources);
438 get_user_global_groups(security_principle, every_rights_sources);
439
440 /* Iterate over the items */
441 bool privileges_enabled[OVAL_PRIVILEGES_COUNT] = { false };
442 struct oscap_iterator *it = oscap_iterator_new(every_rights_sources);
443 while (oscap_iterator_has_more(it)) {
444 WCHAR *account_name = oscap_iterator_next(it);
445
446 DWORD account_sid_len = 0;
447 DWORD account_domain_name_len = 0;
448 SID_NAME_USE account_sid_type;
449 LookupAccountNameW(NULL, account_name, NULL, &account_sid_len, NULL, &account_domain_name_len, &account_sid_type);
450 SID *account_sid = malloc(account_sid_len);
451 WCHAR *account_domain_name = malloc(account_domain_name_len * sizeof(WCHAR));
452 if (!LookupAccountNameW(NULL, account_name, account_sid, &account_sid_len, account_domain_name, &account_domain_name_len, &account_sid_type)) {
453 free(account_sid);
454 free(account_domain_name);
455 DWORD err = GetLastError();
456 char *error_message = oscap_windows_error_message(err);
457 dD("LookupAccountNameW failed for '%s': %s", security_principle_str, error_message);
458 free(error_message);
459 free(security_principle_str);
460 return 1;
461 }
462
463 LSA_UNICODE_STRING *granted_rights = NULL;
464 ULONG granted_rights_count = 0;
465 status = LsaEnumerateAccountRights(lsa_policy_handle, account_sid, &granted_rights, &granted_rights_count);
466 if (status != STATUS_SUCCESS) {
467 free(account_sid);
468 free(account_domain_name);
469 DWORD err = LsaNtStatusToWinError(status);
470 char *error_message = oscap_windows_error_message(err);
471 dD("LsaEnumerateAccountRights failed for '%s': %s", security_principle_str, error_message);
472 free(error_message);
473 /* We should not exit here, because when LsaEnumerateAccountRights
474 * failed it can mean that the entity simply doesn't have any specific
475 * privileges, it only inhertis privileges form its parent group(s).
476 */
477 continue;
478 }
479
480 for (int i = 0; i < OVAL_PRIVILEGES_COUNT; i++) {
481 if (!privileges_enabled[i]) {
482 for (ULONG j = 0; j < granted_rights_count; j++) {
483 if (wcscmp(granted_rights[j].Buffer, privileges_texts[i]) == 0) {
484 privileges_enabled[i] = true;
485 break;
486 }
487 }
488 }
489 }
490 LsaFreeMemory(granted_rights);
491 free(account_sid);
492 free(account_domain_name);
493
494 }
495 oscap_iterator_free(it);
496 oscap_list_free(every_rights_sources, free);
497
498 /* Collect the OVAL item */
499 SEXP_t *item = probe_item_create(OVAL_WINDOWS_ACCESS_TOKEN, NULL,
500 "security_principle", OVAL_DATATYPE_STRING, strdup(security_principle_str), NULL);
501 for (int i = 0; i < OVAL_PRIVILEGES_COUNT; i++) {
502 char *privilege_name = oscap_windows_wstr_to_str(privileges_texts[i]);
503 /* Convert the element name to lowercase */
504 for (char *p = privilege_name; *p; p++) {
505 *p = tolower(*p);
506 }
507 SEXP_t *privilege_value = SEXP_number_newb(privileges_enabled[i]);
508 probe_item_ent_add(item, privilege_name, NULL, privilege_value);
509 free(privilege_name);
510 SEXP_free(privilege_value);
511 }
512 probe_item_collect(ctx, item);
513 free(security_principle_str);
514
515 return 0;
516 }
517
518 int accesstoken_probe_main(probe_ctx *ctx, void *arg)
519 {
520 SEXP_t *probe_in = probe_ctx_getobject(ctx);
521 SEXP_t *behaviors_ent = probe_obj_getent(probe_in, "behaviors", 1);
522 SEXP_t *security_principle_ent = probe_obj_getent(probe_in, "security_principle", 1);
523 SEXP_t *security_principle_val = probe_ent_getval(security_principle_ent);
524
525 bool include_group = accesstoken_behaviors_get_include_group(behaviors_ent);
526 bool resolve_group = accesstoken_behaviors_get_resolve_group(behaviors_ent);
527
528 oval_operation_t operation = probe_ent_getoperation(security_principle_ent, OVAL_OPERATION_EQUALS);
529 if (operation == OVAL_OPERATION_EQUALS) {
530 char *security_principle_str = SEXP_string_cstr(security_principle_val);
531 WCHAR *security_principle_wstr = oscap_windows_str_to_wstr(security_principle_str);
532 collect_access_rights(ctx, security_principle_wstr, include_group, resolve_group);
533 free(security_principle_str);
534 free(security_principle_wstr);
535 } else {
536 struct oscap_list *trustees_list = oscap_list_new();
537 get_all_trustee_names(trustees_list);
538
539 struct oscap_iterator *it = oscap_iterator_new(trustees_list);
540 while (oscap_iterator_has_more(it)) {
541 WCHAR *trustee_wstr = oscap_iterator_next(it);
542 char *trustee_str = oscap_windows_wstr_to_str(trustee_wstr);
543 SEXP_t *tmp = SEXP_string_new(trustee_str, strlen(trustee_str));
544 if (probe_entobj_cmp(security_principle_ent, tmp) == OVAL_RESULT_TRUE) {
545 collect_access_rights(ctx, trustee_wstr, include_group, resolve_group);
546 }
547 free(trustee_str);
548 SEXP_free(tmp);
549 }
550 oscap_iterator_free(it);
551 oscap_list_free(trustees_list, free);
552 }
553
554 SEXP_free(behaviors_ent);
555 SEXP_free(security_principle_ent);
556 SEXP_free(security_principle_val);
557 return 0;
558 }