Hooking mono_compile_method

1 day ago 1
ARTICLE AD BOX

I tried to create a cheat in C++ for the Unity Mono game. Since the game uses a Mono build, methods are compiled at runtime and don't have a static address. Because of this, I hooked mono_compile_method to track which methods are compiled. But the game crashes when this function is called (the addresses are first printed to the console, and then the game crashes). I checked Cheat Engine, and the hook is placed in the right place, but the hook function doesn't have a return address (in Cheat Engine). I'm not sure if this is normal. Here's my code:

#include "pch.h" #include <Minhook.h> #include <Windows.h> #include <iostream> void* (__fastcall* mono_compile_method_o)(void* method, DWORD a1, DWORD a2); void* mono_compile_method_h(void* method, DWORD a1, DWORD a2) { std::cout << "a1: " << a1 << std::endl; std::cout << "a2: " << a2 << std::endl; std::cout << "method: " << method << std::endl; return mono_compile_method_o(method, a1, a2); } void InitHooks() { uintptr_t monoDll = (uintptr_t)GetModuleHandle(L"mono.dll"); uintptr_t mono_compile_method_address = (uintptr_t)GetProcAddress((HMODULE)monoDll, "mono_compile_method"); std::cout << mono_compile_method_address << std::endl; MH_Initialize(); MH_CreateHook(reinterpret_cast<LPVOID*>(mono_compile_method_address), &mono_compile_method_h, (LPVOID*)mono_compile_method_o); MH_EnableHook(MH_ALL_HOOKS); return; }
Read Entire Article