• <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下判斷用戶是否有管理員權限

            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;
            }


            注意有時我們只需要判斷當前process是否以管理員權限運行。貌似可以通過調(diào)用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,
                             
            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;
            }

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

            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

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

            評論

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

            有嚴重缺陷:如果用戶原來是管理員,后從管理員組中刪除,則上述代碼仍然認為是管理員。  回復  更多評論   

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

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

            精品国产乱码久久久久软件| 韩国无遮挡三级久久| 四虎影视久久久免费观看| 亚洲精品午夜国产va久久| 狠狠综合久久AV一区二区三区| 国产精品一久久香蕉国产线看观看 | 色综合久久88色综合天天 | 一97日本道伊人久久综合影院| 久久99热这里只有精品国产| 国产精品禁18久久久夂久| 色综合久久久久综合99| 狠色狠色狠狠色综合久久| 亚洲伊人久久成综合人影院 | 99精品国产免费久久久久久下载| 蜜臀av性久久久久蜜臀aⅴ| 久久99精品九九九久久婷婷| 日韩乱码人妻无码中文字幕久久| 精品久久久久久久| 久久精品人人做人人爽电影 | 国产亚洲精品自在久久| 国产成人综合久久精品红| 狠狠精品久久久无码中文字幕 | 国产精自产拍久久久久久蜜| 欧美一区二区久久精品| 久久精品99无色码中文字幕| 久久精品国产精品青草| 午夜久久久久久禁播电影| 久久无码一区二区三区少妇| 91精品国产高清久久久久久91| 久久天堂AV综合合色蜜桃网 | 久久天天日天天操综合伊人av| 2021久久国自产拍精品| 性做久久久久久久| 伊人久久综合无码成人网| 久久夜色精品国产| 久久国产高清一区二区三区| 亚洲综合久久综合激情久久| 久久九九亚洲精品| 国内精品久久久久久久coent| 亚洲国产精品久久久久婷婷软件 | 91精品国产综合久久香蕉 |