NT下判断用户是否有管理员权限

NT下判断用户是否有管理员权限

OK,  Thanks to Loaden found out that my original code to determine whether an user is in Administrators group is totally bullshit.

And after googling for a while, I found something and test it, simply works. I will just paste the code here. Thanks to the author and google and God.

http://www.mihai-nita.net/article.php?artID=20070413a
//  IsAdminAPI.cpp : Tests if user is Administrator using plain Win32 API
//  Copyright (c) April 2007, Mihai Nita
//

#include <wtypes.h>
#include <Lm.h>

//  for ASSERT
#include <crtdbg.h>

#include "IsAdminAPI.h"

bool IsAdminAPI( WCHAR  const *szUserName )
{
    _ASSERT(szUserName);

     bool bAdmin = FALSE;
    LOCALGROUP_USERS_INFO_0* localGroups;
    DWORD entriesread, totalentries;
    NET_API_STATUS nts = NetUserGetLocalGroups( NULL, szUserName, 0, 0, (unsigned  char**)&localGroups, MAX_PREFERRED_LENGTH, &entriesread, &totalentries);

     if( nts != NERR_Success ) {
        NetApiBufferFree(localGroups);
         return FALSE;
    }

     //  Retrieve the Administrators group well-known SID

    
//  For some reason CreateWellKnownSid generates error C3861 on Developer Studio .NET:
    
//  error C3861: 'CreateWellKnownSid': identifier not found, even with argument-dependent lookup
    BYTE    SidAuth[] = SECURITY_NT_AUTHORITY;
    PSID    pAdminSid;
    AllocateAndInitializeSid( (PSID_IDENTIFIER_AUTHORITY)SidAuth, 
        2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 
        NULL, NULL, NULL, NULL, NULL, NULL, &pAdminSid );

     //  Will use this to retrieve the SID of the group
    BYTE    buffSid[SECURITY_MAX_SID_SIZE];
    wchar_t    buffDomain[DNLEN+1];
    DWORD    dwSidSize;
    DWORD    dwDomainSize;
    SID_NAME_USE m_sidnameuse;

     for( DWORD i = 0; i < entriesread; ++i ) {
        dwSidSize =  sizeof(buffSid);
        dwDomainSize = DNLEN;

         //  Although in general is a bad idea to call directly the W or A versions of API
        
//  we do it here to avoid converting the localGroups[i].lgrui0_name back to ANSI
        
//  This kind of security API is only present on NT/2000/XP family only, so
        
//  the W version is present and safe to use
         if( LookupAccountNameW( NULL, localGroups[i].lgrui0_name, buffSid, &dwSidSize, (LPWSTR)buffDomain, &dwDomainSize, &m_sidnameuse) )  //  no sid for the actual group
             if( EqualSid( buffSid, pAdminSid ) ) {
                bAdmin = TRUE;
                 break;
            }
    }
    FreeSid( pAdminSid );
    NetApiBufferFree(localGroups);

     return bAdmin;
}


注意有时我们只需要判断当前process是否以管理员权限运行。貌似可以通过调用GetTokenInformation 和AllocateAndInitializeSid 来判断,

google "Searching for a SID in an Access Token in C++" site:msdn.microsoft.com可找到一段代码, 请自行研究自行测试。

#define MAX_NAME 256

BOOL SearchTokenGroupsForSID (VOID) 
{
DWORD i, dwSize = 0, dwResult = 0;
HANDLE hToken;
PTOKEN_GROUPS pGroupInfo;
SID_NAME_USE SidType;
char lpName[MAX_NAME];
char lpDomain[MAX_NAME];
BYTE sidBuffer[100];
PSID pSID = (PSID)&sidBuffer;
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
   
//  Open a handle to the access token for the calling process.

if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken )) {
    printf( "OpenProcessToken Error %u\n", GetLastError() );
     return FALSE;
}

//  Call GetTokenInformation to get the buffer size.

if(!GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize)) {
    dwResult = GetLastError();
     if( dwResult != ERROR_INSUFFICIENT_BUFFER ) {
        printf( "GetTokenInformation Error %u\n", dwResult );
         return FALSE;
    }
}

//  Allocate the buffer.

pGroupInfo = (PTOKEN_GROUPS) GlobalAlloc( GPTR, dwSize );

//  Call GetTokenInformation again to get the group information.

if(! GetTokenInformation(hToken, TokenGroups, pGroupInfo, 
                        dwSize, &dwSize ) ) {
    printf( "GetTokenInformation Error %u\n", GetLastError() );
     return FALSE;
   }

//  Create a SID for the BUILTIN\Administrators group.

if(! AllocateAndInitializeSid( &SIDAuth, 2,
                 SECURITY_BUILTIN_DOMAIN_RID,
                 DOMAIN_ALIAS_RID_ADMINS,
                 0, 0, 0, 0, 0, 0,
                 &pSID) ) {
    printf( "AllocateAndInitializeSid Error %u\n", GetLastError() );
     return FALSE;
   }

