【发布时间】:2012-05-13 02:53:36
【问题描述】:
我正在编写代码以在 Windows 中执行 IAT 挂钩。我可以在 IAT (Kernel32!GetCurrentProcessId) 中更改目标函数的地址,但是稍后在程序中调用挂钩函数时会调用 Kernel32!GetCurrentProcessId 而不是挂钩。
在调试过程中,我可以看到 Kernel!GetCurrentProcessId 的原始 IAT 地址:
GetCurrentProcessId 地址:7C8099C0
我要换入的函数是:
MyGetCurrentProcessId 地址:100118BB
我挂钩 thunkIAT->u1.Function 的地址并将其从 7C8099C0 更改为 100118BB,但是正如我之前提到的,当从程序中调用 GetCurrentProcessId() 时,会调用 Kernel32 函数(不是我注入的那个)。
执行钩子的部分代码是:
if(strcmp(apiName,(char*)(*nameData).Name)==0)
{
DBG_PRINT2("[processImportDescriptor]: found match for %s\n", apiName);
VirtualProtect(
&thunkIAT->u1.Function, // start addres of the zone to "unlock"
0x010, // size to protect
PAGE_EXECUTE_READWRITE, // new permission
&dwOldProtect // old permission
);
procPtr = MyGetCurrentProcessId;
thunkIAT->u1.Function = (DWORD)procPtr;
DBG_PRINT2("MyGetCurrentProcessId() address: %08X\n", MyGetCurrentProcessId);
DBG_PRINT2("procPtr address: %08X\n", procPtr);
DBG_PRINT2("thunkIAT->u1.Function address: %08X\n", thunkIAT->u1.Function);
VirtualProtect(
&thunkIAT->u1.Function, // start addres of the zone to "relock"
0x0010, // size to protect
dwOldProtect, // new permission
&dwOldProtect2 // old permission
);
}
有什么想法吗?谢谢。
【问题讨论】:
-
一方面,被调用的函数可能是通过
GetProcAddress检索的,即使二进制文件导入了它。但问题本身太宽泛,您提供的信息很少。 -
您是否有机会注入 DLL?如果是这样,您可能需要注意可执行文件中的每个模块都有自己的 IAT。这意味着您可能刚刚挂钩了您的 DLL 模块的 IAT,而不是主可执行文件的 IAT。您可以使用
Create32Toolhelp32Snapshot/Module32First/Next遍历模块并将它们全部挂钩。 -
感谢您的意见,谢谢。为了详细说明,我正在使用 CreateRemoteThread 将 DLL 注入到 mspaint.exe 中。 DLL 中的代码能够遍历 mspaint 中的 IAT(大约十几个,包括 Kernel32 IAT)。在 DLL 中,我直接在“case DLL_THREAD_DETACH:”中调用 GetCurrentProcessId()(在实现挂钩之后)。使用 Windbg 单步执行程序,似乎对 GetCurrentProcessId() 的调用不会调用 IAT 中的查找(暗示该值可能是从较早的调用中缓存的?)。仍然plugg'n离开。谢谢。