... For i = 0 To @pTokenGroups.GroupCount - 1 ...... (To 12 - 1)
is exited when i = 3 regardless of program status. Here is the code with the necessary Win32API information and a demonstration PBMain:
Code:
Type SID_AND_ATTRIBUTES pSid As Dword Attributes As Dword End Type Type TOKEN_GROUPS GroupCount As Dword Groups(0 To 0) As SID_AND_ATTRIBUTES 'array size varies End Type Type SID_IDENTIFIER_AUTHORITY Value(0 To 5) As Byte End Type '-------------------------------------------------------------------- %TOKEN_QUERY = 8 %TokenGroups = 2 %SECURITY_BUILTIN_DOMAIN_RID = &H00000020& %DOMAIN_ALIAS_RID_ADMINS = &H00000220& '-------------------------------------------------------------------- Declare Function OpenProcessToken Lib "ADVAPI32.DLL" Alias "OpenProcessToken" (ByVal ProcessHandle As Dword, ByVal DesiredAccess As Dword, TokenHandle As Dword) As Long Declare Function GetCurrentProcess Lib "KERNEL32.DLL" Alias "GetCurrentProcess" () As Long Declare Function GetTokenInformation Lib "ADVAPI32.DLL" Alias "GetTokenInformation" (ByVal TokenHandle As Dword, ByVal TokenInformationClass As Long, TokenInformation As Any, ByVal TokenInformationLength As Dword, ReturnLength As Dword) As Long Declare Function CloseHandle Lib "KERNEL32.DLL" Alias "CloseHandle" (ByVal hObject As Dword) As Long Declare Sub FreeSid Lib "ADVAPI32.DLL" Alias "FreeSid" (pSid As Any) Declare Function AllocateAndInitializeSid Lib "ADVAPI32.DLL" Alias "AllocateAndInitializeSid" (pIdentifierAuthority As SID_IDENTIFIER_AUTHORITY, ByVal nSubAuthorityCount As Byte, ByVal nSubAuthority0 As Long, ByVal nSubAuthority1 As Long, _ ByVal nSubAuthority2 As Long, ByVal nSubAuthority3 As Long, ByVal nSubAuthority4 As Long, ByVal nSubAuthority5 As Long, ByVal nSubAuthority6 As Long, ByVal nSubAuthority7 As Long, lpPSid As Any) As Long Declare Function EqualSid Lib "ADVAPI32.DLL" Alias "EqualSid" (pSid1 As Any, pSid2 As Any) As Long '-------------------------------------------------------------------- ' Returns -1 on failure ' 1 if running as administrator ' 0 if not running as administrator ' Assumes the operating system is Windows 2000 or later (XP, 7, etc.) Function IsAdmin As Long Local hAccessToken As Long, i As Long Local Info As String Local szInfo As Dword Local pTokenGroups As TOKEN_GROUPS Ptr Local siaNtAuthority As SID_IDENTIFIER_AUTHORITY Local psidAdministrators As Long 'SID Ptr If IsFalse(OpenProcessToken(GetCurrentProcess, %TOKEN_QUERY, hAccessToken)) Then Function = -1 Exit Function End If GetTokenInformation hAccessToken, %TOKENGROUPS, ByVal 0&, 0&, szInfo Info = Space$(szInfo) i = GetTokenInformation(hAccessToken, %TOKENGROUPS, ByVal StrPtr(Info), Len(Info), szInfo) CloseHandle hAccessToken If IsFalse(i) Then Function = -1 Exit Function End If siaNtAuthority.Value(5) = 5 ' = SECURITY_NT_AUTHORITY If IsFalse(AllocateAndInitializeSid(siaNtAuthority, 2, %SECURITY_BUILTIN_DOMAIN_RID, %DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdministrators)) Then Function = -1 Exit Function End If pTokenGroups = StrPtr(Info) For i = 0 To @pTokenGroups.GroupCount - 1 'To 13 If EqualSid (ByVal psidAdministrators, ByVal @pTokenGroups.Groups(i).pSid) Then Function = 1 'get here when i = 3 regardless Exit For End If Next FreeSid ByVal psidAdministrators End Function Function PBMain Select Case IsAdmin Case 1 : Print "Running as an administrtor. Case 0 : Print "Not running as an administrtor. Case -1 : Print "FAILURE" End Select WaitKey$ End Function
Leave a comment: