【发布时间】:2011-08-08 08:42:21
【问题描述】:
在相当长的序言之后,我有 2 个问题。
通过查看void* 处的任何函数指针,我可以修改它的第一条指令,将它们转换为jmp(32 位相对或 64 位绝对,通过r11,取决于 x86/ x86-64)。我相信在 C 和 C++ 中将函数代码视为数据是非法的,但它似乎以一种不受支持的方式在 MSVC (Win32) 和 GCC (OS X) 中工作。网上有好几个地方说将函数指针转换为void*is illegal。
不只是简单地获得指向类成员的指针。我的意思是,编译器在尝试以与查看 void * 相同的方式查看此类指针时直接在构建时抛出错误,这种做法对于非成员函数似乎工作得很好。
幸运的是,为了挂钩 Direct3D9,我正在使用像 IDirect3DDevice9 这样的东西,它有一个 vtable。对于IDirect3DDevice9* 类型的pDev,我将pDev 视为PVOID* 就足够了。然后,pDev 的第一个值是函数指针数组的地址(vtable):
// IDirect3DDevice9::Present()
typedef HRESULT (CALLBACK *PRESENT_PROC)(
LPDIRECT3DDEVICE9, const RECT*,
const RECT*,
HWND,
const RGNDATA*
);
PVOID (*vPtr)[] = reinterpret_cast<PVOID (*)[]>(
*reinterpret_cast<PVOID*>(pDev)
);
PRESENT_PROC pDevicePresent = reinterpret_cast<PRESENT_PROC>(
(*vPtr)[17]
);
因为 Present 是第 18 个条目。
The first answer from here 提供了一种更优雅、更高级别的方法,从定义CINTERFACE 开始。我还没有测试过,但根据它我可以做类似的事情
reinterpret_cast<PVOID>(pDev->lpVtbl->Present)
没有错误。
第一个问题。我不是一个很棒的 C++ 程序员;一般来说,我如何获得一个指向成员函数的指针,以便我可以覆盖该函数的可执行字节。对于非会员,我这样做:
#include <windows.h>
#include <cstdio>
using namespace std;
const unsigned char OP_JMP = 0xE9; // 32 bit relative jmp
const SIZE_T SIZE_PATCH = 5; // jmp dword ptr distance; 1 byte + 4 bytes
typedef void (*MyProc)();
void SimpleFunction1()
{
printf("foo\n");
}
void SimpleFunction2()
{
printf("bar\n");
}
int main()
{
PBYTE foo = reinterpret_cast<PBYTE>(SimpleFunction1);
PBYTE bar = reinterpret_cast<PBYTE>(SimpleFunction2);
DWORD oldProtection;
// make sure the bytes of the function are writable
// by default they are only readable and executable
BOOL res = VirtualProtect(
foo,
SIZE_PATCH,
PAGE_EXECUTE_READWRITE,
&oldProtection
);
if (!res) return 1;
// be mindful of pointer arithmetic
// works with PBYTE, won't with PDWORD
DWORD distanceToNewFoo = bar - foo - SIZE_PATCH;
*foo = OP_JMP;
*reinterpret_cast<PDWORD>(foo + 1) = distanceToNewFoo;
// called though the pointer instead of foo()
// to make sure the compiler won't inline or do some other stupid stuff
reinterpret_cast<MyProc>(foo)(); // will print "bar\n"
return 0;
}
和 x86-64 的内容相同。对于对象的虚拟成员,我从vtable 本身获得foo 指针,如上所示:
reinterpret_cast<FUNC_TYPE>(
*(reinterpret_cast<void**>(
*reinterpret_cast<void**>(objptr)) + n
)
)
第二个问题。我不能简单地修改 vtable 中的条目为我的对象吗?这是一个例子,不用说,它不适用于直接取自 Direct3D 的 pDev 对象,但 Taksi 似乎使用这种方法:
#include <cstdio>
using namespace std;
class BaseClass
{
public:
BaseClass(int a = 0, int b = 0);
int GetA();
int GetB();
virtual void Test();
private:
int _a;
int _b;
};
BaseClass::BaseClass(int a, int b) :
_a(a),
_b(b)
{
}
int BaseClass::GetA()
{
return _a;
}
int BaseClass::GetB()
{
return _b;
}
void BaseClass::Test()
{
printf("test %d; %d\n", _a, _b);
}
void TheNewFunction(BaseClass *bc)
{
printf("I am an intruder\n");
}
typedef void (*PROC_TYPE)(BaseClass *);
int main()
{
BaseClass foo(5, 56);
PROC_TYPE proc = 0;
proc = reinterpret_cast<PROC_TYPE>(
*reinterpret_cast<void**>(
*reinterpret_cast<void**>(&foo)
)
);
proc(&foo);
reinterpret_cast<void**>(
*reinterpret_cast<void**>(&foo)
)[0] = reinterpret_cast<void*>(TheNewFunction);
foo.Test(); // runs same old Test(); maybe due to compiler optimization?
proc = reinterpret_cast<PROC_TYPE>(
*reinterpret_cast<void**>(
*reinterpret_cast<void**>(&foo)
)
);
proc(&foo); // runs TheNewFunction
BaseClass *goo = &foo;
goo->Test(); // runs TheNewFunction
return 0;
}
【问题讨论】: