-
Notifications
You must be signed in to change notification settings - Fork 178
/
Process.cpp
660 lines (567 loc) · 19.1 KB
/
Process.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// Process.cpp: Executing requested process
//
// Copyright (c) Power Admin LLC, 2012 - 2013
//
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PAExec.h"
#include <UserEnv.h>
#include <winsafer.h>
#include <Sddl.h>
#include <Psapi.h>
#include <WtsApi32.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE GetLocalSystemProcessToken();
bool LimitRights(HANDLE& hUser);
bool ElevateUserToken(HANDLE& hEnvUser);
void Duplicate(HANDLE& h, LPCSTR file, int line)
{
HANDLE hDupe = NULL;
if(DuplicateTokenEx(h, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &hDupe))
{
CloseHandle(h);
h = hDupe;
hDupe = NULL;
}
else
{
DWORD gle = GetLastError();
_ASSERT(0);
Log(StrFormat(L"Error duplicating a user token (%S, %d)", file, line), GetLastError());
}
}
#ifdef _DEBUG
void GrantAllPrivs(HANDLE h)
{
Log(L"DEBUG: GrantAllPrivs", false);
CString privs = L"SeCreateTokenPrivilege,SeAssignPrimaryTokenPrivilege,SeLockMemoryPrivilege,SeIncreaseQuotaPrivilege,SeMachineAccountPrivilege,"
L"SeTcbPrivilege,SeSecurityPrivilege,SeTakeOwnershipPrivilege,SeLoadDriverPrivilege,SeSystemProfilePrivilege,SeSystemtimePrivilege,SeProfileSingleProcessPrivilege,"
L"SeIncreaseBasePriorityPrivilege,SeCreatePagefilePrivilege,SeCreatePermanentPrivilege,SeBackupPrivilege,SeRestorePrivilege,SeShutdownPrivilege,SeDebugPrivilege,"
L"SeAuditPrivilege,SeSystemEnvironmentPrivilege,SeChangeNotifyPrivilege,SeRemoteShutdownPrivilege,SeUndockPrivilege,SeSyncAgentPrivilege,SeEnableDelegationPrivilege,"
L"SeManageVolumePrivilege,SeImpersonatePrivilege,SeCreateGlobalPrivilege,SeTrustedCredManAccessPrivilege,SeRelabelPrivilege,SeIncreaseWorkingSetPrivilege,"
L"SeTimeZonePrivilege,SeCreateSymbolicLinkPrivilege";
wchar_t* pC = wcstok(privs.LockBuffer(), L",");
while(NULL != pC)
{
EnablePrivilege(pC, h); //needed to call CreateProcessAsUser
pC = wcstok(NULL, L",");
}
}
#endif
void GetUserDomain(LPCWSTR userIn, CString& user, CString& domain)
{
//run as specified user
CString tmp = userIn;
LPCWSTR userStr = NULL, domainStr = NULL;
if(NULL != wcschr(userIn, L'@'))
userStr = userIn; //leave domain as NULL
else
{
if(NULL != wcschr(userIn, L'\\'))
{
domainStr = wcstok(tmp.LockBuffer(), L"\\");
userStr = wcstok(NULL, L"\\");
}
else
{
//no domain given
userStr = userIn;
domainStr = L".";
}
}
user = userStr;
domain = domainStr;
}
bool GetUserHandle(Settings& settings, BOOL& bLoadedProfile, PROFILEINFO& profile, HANDLE hCmdPipe)
{
DWORD gle = 0;
if(settings.bUseSystemAccount)
{
if(BAD_HANDLE(settings.hUser)) //might already have hUser from a previous call
{
EnablePrivilege(SE_DEBUG_NAME); //helps with OpenProcess, required for GetLocalSystemProcessToken
settings.hUser = GetLocalSystemProcessToken();
if(BAD_HANDLE(settings.hUser))
{
Log(L"Not able to get Local System token", true);
return false;
}
else
Log(L"Got Local System handle", false);
Duplicate(settings.hUser, __FILE__, __LINE__);
}
return true;
}
else
{
//not Local System, so either as specified user, or as current user
if(FALSE == settings.user.IsEmpty())
{
CString user, domain;
GetUserDomain(settings.user, user, domain);
BOOL bLoggedIn = LogonUser(user, domain.IsEmpty() ? NULL : domain, settings.password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_WINNT50, &settings.hUser);
gle = GetLastError();
#ifdef _DEBUG
Log(L"DEBUG: LogonUser", gle);
#endif
if((FALSE == bLoggedIn) || BAD_HANDLE(settings.hUser))
{
Log(StrFormat(L"Error logging in as %s.", settings.user), gle);
return false;
}
else
Duplicate(settings.hUser, __FILE__, __LINE__); //gives max rights
if(!BAD_HANDLE(settings.hUser) && (false == settings.bDontLoadProfile))
{
EnablePrivilege(SE_RESTORE_NAME);
EnablePrivilege(SE_BACKUP_NAME);
bLoadedProfile = LoadUserProfile(settings.hUser, &profile);
#ifdef _DEBUG
gle = GetLastError();
Log(L"DEBUG: LoadUserProfile", gle);
#endif
}
return true;
}
else
{
//run as current user
if(NULL != hCmdPipe)
{
BOOL b = ImpersonateNamedPipeClient(hCmdPipe);
DWORD gle = GetLastError();
if(FALSE == b)
Log(L"Failed to impersonate client user", gle);
else
Log(L"Impersonated caller", false);
}
HANDLE hThread = GetCurrentThread();
BOOL bDupe = DuplicateHandle(GetCurrentProcess(), hThread, GetCurrentProcess(), &hThread, 0, TRUE, DUPLICATE_SAME_ACCESS);
DWORD gle = GetLastError();
BOOL bOpen = OpenThreadToken(hThread, TOKEN_DUPLICATE | TOKEN_QUERY, TRUE, &settings.hUser);
gle = GetLastError();
if(1008 == gle) //no thread token
{
bOpen = OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &settings.hUser);
gle = GetLastError();
}
if(FALSE == bOpen)
Log(L"Failed to open current user token", GetLastError());
Duplicate(settings.hUser, __FILE__, __LINE__); //gives max rights
RevertToSelf();
return !BAD_HANDLE(settings.hUser);
}
}
}
bool StartProcess(Settings& settings, HANDLE hCmdPipe)
{
//Launching as one of:
//1. System Account
//2. Specified account (or limited account)
//3. As current process
DWORD gle = 0;
BOOL bLoadedProfile = FALSE;
PROFILEINFO profile = {0};
profile.dwSize = sizeof(profile);
profile.lpUserName = (LPWSTR)(LPCWSTR)settings.user;
profile.dwFlags = PI_NOUI;
if(false == GetUserHandle(settings, bLoadedProfile, profile, hCmdPipe))
return false;
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
if(!settings.bInteractive)
si.wShowWindow = SW_HIDE;
if(!BAD_HANDLE(settings.hStdErr))
{
si.hStdError = settings.hStdErr;
si.hStdInput = settings.hStdIn;
si.hStdOutput = settings.hStdOut;
si.dwFlags |= STARTF_USESTDHANDLES;
#ifdef _DEBUG
Log(L"DEBUG: Using redirected handles", false);
#endif
}
#ifdef _DEBUG
else
Log(L"DEBUG: Not using redirected IO", false);
#endif
CString path = StrFormat(L"\"%s\"", settings.app);
if(FALSE == settings.appArgs.IsEmpty())
{
path += L" ";
path += settings.appArgs;
}
LPCWSTR startingDir = NULL;
if(FALSE == settings.workingDir.IsEmpty())
startingDir = settings.workingDir;
DWORD launchGLE = 0;
CleanupInteractive ci = {0};
if(settings.bInteractive || settings.bShowUIOnWinLogon)
{
BOOL b = PrepForInteractiveProcess(settings, &ci, settings.sessionToInteractWith);
if(FALSE == b)
Log(L"Failed to PrepForInteractiveProcess", true);
if(NULL == si.lpDesktop)
si.lpDesktop = L"WinSta0\\Default";
if(settings.bShowUIOnWinLogon)
si.lpDesktop = L"winsta0\\Winlogon";
//Log(StrFormat(L"Using desktop: %s", si.lpDesktop), false);
//http://blogs.msdn.com/b/winsdk/archive/2009/07/14/launching-an-interactive-process-from-windows-service-in-windows-vista-and-later.aspx
//indicates desktop names are case sensitive
}
#ifdef _DEBUG
Log(StrFormat(L"DEBUG: PAExec using desktop %s", si.lpDesktop == NULL ? L"{default}" : si.lpDesktop), false);
#endif
DWORD dwFlags = CREATE_SUSPENDED | CREATE_NEW_CONSOLE;
LPVOID pEnvironment = NULL;
VERIFY(CreateEnvironmentBlock(&pEnvironment, settings.hUser, TRUE));
if(NULL != pEnvironment)
dwFlags |= CREATE_UNICODE_ENVIRONMENT;
#ifdef _DEBUG
gle = GetLastError();
Log(L"DEBUG: CreateEnvironmentBlock", gle);
#endif
if(settings.bDisableFileRedirection)
DisableFileRedirection();
if(settings.bRunLimited)
if(false == LimitRights(settings.hUser))
return false;
if(settings.bRunElevated)
if(false == ElevateUserToken(settings.hUser))
return false;
CString user, domain;
GetUserDomain(settings.user, user, domain);
#ifdef _DEBUG
Log(StrFormat(L"DEBUG: U:%s D:%s P:%s bP:%d Env:%s WD:%s",
user, domain, settings.password, settings.bDontLoadProfile,
pEnvironment ? L"true" : L"null", startingDir ? startingDir : L"null"), false);
#endif
BOOL bLaunched = FALSE;
if(settings.bUseSystemAccount)
{
Log(StrFormat(L"PAExec starting process [%s] as Local System", path), false);
if(BAD_HANDLE(settings.hUser))
Log(L"Have bad user handle", true);
EnablePrivilege(SE_IMPERSONATE_NAME);
BOOL bImpersonated = ImpersonateLoggedOnUser(settings.hUser);
if(FALSE == bImpersonated)
{
Log(L"Failed to impersonate", GetLastError());
_ASSERT(bImpersonated);
}
EnablePrivilege(SE_ASSIGNPRIMARYTOKEN_NAME);
EnablePrivilege(SE_INCREASE_QUOTA_NAME);
bLaunched = CreateProcessAsUser(settings.hUser, NULL, path.LockBuffer(), NULL, NULL, TRUE, dwFlags, pEnvironment, startingDir, &si, &pi);
launchGLE = GetLastError();
path.UnlockBuffer();
#ifdef _DEBUG
if(0 != launchGLE)
Log(StrFormat(L"Launch (launchGLE=%u) params: user=[x%X] path=[%s] flags=[x%X], pEnv=[%s], dir=[%s], stdin=[x%X], stdout=[x%X], stderr=[x%X]",
launchGLE, (DWORD)settings.hUser, path, dwFlags, pEnvironment ? L"{env}" : L"{null}", startingDir ? startingDir : L"{null}",
(DWORD)si.hStdInput, (DWORD)si.hStdOutput, (DWORD)si.hStdError), false);
#endif
RevertToSelf();
}
else
{
if(FALSE == settings.user.IsEmpty()) //launching as a specific user
{
Log(StrFormat(L"PAExec starting process [%s] as %s", path, settings.user), false);
if(false == settings.bRunLimited)
{
bLaunched = CreateProcessWithLogonW(user, domain.IsEmpty() ? NULL : domain, settings.password, settings.bDontLoadProfile ? 0 : LOGON_WITH_PROFILE, NULL, path.LockBuffer(), dwFlags, pEnvironment, startingDir, &si, &pi);
launchGLE = GetLastError();
path.UnlockBuffer();
#ifdef _DEBUG
if(0 != launchGLE)
Log(StrFormat(L"Launch (launchGLE=%u) params: user=[%s] domain=[%s] prof=[x%X] path=[%s] flags=[x%X], pEnv=[%s], dir=[%s], stdin=[x%X], stdout=[x%X], stderr=[x%X]",
launchGLE, user, domain, settings.bDontLoadProfile ? 0 : LOGON_WITH_PROFILE,
path, dwFlags, pEnvironment ? L"{env}" : L"{null}", startingDir ? startingDir : L"{null}",
(DWORD)si.hStdInput, (DWORD)si.hStdOutput, (DWORD)si.hStdError), false);
#endif
}
else
bLaunched = FALSE; //force to run with CreateProcessAsUser so rights can be limited
//CreateProcessWithLogonW can't be called from LocalSystem on Win2003 and earlier, so LogonUser/CreateProcessAsUser must be used. Might as well try for everyone
if((FALSE == bLaunched) && !BAD_HANDLE(settings.hUser))
{
#ifdef _DEBUG
Log(L"DEBUG: Failed CreateProcessWithLogonW - trying CreateProcessAsUser", GetLastError());
#endif
EnablePrivilege(SE_ASSIGNPRIMARYTOKEN_NAME);
EnablePrivilege(SE_INCREASE_QUOTA_NAME);
EnablePrivilege(SE_IMPERSONATE_NAME);
BOOL bImpersonated = ImpersonateLoggedOnUser(settings.hUser);
if(FALSE == bImpersonated)
{
Log(L"Failed to impersonate", GetLastError());
_ASSERT(bImpersonated);
}
bLaunched = CreateProcessAsUser(settings.hUser, NULL, path.LockBuffer(), NULL, NULL, TRUE, CREATE_SUSPENDED | CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_CONSOLE, pEnvironment, startingDir, &si, &pi);
if(0 == GetLastError())
launchGLE = 0; //mark as successful, otherwise return our original error
path.UnlockBuffer();
#ifdef _DEBUG
if(0 != launchGLE)
Log(StrFormat(L"Launch (launchGLE=%u) params: user=[x%X] path=[%s] pEnv=[%s], dir=[%s], stdin=[x%X], stdout=[x%X], stderr=[x%X]",
launchGLE, (DWORD)settings.hUser, path, pEnvironment ? L"{env}" : L"{null}", startingDir ? startingDir : L"{null}",
(DWORD)si.hStdInput, (DWORD)si.hStdOutput, (DWORD)si.hStdError), false);
#endif
RevertToSelf();
}
}
else
{
Log(StrFormat(L"PAExec starting process [%s] as current user", path), false);
EnablePrivilege(SE_ASSIGNPRIMARYTOKEN_NAME);
EnablePrivilege(SE_INCREASE_QUOTA_NAME);
EnablePrivilege(SE_IMPERSONATE_NAME);
if(NULL != settings.hUser)
bLaunched = CreateProcessAsUser(settings.hUser, NULL, path.LockBuffer(), NULL, NULL, TRUE, dwFlags, pEnvironment, startingDir, &si, &pi);
if(FALSE == bLaunched)
bLaunched = CreateProcess(NULL, path.LockBuffer(), NULL, NULL, TRUE, dwFlags, pEnvironment, startingDir, &si, &pi);
launchGLE = GetLastError();
//#ifdef _DEBUG
if(0 != launchGLE)
Log(StrFormat(L"Launch (launchGLE=%u) params: path=[%s] user=[%s], pEnv=[%s], dir=[%s], stdin=[x%X], stdout=[x%X], stderr=[x%X]",
launchGLE, path, settings.hUser ? L"{non-null}" : L"{null}", pEnvironment ? L"{env}" : L"{null}", startingDir ? startingDir : L"{null}",
(DWORD)si.hStdInput, (DWORD)si.hStdOutput, (DWORD)si.hStdError), false);
//#endif
path.UnlockBuffer();
}
}
if(bLaunched)
{
if(gbInService)
Log(L"Successfully launched", false);
settings.hProcess = pi.hProcess;
settings.processID = pi.dwProcessId;
if(false == settings.allowedProcessors.empty())
{
DWORD sysMask = 0, procMask = 0;
VERIFY(GetProcessAffinityMask(pi.hProcess, &procMask, &sysMask));
procMask = 0;
for(std::vector<WORD>::iterator itr = settings.allowedProcessors.begin(); settings.allowedProcessors.end() != itr; itr++)
{
DWORD bit = 1;
bit = bit << (*itr - 1);
procMask |= bit & sysMask;
}
VERIFY(SetProcessAffinityMask(pi.hProcess, procMask));
}
VERIFY(SetPriorityClass(pi.hProcess, settings.priority));
ResumeThread(pi.hThread);
VERIFY(CloseHandle(pi.hThread));
}
else
{
Log(StrFormat(L"Failed to start %s.", path), launchGLE);
if((ERROR_ELEVATION_REQUIRED == launchGLE) && (false == gbInService))
Log(L"HINT: PAExec probably needs to be \"Run As Administrator\"", false);
}
if(ci.bPreped)
CleanUpInteractiveProcess(&ci);
if(settings.bDisableFileRedirection)
RevertFileRedirection();
if(NULL != pEnvironment)
DestroyEnvironmentBlock(pEnvironment);
pEnvironment = NULL;
if(bLoadedProfile)
UnloadUserProfile(settings.hUser, profile.hProfile);
if(!BAD_HANDLE(settings.hUser))
{
CloseHandle(settings.hUser);
settings.hUser = NULL;
}
return bLaunched ? true : false;
}
CString GetTokenUserSID(HANDLE hToken)
{
DWORD tmp = 0;
CString userName;
DWORD sidNameSize = 64;
std::vector<WCHAR> sidName;
sidName.resize(sidNameSize);
DWORD sidDomainSize = 64;
std::vector<WCHAR> sidDomain;
sidDomain.resize(sidNameSize);
DWORD userTokenSize = 1024;
std::vector<WCHAR> tokenUserBuf;
tokenUserBuf.resize(userTokenSize);
TOKEN_USER *userToken = (TOKEN_USER*)&tokenUserBuf.front();
if(GetTokenInformation(hToken, TokenUser, userToken, userTokenSize, &tmp))
{
WCHAR *pSidString = NULL;
if(ConvertSidToStringSid(userToken->User.Sid, &pSidString))
userName = pSidString;
if(NULL != pSidString)
LocalFree(pSidString);
}
else
_ASSERT(0);
return userName;
}
HANDLE GetLocalSystemProcessToken()
{
DWORD pids[1024*10] = {0}, cbNeeded = 0, cProcesses = 0;
if ( !EnumProcesses(pids, sizeof(pids), &cbNeeded))
{
Log(L"Can't enumProcesses - Failed to get token for Local System.", true);
return NULL;
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
for(DWORD i = 0; i<cProcesses; ++i)
{
DWORD gle = 0;
DWORD dwPid = pids[i];
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPid);
if (hProcess)
{
HANDLE hToken = 0;
if (OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_READ | TOKEN_IMPERSONATE | TOKEN_QUERY_SOURCE | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY | TOKEN_EXECUTE, &hToken))
{
try
{
CString name = GetTokenUserSID(hToken);
//const wchar_t arg[] = L"NT AUTHORITY\\";
//if(0 == _wcsnicmp(name, arg, sizeof(arg)/sizeof(arg[0])-1))
if(name == L"S-1-5-18") //Well known SID for Local System
{
CloseHandle(hProcess);
return hToken;
}
}
catch(...)
{
_ASSERT(0);
}
}
else
gle = GetLastError();
CloseHandle(hToken);
}
else
gle = GetLastError();
CloseHandle(hProcess);
}
Log(L"Failed to get token for Local System.", true);
return NULL;
}
typedef BOOL (WINAPI *SaferCreateLevelProc)(DWORD dwScopeId, DWORD dwLevelId, DWORD OpenFlags, SAFER_LEVEL_HANDLE* pLevelHandle, LPVOID lpReserved);
typedef BOOL (WINAPI *SaferComputeTokenFromLevelProc)(SAFER_LEVEL_HANDLE LevelHandle, HANDLE InAccessToken, PHANDLE OutAccessToken, DWORD dwFlags, LPVOID lpReserved);
typedef BOOL (WINAPI *SaferCloseLevelProc)(SAFER_LEVEL_HANDLE hLevelHandle);
bool LimitRights(HANDLE& hUser)
{
DWORD gle = 0;
static SaferCreateLevelProc gSaferCreateLevel = NULL;
static SaferComputeTokenFromLevelProc gSaferComputeTokenFromLevel = NULL;
static SaferCloseLevelProc gSaferCloseLevel = NULL;
if((NULL == gSaferCloseLevel) || (NULL == gSaferComputeTokenFromLevel) || (NULL == gSaferCreateLevel))
{
HMODULE hMod = LoadLibrary(L"advapi32.dll"); //GLOK
if(NULL != hMod)
{
gSaferCreateLevel = (SaferCreateLevelProc)GetProcAddress(hMod, "SaferCreateLevel");
gSaferComputeTokenFromLevel = (SaferComputeTokenFromLevelProc)GetProcAddress(hMod, "SaferComputeTokenFromLevel");
gSaferCloseLevel = (SaferCloseLevelProc)GetProcAddress(hMod, "SaferCloseLevel");
}
}
if((NULL == gSaferCloseLevel) || (NULL == gSaferComputeTokenFromLevel) || (NULL == gSaferCreateLevel))
{
Log(L"Safer... calls not supported on this OS -- can't limit rights", true);
return false;
}
if(!BAD_HANDLE(hUser))
{
HANDLE hNew = NULL;
SAFER_LEVEL_HANDLE safer = NULL;
if(FALSE == gSaferCreateLevel(SAFER_SCOPEID_USER, SAFER_LEVELID_NORMALUSER, SAFER_LEVEL_OPEN, &safer, NULL))
{
gle = GetLastError();
Log(L"Failed to limit rights (SaferCreateLevel).", gle);
return false;
}
if(NULL != safer)
{
if(FALSE == gSaferComputeTokenFromLevel(safer, hUser, &hNew, 0, NULL))
{
gle = GetLastError();
Log(L"Failed to limit rights (SaferComputeTokenFromLevel).", gle);
VERIFY(gSaferCloseLevel(safer));
return false;
}
VERIFY(gSaferCloseLevel(safer));
}
//if(BAD_HANDLE(hNew)) //try a second approach?
//{
// if(FALSE == CreateRestrictedToken(hUser, DISABLE_MAX_PRIVILEGE, 0, 0, 0, 0, 0, 0, &hNew))
// {
// gle = GetLastError();
// Log(L"Failed to limit rights (CreateRestrictedToken).", gle);
// }
//}
if(!BAD_HANDLE(hNew))
{
VERIFY(CloseHandle(hUser));
hUser = hNew;
Duplicate(hUser, __FILE__, __LINE__);
return true;
}
}
Log(L"Don't have a good user -- can't limit rights", true);
return false;
}
bool ElevateUserToken(HANDLE& hEnvUser)
{
TOKEN_ELEVATION_TYPE tet;
DWORD needed = 0;
DWORD gle = 0;
if(GetTokenInformation(hEnvUser, TokenElevationType, (LPVOID)&tet, sizeof(tet), &needed))
{
if(tet == TokenElevationTypeLimited)
{
//get the associated token, which is the full-admin token
TOKEN_LINKED_TOKEN tlt = {0};
if(GetTokenInformation(hEnvUser, TokenLinkedToken, (LPVOID)&tlt, sizeof(tlt), &needed))
{
Duplicate(tlt.LinkedToken, __FILE__, __LINE__);
hEnvUser = tlt.LinkedToken;
return true;
}
else
{
gle = GetLastError();
Log(L"Failed to get elevated token", gle);
return false;
}
}
else
return true;
}
else
{
//can't tell if it's elevated or not -- continue anyway
gle = GetLastError();
switch(gle)
{
case ERROR_INVALID_PARAMETER: //expected on 32-bit XP
case ERROR_INVALID_FUNCTION: //expected on 64-bit XP
break;
default:
Log(L"Can't query token to run elevated - continuing anyway", gle);
break;
}
return true;
}
}