InstallHook( IntPtr TargetAddr, IntPtr HookHandlerAddr, IntPtr TrampolineAddr ) { // // This code expects either MinHook.x86.dll or MinHook.x64.dll is // located in any of the DLL search path. Such as the current folder // and %PATH%. // string architecture = (IntPtr.Size == 4) ? "x86" : "x64"; string dllPath = "MinHook." + architecture + ".dll"; IntPtr moduleHandle = LoadLibrary(dllPath); if (moduleHandle == IntPtr.Zero) { Console.WriteLine("[-] An inline hook DLL not found. Did you locate " + dllPath + " under the DLL search path?"); return(false); } var MH_Initialize = GetExport <MH_InitializeType>(moduleHandle, "MH_Initialize"); var MH_CreateHook = GetExport <MH_CreateHookType>(moduleHandle, "MH_CreateHook"); var MH_EnableHook = GetExport <MH_EnableHookType>(moduleHandle, "MH_EnableHook"); MH_STATUS status = MH_Initialize(); Trace.Assert(status == MH_STATUS.MH_OK); // // Modify the target method to jump to the HookHandler method. The // original receives an address of trampoline code to call the // original implementation of the target method. // status = MH_CreateHook(TargetAddr, HookHandlerAddr, out IntPtr original); Trace.Assert(status == MH_STATUS.MH_OK); // // Modify the Trampoline method to jump to the original // implementation of the target method. // status = MH_CreateHook(TrampolineAddr, original, out _); Trace.Assert(status == MH_STATUS.MH_OK); // // Commit and activate the above two hooks. // status = MH_EnableHook(MH_ALL_HOOKS); Trace.Assert(status == MH_STATUS.MH_OK); return(true); }
private Exception CheckError(MH_STATUS status, bool throwOnError = true) { if (status == MH_STATUS.MH_OK) { return(null); } var ex = new Exception(status.ToString()); if (throwOnError) { throw ex; } return(ex); }
private TimeTravelerException CheckError(MH_STATUS status, bool throwOnError = true, [CallerMemberName] string methodName = null) { if (status == MH_STATUS.MH_OK) { return(null); } var ex = new TimeTravelerException(status.ToString()); if (throwOnError) { throw ex; } return(ex); }