gpt4 book ai didi

c++ - 如何破解虚拟表?

转载 作者:IT老高 更新时间:2023-10-28 22:33:07 25 4
gpt4 key购买 nike

我想知道如何将虚拟表中的Test的地址更改为HackedVTable的地址。

void HackedVtable()
{
cout << "Hacked V-Table" << endl;
}

class Base
{
public:
virtual Test() { cout <<"base"; }
virtual Test1() { cout << "Test 1"; }
void *prt;
Base(){}
};

class Derived : public Base
{
public:
Test()
{
cout <<"derived";
}
};

int main()
{
Base b1;

b1.Test(); // how to change this so that `HackedVtable` should be called instead of `Test`?

return 0;
}

最佳答案

这适用于 32 位 MSVC 构建(它是一些已使用一年多的生产代码的非常简化的版本)。请注意,您的替换方法必须明确指定 this 参数(指针)。

// 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<FunctionType*>(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<FunctionType>(&ReplacementMethod);

if ( ::VirtualProtectEx( process, &vtable[VTableOffset], sizeof(int), oldProtection, &oldProtection ) )
hooked = true;
}

关于c++ - 如何破解虚拟表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1542108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com