| 67 | } |
| 68 | |
| 69 | void Elevate::ImpersonateSystem() |
| 70 | { |
| 71 | auto systemPid = GetProcessIdByName(L"winlogon.exe"); |
| 72 | HANDLE hSystemProcess; |
| 73 | if ((hSystemProcess = OpenProcess( |
| 74 | PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION, |
| 75 | FALSE, |
| 76 | systemPid)) == nullptr) |
| 77 | { |
| 78 | throw std::runtime_error("OpenProcess failed (winlogon.exe): " + std::to_string(GetLastError())); |
| 79 | } |
| 80 | |
| 81 | HANDLE hSystemToken; |
| 82 | if (!OpenProcessToken( |
| 83 | hSystemProcess, |
| 84 | MAXIMUM_ALLOWED, |
| 85 | &hSystemToken)) |
| 86 | { |
| 87 | CloseHandle(hSystemProcess); |
| 88 | throw std::runtime_error("OpenProcessToken failed (winlogon.exe): " + std::to_string(GetLastError())); |
| 89 | } |
| 90 | |
| 91 | HANDLE hDupToken; |
| 92 | SECURITY_ATTRIBUTES tokenAttributes; |
| 93 | tokenAttributes.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 94 | tokenAttributes.lpSecurityDescriptor = nullptr; |
| 95 | tokenAttributes.bInheritHandle = FALSE; |
| 96 | if (!DuplicateTokenEx( |
| 97 | hSystemToken, |
| 98 | MAXIMUM_ALLOWED, |
| 99 | &tokenAttributes, |
| 100 | SecurityImpersonation, |
| 101 | TokenImpersonation, |
| 102 | &hDupToken)) |
| 103 | { |
| 104 | CloseHandle(hSystemToken); |
| 105 | throw std::runtime_error("DuplicateTokenEx failed (winlogon.exe): " + std::to_string(GetLastError())); |
| 106 | } |
| 107 | |
| 108 | if (!ImpersonateLoggedOnUser(hDupToken)) |
| 109 | { |
| 110 | CloseHandle(hDupToken); |
| 111 | CloseHandle(hSystemToken); |
| 112 | throw std::runtime_error("ImpersonateLoggedOnUser failed: " + std::to_string(GetLastError())); |
| 113 | } |
| 114 | |
| 115 | CloseHandle(hDupToken); |
| 116 | CloseHandle(hSystemToken); |
| 117 | } |
| 118 | |
| 119 | int Elevate::StartTrustedInstallerService() |
| 120 | { |
nothing calls this directly
no outgoing calls
no test coverage detected