Utilisation de advapi32.dll: LogonUserA () pour emprunter l'identité d'un utilisateur local d'un ordinateur distant

StackOverflow https://stackoverflow.com/questions/1035116

Question

Je dois pouvoir exécuter RegLoadKey () sur une machine distante. Il se peut que ma machine et la machine distante ne soient pas dans le même domaine. S'ils le sont, le code ci-dessous fonctionne correctement et je peux emprunter l'identité d'un utilisateur disposant de privilèges d'administrateur sur la machine. Sinon, si nous parlons d'utilisateurs locaux, d'après cette discussion, j'ai trouvé ...

http://www.eggheadcafe.com/conversation.aspx?message = 34224301 & amp; threadid = 34224226

... Il doit y avoir un utilisateur local sur ma machine avec le même nom d'utilisateur et le même mot de passe. Pouah. Y a-t-il un moyen de contourner cela?

using System.Runtime.InteropServices;
using System.Security.Principal;

[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);

public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

public WindowsImpersonationContext WearDrag(string Username, string Password, string DomainOrMachine)
{
    WindowsImpersonationContext impersonationContext;
    WindowsIdentity tempWindowsIdentity;
    IntPtr token = IntPtr.Zero;
    IntPtr tokenDuplicate = IntPtr.Zero;

    if (RevertToSelf())
    {
        if (LogonUserA(Username, DomainOrMachine, Password,
            LOGON32_LOGON_INTERACTIVE,
            LOGON32_PROVIDER_DEFAULT, ref token) != 0)
        {
            if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
            {
                tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                impersonationContext = tempWindowsIdentity.Impersonate();
                if (impersonationContext != null)
                {
                    CloseHandle(token);
                    CloseHandle(tokenDuplicate);
                    return impersonationContext;
                }
            }
        }
    }
    if (token != IntPtr.Zero)
        CloseHandle(token);
    if (tokenDuplicate != IntPtr.Zero)
        CloseHandle(tokenDuplicate);
    return null;
}
Était-ce utile?

La solution

Voici ce que j'utilise sans avoir à définir d'utilisateur local:

const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
const int LOGON32_PROVIDER_DEFAULT = 0;

bool isSuccess = LogonUser(username, domain, password,
            LOGON32_LOGON_NEW_CREDENTIALS,
            LOGON32_PROVIDER_DEFAULT, ref token);

Après cela:

WindowsIdentity newIdentity = new WindowsIdentity(token);
WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate();

Cependant, je ne duplique pas le descripteur.

Autre observation: je n’utilise pas LogonUserA, j’utilise simplement LogonUser.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top