Partage
  • Partager sur Facebook
  • Partager sur Twitter

Impersonation en vb.net possible ?

    9 août 2021 à 13:15:25

    Bonjour,

    j’essaie de lancer un utilitaire .exe qui peut être ouvert qu'avec un identifiant et un mot de passe du domaine. J'ai utilisé plusieurs code trouvé sur internet mais rien ne fonctionne, pouvez me partager un code qui fonction s'il vous plait voici celui que j'utilise :

    Option Explicit On
    Option Infer Off
    
    Imports System
    Imports System.Runtime.InteropServices '   DLL Import
    Imports System.Security.Principal '  WindowsImpersonationContext
    Imports System.ComponentModel
    Imports System.Security.Permissions
    
    
    Public Class RunAs_Impersonator
    #Region "Private Variables and Enum Constants"
        Private tokenHandle As New IntPtr(0)
        Private dupeTokenHandle As New IntPtr(0)
        Private impersonatedUser As WindowsImpersonationContext
    #End Region
    #Region "Properties"
    
    #End Region
    #Region "Public Methods"
        Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
    
        Public Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As IntPtr,
          ByVal SECURITY_IMPERSONATION_LEVEL As Integer,
          ByRef DuplicateTokenHandle As IntPtr) As Boolean
    
        ' Test harness.
        ' If you incorporate this code into a DLL, be sure to demand FullTrust.
        <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")>
        Public Sub ImpersonateStart(ByVal Domain As String, ByVal userName As String, ByVal Password As String)
            Try
                tokenHandle = IntPtr.Zero
                ' Call LogonUser to obtain a handle to an access token.
                Dim returnValue As Boolean = LogonUser(userName, Domain, Password, 2, 0, tokenHandle)
    
                'check if logon successful
                If returnValue = False Then
                    Dim ret As Integer = Marshal.GetLastWin32Error()
                    ' Console.WriteLine("LogonUser failed with error code : {0}", ret)
                    MessageBox.Show("erreur logon")
                    Throw New System.ComponentModel.Win32Exception(ret)
                    Exit Sub
                End If
    
                'Logon succeeded
    
                ' Use the token handle returned by LogonUser.
                Dim newId As New WindowsIdentity(tokenHandle)
                impersonatedUser = newId.Impersonate()
            Catch ex As Exception
                Throw ex
                Exit Sub
            End Try
            MsgBox("running as " & impersonatedUser.ToString & " -- " & WindowsIdentity.GetCurrent.Name)
        End Sub
        <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")>
        Public Sub ImpersonateStop()
            ' Stop impersonating the user.
            impersonatedUser.Undo()
    
            ' Free the tokens.
            If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
                CloseHandle(tokenHandle)
            End If
            MsgBox("running as " & Environment.UserName)
        End Sub
    #End Region
    #Region "Private Methods"
        Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As [String],
         ByVal lpszDomain As [String], ByVal lpszPassword As [String],
         ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer,
         ByRef phToken As IntPtr) As Boolean
    
        <DllImport("kernel32.dll")>
        Public Shared Function FormatMessage(ByVal dwFlags As Integer, ByRef lpSource As IntPtr,
         ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef lpBuffer As [String],
         ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
        End Function
    #End Region
    End Class
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            Dim imp As New RunAs_Impersonator
            Try
                imp.ImpersonateStart("domaine", "user", "mdp")  'creates new context using token for user
    
                'Add code to run as UserName here  'everything between ImpersonateStart and ImpersonateStop will be run as the impersonated user
    
                Process.Start("C:\test.exe")
    
                imp.ImpersonateStop()
            Catch ex As Exception  'make sure impersonation is stopped whether code succeeds or not
                MsgBox(ex.Message)
                imp.ImpersonateStop()
            End Try
        End Sub
    
    
    
    End Class




    Cordialement,

    -
    Edité par Mojow 9 août 2021 à 13:17:48

    • Partager sur Facebook
    • Partager sur Twitter

    Impersonation en vb.net possible ?

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    • Editeur
    • Markdown