2012-05-13 91 views
4

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

在调试过程中我能看到原来的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 
); 

} 

任何想法?谢谢。

+0

原因之一很可能是得到函数调用即使二进制文件导入它,也会通过'GetProcAddress'获取。但是,这个问题本身太宽泛了,你提供的信息很少。 – 0xC0000022L

+0

你是否注入了DLL?如果是这样,你可能要注意的是,可执行文件内的每个模块都有自己的IAT。这意味着你可能刚刚挂钩了你的DLL模块的IAT而不是主要的可执行文件。你可以用'Create32Toolhelp32Snapshot' /'Module32First' /'Next'遍历模块,并将它们全部挂钩。 –

+0

我感谢您的意见,谢谢。为了详细阐述一下,我使用CreateRemoteThread注入一个DLL来将DLL注入到mspaint.exe中。 DLL中的代码能够遍历mspaint中的IAT(大约12个,包括Kernel32 IAT)。在DLL中,我直接在“case DLL_THREAD_DETACH:”(在实现hook之后)内调用GetCurrentProcessId()。通过使用Windbg浏览程序,看起来对GetCurrentProcessId()的调用不会在IAT中调用查找(表明该值可能会从先前的调用中缓存?)。仍然插上去。谢谢。 – Chris

回答

2

利用的CreateToolhelp32Snapshot API我能钩所有IAT中函数调用(没有插入注入DLL IAT中,因为这内钩导致崩溃),以GetCurrentProcessId()我写着Helloworld程序中简单地报告其每隔几秒处理一次id。在注入DLL之后,如预期的那样开始调用挂钩函数。在我的研究确实发现了一些信息,为什么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