这是DLL代码:
CAPIHook::CAPIHook(PSTR pszCalleeModname,PSTR pszFuncname,PROC pfnHook) { // Note: the function can be hooked only if the exporting module // is already loaded. A solution Could be to store the function // name as a member; then,in the hooked Loadlibrary* handlers,parse // the List of CAPIHook instances,check if pszCalleeModname // is the name of the loaded module to hook its export table and // re-hook the import tables of all loaded modules. m_pNext = sm_phead; // The next node was at the head sm_phead = this; // This node is Now at the head // Save information about this hooked function m_pszCalleeModname = pszCalleeModname; m_pszFuncname = pszFuncname; m_pfnHook = pfnHook; m_pfnorig = GetProcAddressRaw(GetModuleHandleA(pszCalleeModname),m_pszFuncname); // If function does not exit,... bye bye // This happens when the module is not already loaded if (m_pfnorig == NulL) { wchar_t szPathname[MAX_PATH]; GetmodulefilenameW(NulL,szPathname,_countof(szPathname)); wchar_t sz[1024]; StringCchPrintfW(sz,_countof(sz),TEXT("[%4u - %s] impossible to find %s\r\n"),GetCurrentProcessID(),pszFuncname); OutputDeBUGString(sz); return; } // Hook this function in all currently loaded modules ReplaceIATEntryInAllMods(m_pszCalleeModname,m_pfnorig,m_pfnHook);}
这是钩子函数:
HMODulE WINAPI CAPIHook::LoadlibraryA(PCSTR pszModulePath) { HMODulE hmod = ::LoadlibraryA(pszModulePath); FixupNewlyLoadedModule(hmod,0); return(hmod);}HMODulE WINAPI CAPIHook::LoadlibraryW(PCWSTR pszModulePath) { HMODulE hmod = ::LoadlibraryW(pszModulePath); FixupNewlyLoadedModule(hmod,0); return(hmod);}HMODulE WINAPI CAPIHook::LoadlibraryExA(PCSTR pszModulePath,HANDLE hfile,DWORD DWFlags) { HMODulE hmod = ::LoadlibraryExA(pszModulePath,hfile,DWFlags); FixupNewlyLoadedModule(hmod,DWFlags); return(hmod);}HMODulE WINAPI CAPIHook::LoadlibraryExW(PCWSTR pszModulePath,DWORD DWFlags) { HMODulE hmod = ::LoadlibraryExW(pszModulePath,DWFlags); return(hmod);}
替换IAT的方法:
voID CAPIHook::ReplaceIATEntryInOneMod(PCSTR pszCalleeModname,PROC pfnCurrent,PROC pfnNew,HMODulE hmodCaller) { // Get the address of the module's import section ulONG ulSize; // An exception was triggered by Explorer (when browsing the content of // a folder) into imagehlp.dll. It looks like one module was unloaded... // Maybe some threading problem: the List of modules from Toolhelp might // not be accurate if Freelibrary is called during the enumeration. PIMAGE_import_DESCRIPTOR pimportDesc = NulL; __try { pimportDesc = (PIMAGE_import_DESCRIPTOR) ImageDirectoryEntryToData( hmodCaller,TRUE,IMAGE_DIRECTORY_ENTRY_import,&ulSize); } __except (InvalIDReadExceptionFilter(GetExceptioninformation())) { // nothing to do in here,thread continues to run normally // with NulL for pimportDesc } if (pimportDesc == NulL) return; // This module has no import section or is no longer loaded // Find the import descriptor containing references to callee's functions for (; pimportDesc->name; pimportDesc++) { PSTR pszModname = (PSTR) ((PBYTE) hmodCaller + pimportDesc->name); if (lstrcmpiA(pszModname,pszCalleeModname) == 0) { // Get caller's import address table (IAT) for the callee's functions PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA) ((PBYTE) hmodCaller + pimportDesc->FirstThunk); // Replace current function address with new function address for (; pThunk->u1.Function; pThunk++) { // Get the address of the function address PROC* ppfn = (PROC*) &pThunk->u1.Function; // Is this the function we're looking for? BOol bFound = (*ppfn == pfnCurrent); if (bFound) { if (!WriteProcessMemory(GetCurrentProcess(),ppfn,&pfnNew,sizeof(pfnNew),NulL) && (ERROR_NOACCESS == GetLastError())) { DWORD DWoldProtect; if (VirtualProtect(ppfn,PAGE_WRITEcopY,&DWoldProtect)) { WriteProcessMemory(GetCurrentProcess(),NulL); VirtualProtect(ppfn,DWoldProtect,&DWoldProtect); } } return; // We dID it,get out } } } // Each import section is parsed until the right entry is found and patched }}
作者添加了评论来添加此功能,但我不知道该怎么做
Note: the function can be hooked only if the exporting module
is already loaded. A solution Could be to store the function
name as a member; then,parse
the List of CAPIHook instances,check if pszCalleeModname
is the name of the loaded module to hook its export table and
re-hook the import tables of all loaded modules.
他也在书上写了这个,但我又不知道该怎么做
A possible solution is to use the hooked Loadlibrary* functions to
detect when a module is exporting an unpatched hooked function and
then execute two actions:Hook again the import table of the module already loaded because it is
Now possible to call GetProcAddress and get a pointer to the original
implementation of the function to hook. Notice that the name of the
function needs to be stored as a class member and set in the
constructor.Directly update this hooked function in the Export Address table of
the exporting module as shown by the implementation of the
ReplaceEATEntryInOneMod function. That way,all new modules calling
the hooked function will call our handler
我尝试在加载DLL后修改IAT,但我的钩子函数没有被调用
HMODulE WINAPI CAPIHook::LoadlibraryW(PCWSTR pszModulePath) { HMODulE hmod = ::LoadlibraryW(pszModulePath); if (StrCmpIW(pszModulePath,myDLLUnicodename.c_str()) == 0 ) { PROC proc = GetProcAddressRaw(GetModuleHandleA(myDLLname.c_str()),myFunctionname.c_str()); if ( proc != NulL ) { for (CAPIHook* p = sm_phead; p != NulL; p = p->m_pNext) { if (StrCmpIA(p->m_pszCalleeModname,myDLLname.c_str()) == 0) { MessageBox(NulL,L"This is the New Dynamic DLL",L"Test!",0); ReplaceIATEntryInAllMods(p->m_pszCalleeModname,proc,p->m_pfnHook); } } } } FixupNewlyLoadedModule(hmod,0); return(hmod);}
那么,如何修改此代码来处理动态加载情况?
解决方法 我之前做过这个.你想要的是EAT挂钩而不是IAT.此外,挂钩:: Loadlibrary API本身,以便您知道何时加载DLL,并在加载后从DLL挂钩所请求的API.
互联网上有一些关于如何做到这一点的例子.这是我刚才发现的一个:
http://board.cheat-project.com/showthread.php?t=10633
以上是内存溢出为你收集整理的c – 如何修改运行时加载的DLL的导入地址表全部内容,希望文章能够帮你解决c – 如何修改运行时加载的DLL的导入地址表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)