BOOL SetSecurity(LPCTSTR Token_Name, BOOL bEnable = TRUE) { BOOL Result = FALSE; TOKEN_PRIVILEGES TokenPri = { 0 }; HANDLE hToken = 0; // 获取当前进程的访问令牌 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken)) { // 获取特权的LUID if (LookupPrivilegeValue(NULL, Token_Name, &TokenPri.Privileges[0].Luid)) { TokenPri.PrivilegeCount = 1; TokenPri.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : SE_PRIVILEGE_REMOVED; // 开启或关闭特权 Result = AdjustTokenPrivileges(hToken, FALSE, &TokenPri, sizeof(TokenPri), NULL, 0); } CloseHandle(hToken); } return Result; }
#include <windows.h> #include <winbase.h> BOOL SetSecurity(LPCSTR sPrivilege, BOOL bEnable) { BOOL bResult = FALSE; HANDLE hToken = NULL; do { BOOL bRet = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken); if (!bRet || hToken == NULL) { break; } TOKEN_PRIVILEGES currtp = {}; bRet = LookupPrivilegeValue(NULL, sPrivilege, &currtp.Privileges[0].Luid);//修改进程权限 if (currtp.Privileges[0].Luid.HighPart == 0 && currtp.Privileges[0].Luid.LowPart == 0) { break; } currtp.PrivilegeCount = 1; TOKEN_PRIVILEGES Prevtp = {}; DWORD Required = 0; //通知系统修改进程权限 bRet = AdjustTokenPrivileges(hToken, FALSE, &currtp, sizeof(currtp), &Prevtp, &Required); if (!bRet) { break; } Prevtp.PrivilegeCount = 1; Prevtp.Privileges[ 0].Luid = currtp.Privileges [0].Luid; DWORD Attributes = Prevtp.Privileges[ 0].Attributes; Prevtp.Privileges[0].Attributes = bEnable ? (Attributes | SE_PRIVILEGE_ENABLED) : (Attributes & (~SE_PRIVILEGE_ENABLED)); bRet = AdjustTokenPrivileges(hToken, FALSE, &Prevtp, sizeof(Prevtp), &currtp, &Required); if (bRet) { break ; } bResult = TRUE ; } while (false); if (hToken != NULL) { CloseHandle(hToken); hToken = NULL; } return bResult; }
本篇文章链接 地址:https://wmzos.com/?id=43
添加新评论