コード例 #1
0
 public static extern bool DetourCreateProcessWithDllExA(
     string lpApplicationName,
     string lpCommandLine,
     IntPtr lpProcessAttributes,
     IntPtr lpThreadAttributes,
     bool bInheritHandles,
     uint dwCreationFlags,
     IntPtr lpEnvironment,
     string lpCurrentDirectory,
     ref NativeMethods.StartupInfo lpStartupInfo,
     out NativeMethods.ProcessInformation lpProcessInformation,
     string lpDllName,
     IntPtr pfCreateProcessA);
コード例 #2
0
 public static extern bool DetourCreateProcessWithDllsExW(
     [MarshalAs(UnmanagedType.LPWStr)] string lpApplicationName,
     string lpCommandLine,
     IntPtr lpProcessAttributes,
     IntPtr lpThreadAttributes,
     bool bInheritHandles,
     uint dwCreationFlags,
     IntPtr lpEnvironment,
     string lpCurrentDirectory,
     ref NativeMethods.StartupInfo lpStartupInfo,
     out NativeMethods.ProcessInformation lpProcessInformation,
     uint nDlls,
     IntPtr rlpDlls,
     IntPtr pfCreateProcessW);
コード例 #3
0
 public static bool DetourCreateProcessWithDllsExW(
     string lpApplicationName,
     string lpCommandLine,
     IntPtr lpProcessAttributes,
     IntPtr lpThreadAttributes,
     bool bInheritHandles,
     uint dwCreationFlags,
     IntPtr lpEnvironment,
     string lpCurrentDirectory,
     ref NativeMethods.StartupInfo lpStartupInfo,
     out NativeMethods.ProcessInformation lpProcessInformation,
     uint nDlls,
     IntPtr rlpDlls,
     IntPtr pfCreateProcessW)
 {
     if (Is64Bit)
     {
         return(NativeApi64.DetourCreateProcessWithDllsExW(lpApplicationName,
                                                           lpCommandLine,
                                                           lpProcessAttributes,
                                                           lpThreadAttributes,
                                                           bInheritHandles,
                                                           dwCreationFlags,
                                                           lpEnvironment,
                                                           lpCurrentDirectory,
                                                           ref lpStartupInfo,
                                                           out lpProcessInformation,
                                                           nDlls,
                                                           rlpDlls,
                                                           pfCreateProcessW));
     }
     else
     {
         return(NativeApi32.DetourCreateProcessWithDllsExW(lpApplicationName,
                                                           lpCommandLine,
                                                           lpProcessAttributes,
                                                           lpThreadAttributes,
                                                           bInheritHandles,
                                                           dwCreationFlags,
                                                           lpEnvironment,
                                                           lpCurrentDirectory,
                                                           ref lpStartupInfo,
                                                           out lpProcessInformation,
                                                           nDlls,
                                                           rlpDlls,
                                                           pfCreateProcessW));
     }
 }
