Ejemplo n.º 1
0
 public static ProcessInformation CreateAsUser(TokenHandle tokenHandle, string applicationName, string commandLine, bool inheritHandles, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environmentHandle, string currentDirectory, ProcessStartInfo startInfo, ProcessSecurity processSecurity = null, ThreadSecurity threadSecurity = null)
 {
     using (var processSecurityAttributes = processSecurity == null ? new SecurityAttributes() : new SecurityAttributes(processSecurity))
     {
         using (var threadSecurityAttributes = threadSecurity == null ? new SecurityAttributes() : new SecurityAttributes(threadSecurity))
         {
             ProcessInformationOut processInformation;
             if (!NativeMethods.CreateProcessAsUser(tokenHandle, applicationName, commandLine, processSecurityAttributes, threadSecurityAttributes, inheritHandles, creationFlags, environmentHandle ?? new EnvironmentBlockHandle(), currentDirectory, startInfo, out processInformation) || processInformation.ProcessHandle == IntPtr.Zero || processInformation.ThreadHandle == IntPtr.Zero)
             {
                 ErrorHelper.ThrowCustomWin32Exception();
             }
             return new ProcessInformation(processInformation.ProcessHandle, processInformation.ProcessId, processInformation.ThreadHandle, processInformation.ThreadId);
         }
     }
 }
Ejemplo n.º 2
0
 public static extern bool CreateProcessWithLogonW(string username, string domain, string password, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startupInfo, out ProcessInformationOut processInformation);
Ejemplo n.º 3
0
 public static extern bool CreateProcessWithTokenW(TokenHandle tokenHandle, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startupInfo, out ProcessInformationOut processInformation);
Ejemplo n.º 4
0
 public static ProcessInformation CreateWithToken(TokenHandle tokenHandle, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startupInfo)
 {
     ProcessInformationOut processInformation;
     if (!NativeMethods.CreateProcessWithTokenW(tokenHandle, logonFlags, applicationName, commandLine, creationFlags, environment, currentDirectory, startupInfo, out processInformation) || processInformation.ProcessHandle == IntPtr.Zero || processInformation.ThreadHandle == IntPtr.Zero)
     {
         ErrorHelper.ThrowCustomWin32Exception();
     }
     return new ProcessInformation(processInformation.ProcessHandle, processInformation.ProcessId, processInformation.ThreadHandle, processInformation.ThreadId);
 }
Ejemplo n.º 5
0
 public static extern bool CreateProcessAsUser(TokenHandle tokenHandle, string applicationName, string commandLine, SecurityAttributes processAttributes, SecurityAttributes threadAttributes, bool inheritHandles, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startInfo, out ProcessInformationOut processInformation);
Ejemplo n.º 6
0
        public static void Main(string[] args)
        {
            var tokenFactory = new TokenFactory();
            var environmentBlockFactory = new EnvironmentBlockFactory();
            var processFactory = new ProcessFactory();

            const string username = "******";
            const string password = "******";

            GetOrCreateUser(username, password);

            using (var token = tokenFactory.Logon(username, ".", GetSecureString(password)))
            {
                using (var environmentBlockHandle = environmentBlockFactory.Create(token, false))
                {
                    var profileInfo = new ProfileInfo
                    {
                        Size = Marshal.SizeOf(typeof(ProfileInfo)),
                        Username = username,
                        DefaultPath = null,
                    };
                    token.LoadUserProfile(ref profileInfo);

                    IProcessInformation processInformation;
                    var processStartInfo = new ProcessStartInfo
                    {
                        Desktop = string.Empty,
                    };
                    string commandLine = string.Format("\"{0}\"", typeof(TestProgramWhileTrue.Program).Assembly.Location);

                    if (Environment.UserInteractive)
                    {
                        processInformation = processFactory.CreateWithLogin(username, "", password,
                            ProcessLogonFlags.None,
                            null,
                            commandLine,
                            ProcessCreationFlags.NewConsole | ProcessCreationFlags.UnicodeEnvironment,
                            environmentBlockHandle,
                            Environment.CurrentDirectory,
                            processStartInfo);
                    }
                    else
                    {
                        processInformation = processFactory.CreateAsUser(token,
                            null,
                            commandLine,
                            false,
                            ProcessCreationFlags.NewConsole | ProcessCreationFlags.UnicodeEnvironment,
                            environmentBlockHandle,
                            Environment.CurrentDirectory,
                            processStartInfo);
                    }

                    using (processInformation)
                    {
                        Console.WriteLine("Press any key to kill");
                        Console.ReadKey(intercept: true);
                        processInformation.Process.Terminate(0);

                        token.UnloadUserProfile(ref profileInfo);
                        token.DeleteUserProfile();
                        DeleteUser(username);
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public IProcessInformation Create(string applicationName, string commandLine, bool inheritHandles, ProcessCreationFlags creationFlags, IEnvironmentBlock environment, string currentDirectory, ProcessStartInfo startInfo, ProcessSecurity processSecurity = null, ThreadSecurity threadSecurity = null)
 {
     return Create(applicationName, commandLine, inheritHandles, creationFlags, environment == null ? null : environment.Handle, currentDirectory, startInfo, processSecurity, threadSecurity);
 }
Ejemplo n.º 8
0
 public IProcessInformation CreateWithToken(TokenHandle token, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startupInfo)
 {
     return ProcessHandle.CreateWithToken(token, logonFlags, applicationName, commandLine, creationFlags, environment, currentDirectory, startupInfo);
 }
Ejemplo n.º 9
0
 public IProcessInformation CreateWithLogin(string username, string domain, string password, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startupInfo)
 {
     return ProcessHandle.CreateWithLogin(username, domain, password, logonFlags, applicationName, commandLine, creationFlags, environment, currentDirectory, startupInfo);
 }
Ejemplo n.º 10
0
 public IProcessInformation CreateAsUser(TokenHandle token, string applicationName, string commandLine, bool inheritHandles, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environment, string currentDirectory, ProcessStartInfo startInfo, ProcessSecurity processSecurity = null, ThreadSecurity threadSecurity = null)
 {
     return ProcessHandle.CreateAsUser(token, applicationName, commandLine, inheritHandles, creationFlags, environment, currentDirectory, startInfo, processSecurity, threadSecurity);
 }
Ejemplo n.º 11
-1
 public static ProcessInformation CreateWithLogin(string username, string domain, string password, ProcessLogonFlags logonFlags, string applicationName, string commandLine, ProcessCreationFlags creationFlags, EnvironmentBlockHandle environmentHandle, string currentDirectory, ProcessStartInfo startupInfo)
 {
     ProcessInformationOut processInformation;
     if (!NativeMethods.CreateProcessWithLogonW(username, domain, password, logonFlags, applicationName, commandLine, creationFlags, environmentHandle ?? new EnvironmentBlockHandle(), currentDirectory, startupInfo, out processInformation) || processInformation.ProcessHandle == IntPtr.Zero || processInformation.ThreadHandle == IntPtr.Zero)
     {
         ErrorHelper.ThrowCustomWin32Exception();
     }
     return new ProcessInformation(processInformation.ProcessHandle, processInformation.ProcessId, processInformation.ThreadHandle, processInformation.ThreadId);
 }