如何使用GetTokenInformation()恢复特权? c ++

时间:2019-05-08 14:57:11

标签: c++ winapi window access-token

我想从用户名中恢复所有特权。例如特权:“ SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE”,“ SE_AUDIT_PRIVILEGE”,“ SE_DEBUG_PRIVILEGE” ...我在Microsoft文档中进行搜索,发现GetTokenInformation() https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-gettokeninformation,但我不了解如何访问所有特权并查看此特权的值。 有人会使用这种方法作为示例还是如何进行?

1 个答案:

答案 0 :(得分:2)

您可以尝试以下代码:

#include <iostream>
#include <windows.h>
#include <tchar.h>

BOOL CheckWindowsPrivilege(const TCHAR *Privilege)
{
    /* Checks for Privilege and returns True or False. */
    LUID luid;
    PRIVILEGE_SET privs;
    HANDLE hProcess;
    HANDLE hToken;
    hProcess = GetCurrentProcess();
    if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hToken)) return FALSE;
    if (!LookupPrivilegeValue(NULL, Privilege, &luid)) return FALSE;
    privs.PrivilegeCount = 1;
    privs.Control = PRIVILEGE_SET_ALL_NECESSARY;
    privs.Privilege[0].Luid = luid;
    privs.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
    BOOL bResult;
    PrivilegeCheck(hToken, &privs, &bResult);
    return bResult;
}

int wmain(void)
{
    if (!CheckWindowsPrivilege(SE_ASSIGNPRIMARYTOKEN_NAME))
    {
        wprintf(L"I do not have SeAssignPrimaryTokenPrivilege!\n");
        return 1;
    }
    wprintf(L"I do have SeAssignPrimaryTokenPrivilege!\n");
    return 0;
}

然后调用SetPrivilege(不是win32 api,而是MSDN示例中的函数)