1

I am trying to convert the C# code given in this link. I got some errors after conversion, but i have corrected some. Still i get error for the line cbSize = sizeof(TOKEN_ELEVATION_TYPE) The errors are:

  • 'sizeof' is not declared. It may be inaccessible due to its protection level. D:\Desktop\WindowsApplication3\WindowsApplication3\Form1.vb 103 26 WindowsApplication3
  • 'TOKEN_ELEVATION_TYPE' is a type and cannot be used as an expression. D:\Desktop\WindowsApplication3\WindowsApplication3\Form1.vb 103 33 WindowsApplication3

I tried using Len instead of sizeof, but the second error still exists. So can anyone help me to solve both errors. Below is the VB.NET code which has said error.

Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.ComponentModel

Public Class Form1
    Public Const TOKEN_DUPLICATE As UInt32 = &H2
    Public Const TOKEN_IMPERSONATE As UInt32 = &H4
    Public Const TOKEN_QUERY As UInt32 = &H8
    Public Declare Function GetTokenInformation Lib "advapi32.dll" ( _
   ByVal TokenHandle As IntPtr, ByVal TokenInformationClass As TOKEN_INFORMATION_CLASS, _
   ByVal TokenInformation As IntPtr, ByVal TokenInformationLength As System.UInt32, _
   ByRef ReturnLength As System.UInt32) As Boolean
    Declare Function DuplicateToken Lib "advapi32.dll" (ExistingTokenHandle As IntPtr, _
   SECURITY_IMPERSONATION_LEVEL As Int16, ByRef DuplicateTokenHandle As IntPtr) _
   As Boolean
    Enum TOKEN_ELEVATION_TYPE
        TokenElevationTypeDefault = 1
        TokenElevationTypeFull
        TokenElevationTypeLimited
    End Enum


    Public Enum TOKEN_INFORMATION_CLASS
        TokenUser = 1
        TokenGroups
        TokenPrivileges
        TokenOwner
        TokenPrimaryGroup
        TokenDefaultDacl
        TokenSource
        TokenType
        TokenImpersonationLevel
        TokenStatistics
        TokenRestrictedSids
        TokenSessionId
        TokenGroupsAndPrivileges
        TokenSessionReference
        TokenSandBoxInert
        TokenAuditPolicy
        TokenOrigin
        TokenElevationType
        TokenLinkedToken
        TokenElevation
        TokenHasRestrictions
        TokenAccessInformation
        TokenVirtualizationAllowed
        TokenVirtualizationEnabled
        TokenIntegrityLevel
        TokenUIAccess
        TokenMandatoryPolicy
        TokenLogonSid
        MaxTokenInfoClass
        ' MaxTokenInfoClass should always be the last enum 
    End Enum

    Public Enum SECURITY_IMPERSONATION_LEVEL
        SecurityAnonymous
        SecurityIdentification
        SecurityImpersonation
        SecurityDelegation
    End Enum


    Function IsAdmin() As Boolean
        Dim identity = WindowsIdentity.GetCurrent()
        Return (identity IsNot Nothing AndAlso New WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator))
    End Function

    ''' <summary>
    ''' The function checks whether the primary access token of the process belongs
    ''' to user account that is a member of the local Administrators group, even if
    ''' it currently is not elevated.
    ''' </summary>
    ''' <returns>
    ''' Returns true if the primary access token of the process belongs to user
    ''' account that is a member of the local Administrators group. Returns false
    ''' if the token does not.
    ''' </returns>
    Function CanBeAdmin() As Boolean
        Dim fInAdminGroup As Boolean = False
        Dim hToken As IntPtr = IntPtr.Zero
        Dim hTokenToCheck As IntPtr = IntPtr.Zero
        Dim pElevationType As IntPtr = IntPtr.Zero
        Dim pLinkedToken As IntPtr = IntPtr.Zero
        Dim cbSize As Integer = 0

        If IsAdmin() Then
            Return True
        End If

        Try
            ' Check the token for this user
            hToken = WindowsIdentity.GetCurrent().Token

            ' Determine whether system is running Windows Vista or later operating
            ' systems (major version >= 6) because they support linked tokens, but
            ' previous versions (major version < 6) do not.
            If Environment.OSVersion.Version.Major >= 6 Then
                ' Running Windows Vista or later (major version >= 6).
                ' Determine token type: limited, elevated, or default.

                ' Allocate a buffer for the elevation type information.
                cbSize = sizeof(TOKEN_ELEVATION_TYPE)
                Dim cbSizeuint As UInteger = Convert.ToUInt32(cbSize)
                pElevationType = Marshal.AllocHGlobal(cbSize)
                If pElevationType = IntPtr.Zero Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error())
                End If

                ' Retrieve token elevation type information.
                If Not GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenElevationType, pElevationType, cbSizeuint, cbSizeuint) Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error())
                End If

                ' Marshal the TOKEN_ELEVATION_TYPE enum from native to .NET.
                Dim elevType As TOKEN_ELEVATION_TYPE = CType(Marshal.ReadInt32(pElevationType), TOKEN_ELEVATION_TYPE)

                ' If limited, get the linked elevated token for further check.
                If elevType = TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited Then
                    ' Allocate a buffer for the linked token.
                    cbSize = IntPtr.Size
                    Dim cbSizeuint_ee As UInteger = Convert.ToUInt32(cbSize)
                    pLinkedToken = Marshal.AllocHGlobal(cbSize)
                    If pLinkedToken = IntPtr.Zero Then
                        Throw New Win32Exception(Marshal.GetLastWin32Error())
                    End If

                    ' Get the linked token.
                    If Not GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenLinkedToken, pLinkedToken, cbSizeuint_ee, cbSizeuint_ee) Then
                        Throw New Win32Exception(Marshal.GetLastWin32Error())
                    End If

                    ' Marshal the linked token value from native to .NET.
                    hTokenToCheck = Marshal.ReadIntPtr(pLinkedToken)
                End If
            End If

            ' CheckTokenMembership requires an impersonation token. If we just got
            ' a linked token, it already is an impersonation token.  If we did not
            ' get a linked token, duplicate the original into an impersonation
            ' token for CheckTokenMembership.
            If hTokenToCheck = IntPtr.Zero Then
                If Not DuplicateToken(hToken, CInt(SECURITY_IMPERSONATION_LEVEL.SecurityIdentification), hTokenToCheck) Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error())
                End If
            End If

            ' Check if the token to be checked contains admin SID.
            Dim id As New WindowsIdentity(hTokenToCheck)
            Dim principal As New WindowsPrincipal(id)

            fInAdminGroup = principal.IsInRole(WindowsBuiltInRole.Administrator)
        Catch
            Return False
        Finally
            ' Centralized cleanup for all allocated resources.
            If pElevationType <> IntPtr.Zero Then
                Marshal.FreeHGlobal(pElevationType)
                pElevationType = IntPtr.Zero
            End If
            If pLinkedToken <> IntPtr.Zero Then
                Marshal.FreeHGlobal(pLinkedToken)
                pLinkedToken = IntPtr.Zero
            End If
        End Try

        Return fInAdminGroup
    End Function
    Private Sub Form1_Load(sender As Object, e As EventArgs)
        If CanBeAdmin() Then
            MessageBox.Show("admin")
        Else
            MessageBox.Show("not admin")
        End If
    End Sub
