gpt4 book ai didi

C++函数钩子(Hook)(dll, asm)

转载 作者:行者123 更新时间:2023-11-30 03:06:15 25 4
gpt4 key购买 nike

我编写了一个 dll。在这个dll中,我想 Hook 另一个dll的函数加载到内存中。这是许多小时工作的结果:

typedef int (__fastcall *def_cry)(int a,int b,int fromlen);
def_cry Real_cry;
int __fastcall custom_cry(int a,int b,int fromlen) {
Log("cry ...");
__asm nop;
return Real_cry(a, b, fromlen);
}
DWORD imageBaseOtherDll = 0x39500000;
DWORD functionOffset = 0x395742F8;
DWORD imageBase = (DWORD)GetModuleHandle("otherDll.dll");
DWORD functionOffset = imageBase + (functionOffset - imageBaseOtherDll);
Real_cry = (def_cry)DetourFunction((PBYTE)functionOffset,(PBYTE)&custom_cry);

看来,我的钩子(Hook)不起作用了。我想我在代码中加入了一些逻辑错误,但我是初学者,需要帮助!

最佳答案

你确定你 Hook 的函数使用了__fastcall吗?调用约定?

为了 Hook 从 DLL 导出的函数,您需要修补调用它的所有模块(dll/exe)的导入表,在运行时重写函数入口点。可以在 CodeProject here 上找到一篇关于修补导入表的不错的文章 here 可以找到有关使用 MS Detours 的很好的教程.

您需要在调用 DetourFunction 时提供要 Hook 的函数的地址。这些值不应被硬编码,因为在您的 DLL 中不能保证加载到特定地址。使用以下代码可以很容易地完成此操作:

// Get the module containing the function to hook
HMODULE targetModule = GetModuleHandle("otherDll.dll");
// Get the address of the function to hook
FARPROC targetFunction = GetProcAddress(targetModule, "cry");
// Go hook it.
Real_cry = (def_cry)DetourFunction((PBYTE)targetFunction,(PBYTE)&custom_cry);

关于C++函数钩子(Hook)(dll, asm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824136/

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