コード例 #4
0
        public bool Start()
        {
            processInfo = new NativeMethods.ProcessInformation();
            var startInfo = new NativeMethods.StartupInfo();
            var success   = false;

            SafeFileHandle hToken, hReadOut, hWriteOut, hReadErr, hWriteErr, hReadIn, hWriteIn;

            var securityAttributes = new NativeMethods.SecurityAttributes();

            securityAttributes.bInheritHandle = true;

            success = NativeMethods.CreatePipe(out hReadOut, out hWriteOut, securityAttributes, 0);
            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            success = NativeMethods.CreatePipe(out hReadErr, out hWriteErr, securityAttributes, 0);
            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            success = NativeMethods.CreatePipe(out hReadIn, out hWriteIn, securityAttributes, 0);
            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            success = NativeMethods.SetHandleInformation(hReadOut, NativeMethods.Constants.HANDLE_FLAG_INHERIT, 0);
            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            // Logon user
            success = NativeMethods.LogonUser(
                runSpec.Credentials.UserName,
                runSpec.Credentials.Domain,
                runSpec.Credentials.Password,
                NativeMethods.LogonType.LOGON32_LOGON_BATCH,
                NativeMethods.LogonProvider.LOGON32_PROVIDER_DEFAULT,
                out hToken
                );
            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            NativeMethods.LUID privilegeLuid;
            if (!NativeMethods.LookupPrivilegeValue(null, "SeImpersonatePrivilege", out privilegeLuid))
            {
                int lastError = Marshal.GetLastWin32Error();
                throw new Win32Exception(lastError, "Error calling LookupPrivilegeValue: " + lastError);
            }

            var tokenPrivileges = new NativeMethods.TOKEN_PRIVILEGES();

            tokenPrivileges.PrivilegeCount = 1;
            tokenPrivileges.Attributes     = 1;
            tokenPrivileges.Luid           = privilegeLuid;
            if (!NativeMethods.AdjustTokenPrivileges(hToken, false,
                                                     ref tokenPrivileges,
                                                     1024, IntPtr.Zero, IntPtr.Zero))
            {
                var lastError = Marshal.GetLastWin32Error();
                throw new Win32Exception(lastError, "Error calling AdjustTokenPrivileges: " + lastError);
            }

            IntPtr unmanagedEnv;

            if (!NativeMethods.CreateEnvironmentBlock(out unmanagedEnv, hToken.DangerousGetHandle(), false))
            {
                int lastError = Marshal.GetLastWin32Error();
                throw new Win32Exception(lastError, "Error calling CreateEnvironmentBlock: " + lastError);
            }

            // Create process
            startInfo.cb         = Marshal.SizeOf(startInfo);
            startInfo.dwFlags    = NativeMethods.Constants.STARTF_USESTDHANDLES;
            startInfo.hStdOutput = hWriteOut;
            startInfo.hStdError  = hWriteErr;
            startInfo.hStdInput  = hReadIn;

            success = NativeMethods.CreateProcessWithTokenW(
                hToken,
                NativeMethods.LogonFlags.WithProfile,
                null,
                CommandLine(),
                NativeMethods.CreateProcessFlags.CREATE_UNICODE_ENVIRONMENT,
                unmanagedEnv,
                null,
                ref startInfo,
                out processInfo
                );

            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            NativeMethods.DestroyEnvironmentBlock(unmanagedEnv);
            NativeMethods.CloseHandle(processInfo.hThread);

            Handle = processInfo.hProcess;

            startInfo.hStdOutput.Close();
            startInfo.hStdError.Close();
            startInfo.hStdInput.Close();
            StandardOutput = new StreamReader(new FileStream(hReadOut, FileAccess.Read), Console.OutputEncoding);
            StandardError  = new StreamReader(new FileStream(hReadErr, FileAccess.Read), Console.OutputEncoding);
            StandardInput  = new StreamWriter(new FileStream(hWriteIn, FileAccess.Write), Console.InputEncoding)
            {
                AutoFlush = true
            };

            WaitForExitAsync();

            return(success);
        }
