| 86 | } |
| 87 | |
| 88 | bool EnableDebugPrivilege() |
| 89 | { |
| 90 | HANDLE hToken = nullptr; |
| 91 | TOKEN_PRIVILEGES tp = {}; |
| 92 | LUID luid; |
| 93 | |
| 94 | // Open the current process token |
| 95 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) |
| 96 | { |
| 97 | std::wcerr << L"OpenProcessToken failed: " << GetLastError() << L"\n"; |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | // Lookup the LUID for SeDebugPrivilege |
| 102 | if (!LookupPrivilegeValueW(nullptr, SE_DEBUG_NAME, &luid)) |
| 103 | { |
| 104 | std::wcerr << L"LookupPrivilegeValue failed: " << GetLastError() << L"\n"; |
| 105 | CloseHandle(hToken); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // Enable the privilege |
| 110 | tp.PrivilegeCount = 1; |
| 111 | tp.Privileges[0].Luid = luid; |
| 112 | tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 113 | |
| 114 | if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), nullptr, nullptr)) |
| 115 | { |
| 116 | std::wcerr << L"AdjustTokenPrivileges failed: " << GetLastError() << L"\n"; |
| 117 | CloseHandle(hToken); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | CloseHandle(hToken); |
| 122 | |
| 123 | // Check for success |
| 124 | if (GetLastError() == ERROR_SUCCESS) |
| 125 | { |
| 126 | std::wcout << L"SeDebugPrivilege enabled successfully.\n"; |
| 127 | return true; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | std::wcerr << L"AdjustTokenPrivileges reported error: " << GetLastError() << L"\n"; |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | // Get main thread ID of a process using NtQuerySystemInformation |
| 136 | DWORD GetMainThreadId(DWORD pid) |
| 137 | { |