End Class
Community
  • 1
  • 1
IT researcher
  • 3,274
  • 17
  • 79
  • 143
  • Google threw up http://stackoverflow.com/questions/26407294/vb-net-and-sizeof as a point of interest. – Chris Feb 12 '15 at 13:35
  • @Chris Also i have another error "'TOKEN_ELEVATION_TYPE' is a type and cannot be used as an expression" which i have mentioned in question.So how my question will be duplicate question. Please help me to solve that too.(using len i have already mentioned in my question, so i need answer for my second error) – IT researcher Feb 12 '15 at 13:59
  • The accepted answer of that talks about that. "The 'Len' operator in VB will do this (but it works on instances, so you need to adjust accordingly)". You are not passing it an instance but a type and it doesn't expect a type. The answer there tells you all that you need to know as far as I can see. You just need to apply that knowledge now. – Chris Feb 12 '15 at 15:03

1 Answers1

5

You can get the effect of C# cbSize = sizeof(TOKEN_ELEVATION_TYPE) using Marshal.SizeOf to get the size of the underlying type.

Dim undertype As Type = [Enum].GetUnderlyingType(GetType(TOKEN_ELEVATION_TYPE))
cbSize = System.Runtime.InteropServices.Marshal.SizeOf(undertype)

When I ran it, undertype was System.Int32, and cbSize was 4.

Blackwood
  • 4,504
  • 16
  • 32
  • 41