原帖:
我正在尝试从我注入的DLL中绕过一个成员函数。我有我试图挂钩的函数的地址,但无法弄清楚通过绕道库挂钩它的正确语法或方法。我已经评论了错误消息给我错误的行。
我已经阅读了成员函数挂钩的弯路示例的来源,这就是这段代码的基础,但由于某种原因,它无法正常工作。
非常感谢任何帮助,谢谢!
#include <windows.h>
#include <detours.h>
class CDetour
{
public:
bool My_MemFn(unsigned int unk1);
static bool (CDetour::* Real_MemFn)(unsigned int);
};
bool CDetour::My_MemFn(unsigned int unk1)
{
/* do stuff here */
return (this->*Real_MemFn)(unk1);
}
typedef bool (CDetour::* MemFn_t)(unsigned int unk1);
MemFn_t CDetour::Real_MemFn= *(MemFn_t *)((void*)0x23234545);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)CDetour::Real_MemFn, *(PBYTE*)&CDetour::My_MemFn); // ERROR: C2440: 'type cast' : cannot convert from 'bool __thiscall CDetour::* )(unsigned int)' to 'PBYTE *'
DetourTransactionCommit();
break;
}
}
return TRUE;
}
的解决方案: 的
#include <windows.h>
#include <detours.h>
typedef void (__thiscall * CClassFunction_t)(void *__this, unsigned int unk1);
CClassFunction_t Real_CClassFunction;
void __fastcall Mine_CClassFunction(void *__this, int edx, unsigned int unk1)
{
Real_CClassFunction(__this, unk1);
}
template<typename T>
void HookFunction(const char *module, char *signature, T &fn_real, PVOID fn_mine)
{
HookFunction<T>(DetourFindFunction(module, signature), fn_real, fn_mine);
}
template<typename T>
void HookFunction(DWORD address, T &fn_real, PVOID fn_mine)
{
HookFunction<T>(reinterpret_cast<PVOID>(address), fn_real, fn_mine);
}
template<typename T>
void HookFunction(PVOID target, T &fn_real, PVOID fn_mine)
{
fn_real = reinterpret_cast<T>(target);
HookFunction<T>(fn_real, fn_mine);
}
template<typename T>
void HookFunction(T &fn_real, PVOID fn_mine)
{
DetourAttach(&(PVOID&)fn_real, fn_mine);
}
void ApplyHooks()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DWORD function_address = 0x12345678;
HookFunction<CClassFunction_t>(address, Real_CClassFunction, Mine_CClassFunction);
DetourTransactionCommit();
}
BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
{
DisableThreadLibraryCalls(hInstance);
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ApplyHooks, 0, 0, 0);
break;
}
}
return TRUE;
}
答案 0 :(得分:2)
尝试使用:
union {
bool (CDetour::*lpMyFunction)(unsigned int);
PBYTE lpAddr;
} u;
将指针保存在u.lpMyFunction上并从u.lpAddr获取
这是否解决了编译问题?
答案 1 :(得分:2)
经过几个小时的搜索并试图找到解决方案后,我想出了一个很好的小解决方案:
#include <windows.h>
#include <detours.h>
typedef void (__thiscall * CClassFunction_t)(void *__this, unsigned int unk1);
CClassFunction_t Real_CClassFunction;
void __fastcall Mine_CClassFunction(void *__this, int edx, unsigned int unk1)
{
Real_CClassFunction(__this, unk1);
}
template<typename T>
void HookFunction(const char *module, char *signature, T &fn_real, PVOID fn_mine)
{
HookFunction<T>(DetourFindFunction(module, signature), fn_real, fn_mine);
}
template<typename T>
void HookFunction(DWORD address, T &fn_real, PVOID fn_mine)
{
HookFunction<T>(reinterpret_cast<PVOID>(address), fn_real, fn_mine);
}
template<typename T>
void HookFunction(PVOID target, T &fn_real, PVOID fn_mine)
{
fn_real = reinterpret_cast<T>(target);
HookFunction<T>(fn_real, fn_mine);
}
template<typename T>
void HookFunction(T &fn_real, PVOID fn_mine)
{
DetourAttach(&(PVOID&)fn_real, fn_mine);
}
void ApplyHooks()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DWORD function_address = 0x12345678;
HookFunction<CClassFunction_t>(address, Real_CClassFunction, Mine_CClassFunction);
DetourTransactionCommit();
}
BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
{
DisableThreadLibraryCalls(hInstance);
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ApplyHooks, 0, 0, 0);
break;
}
}
return TRUE;
}
答案 2 :(得分:0)
是的,我真的讨厌那些演员,所以我自己编造:
// Cast a member function pointer that cannot have a reference taken to a void *
template <typename RET_TYPE, typename CLASS, typename...ARGs>
void* castToVoidPtr(RET_TYPE(CLASS::*&&pOriginalFunction)(ARGs...))
{
union
{
RET_TYPE(CLASS::*pMemFn)(ARGs...);
void* voidPtr;
} cast = { pOriginalFunction };
static_assert(sizeof(cast.pMemFn) == sizeof(cast.voidPtr), "Cannot cast this member function pointer to a void*. Not the same size.");
return cast.voidPtr;
}
// Cast a member function pointer to a void*&
template <typename RET_TYPE, typename CLASS, typename...ARGs>
void*& castToVoidPtr(RET_TYPE(CLASS::*&pOriginalFunction)(ARGs...))
{
union
{
RET_TYPE(CLASS::*&pMemFn)(ARGs...);
void*& voidPtr;
} cast = { pOriginalFunction };
static_assert(sizeof(cast.pMemFn) == sizeof(cast.voidPtr), "Cannot cast this member function pointer to a void*. Not the same size.");
return cast.voidPtr;
}
您解决方案的唯一问题是您需要将额外的DWORD压入堆栈(EDX寄存器),这是不必要的。这些强制转换应该适用于您将使用它的所有情况。从我收集的内容来看,它不适用于多重继承的类函数,这不是您可能用于绕行的东西,并且如果您要尝试则会断言
这将允许您这样做:
DetourAttach(&castToVoidPtr(CDetour::Real_MemFn), castToVoidPtr(&CDetour::My_MemFn));
答案 3 :(得分:0)
"void* castToVoidPtr(RET_TYPE(CLASS::*&&pOriginalFunction)(ARGs...))"
这是&amp;&amp;你想写什么? VS2008在编译时出错。