获取提升类型和判断进程是否以管理员身份运行

在任务管理器的例子中,任务管理器在进程选项卡的底部要么显示一个盾牌图标,要么显示一个复选框,具体取决于它是如何生成的。由此带来两个问题:如何判断应用程序是否是以管理员身份运行;如何判断它是以提升的权限来启动的,还是正在使用筛选的令牌运行。
下面的GetProcessElevation函数能返回提升类型和一个指出进程是否正在以管理员身份运行的布尔值。具体代码如下:

#include <shlobj.h> // for IsUserAnAdmin. BOOL GetProcessElevation(TOKEN_ELEVATION_TYPE* pElevationType, BOOL* pIsAdmin) { HANDLE hToken = NULL; DWORD dwSize; // Get current process token if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) return(FALSE); BOOL bResult = FALSE; // Retrieve elevation type information if (GetTokenInformation(hToken, TokenElevationType, pElevationType, sizeof(TOKEN_ELEVATION_TYPE), &dwSize)) { // Create the SID corresponding to the Administrators group byte adminSID[SECURITY_MAX_SID_SIZE]; dwSize = sizeof(adminSID); CreateWellKnownSid(WinBuiltinAdministratorsSid, NULL, &adminSID, &dwSize); if (*pElevationType == TokenElevationTypeLimited) { // Get handle to linked token (will have one if we are lua) HANDLE hUnfilteredToken = NULL; GetTokenInformation(hToken, TokenLinkedToken, (VOID*) &hUnfilteredToken, sizeof(HANDLE), &dwSize); // Check if this original token contains admin SID if (CheckTokenMembership(hUnfilteredToken, &adminSID, pIsAdmin)) { bResult = TRUE; } // Don't forget to close the unfiltered token CloseHandle(hUnfilteredToken); } else { *pIsAdmin = IsUserAnAdmin(); bResult = TRUE; } } // Don't forget to close the process token CloseHandle(hToken); return(bResult); }  

你可能感兴趣的:(Security,null,query,lua,token,任务)