@@ -24,98 +24,48 @@ SOFTWARE.
2424
2525namespace {
2626
27- typedef BOOL (WINAPI *OpenProcessTokenProc)(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle);
28- typedef BOOL (WINAPI *GetTokenInformationProc)(HANDLE TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, LPVOID TokenInformation, DWORD TokenInformationLength, DWORD *ReturnLength);
29- typedef BOOL (WINAPI *LookupPrivilegeValueAProc)(LPCSTR lpSystemName, LPCSTR lpName, PLUID lpLuid);
30- typedef BOOL (WINAPI *AdjustTokenPrivilegesProc)(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength);
31-
32- struct Advapi {
33- HMODULE HModule;
34- OpenProcessTokenProc OpenProcessToken;
35- GetTokenInformationProc GetTokenInformation;
36- LookupPrivilegeValueAProc LookupPrivilegeValueA;
37- AdjustTokenPrivilegesProc AdjustTokenPrivileges;
38-
39- Advapi ()
40- : HModule(NULL )
41- {
42- }
43-
44- ~Advapi ()
45- {
46- if (HModule != NULL ) {
47- FreeLibrary (HModule);
48- }
27+ bool EnableDebugPrivilege ()
28+ {
29+ auto hmodule = LoadLibraryA (" advapi32.dll" );
30+ auto pOpenProcessToken = (decltype (&OpenProcessToken)) GetProcAddress (hmodule, " OpenProcessToken" );
31+ auto pGetTokenInformation = (decltype (&GetTokenInformation)) GetProcAddress (hmodule, " GetTokenInformation" );
32+ auto pLookupPrivilegeValue = (decltype (&LookupPrivilegeValueA)) GetProcAddress (hmodule, " LookupPrivilegeValueA" );
33+ auto pAdjustTokenPrivileges = (decltype (&AdjustTokenPrivileges)) GetProcAddress (hmodule, " AdjustTokenPrivileges" );
34+ if (pOpenProcessToken == nullptr ||
35+ pGetTokenInformation == nullptr ||
36+ pLookupPrivilegeValue == nullptr ||
37+ pAdjustTokenPrivileges == nullptr ) {
38+ FreeLibrary (hmodule);
39+ return false ;
4940 }
5041
51- bool Load ()
52- {
53- HModule = LoadLibraryA (" advapi32.dll" );
54- if (HModule == NULL ) {
55- return false ;
56- }
57-
58- OpenProcessToken = (OpenProcessTokenProc) GetProcAddress (HModule, " OpenProcessToken" );
59- GetTokenInformation = (GetTokenInformationProc) GetProcAddress (HModule, " GetTokenInformation" );
60- LookupPrivilegeValueA = (LookupPrivilegeValueAProc) GetProcAddress (HModule, " LookupPrivilegeValueA" );
61- AdjustTokenPrivileges = (AdjustTokenPrivilegesProc) GetProcAddress (HModule, " AdjustTokenPrivileges" );
62-
63- if (OpenProcessToken == nullptr ||
64- GetTokenInformation == nullptr ||
65- LookupPrivilegeValueA == nullptr ||
66- AdjustTokenPrivileges == nullptr ) {
67- FreeLibrary (HModule);
68- HModule = NULL ;
69- return false ;
70- }
71-
72- return true ;
42+ HANDLE hToken = NULL ;
43+ if (pOpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken) == 0 ) {
44+ FreeLibrary (hmodule);
45+ return false ;
7346 }
7447
75- bool HasElevatedPrivilege () const
76- {
77- HANDLE hToken = NULL ;
78- if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hToken)) {
79- return false ;
80- }
81-
82- /* * BEGIN WORKAROUND: struct TOKEN_ELEVATION and enum value TokenElevation
83- * are not defined in the vs2003 headers, so we reproduce them here. **/
84- enum { WA_TokenElevation = 20 };
85- DWORD TokenIsElevated = 0 ;
86- /* * END WA **/
87-
88- DWORD dwSize = 0 ;
89- if (!GetTokenInformation (hToken, (TOKEN_INFORMATION_CLASS) WA_TokenElevation, &TokenIsElevated, sizeof (TokenIsElevated), &dwSize)) {
90- TokenIsElevated = 0 ;
91- }
48+ // Try to enable required privilege
49+ TOKEN_PRIVILEGES tp = {};
50+ tp.PrivilegeCount = 1 ;
51+ tp.Privileges [0 ].Attributes = SE_PRIVILEGE_ENABLED;
9252
53+ if (pLookupPrivilegeValue (NULL , " SeDebugPrivilege" , &tp.Privileges [0 ].Luid ) == 0 ) {
9354 CloseHandle (hToken);
94-
95- return TokenIsElevated != 0 ;
55+ FreeLibrary (hmodule);
56+ return false ;
9657 }
9758
98- bool EnableDebugPrivilege () const
99- {
100- HANDLE hToken = NULL ;
101- if (!OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
102- return false ;
103- }
104-
105- TOKEN_PRIVILEGES tp = {};
106- tp.PrivilegeCount = 1 ;
107- tp.Privileges [0 ].Attributes = SE_PRIVILEGE_ENABLED;
108-
109- bool enabled =
110- LookupPrivilegeValueA (NULL , " SeDebugPrivilege" , &tp.Privileges [0 ].Luid ) &&
111- AdjustTokenPrivileges (hToken, FALSE , &tp, sizeof (TOKEN_PRIVILEGES), nullptr , nullptr ) &&
112- GetLastError () != ERROR_NOT_ALL_ASSIGNED;
59+ auto adjustResult = pAdjustTokenPrivileges (hToken, FALSE , &tp, sizeof (TOKEN_PRIVILEGES), nullptr , nullptr );
60+ auto adjustError = GetLastError ();
11361
114- CloseHandle (hToken);
62+ CloseHandle (hToken);
63+ FreeLibrary (hmodule);
11564
116- return enabled;
117- }
118- };
65+ return
66+ adjustResult != 0 &&
67+ adjustError != ERROR_NOT_ALL_ASSIGNED;
68+ }
11969
12070int RestartAsAdministrator (
12171 int argc,
@@ -205,8 +155,7 @@ void ElevatePrivilege(int argc, char** argv)
205155 }
206156
207157 // Try to load advapi to check and set required privilege.
208- Advapi advapi;
209- if (advapi.Load () && advapi.EnableDebugPrivilege ()) {
158+ if (EnableDebugPrivilege ()) {
210159 return ;
211160 }
212161
0 commit comments