public bool SetupEndsceneHook() { AmeisenLogger.Instance.Log("HookManager", "Setting up the EndsceneHook...", LogLevel.Verbose); EndsceneAddress = GetEndScene(); // first thing thats 5 bytes big is here // we are going to replace this 5 bytes with // our JMP instruction (JMP (1 byte) + Address (4 byte)) EndsceneAddress = IntPtr.Add(EndsceneAddress, ENDSCENE_HOOK_OFFSET); EndsceneReturnAddress = IntPtr.Add(EndsceneAddress, 0x5); AmeisenLogger.Instance.Log("HookManager", $"Endscene is at: {EndsceneAddress.ToString("X")}", LogLevel.Verbose); // if WoW is already hooked, unhook it if (IsWoWHooked) { DisposeHook(); } else { if (WowInterface.XMemory.ReadBytes(EndsceneAddress, 5, out byte[] bytes))
private void SetupEndsceneHook() { // first thing thats 5 bytes big is here // we are going to replace this 5 bytes with // our JMP instruction (JMP (1 byte) + Address (4 byte)) EndsceneAddress += ENDSCENE_HOOK_OFFSET; // if WoW is already hooked, unhook it if (IsWoWHooked) { DisposeHook(); } else { originalEndsceneBytes = TrashMem.ReadChars(EndsceneAddress, 5); } // if WoW is now/was unhooked, hook it if (!IsWoWHooked) { AmeisenBotLogger.Instance.Log($"[{ProcessId.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}]\tHooking EndScene at \"0x{EndsceneAddress.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}\"", LogLevel.Verbose); // the address that we will return to after // the jump wer'e going to inject EndsceneReturnAddress = EndsceneAddress + 0x5; // integer to check if there is code waiting to be executed CodeToExecuteAddress = TrashMem.AllocateMemory(4); TrashMem.Write(CodeToExecuteAddress.Address, 0); // integer to save the address of the return value ReturnValueAddress = TrashMem.AllocateMemory(4); TrashMem.Write(ReturnValueAddress.Address, 0); // codecave to check if we need to execute something CodecaveForCheck = TrashMem.AllocateMemory(128); AmeisenBotLogger.Instance.Log($"[{ProcessId.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}]\tCCCheck is at \"0x{CodecaveForCheck.Address.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}\"", LogLevel.Verbose); // codecave for the code we wa't to execute CodecaveForExecution = TrashMem.AllocateMemory(2048); AmeisenBotLogger.Instance.Log($"[{ProcessId.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}]\tCCExecution is at \"0x{CodecaveForExecution.Address.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}\"", LogLevel.Verbose); TrashMem.Asm.Clear(); // save registers TrashMem.Asm.AddLine("PUSHFD"); TrashMem.Asm.AddLine("PUSHAD"); // check for code to be executed TrashMem.Asm.AddLine($"MOV EBX, [0x{CodeToExecuteAddress.Address.ToString("X")}]"); TrashMem.Asm.AddLine("TEST EBX, 1"); TrashMem.Asm.AddLine("JE @out"); // execute our stuff and get return address TrashMem.Asm.AddLine($"MOV EDX, 0x{CodecaveForExecution.Address.ToString("X")}"); TrashMem.Asm.AddLine("CALL EDX"); TrashMem.Asm.AddLine($"MOV [0x{ReturnValueAddress.Address.ToString("X")}], EAX"); // finish up our execution TrashMem.Asm.AddLine("@out:"); TrashMem.Asm.AddLine("MOV EDX, 0"); TrashMem.Asm.AddLine($"MOV [0x{CodeToExecuteAddress.Address.ToString("X")}], EDX"); // restore registers TrashMem.Asm.AddLine("POPAD"); TrashMem.Asm.AddLine("POPFD"); byte[] asmBytes = TrashMem.Asm.Assemble(); // needed to determine the position where the original // asm is going to be placed int asmLenght = asmBytes.Length; // inject the instructions into our codecave TrashMem.Asm.Inject(CodecaveForCheck.Address); // --------------------------------------------------- // End of the code that checks if there is asm to be // executed on our hook // --------------------------------------------------- // Prepare to replace the instructions inside WoW TrashMem.Asm.Clear(); // do the original EndScene stuff after we restored the registers // and insert it after our code TrashMem.WriteBytes(CodecaveForCheck.Address + (uint)asmLenght, originalEndsceneBytes); // return to original function after we're done with our stuff TrashMem.Asm.AddLine($"JMP 0x{EndsceneReturnAddress.ToString("X")}"); TrashMem.Asm.Inject(CodecaveForCheck.Address + (uint)asmLenght + 5); TrashMem.Asm.Clear(); // --------------------------------------------------- // End of doing the original stuff and returning to // the original instruction // --------------------------------------------------- // modify original EndScene instructions to start the hook TrashMem.Asm.AddLine($"JMP 0x{CodecaveForCheck.Address.ToString("X")}"); TrashMem.Asm.Inject(EndsceneAddress); AmeisenBotLogger.Instance.Log($"[{ProcessId.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}]\tInjected Hook [IsWoWHooked = {IsWoWHooked}]", LogLevel.Verbose); // we should've hooked WoW now } }