online # ˆl–Ðz¾”Ji?u4 ^¸ËwÒ˜´oÇ „ o:¿Ä=ÿ using Microsoft.Win32.SafeHandles; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Security.Principal; using System.Text; namespace Ansible.AccessToken { internal class NativeHelpers { [StructLayout(LayoutKind.Sequential)] public struct LUID_AND_ATTRIBUTES { public Luid Luid; public UInt32 Attributes; } [StructLayout(LayoutKind.Sequential)] public struct SID_AND_ATTRIBUTES { public IntPtr Sid; public int Attributes; } [StructLayout(LayoutKind.Sequential)] public struct TOKEN_PRIVILEGES { public UInt32 PrivilegeCount; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public LUID_AND_ATTRIBUTES[] Privileges; } [StructLayout(LayoutKind.Sequential)] public struct TOKEN_USER { public SID_AND_ATTRIBUTES User; } public enum TokenInformationClass : uint { TokenUser = 1, TokenPrivileges = 3, TokenStatistics = 10, TokenElevationType = 18, TokenLinkedToken = 19, } } internal class NativeMethods { [DllImport("kernel32.dll", SetLastError = true)] public static extern bool CloseHandle( IntPtr hObject); [DllImport("advapi32.dll", SetLastError = true)] public static extern bool DuplicateTokenEx( SafeNativeHandle hExistingToken, TokenAccessLevels dwDesiredAccess, IntPtr lpTokenAttributes, SecurityImpersonationLevel ImpersonationLevel, TokenType TokenType, out SafeNativeHandle phNewToken); [DllImport("kernel32.dll")] public static extern SafeNativeHandle GetCurrentProcess(); [DllImport("advapi32.dll", SetLastError = true)] public static extern bool GetTokenInformation( SafeNativeHandle TokenHandle, NativeHelpers.TokenInformationClass TokenInformationClass, SafeMemoryBuffer TokenInformation, UInt32 TokenInformationLength, out UInt32 ReturnLength); [DllImport("advapi32.dll", SetLastError = true)] public static extern bool ImpersonateLoggedOnUser( SafeNativeHandle hToken); [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern bool LogonUserW( string lpszUsername, string lpszDomain, string lpszPassword, LogonType dwLogonType, LogonProvider dwLogonProvider, out SafeNativeHandle phToken); [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern bool LookupPrivilegeNameW( string lpSystemName, ref Luid lpLuid, StringBuilder lpName, ref UInt32 cchName); [DllImport("kernel32.dll", SetLastError = true)] public static extern SafeNativeHandle OpenProcess( ProcessAccessFlags dwDesiredAccess, bool bInheritHandle, UInt32 dwProcessId); [DllImport("advapi32.dll", SetLastError = true)] public static extern bool OpenProcessToken( SafeNativeHandle ProcessHandle, TokenAccessLevels DesiredAccess, out SafeNativeHandle TokenHandle); [DllImport("advapi32.dll", SetLastError = true)] public static extern bool RevertToSelf(); } internal class SafeMemoryBuffer : SafeHandleZeroOrMinusOneIsInvalid { public SafeMemoryBuffer() : base(true) { } public SafeMemoryBuffer(int cb) : base(true) { base.SetHandle(Marshal.AllocHGlobal(cb)); } public SafeMemoryBuffer(IntPtr handle) : base(true) { base.SetHandle(handle); } protected over