コード例 #5
0
        public MatchResult Run()
        {
            const string robotBattleExePath = @"C:\Program Files (x86)\Robot Battle\winrob32.exe";

            var loadListFile = Path.GetTempPath() + "loadlist.ll";
            var scoreLogFile = Path.GetTempPath() + "score.log";
            var statsLogFile = Path.GetTempPath() + "stats.log";

            using (var writer = XmlWriter.Create(loadListFile, new XmlWriterSettings { Indent = true })) {
                matchBuilder.ToXml().WriteTo(writer);
            }


            if (hideWindow) {
                var desktopName = "RobotBattle.Automation." + Guid.NewGuid();
                var desktopPtr = NativeMethods.CreateDesktop(desktopName, null, null, 0, 0xff, IntPtr.Zero);
                try {
                    if (desktopPtr == IntPtr.Zero)
                        throw new InvalidOperationException("Failed to create the secondary desktop");

                    // set startup parameters.
                    var si = new NativeMethods.StartupInfo();
                    si.cb = Marshal.SizeOf(si);
                    si.lpDesktop = desktopName;

                    var pi = new NativeMethods.ProcessInformation();

                    var path = string.Format(
                        "\"{0}\" \"{1}\" /t /lf \"{2}\" /slf \"{3}\" /slg 1 /lo 1 /slo 1",
                        robotBattleExePath,
                        loadListFile,
                        scoreLogFile,
                        statsLogFile
                        );

                    if (
                        !NativeMethods.CreateProcess(null, path, IntPtr.Zero, IntPtr.Zero, true,
                                                     0x00000020, /*NORMAL_PRIORITY_CLASS*/
                                                     IntPtr.Zero, null, ref si, ref pi))
                        throw new InvalidOperationException("Failed to launch the Robot Battle process");

                    Process.GetProcessById(pi.dwProcessId)
                        .WaitForExit();
                } finally {
                    NativeMethods.CloseDesktop(desktopPtr);
                }
            } else {
                Process.Start(new ProcessStartInfo {
                    FileName = robotBattleExePath,
                    Arguments = string.Format(
                        "\"{1}\" /t /lf \"{2}\" /slf \"{3}\" /slg 1 /lo 1 /slo 1",
                        robotBattleExePath,
                        loadListFile,
                        scoreLogFile,
                        statsLogFile
                                  )
                }).WaitForExit();
            }

            return MatchResult.FromFiles(loadListFile, scoreLogFile, statsLogFile);
        }
コード例 #6
0
        public bool StartAsUser(IntPtr userToken)
        {
            _processInformation = new NativeMethods.ProcessInformation();
            NativeMethods.StartupInfo startupInfo = new NativeMethods.StartupInfo();
            switch (StartInfo.WindowStyle)
            {
            case ProcessWindowStyle.Hidden:
                startupInfo.wShowWindow = SW_HIDE;
                break;

            case ProcessWindowStyle.Maximized:
                startupInfo.wShowWindow = SW_MAXIMIZE;
                break;

            case ProcessWindowStyle.Minimized:
                startupInfo.wShowWindow = SW_MINIMIZE;
                break;

            case ProcessWindowStyle.Normal:
                startupInfo.wShowWindow = SW_SHOW;
                break;
            }
            CreateStandardPipe(out _stdinReadHandle, out _stdinWriteHandle, STD_INPUT_HANDLE, true, StartInfo.RedirectStandardInput);
            CreateStandardPipe(out _stdoutReadHandle, out _stdoutWriteHandle, STD_OUTPUT_HANDLE, false, StartInfo.RedirectStandardOutput);
            CreateStandardPipe(out _stderrReadHandle, out _stderrWriteHandle, STD_ERROR_HANDLE, false, StartInfo.RedirectStandardError);

            startupInfo.dwFlags    = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
            startupInfo.hStdInput  = _stdinReadHandle;
            startupInfo.hStdOutput = _stdoutWriteHandle;
            startupInfo.hStdError  = _stderrWriteHandle;

            NativeMethods.CreateProcessFlags createFlags = NativeMethods.CreateProcessFlags.CreateNewConsole | NativeMethods.CreateProcessFlags.CreateNewProcessGroup | NativeMethods.CreateProcessFlags.CreateDefaultErrorMode;
            if (StartInfo.CreateNoWindow)
            {
                startupInfo.wShowWindow = SW_HIDE;
                createFlags            |= NativeMethods.CreateProcessFlags.CreateNoWindow;
            }

            // Create process as user, fail hard if this is unsuccessful so it can be caught in EncoderUnit
            if (!NativeMethods.CreateProcessAsUserW(userToken, null, GetCommandLine(), IntPtr.Zero, IntPtr.Zero, true, createFlags, IntPtr.Zero, null, startupInfo, out _processInformation))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "ImpersonationProcess: CreateProcessAsUser failed");
            }

            if (_processInformation.hThread != (IntPtr)(-1))
            {
                ImpersonationHelper.SafeCloseHandle(ref _processInformation.hThread);
                _processInformation.hThread = IntPtr.Zero;
            }

            if (StartInfo.RedirectStandardInput)
            {
                ImpersonationHelper.SafeCloseHandle(ref _stdinReadHandle);
                StreamWriter standardInput = new StreamWriter(new FileStream(_stdinWriteHandle, FileAccess.Write, 4096), Console.Out.Encoding)
                {
                    AutoFlush = true
                };
                SetField("standardInput", standardInput);
            }

            if (StartInfo.RedirectStandardOutput)
            {
                ImpersonationHelper.SafeCloseHandle(ref _stdoutWriteHandle);
                StreamReader standardOutput = new StreamReader(new FileStream(_stdoutReadHandle, FileAccess.Read, 4096), StartInfo.StandardOutputEncoding);
                SetField("standardOutput", standardOutput);
            }

            if (StartInfo.RedirectStandardError)
            {
                ImpersonationHelper.SafeCloseHandle(ref _stderrWriteHandle);
                StreamReader standardError = new StreamReader(new FileStream(_stderrReadHandle, FileAccess.Read, 4096), StartInfo.StandardErrorEncoding);
                SetField("standardError", standardError);
            }

            // Workaround to get process handle as non-public SafeProcessHandle
            Assembly processAssembly   = typeof(System.Diagnostics.Process).Assembly;
            Type     processManager    = processAssembly.GetType("System.Diagnostics.ProcessManager");
            object   safeProcessHandle = processManager.InvokeMember("OpenProcess", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, this, new object[] { _processInformation.dwProcessId, 0x100000, false });

            InvokeMethod("SetProcessHandle", safeProcessHandle);
            InvokeMethod("SetProcessId", _processInformation.dwProcessId);

            return(true);
        }
