I am trying to give my C++ program access to open another process with all rights. To do this, I need to give my current process SE_DEBUG_NAME
.
HANDLE hProc = GetCurrentProcess();
HANDLE hToken = NULL;
DWORD pid;
if (!OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
printf("Failed to open access token\n");
if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE))
printf("Failed to set debug privilege\n");
I took the privilege activation function from MSDN:
BOOL SetPrivilege(
HANDLE hToken, // access token handle
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
BOOL bEnablePrivilege // to enable or disable privilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if ( !LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid ) ) // receives LUID of privilege
{
printf("LookupPrivilegeValue error: %u\n", GetLastError() );
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
// Enable the privilege or disable all privileges.
if ( !AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES) NULL,
(PDWORD) NULL) )
{
printf("AdjustTokenPrivileges error: %u\n", GetLastError() );
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
printf("The token does not have the specified privilege. \n");
return FALSE;
}
return TRUE;
}
In my case, an error ERROR_NOT_ALL_ASSIGNED
occurs.
I used the GetTokenInformation()
function, and realized that I don't have SE_DEBUG_NAME
.
I run the program itself through Visual Studio Code. The UAC execution level requires administrator rights when the program is started.
As a result, I do not understand how I can give the SE_DEBUG_NAME
privilege to my process.
I'm using a laptop, and a Windows 10 version. I can't set up a local security policy because I have a laptop with windows home on it .
The problem was that the user did not have the SE_DEBUG_NAME privilege. On my laptop, I did not find the security settings in the windows control panel. As I read, this could be due to the fact that I do not have a professional version of windows.
In general, I solved the problem by finding a script, with the help of which I provided the SE_DEBUG_NAME privilege to the user I needed.