Пример #1
0
 internal static extern bool DuplicateTokenEx(
     IntPtr existingToken,
     uint desiredAccess,
     ref SecurityAttributes threadAttributes,
     int impersonationLevel,
     int tokenType,
     ref IntPtr newToken);
Пример #2
0
 internal static extern bool CreateProcessAsUser(
     IntPtr tokenHandle,
     string applicationName,
     string commandLine,
     ref SecurityAttributes processAttributes,
     ref SecurityAttributes threadAttributes,
     bool inheritHandles,
     uint creationFlags,
     IntPtr environment,
     string currentDirectory,
     ref StartupInfo startupInfo,
     out ProcessInformation processInformation);
Пример #3
0
        internal static bool LaunchProcessAsUser(string command, IntPtr processToken, IntPtr environmentHandle)
        {
            var result = false;

            var processInformation = new ProcessInformation();
            var processAttributes  = new SecurityAttributes();
            var threadAttributes   = new SecurityAttributes();

            processAttributes.Length = (uint)Marshal.SizeOf(processAttributes);
            threadAttributes.Length  = (uint)Marshal.SizeOf(threadAttributes);

            var startupInfo = new StartupInfo();

            startupInfo.cb = (uint)Marshal.SizeOf(startupInfo);

            //if this member is NULL, the new process inherits the desktop
            //and window station of its parent process. If this member is
            //an empty string, the process does not inherit the desktop and
            //window station of its parent process; instead, the system
            //determines if a new desktop and window station need to be created.
            //If the impersonated user already has a desktop, the system uses the
            //existing desktop.

            startupInfo.lpDesktop   = @"WinSta0\Default"; //Modify as needed
            startupInfo.dwFlags     = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK;
            startupInfo.wShowWindow = SW_SHOW;
            //Set other si properties as required.

            result = CreateProcessAsUser(
                processToken,
                null,
                command,
                ref processAttributes,
                ref threadAttributes,
                false,
                CREATE_UNICODE_ENVIRONMENT,
                environmentHandle,
                null,
                ref startupInfo,
                out processInformation);

            if (!result)
            {
                Log.Error("Failed to create process as user {{error}}", Marshal.GetLastWin32Error());
            }

            return(result);
        }
Пример #4
0
        internal static IntPtr GetPrimaryToken(int processId)
        {
            var     token        = IntPtr.Zero;
            var     primaryToken = IntPtr.Zero;
            var     retVal       = false;
            Process p            = null;

            try
            {
                p = Process.GetProcessById(processId);
            }
            catch (ArgumentException ex)
            {
                Log.Error("Process id {{id}} is not available. {{exception}}", processId, ex);
                throw;
            }

            //Gets impersonation token
            if (!OpenProcessToken(p.Handle, TOKEN_DUPLICATE, ref token))
            {
                Log.Error($"{nameof(OpenProcessToken)} {{error}}", Marshal.GetLastWin32Error());
                return(primaryToken);
            }

            var sa = new SecurityAttributes();

            sa.Length = (uint)Marshal.SizeOf(sa);

            //Convert the impersonation token into Primary token
            retVal = DuplicateTokenEx(
                token,
                TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_QUERY,
                ref sa,
                (int)SecurityImpersonationLevel.SecurityIdentification,
                (int)TokenType.TokenPrimary,
                ref primaryToken);

            //Close the Token that was previously opened.
            CloseHandle(token);
            if (retVal == false)
            {
                Log.Error($"{nameof(DuplicateTokenEx)} {{error}}", Marshal.GetLastWin32Error());
            }

            //We'll Close this token after it is used.
            return(primaryToken);
        }