• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            brent's hut

            NT下判斷用戶是否有管理員權(quán)限

            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, 00, (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;
            }


            注意有時(shí)我們只需要判斷當(dāng)前process是否以管理員權(quán)限運(yùn)行。貌似可以通過調(diào)用GetTokenInformation 和AllocateAndInitializeSid 來判斷,

            google "Searching for a SID in an Access Token in C++" site:msdn.microsoft.com可找到一段代碼, 請(qǐng)自行研究自行測(cè)試。

            #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,
                             
            000000,
                             
            &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;
            }

            向曾被我誤導(dǎo)的同志表示真摯的道歉和沉痛的悼念。

            NSIS下判斷當(dāng)前用戶是否管理員:
            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

            posted on 2005-07-25 09:11 brent 閱讀(3254) 評(píng)論(2)  編輯 收藏 引用 所屬分類: C++Windows

            評(píng)論

            # re: NT下判斷用戶是否有管理員權(quán)限[未登錄] 2008-12-01 17:00 Loaden

            有嚴(yán)重缺陷:如果用戶原來是管理員,后從管理員組中刪除,則上述代碼仍然認(rèn)為是管理員。  回復(fù)  更多評(píng)論   

            # re: NT下判斷用戶是否有管理員權(quán)限 2008-12-02 11:45 brent

            Thanks, 已經(jīng)更新了  回復(fù)  更多評(píng)論   

            国产免费福利体检区久久| 久久精品国产99久久久| 久久99国产综合精品| 2021最新久久久视精品爱 | 中文字幕久久波多野结衣av| 久久久久久av无码免费看大片| 国产午夜久久影院| 精品国产热久久久福利| 国产亚州精品女人久久久久久 | 国内精品久久久久久久久电影网| 青青草原综合久久大伊人导航| 亚洲精品国精品久久99热| 最新久久免费视频| 无码伊人66久久大杳蕉网站谷歌| 无码国产69精品久久久久网站| 国内精品久久人妻互换| 国产精品久久自在自线观看| 亚洲国产精品久久久久婷婷软件| 久久国产成人午夜aⅴ影院| 亚洲精品NV久久久久久久久久 | 久久香综合精品久久伊人| 久久久久人妻一区精品性色av| 99热成人精品热久久669| 久久99热这里只有精品国产| 精品国产日韩久久亚洲| 成人免费网站久久久| 久久久久国色AV免费观看 | 97久久久精品综合88久久| 亚洲国产二区三区久久| 一本久久a久久精品综合香蕉| 久久久久亚洲av无码专区导航| 国产真实乱对白精彩久久| 亚洲国产精品成人久久| 久久激情五月丁香伊人| 色婷婷综合久久久中文字幕| 国产精品一区二区久久精品无码| 久久精品国产亚洲av麻豆蜜芽| 国产高潮久久免费观看| 热re99久久精品国99热| 亚洲精品综合久久| 一本一道久久精品综合|