zoukankan      html  css  js  c++  java
  • win32

    检查当前句柄是否有指定的权限。

    #include <iostream>
    #include <windows.h>
    #include <tchar.h>
    //#pragma comment(lib, "cmcfg32.lib")
    
    HANDLE hProcess;
    
    BOOL CheckWindowsPrivilege(const TCHAR* Privilege)
    {
        /* Checks for Privilege and returns True or False. */
        LUID luid;
        PRIVILEGE_SET privs;
        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 main(void)
    {
        if (!CheckWindowsPrivilege(SE_ASSIGNPRIMARYTOKEN_NAME))
        {
            wprintf(L"I do not have SeAssignPrimaryTokenPrivilege!
    ");
            return 1;
        }
    
        wprintf(L"I do have SeAssignPrimaryTokenPrivilege!
    ");
        return 0;
    }

    打印当前句柄所有的权限。

    void print_privileges(HANDLE hToken)
    {
        DWORD size = 0;
        if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &size) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
            PTOKEN_PRIVILEGES tp = (PTOKEN_PRIVILEGES)malloc(size);
            if (tp != NULL && GetTokenInformation(hToken, TokenPrivileges, tp, size, &size)) {
                size_t i;
                for (i = 0; i < tp->PrivilegeCount; ++i) {
                    char name[64] = "?";
                    DWORD name_size = sizeof name;
                    LookupPrivilegeNameA(0, &tp->Privileges[i].Luid, name, &name_size);
                    PRIVILEGE_SET ps = {
                        1, PRIVILEGE_SET_ALL_NECESSARY, {
                            { { tp->Privileges[i].Luid.LowPart, tp->Privileges[i].Luid.HighPart } }
                        }
                    };
                    BOOL fResult;
                    PrivilegeCheck(hToken, &ps, &fResult);
                    printf("%-*s %s
    ", 32, name, fResult ? "Enabled" : "Disabled");
                }
            }
            free(tp);
        }
    }

    启用或禁用特定的权限。

    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
    ", 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
    ", GetLastError());
            return FALSE;
        }
    
        if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
    
        {
            printf("The token does not have the specified privilege. 
    ");
            return FALSE;
        }
    
        return TRUE;
    }

    相关文档:Enabling and Disabling Privileges in C++

  • 相关阅读:
    友盟—安卓巴士【Android开发原创教程大赛】
    iOS开发视频教程下载/iphone开发视频教程下载
    发一个Android开发的外包项目。欢迎外包团队来骚扰
    安卓巴士总结了近百个Android优秀开源项目,覆盖Android开发的每个领域
    安卓巴士精选Android开发教程
    做了一个系列的Android开发教程列表
    深刻理解C#的传值调用和传引用调用
    《CLR Via C# 第3版》笔记之(四) 类中字段的默认赋值
    《CLR Via C# 第3版》笔记之(七) const和readonly
    《CLR Via C# 第3版》笔记之(十二) 事件
  • 原文地址:https://www.cnblogs.com/strive-sun/p/14168083.html
Copyright © 2011-2022 走看看