@@ -100,11 +100,11 @@ inline auto IsSystemProcessName(std::wstring_view name) {
100100}
101101
102102constexpr DWORD INVALID_PROCESS_ID = 0xFFFFFFFF ;
103- DWORD LookupSystemProcess ( ) {
103+ bool SearchSystemProcess (std::vector<DWORD> &pss ) {
104104 PWTS_PROCESS_INFOW pi{nullptr };
105105 DWORD count{0 };
106106 if (::WTSEnumerateProcessesW (WTS_CURRENT_SERVER_HANDLE, 0 , 1 , &pi, &count) != TRUE ) {
107- return INVALID_PROCESS_ID ;
107+ return false ;
108108 }
109109 auto closer = bela::finally ([&] {
110110 if (pi != nullptr ) {
@@ -115,10 +115,10 @@ DWORD LookupSystemProcess() {
115115 for (auto it = pi; it != end; it++) {
116116 if (it->SessionId == 0 && IsSystemProcessName (it->pProcessName ) &&
117117 IsWellKnownSid (it->pUserSid , WinLocalSystemSid) == TRUE ) {
118- return it->ProcessId ;
118+ pss. emplace_back ( it->ProcessId ) ;
119119 }
120120 }
121- return INVALID_PROCESS_ID ;
121+ return !pss. empty () ;
122122}
123123
124124// Get Current Process SessionID and Enable SeDebugPrivilege
@@ -214,24 +214,24 @@ bool privileges_view_enabled(HANDLE hToken, const privilege_entries *pv) {
214214 return true ;
215215}
216216
217- bool Elavator::impersonation_system_token (bela::error_code &ec) {
217+ bool Elavator::impersonation_system_token (DWORD systemProcessId, bela::error_code &ec) {
218218 HANDLE hExistingToken = INVALID_HANDLE_VALUE;
219219 auto hProcess = ::OpenProcess (MAXIMUM_ALLOWED, FALSE , systemProcessId);
220220 if (hProcess == INVALID_HANDLE_VALUE) {
221221 ec = bela::make_system_error_code (
222- bela::StringCat (L" Elavator:: impersonation_system_token<OpenProcess> " , systemProcessId, L" " ));
222+ bela::StringCat (L" impersonation_system_token<OpenProcess> " , systemProcessId, L" " ));
223223 return false ;
224224 }
225225 auto hpdeleter = bela::finally ([&] { CloseHandle (hProcess); });
226226 if (OpenProcessToken (hProcess, MAXIMUM_ALLOWED, &hExistingToken) != TRUE ) {
227227 ec = bela::make_system_error_code (
228- bela::StringCat (L" Elavator:: impersonation_system_token<OpenProcessToken> " , systemProcessId, L" " ));
228+ bela::StringCat (L" impersonation_system_token<OpenProcessToken> " , systemProcessId, L" " ));
229229 return false ;
230230 }
231231 auto htdeleter = bela::finally ([&] { CloseHandle (hExistingToken); });
232232 if (DuplicateTokenEx (hExistingToken, MAXIMUM_ALLOWED, nullptr , SecurityImpersonation, TokenImpersonation, &hToken) !=
233233 TRUE ) {
234- ec = bela::make_system_error_code (L" Elavator:: impersonation_system_token<DuplicateTokenEx> " );
234+ ec = bela::make_system_error_code (L" impersonation_system_token<DuplicateTokenEx> " );
235235 return false ;
236236 }
237237 return true ;
@@ -242,20 +242,31 @@ bool Elavator::ImpersonationSystemPrivilege(const privilege_entries *pv, bela::e
242242 if (!EnableSeDebugPrivilege (currentSessionId, ec)) {
243243 return false ;
244244 }
245- if (systemProcessId = LookupSystemProcess (); systemProcessId == INVALID_PROCESS_ID ) {
246- ec = bela::make_error_code (1 , L" Elevator:: ImpersonationSystemPrivilege unable lookup system process pid " );
245+ if (! SearchSystemProcess (systemProcesses) ) {
246+ ec = bela::make_error_code (1 , L" ImpersonationSystemPrivilege search system process error " );
247247 return false ;
248248 }
249- if (!impersonation_system_token (ec)) {
249+
250+ auto impersonation_system_token_all = [&]() -> bool {
251+ for (auto pid : systemProcesses) {
252+ if (impersonation_system_token (pid, ec)) {
253+ return true ;
254+ }
255+ }
256+ return false ;
257+ };
258+
259+ if (!impersonation_system_token_all ()) {
250260 return false ;
251261 }
262+
252263 if (!privileges_view_enabled (hToken, pv)) {
253- ec = bela::make_error_code (1 , L" Elevator::ImpersonationSystemPrivilege unable enable privileges: " ,
264+ ec = bela::make_error_code (1 , L" ImpersonationSystemPrivilegeunable enable privileges: " ,
254265 pv == nullptr ? L" all" : pv->format ());
255266 return false ;
256267 }
257268 if (SetThreadToken (nullptr , hToken) != TRUE ) {
258- ec = bela::make_error_code (1 , L" Elevator:: ImpersonationSystemPrivilege<SetThreadToken> " );
269+ ec = bela::make_error_code (1 , L" ImpersonationSystemPrivilege<SetThreadToken> " );
259270 return false ;
260271 }
261272 return true ;
0 commit comments