How to hack the virtual table?

前端 未结 9 877
谎友^
谎友^ 2021-01-31 21:41

I would like to know how to change the address of Test which is in the virtual table with that of HackedVTable.

void HackedVtable()
{
           


        
9条回答
  •  粉色の甜心
    2021-01-31 22:04

    This works for 32-bit MSVC builds (it's a very simplified version of some production code that's been in use for well over a year). Note that your replacement method must explicitly specify the this parameter (pointer).

    // you can get the VTable location either by dereferencing the
    // first pointer in the object or by analyzing the compiled binary.
    unsigned long VTableLocation = 0U;
    // then you have to figure out which slot the function is in. this is easy
    // since they're in the same order as they are declared in the class definition.
    // just make sure to update the index if 1) the function declarations are
    // re-ordered and/or 2) virtual methods are added/removed from any base type.
    unsigned VTableOffset = 0U;
    typedef void (__thiscall Base::*FunctionType)(const Base*);
    FunctionType* vtable = reinterpret_cast(VTableLocation);
    
    bool hooked = false;
    HANDLE process = ::GetCurrentProcess();
    DWORD protection = PAGE_READWRITE;
    DWORD oldProtection;
    if ( ::VirtualProtectEx( process, &vtable[VTableOffset], sizeof(int), protection, &oldProtection ) )
    {
        vtable[VTableOffset] = static_cast(&ReplacementMethod);
    
        if ( ::VirtualProtectEx( process, &vtable[VTableOffset], sizeof(int), oldProtection, &oldProtection ) )
            hooked = true;
    }
    

提交回复
热议问题