Skip to content

2.2 Hook: Relocation Hook

DK edited this page Sep 24, 2023 · 4 revisions

RelHook

Very simple relocation hooks that replaces call/jmp instruction with a hook.

// target instruction:
// ...
// 0x7FF712345675   mov rcx, rax 
// 0x7FF712345678   call Game.exe+0x123456
// ^ this is in fact a 5 byte call [rip + offset] instruction
// ....

class Hook
{
    // hook function
    static bool Hook_123456(void* a_gameInstance)
    {
        return func(a_gameInstance);
    }

    // original function
    static inline std::add_pointer_t<decltype(Hook_123456)> func;

public:
    static void Install()
    {
        dku::Hook::Trampoline::AllocTrampoline(1 << 6); // this is global, once
        auto addr = dku::Hook::Module::get().base() + 0x345678;
        // or absolute
        auto addr = 0x7FF712345678;

        // save original function
        func = dku::Hook::write_call<5>(addr, Hook_123456);
    }
};