private void InitializeAntiCheatHook() { byte[] bytes = HookPtr.GetReturnToPtr(); MemoryModule module = TargetFuncPtr.GetMyModule(); if (module == null) { throw new NullReferenceException("Cannot find a module which belongs to the specified pointer."); } MemoryAddress codeCave = module.FindCodeCaveInModule((uint)bytes.Length); CodeCavePatch = new MemoryPatch(codeCave, bytes); byte[] retToCodeCave = CodeCavePatch.PatchAddress.GetReturnToPtr(); HookPatch = new MemoryPatch(TargetFuncPtr, retToCodeCave); }
/// <summary> /// Initializes a new instance of the <see cref="Hook" /> class. /// /// A hook can be used to create a detour from an original function into your own function. /// You can then proceed to call the original function by using the method <see cref="CallOriginal" />. /// </summary> /// <param name="target">The target delegate we want to detour.</param> /// <param name="hook">The hook delegate where want it to go.</param> public Hook(bool useAntiCheatHook = true) { try { TargetDelegate = GetHookDelegate(); HookDelegate = GetDetourDelegate(); TargetFuncPtr = TargetDelegate.ToFunctionPtr(); HookPtr = HookDelegate.ToFunctionPtr(); byte[] function = HookPtr.CreateFunctionCall(); MemoryPointer codeCavePtr = useAntiCheatHook ? CreateAntiCheatCodeCave(function) : AllocateMemory(function); FillMemoryPointer(codeCavePtr, function); } catch (Exception ex) { LoggingService.Error($"Hook {ToString()}, could not be initialized: {ex.Message}"); } }