private static bool CheckError(bool condition, InjectorErrors.InjectorError onError, ref InjectorErrors.InjectorError resultError) { if (null != resultError) { return(false); } if (condition) { return(true); } resultError = onError; return(false); }
static int Main(string[] args) { InjectorErrors.InjectorError result = null; string strArgs = string.Join(" ", args); try { int delimiterPos = strArgs.IndexOf(" "); string strDwProcessId = strArgs.Substring(0, delimiterPos); UInt32 dwProcessId = UInt32.Parse(strDwProcessId); string strDllPath = strArgs.Substring(delimiterPos + 1); result = Injector.Inject(dwProcessId, strDllPath); } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.ToString(), "injector Error", MessageBoxButtons.OK, MessageBoxIcon.Error); result = InjectorErrors.Unknown; } return((int)(null == result ? 0 : result.Code)); }
public static InjectorErrors.InjectorError Inject(UInt32 dwProcessId, string dllPath) { string baseDirectory = System.IO.Path.GetDirectoryName(dllPath); byte[] datDllPath = Encoding.Unicode.GetBytes(dllPath + "\0"); byte[] datBaseDirectory = Encoding.Unicode.GetBytes(baseDirectory + "\0"); byte[] image = null; IntPtr argDllDir = IntPtr.Zero; IntPtr argDllFilePath = IntPtr.Zero; UIntPtr dllDirectorySz = new UIntPtr((ulong)datBaseDirectory.LongLength); UIntPtr dllFilePathSz = new UIntPtr((ulong)datDllPath.LongLength); UIntPtr imageSz = UIntPtr.Zero; IntPtr hProc = IntPtr.Zero; IntPtr hThread = IntPtr.Zero; IntPtr imageAfxHook = IntPtr.Zero; InjectorErrors.InjectorError error = null; bool bOk = true; try { bOk = true && CheckError(IntPtr.Zero != (hProc = OpenProcess(createThreadAccess, false, dwProcessId)), InjectorErrors.OpenProcessFailed, ref error) && CheckError(IntPtr.Zero != (argDllDir = VirtualAllocEx(hProc, IntPtr.Zero, dllDirectorySz, AllocationType.Reserve | AllocationType.Commit, MemoryProtection.ReadWrite)), InjectorErrors.VirtualAllocExReadWriteFailed, ref error) && CheckError(IntPtr.Zero != (argDllFilePath = VirtualAllocEx(hProc, IntPtr.Zero, dllFilePathSz, AllocationType.Reserve | AllocationType.Commit, MemoryProtection.ReadWrite)), InjectorErrors.VirtualAllocExReadWriteFailed, ref error) && CheckError(null != (image = GetImage(m_PGetModuleHandleW, m_PGetProcAddress, argDllDir, argDllFilePath)), InjectorErrors.GetImageFailed, ref error) ; if (bOk) { imageSz = new UIntPtr((ulong)image.LongLength); bOk = true && CheckError(IntPtr.Zero != (imageAfxHook = VirtualAllocEx(hProc, IntPtr.Zero, imageSz, AllocationType.Reserve | AllocationType.Commit, MemoryProtection.ExecuteReadWrite)), InjectorErrors.VirtualAllocExReadWriteExecuteFailed, ref error) && CheckError(WriteProcessMemory(hProc, argDllDir, datBaseDirectory, dllDirectorySz, IntPtr.Zero), InjectorErrors.WriteProcessMemoryFailed, ref error) && CheckError(WriteProcessMemory(hProc, argDllFilePath, datDllPath, dllFilePathSz, IntPtr.Zero), InjectorErrors.WriteProcessMemoryFailed, ref error) && CheckError(WriteProcessMemory(hProc, imageAfxHook, image, imageSz, IntPtr.Zero), InjectorErrors.WriteProcessMemoryFailed, ref error) && CheckError(FlushInstructionCache(hProc, imageAfxHook, imageSz), InjectorErrors.FlushInstructionCacheFailed, ref error) && CheckError(IntPtr.Zero != (hThread = CreateRemoteThread(hProc, IntPtr.Zero, UIntPtr.Zero, imageAfxHook, IntPtr.Zero, 0, IntPtr.Zero)), InjectorErrors.CreateRemoteThreadFailed, ref error) ; if (bOk) { bOk = false; bool bWait; do { bWait = false; for (int i = 0; i < 60; i++) { if (WAIT_OBJECT_0 == WaitForSingleObject(hThread, 1000)) { bOk = true; break; } } if (!bOk) { bWait = DialogResult.Yes == MessageBox.Show("Image injection problem.\nContinue waiting?", "injector Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); } } while (bWait); if (!bOk) { TerminateThread(hThread, 1); } else { bOk = GetExitCodeThread(hThread, out UInt32 exitCode); if (bOk) { if (0 != exitCode) { bOk = false; if (1 <= exitCode && exitCode <= 15) { error = InjectorErrors.Instance.GetById((int)exitCode); } else { error = InjectorErrors.AfxHookUnknown; MessageBox.Show("Unknown AfxHook exit code: " + exitCode.ToString(), "injector Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } } } } finally { if (IntPtr.Zero != hThread) { CloseHandle(hThread); } if (IntPtr.Zero != imageAfxHook) { VirtualFreeEx(hProc, imageAfxHook, UIntPtr.Zero, AllocationType.Release); } if (IntPtr.Zero != argDllDir) { VirtualFreeEx(hProc, argDllFilePath, UIntPtr.Zero, AllocationType.Release); } if (IntPtr.Zero != argDllFilePath) { VirtualFreeEx(hProc, argDllDir, UIntPtr.Zero, AllocationType.Release); } if (IntPtr.Zero != hProc) { CloseHandle(hProc); } } CheckError(bOk, InjectorErrors.Unknown, ref error); return(error); }