VC++ Windows提升权限

头文件声明:

#pragma once

#if !defined SW_WINDOW_PRIVILEGE
#define SW_WINDOW_PRIVILEGE

#include <Windows.h>

// Windows提权类
class CSWWindowPrivilege
{
public:
    CSWWindowPrivilege(void);
    ~CSWWindowPrivilege(void);
    
    // 提升关机权限
    static BOOL EnableShutdownPrivileges(BOOL fEnable);
    // 关机
    static VOID Shutdown();
    // 重启
    static VOID Reboot();

    // 提升结束进程的权限
    static BOOL EnableTermProcessPrivileges(BOOL fEnable);
    // 提升加载驱动的权限
    static BOOL EnableLoadDriverPrivileges(BOOL fEnable);
    // 提升管理员的权限
    static BOOL EnableDebugPrivilege(BOOL fEnable);
};

#endif

源码实现:

#include "StdAfx.h"
#include "SWWindowPrivilege.h"


CSWWindowPrivilege::CSWWindowPrivilege(void)
{
}

CSWWindowPrivilege::~CSWWindowPrivilege(void)
{
}

BOOL CSWWindowPrivilege::EnableShutdownPrivileges(VOID)
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    BOOL bResult = FALSE;

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        return bResult;

    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &(tkp.Privileges[0].Luid));
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, 0);
    bResult = (GetLastError() != ERROR_NOT_ALL_ASSIGNED);
    CloseHandle(hToken);
    return bResult;
}

VOID CSWWindowPrivilege::Shutdown()
{
     /*
    ExitWindowsEx(UINT uFlags,DWORD dwReserved);
    其中uFlags取值含义:
    EWX_LOGOFF:注消; EWX_POWEROFF:强制关机; EWX_REBOOT:重启;
    EWX_SHUTDOWN:先保存再关机; EWX_FORCE:不保存就关机; 
    EWX_FORCEIFHUNG:不保存就关机(WIN2K以上版本);
    */
    ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
}

VOID CSWWindowPrivilege::Reboot()
{
    ExitWindowsEx(EWX_REBOOT, 0);
}

BOOL CSWWindowPrivilege::EnableTermProcessPrivileges(BOOL fEnable)
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    BOOL bResult = FALSE;

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        return bResult;

    LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &(tkp.Privileges[0].Luid));
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = fEnable ? SE_PRIVILEGE_ENABLED : 0;

    AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, 0);
    bResult = (GetLastError() != ERROR_NOT_ALL_ASSIGNED);
    CloseHandle(hToken);
    return bResult;
}

BOOL CSWWindowPrivilege::EnableLoadDriverPrivileges(BOOL fEnable)
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    BOOL Result = FALSE;

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
        return FALSE;

    LookupPrivilegeValue(NULL, SE_LOAD_DRIVER_NAME, &(tkp.Privileges[0].Luid));
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = fEnable ? SE_PRIVILEGE_ENABLED : 0;

    AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, 0);
    bResult = (GetLastError() != ERROR_NOT_ALL_ASSIGNED);
    CloseHandle(hToken);
    return bResult;
}

BOOL CSWWindowPrivilege::EnableDebugPrivilege(BOOL fEnable)
{
    BOOL bResult = FALSE;
    HANDLE   hToken;
    
    // 打开本进程访问信令
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
        return bResult;

    // 查询本进程权限
    TOKEN_PRIVILEGES   tkp;
    tkp.PrivilegeCount = 1;
    LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid);
    tkp.Privileges[0].Attributes = fEnable ? SE_PRIVILEGE_ENABLED : 0;

    // 通知系统已修改
    AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, NULL);
    bResult = (GetLastError() != ERROR_NOT_ALL_ASSIGNED);
    CloseHandle(hToken);
    return (bResult);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值