Beispiel #1
0
        public static bool Load(IEnumerable <GetHookPathDelegate> getHookPathCollection, string programPath, string cmdLine, string environment = null, bool showErrorMessage = true)
        {
            bool bOk = true;

            try
            {
                string programOptions   = "\"" + programPath + "\" " + cmdLine;
                string programDirectory = System.IO.Path.GetDirectoryName(programPath);

                PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();

                STARTUPINFOW startupInfo = new STARTUPINFOW();
                startupInfo.cb = (UInt32)Marshal.SizeOf(startupInfo);

                if (!CreateProcessW(
                        programPath
                        , programOptions
                        , null
                        , null
                        , true // inherit handles
                        ,      //CREATE_DEFAULT_ERROR_MODE|
                        CREATE_NEW_PROCESS_GROUP |
                        DETACHED_PROCESS |
                        CREATE_SUSPENDED
                        //DEBUG_ONLY_THIS_PROCESS|
                        //DEBUG_PROCESS				// we want to catch debug event's (sadly also of childs)
                        | CREATE_UNICODE_ENVIRONMENT
                        , environment
                        , programDirectory
                        , ref startupInfo
                        , out processInfo)
                    )
                {
                    throw new System.ApplicationException("Failed to launch program, error code: " + Marshal.GetLastWin32Error());
                }

                try
                {
                    bool isProcess64Bit = IsProcess64Bit(processInfo.hProcess);

                    foreach (GetHookPathDelegate getHookPath in getHookPathCollection)
                    {
                        string hookPath = getHookPath(isProcess64Bit);

                        using (System.Diagnostics.Process injector = new System.Diagnostics.Process())
                        {
                            injector.StartInfo.UseShellExecute = false;
                            injector.StartInfo.FileName        = System.AppDomain.CurrentDomain.BaseDirectory + (isProcess64Bit ? "\\x64" : "") + "\\injector.exe";
                            injector.StartInfo.CreateNoWindow  = true;
                            injector.StartInfo.Arguments       = processInfo.dwProcessId.ToString() + " " + hookPath;

                            try
                            {
                                injector.Start();
                            }
                            catch (Exception e)
                            {
                                throw HlaeErrors.InjectorStartException(
                                          injector.StartInfo.FileName,
                                          e
                                          );
                            }

                            injector.WaitForExit();

                            if (0 != injector.ExitCode)
                            {
                                throw InjectorErrors.Instance.GetById(injector.ExitCode);
                            }
                        }
                    }
                }
                finally
                {
                    System.Threading.Thread.Sleep(2000);

                    ResumeThread(processInfo.hThread);

                    CloseHandle(processInfo.hThread);
                    CloseHandle(processInfo.hProcess);
                }
            }
            catch (Exception e)
            {
                if (showErrorMessage)
                {
                    using (ErrorDialogue frm = new ErrorDialogue())
                    {
                        frm.Error = HlaeErrors.LoaderException(e);
                        frm.ShowDialog();
                    }
                }

                return(false);
            }

            return(bOk);
        }
