Exemple #1
0
 public static AuxProcess CreateProcess(string fileName)
 {
     return(new AuxProcess {
         _proc = Environment.UserInteractive?Process.Start(new ProcessStartInfo {
             CreateNoWindow = true,
             WorkingDirectory = Directory.GetCurrentDirectory(),
             FileName = fileName,
             UseShellExecute = false,
             RedirectStandardInput = true,
             RedirectStandardOutput = true,
             StandardOutputEncoding = Encoding.GetEncoding(866),
             RedirectStandardError = true,
             StandardErrorEncoding = Encoding.GetEncoding(866)
         }):ImpersonationUtils.RunAsCurrentUser(fileName)
     });
 }
Exemple #2
0
        private static Process _LaunchProcessAsUser(string fileName, IntPtr token, IntPtr envBlock, out IntPtr hStdIn, out IntPtr hStdOut, out IntPtr hStdError)
        {
            var _saProcess = new _SecurityAttributes {
                nLength = Marshal.SizeOf(typeof(_SecurityAttributes))
            };
            var _saThread = new _SecurityAttributes {
                nLength = Marshal.SizeOf(typeof(_SecurityAttributes))
            };
            var _pi = new _ProcessInformation();

            var _si = new _StartupInfo {
                cb          = Marshal.SizeOf(typeof(_StartupInfo)),
                lpDesktop   = @"WinSta0\Default",
                wShowWindow = SW.Hide,
                dwFlags     = StartFlags.UseShowWindow | StartFlags.UseStdHandles
            };

            ImpersonationUtils._CreatePipe(out _si.hStdInput, out hStdIn);
            if (!Win32.SetHandleInformation(_si.hStdInput, HandleFlags.Inherit, HandleFlags.Inherit))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "SetHandleInformation failed.");
            }

            ImpersonationUtils._CreatePipe(out hStdOut, out _si.hStdOutput);
            if (!Win32.SetHandleInformation(_si.hStdOutput, HandleFlags.Inherit, HandleFlags.Inherit))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "SetHandleInformation failed.");
            }

            ImpersonationUtils._CreatePipe(out hStdError, out _si.hStdError);
            if (!Win32.SetHandleInformation(_si.hStdError, HandleFlags.Inherit, HandleFlags.Inherit))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "SetHandleInformation failed.");
            }

            if (!Win32.CreateProcessAsUser(token, null, fileName, _saProcess, _saThread, true, ProcessCreationFlags.CreateUnicodeEnvironment, envBlock, null, _si, _pi))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateProcessAsUser failed.");
            }
            Win32.CloseHandle(_pi.hProcess);
            Win32.CloseHandle(_pi.hThread);
            Win32.CloseHandle(_si.hStdInput);
            Win32.CloseHandle(_si.hStdOutput);
            Win32.CloseHandle(_si.hStdError);
            return(Process.GetProcessById(_pi.dwProcessId));
        }
Exemple #3
0
        public static Process RunAsCurrentUser(string fileName)
        {
            var _token = ImpersonationUtils._GetPrimaryToken();

            try {
                for (var _env = IntPtr.Zero; Win32.CreateEnvironmentBlock(ref _env, _token, false) && _env != IntPtr.Zero;)
                {
                    try {
                        IntPtr _hStdIn, _hStdOut, _hStdError;
                        var    _result = ImpersonationUtils._LaunchProcessAsUser(fileName, _token, _env, out _hStdIn, out _hStdOut, out _hStdError);

                        var _type = typeof(Process);
                        for (var _field = _type.GetField("standardInput", BindingFlags.Instance | BindingFlags.NonPublic); _field != null && _hStdIn != IntPtr.Zero && _hStdIn != new IntPtr(-1);)
                        {
                            _field.SetValue(_result, new StreamWriter(new FileStream(new SafeFileHandle(_hStdIn, true), FileAccess.Write), Encoding.GetEncoding(866))
                            {
                                AutoFlush = true
                            });
                            break;
                        }
                        for (var _field = _type.GetField("standardOutput", BindingFlags.Instance | BindingFlags.NonPublic); _field != null && _hStdOut != IntPtr.Zero && _hStdOut != new IntPtr(-1);)
                        {
                            _field.SetValue(_result, new StreamReader(new FileStream(new SafeFileHandle(_hStdOut, true), FileAccess.Read), Encoding.GetEncoding(866), true));
                            break;
                        }
                        for (var _field = _type.GetField("standardError", BindingFlags.Instance | BindingFlags.NonPublic); _field != null && _hStdError != IntPtr.Zero && _hStdError != new IntPtr(-1);)
                        {
                            _field.SetValue(_result, new StreamReader(new FileStream(new SafeFileHandle(_hStdError, true), FileAccess.Read), Encoding.GetEncoding(866), true));
                            break;
                        }
                        return(_result);
                    } finally {
                        Win32.DestroyEnvironmentBlock(_env);
                    }
                }
                throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateEnvironmentBlock failed.");
            } finally {
                Win32.CloseHandle(_token);
            }
        }
Exemple #4
0
 public static IDisposable ImpersonateCurrentUser()
 {
     return(new WindowsIdentity(ImpersonationUtils._GetPrimaryToken()).Impersonate());
 }