Advapi32.dll 사용 : logonusera ()를 사용하여 원격 기계의 로컬 사용자를 가장합니다.
-
10-07-2019 - |
문제
원격 시스템에서 regloadkey ()를 실행할 수 있어야하며, 내 컴퓨터와 원격 머신이 같은 도메인에 있지 않을 수 있습니다. 아래 코드가 작동하며 컴퓨터에 관리자 권한이있는 사용자를 가장 할 수 있습니다. 그렇지 않으면, 우리가 현지 사용자에 대해 이야기하고 있다면,이 토론에 따르면 나는 ...
http://www.eggeadcafe.com/conversation.aspx?messageid=34224301&threadid=34224226
... 내 컴퓨터에는 동일한 사용자 이름과 비밀번호를 가진 로컬 사용자가 있어야합니다. 어. 그 주위에 방법이 있습니까?
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;
}
해결책
로컬 사용자를 정의하지 않고 사용한 내용은 다음과 같습니다.
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);
이후:
WindowsIdentity newIdentity = new WindowsIdentity(token);
WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate();
그래도 손잡이를 복제하지는 않습니다.
또 다른 관찰 - 나는 logonusera를 사용하지 않고 단순히 Logonuser를 사용합니다.
제휴하지 않습니다 StackOverflow