IAT挂钩,但钩子函数没有被调用

时间:2012-05-13 02:53:37

标签: c++ c windows api hook

我正在编写代码以在Windows中执行IAT的挂钩。我可以在IAT(Kernel32!GetCurrentProcessId)中更改目标函数的地址,但是稍后在程序中,当钩子函数被称为Kernel32时!调用GetCurrentProcessId而不是钩子。

在调试过程中,我能够看到内核的原始IAT地址!GetCurrentProcessId:

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
);

}

有什么想法?谢谢。

2 个答案:

答案 0 :(得分:2)

利用CreateToolhelp32Snapshot API我能够挂钩所有IAT的函数调用(没有在注入的DLL IAT中插入钩子,因为这会导致崩溃)到我GetCurrentProcessId() Helloworld编写的程序只是每隔几秒就会报告一次进程ID。注入DLL并挂钩GetCurrentProcessId() Helloworld后,开始按预期调用钩子函数。在我的研究过程中,我确实发现了一些信息,说明为什么IAT挂钩在某些情况下由于现代程序中的内置防御而无法工作:

http://www.codeproject.com/Articles/12516/Win32-API-hooking-Another-reason-why-it-might-not
http://www.codeproject.com/Articles/21414/Powerful-x86-x64-Mini-Hook-Engine

答案 1 :(得分:0)

也许 exe 有一个打包的自执行代码。如果是这种情况,请尝试在启动后或调用该函数后注入它。