//  Loop through the group SIDs looking for the administrator SID.

for(i=0; i<pGroupInfo->GroupCount; i++) {
     if ( EqualSid(pSID, pGroupInfo->Groups[i].Sid) ) {

         //  Lookup the account name and print it.

        dwSize = MAX_NAME;
         if( !LookupAccountSid( NULL, pGroupInfo->Groups[i].Sid,
                              lpName, &dwSize, lpDomain, 
                              &dwSize, &SidType ) ) {
            dwResult = GetLastError();
             if( dwResult == ERROR_NONE_MAPPED )
               strcpy_s (lpName, dwSize, "NONE_MAPPED" );
             else {
                printf("LookupAccountSid Error %u\n", GetLastError());
                 return FALSE;
            }
        }
        printf( "Current user is a member of the %s\\%s group\n", 
                lpDomain, lpName );

         //  Find out whether the SID is enabled in the token.
         if (pGroupInfo->Groups[i].Attributes & SE_GROUP_ENABLED)
            printf("The group SID is enabled.\n");
          else  if (pGroupInfo->Groups[i].Attributes & 
                          SE_GROUP_USE_FOR_DENY_ONLY)
            printf("The group SID is a deny-only SID.\n");
          else 
            printf("The group SID is not enabled.\n");
    }
}

if (pSID)
    FreeSid(pSID);
if ( pGroupInfo )
    GlobalFree( pGroupInfo );
return TRUE;
}

向曾被我误导的同志表示真挚的道歉和沉痛的悼念。

NSIS下判断当前用户是否管理员:
http://nsis.sourceforge.net/Check_if_the_current_user_is_an_Administrator
!macro IsUserAdmin RESULT
 !define Index  " Line${__LINE__} "
   StrCpy ${RESULT} 0
   System::Call  ' *(&i1 0,&i4 0,&i1 5)i.r0 '
   System::Call  ' advapi32::AllocateAndInitializeSid(i r0,i 2,i 32,i 544,i 0,i 0,i 0,i 0,i 0, \
   i 0,*i .R0)i.r5 '
   System::Free $0
   System::Call  ' advapi32::CheckTokenMembership(i n,i R0,*i .R1)i.r5 '
   StrCmp $5 0 ${Index}_Error
   StrCpy ${RESULT} $R1
   Goto ${Index}_End
 ${Index}_Error:
   StrCpy ${RESULT} -1
 ${Index}_End:
   System::Call  ' advapi32::FreeSid(i R0)i.r5 '
 !undef Index
!macroend

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Windows API 的 `CheckTokenMembership` 函数来判断某个进程是否需要管理员权限。具体步骤如下: 1. 获取当前进程的令牌句柄,可以使用 `OpenProcessToken` 函数。 2. 判断当前进程是否管理员,可以使用 `CheckTokenMembership` 函数,将管理员组的 SID 作为参数传入,判断当前进程是否管理员。 3. 获取要判断的进程的句柄,可以使用 `OpenProcess` 函数。 4. 获取要判断的进程的令牌句柄,可以使用 `OpenProcessToken` 函数。 5. 判断判断的进程是否需要管理员权限,可以使用 `CheckTokenMembership` 函数,将管理员组的 SID 作为参数传入,判断判断的进程是否需要管理员权限。 下面是一个示例代码,用于判断某个进程是否需要管理员权限: ```c++ #include <Windows.h> #include <iostream> bool IsAdmin(HANDLE token) { BOOL is_admin = FALSE; PSID admin_group = NULL; // 获取管理员组的 SID SID_IDENTIFIER_AUTHORITY nt_authority = SECURITY_NT_AUTHORITY; if (!AllocateAndInitializeSid(&nt_authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &admin_group)) { return false; } // 判断当前进程是否管理员 if (!CheckTokenMembership(token, admin_group, &is_admin)) { is_admin = false; } FreeSid(admin_group); return is_admin == TRUE; } bool IsProcessAdmin(DWORD pid) { HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); if (!process) { return false; } HANDLE token; if (!OpenProcessToken(process, TOKEN_QUERY, &token)) { CloseHandle(process); return false; } bool is_admin = IsAdmin(token); CloseHandle(token); CloseHandle(process); return !is_admin; } int main(int argc, char* argv[]) { if (argc != 2) { std::cout << "Usage: " << argv[0] << " <pid>" << std::endl; return 1; } DWORD pid = std::stoul(argv[1]); bool is_admin = IsProcessAdmin(pid); std::cout << "Process " << pid << " requires admin privilege: " << (is_admin ? "Yes" : "No") << std::endl; return 0; } ``` 其中,`IsAdmin` 函数用于判断当前进程是否管理员,`IsProcessAdmin` 函数用于判断判断的进程是否需要管理员权限。在 `main` 函数中,通过命令行参数传入要判断的进程的 PID,输出该进程是否需要管理员权限

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值