-
Notifications
You must be signed in to change notification settings - Fork 76
/
SetPrivilege.cpp
39 lines (33 loc) · 1.2 KB
/
SetPrivilege.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
#include "common.hpp"
bool SetPrivilege(
_In_ HANDLE token, // won't be using RAII as we don't need the HANDLE to get closed after the function returns
_In_ std::wstring privilege,
_In_ bool enableDisable
)
{
TOKEN_PRIVILEGES tp{};
LUID luid;
auto success = LookupPrivilegeValueW(NULL, privilege.c_str(), &luid);
if (!success)
{
Error(GetLastError());
std::wcout << L"[-] The call to LookupPrivilegeValueW failed...\n";
return false;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (enableDisable)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
success = AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL);
auto result = GetLastError(); // GetLastError tells us if the privilege has been successfully assigned
if (result != ERROR_SUCCESS)
{
Error(GetLastError());
std::wcout << L"[-] Couldn't add privilege " << privilege << L" to the current token...\n";
return false;
}
std::wcout << L"[+] Privilege " << privilege << L" added to the current token!\n";
return true;
}