Beispiel #2
0
        public static bool Load(IEnumerable <GetHookPathDelegate> getHookPathCollection, string programPath, string cmdLine, string environment = null, bool showErrorMessage = true)
        {
            try
            {
                string programOptions   = "\"" + programPath + "\" " + cmdLine;
                string programDirectory = System.IO.Path.GetDirectoryName(programPath);

                PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();

                STARTUPINFOW startupInfo = new STARTUPINFOW();
                startupInfo.cb = (UInt32)Marshal.SizeOf(startupInfo);

                if (!CreateProcessW(
                        programPath
                        , programOptions
                        , null
                        , null
                        , true // inherit handles
                        ,      //CREATE_DEFAULT_ERROR_MODE|
                        CREATE_NEW_PROCESS_GROUP |
                        DETACHED_PROCESS |
                        CREATE_SUSPENDED
                        //DEBUG_ONLY_THIS_PROCESS|
                        //DEBUG_PROCESS				// we want to catch debug event's (sadly also of childs)
                        | CREATE_UNICODE_ENVIRONMENT
                        , environment
                        , programDirectory
                        , ref startupInfo
                        , out processInfo)
                    )
                {
                    throw HlaeErrors.LoaderCreateProcessException(Marshal.GetLastWin32Error());
                }

                try
                {
                    bool isProcess64Bit = IsProcess64Bit(processInfo.hProcess);

                    foreach (GetHookPathDelegate getHookPath in getHookPathCollection)
                    {
                        string hookPath = getHookPath(isProcess64Bit);

                        using (System.Diagnostics.Process injector = new System.Diagnostics.Process())
                        {
                            injector.StartInfo.UseShellExecute        = false;
                            injector.StartInfo.FileName               = System.AppDomain.CurrentDomain.BaseDirectory + (isProcess64Bit ? "\\x64" : "") + "\\injector.exe";
                            injector.StartInfo.CreateNoWindow         = true;
                            injector.StartInfo.RedirectStandardInput  = true;
                            injector.StartInfo.RedirectStandardOutput = true;

                            try
                            {
                                injector.Start();
                            }
                            catch (Exception e)
                            {
                                throw HlaeErrors.InjectorStartException(
                                          injector.StartInfo.FileName,
                                          e
                                          );
                            }

                            AfxError error = null;

                            IFormatter formatter = new advancedfx.injector.interop.Formatter();

                            using (Stream injectorIn = injector.StandardInput.BaseStream)
                            {
                                using (Stream injectorOut = injector.StandardOutput.BaseStream)
                                {
                                    advancedfx.injector.interop.InjectMessage injectMessage = new advancedfx.injector.interop.InjectMessage();
                                    injectMessage.ProcessId = processInfo.dwProcessId;
                                    injectMessage.DllPath   = hookPath;

                                    formatter.Serialize(injectorIn, injectMessage);
                                    injectorIn.Flush();

                                    bool injectorExit = false;

                                    while (!injectorExit)
                                    {
                                        advancedfx.injector.interop.ProcessMessage m = (advancedfx.injector.interop.ProcessMessage)formatter.Deserialize(injectorOut);

                                        switch (m)
                                        {
                                        case advancedfx.injector.interop.ExceptionError exceptionError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.Unknown;
                                            }
                                            break;

                                        case advancedfx.injector.interop.OpenProcessError openProcessError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.OpenProcessFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.VirtualAllocExArgDllDirError virtualAllocExArgDllDirError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.VirtualAllocExReadWriteFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.VirtualAllocExArgDllFilePathError virtualAllocExArgDllFilePathError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.VirtualAllocExReadWriteFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.GetImageError getImageError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.GetImageFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.VirtualAllocExImageError virtualAllocExImageError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.VirtualAllocExReadWriteExecuteFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.WriteProcessMemoryArgDllDirError writeProcessMemoryArgDllDirError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.WriteProcessMemoryFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.WriteProcessMemoryArgDllFilePathError writeProcessMemoryArgDllFilePathError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.WriteProcessMemoryFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.WriteProcessMemoryImageError writeProcessMemoryImageError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.WriteProcessMemoryFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.FlushInstructionCacheError flushInstructionCacheError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.FlushInstructionCacheFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.CreateRemoteThreadError createRemoteThreadError:
                                            if (null == error)
                                            {
                                                error = HlaeErrors.CreateRemoteThreadFailed;
                                            }
                                            break;

                                        case advancedfx.injector.interop.ContinueWaitingQuestion contineWaitingQuestion:
                                        {
                                            advancedfx.injector.interop.ContinueWaiting r = new advancedfx.injector.interop.ContinueWaiting();
                                            r.Response = DialogResult.Yes == MessageBox.Show(L10n._("Image injection problem.\nContinue waiting?"), L10n._("injector Warning"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                                            formatter.Serialize(injectorIn, r);
                                            injectorIn.Flush();
                                        }
                                        break;

                                        case advancedfx.injector.interop.TerminateThreadError terminateThreadError:
                                            // ignore for now
                                            break;

                                        case advancedfx.injector.interop.GetExitCodeThreadError getExitCodeThreadError:
                                            // ignore for now
                                            break;

                                        case advancedfx.injector.interop.InvalidExitCodeError invalidExitCodeError:
                                            if (null == error)
                                            {
                                                error = InjectorErrors.AfxHookUnknown;
                                            }
                                            break;

                                        case advancedfx.injector.interop.KnownExitCodeError knownExitCodeError:
                                            if (null == error)
                                            {
                                                error = InjectorErrors.Instance.GetById((int)knownExitCodeError.ThreadExitCode);
                                            }
                                            break;

                                        case advancedfx.injector.interop.CloseHandleThreadError closeHandleError:
                                            // ignore for now
                                            break;

                                        case advancedfx.injector.interop.VirtualFreeExImageError virtualFreeExImageError:
                                            // ignore for now
                                            break;

                                        case advancedfx.injector.interop.VirtualFreeExArgFilePathError virtualFreeExArgFilePathError:
                                            // ignore for now
                                            break;

                                        case advancedfx.injector.interop.VirtualFreeExArgDllDirError virtualFreeExArgDllDirError:
                                            // ignore for now
                                            break;

                                        case advancedfx.injector.interop.CloseHandleProcessError closeHandleProcessError:
                                            // ignore for now
                                            break;

                                        case advancedfx.injector.interop.InjectResponse injectResponse:
                                            bool injectorOk = injectResponse.Response;
                                            injector.WaitForExit();
                                            if (!injectorOk)
                                            {
                                                throw null == error ? HlaeErrors.Unknown : error;
                                            }
                                            injectorExit = true;
                                            break;

                                        default:
                                            throw HlaeErrors.Unknown;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    System.Threading.Thread.Sleep(2000);

                    ResumeThread(processInfo.hThread);

                    CloseHandle(processInfo.hThread);
                    CloseHandle(processInfo.hProcess);
                }
            }
            catch (AfxError e)
            {
                if (showErrorMessage)
                {
                    using (ErrorDialogue frm = new ErrorDialogue())
                    {
                        frm.Error = e;
                        frm.ShowDialog();
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                if (showErrorMessage)
                {
                    using (ErrorDialogue frm = new ErrorDialogue())
                    {
                        frm.Error = HlaeErrors.LoaderException(e);
                        frm.ShowDialog();
                    }
                }

                return(false);
            }

            return(true);
        }