ReactOS 0.4.16-dev-92-g0c2cdca
NtImpersonateAnonymousToken.c File Reference
#include "precomp.h"
#include <winreg.h>
Include dependency graph for NtImpersonateAnonymousToken.c:

Go to the source code of this file.

Macros

#define TOKEN_WITH_EVERYONE_GROUP   1
 
#define TOKEN_WITHOUT_EVERYONE_GROUP   0
 

Functions

static HANDLE GetThreadFromCurrentProcess (_In_ DWORD DesiredAccess)
 
static VOID ImpersonateTokenWithEveryoneOrWithout (_In_ DWORD Value)
 
 START_TEST (NtImpersonateAnonymousToken)
 

Macro Definition Documentation

◆ TOKEN_WITH_EVERYONE_GROUP

#define TOKEN_WITH_EVERYONE_GROUP   1

Definition at line 11 of file NtImpersonateAnonymousToken.c.

◆ TOKEN_WITHOUT_EVERYONE_GROUP

#define TOKEN_WITHOUT_EVERYONE_GROUP   0

Definition at line 12 of file NtImpersonateAnonymousToken.c.

Function Documentation

◆ GetThreadFromCurrentProcess()

static HANDLE GetThreadFromCurrentProcess ( _In_ DWORD  DesiredAccess)
static

Definition at line 16 of file NtImpersonateAnonymousToken.c.

17{
19
21 if (!Thread)
22 {
23 skip("OpenThread() has failed to open the current process' thread (error code: %lu)\n", GetLastError());
24 return NULL;
25 }
26
27 return Thread;
28}
#define skip(...)
Definition: atltest.h:64
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
HANDLE WINAPI OpenThread(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwThreadId)
Definition: thread.c:403
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
DWORD WINAPI GetCurrentThreadId(void)
Definition: thread.c:459

Referenced by START_TEST().

◆ ImpersonateTokenWithEveryoneOrWithout()

static VOID ImpersonateTokenWithEveryoneOrWithout ( _In_ DWORD  Value)
static

Definition at line 32 of file NtImpersonateAnonymousToken.c.

33{
35 HKEY Key;
36
38 L"SYSTEM\\CurrentControlSet\\Control\\Lsa",
39 0,
41 &Key);
42 if (Result != ERROR_SUCCESS)
43 {
44 skip("RegOpenKeyExW() has failed to open the key (error code: %li)\n", Result);
45 return;
46 }
47
49 L"EveryoneIncludesAnonymous",
50 0,
52 (PBYTE)&Value,
53 sizeof(Value));
54 if (Result != ERROR_SUCCESS)
55 {
56 skip("RegSetValueExW() has failed to set the value (error code: %li)\n", Result);
58 return;
59 }
60
62}
#define RegCloseKey(hKey)
Definition: registry.h:49
#define ERROR_SUCCESS
Definition: deptool.c:10
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4882
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define L(x)
Definition: ntvdm.h:50
BYTE * PBYTE
Definition: pedump.c:66
long LONG
Definition: pedump.c:60
#define REG_DWORD
Definition: sdbapi.c:596
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( NtImpersonateAnonymousToken  )

Definition at line 64 of file NtImpersonateAnonymousToken.c.

65{
68 HANDLE ThreadHandle;
69
71
72 /* We give an invalid thread handle */
75
76 /* We want to impersonate the token including Everyone Group SID */
78
79 /* Impersonate the anonymous logon token */
80 Status = NtImpersonateAnonymousToken(ThreadHandle);
82
83 /* Now revert to the previous security properties */
85 ok(Success == TRUE, "We should have terminated the impersonation but we couldn't (error code: %lu)\n", GetLastError());
86
87 /* Return to default setting -- token without Everyone Group SID */
89
90 /* Impersonate the anonymous logon token again */
91 Status = NtImpersonateAnonymousToken(ThreadHandle);
93
94 /* Now revert to the previous security properties */
96 ok(Success == TRUE, "We should have terminated the impersonation but we couldn't (error code: %lu)\n", GetLastError());
97
98 /*
99 * Invalidate the handle and open a new one. This time
100 * with the wrong access right mask, the function will
101 * outright fail on impersonating the token.
102 */
103 CloseHandle(ThreadHandle);
105
106 /* The thread handle has incorrect right access */
107 Status = NtImpersonateAnonymousToken(ThreadHandle);
109
110 /* We're done with the tests */
111 CloseHandle(ThreadHandle);
112}
static HANDLE GetThreadFromCurrentProcess(_In_ DWORD DesiredAccess)
#define TOKEN_WITHOUT_EVERYONE_GROUP
#define TOKEN_WITH_EVERYONE_GROUP
static VOID ImpersonateTokenWithEveryoneOrWithout(_In_ DWORD Value)
#define ok_hex(expression, result)
Definition: atltest.h:94
#define ok(value,...)
Definition: atltest.h:57
LONG NTSTATUS
Definition: precomp.h:26
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:739
@ Success
Definition: eventcreate.c:712
unsigned int BOOL
Definition: ntddk_ex.h:94
Status
Definition: gdiplustypes.h:25
#define THREAD_IMPERSONATE
Definition: pstypes.h:152
#define SYNCHRONIZE
Definition: nt_native.h:61
NTSTATUS NTAPI NtImpersonateAnonymousToken(_In_ HANDLE ThreadHandle)
Allows the calling thread to impersonate the system's anonymous logon token.
Definition: token.c:2612
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:245
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
BOOL WINAPI RevertToSelf(void)
Definition: security.c:1608