コード例 #7
0
        public MatchResult Run()
        {
            const string robotBattleExePath = @"C:\Program Files (x86)\Robot Battle\winrob32.exe";

            var loadListFile = Path.GetTempPath() + "loadlist.ll";
            var scoreLogFile = Path.GetTempPath() + "score.log";
            var statsLogFile = Path.GetTempPath() + "stats.log";

            using (var writer = XmlWriter.Create(loadListFile, new XmlWriterSettings {
                Indent = true
            })) {
                matchBuilder.ToXml().WriteTo(writer);
            }


            if (hideWindow)
            {
                var desktopName = "RobotBattle.Automation." + Guid.NewGuid();
                var desktopPtr  = NativeMethods.CreateDesktop(desktopName, null, null, 0, 0xff, IntPtr.Zero);
                try {
                    if (desktopPtr == IntPtr.Zero)
                    {
                        throw new InvalidOperationException("Failed to create the secondary desktop");
                    }

                    // set startup parameters.
                    var si = new NativeMethods.StartupInfo();
                    si.cb        = Marshal.SizeOf(si);
                    si.lpDesktop = desktopName;

                    var pi = new NativeMethods.ProcessInformation();

                    var path = string.Format(
                        "\"{0}\" \"{1}\" /t /lf \"{2}\" /slf \"{3}\" /slg 1 /lo 1 /slo 1",
                        robotBattleExePath,
                        loadListFile,
                        scoreLogFile,
                        statsLogFile
                        );

                    if (
                        !NativeMethods.CreateProcess(null, path, IntPtr.Zero, IntPtr.Zero, true,
                                                     0x00000020, /*NORMAL_PRIORITY_CLASS*/
                                                     IntPtr.Zero, null, ref si, ref pi))
                    {
                        throw new InvalidOperationException("Failed to launch the Robot Battle process");
                    }

                    Process.GetProcessById(pi.dwProcessId)
                    .WaitForExit();
                } finally {
                    NativeMethods.CloseDesktop(desktopPtr);
                }
            }
            else
            {
                Process.Start(new ProcessStartInfo {
                    FileName  = robotBattleExePath,
                    Arguments = string.Format(
                        "\"{1}\" /t /lf \"{2}\" /slf \"{3}\" /slg 1 /lo 1 /slo 1",
                        robotBattleExePath,
                        loadListFile,
                        scoreLogFile,
                        statsLogFile
                        )
                }).WaitForExit();
            }

            return(MatchResult.FromFiles(loadListFile, scoreLogFile, statsLogFile));
        }