简体   繁体   English

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

[英]How to recover privileges with GetTokenInformation () ? c++

I would like to recover all privileges from a username. 我想从用户名中恢复所有特权。 For example privileges : "SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE", "SE_AUDIT_PRIVILEGE", "SE_DEBUG_PRIVILEGE"... I searched on the microsoft documentation and I found GetTokenInformation() https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-gettokeninformation but I do not understand how to access all the privileges and see the value of this privilege. 例如特权:“ SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE”,“ SE_AUDIT_PRIVILEGE”,“ SE_DEBUG_PRIVILEGE” ...我在Microsoft文档中进行搜索,发现GetTokenInformation() https://docs.microsoft.com/zh-cn/windows/desktop/api/ securitybaseapi / nf-securitybaseapi-gettokeninformation信息,但我不了解如何访问所有特权并查看此特权的值。 Would anyone already use this method with an example or how to proceed please? 有人会已经使用此方法作为示例或如何进行吗?

You could try the code below: 您可以尝试以下代码:

#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;
}

And then call the SetPrivilege (not the win32 api but the function from the MSDN example) 然后调用SetPrivilege (不是win32 api,而是MSDN示例中的函数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM