我试图在Internet Explorer 8(不在保护模式下运行)Windows 7 x64上使用IAT挂钩。这段代码也不适用于WinXP上的IE7。
我获得了指向函数地址存储位置的指针。
我正在添加我的代码片段,使事情变得简单。

typedef int (WINAPI *_MessageBoxW)(HWND,LPCWSTR,LPCWSTR,UINT);
int WINAPI TestMessageBox(HWND,LPCWSTR,LPCWSTR,UINT);

我获取导入地址表和存储MessageBoxW地址的地址。
PIMAGE_THUNK_DATA pThunk = MakePtr(PIMAGE_THUNK_DATA, hMod, pImportDesc->FirstThunk);

PROC *pLocation=(PROC*)&(pThunk->u1.Function);
_MessageBoxW testVar1=&MessageBoxW;

这里*pLocation的值与testVar1相同,所以我假设我得到了正确的地址
_MessageBoxW testVar2=&TestMessageBox;

现在我使用VirtualProtect更改地址PROC的权限。做完这些之后,我用新地址覆盖它。
*pLocation=(PROC)testVar2;

我已经验证了地址已经成功更改,尽管这样做,我的函数还是没有被调用。
我有什么遗漏吗?我已经粘贴了注入的dll的全部代码
// injected.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "injected.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
    break;
}
return TRUE;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif



extern "C"{

INJECTED_API LRESULT CALLBACK fninjected(int code,WPARAM wParam,LPARAM lParam)
{
    if(gdi32Handle==NULL){
        GetBaseAddressOfModulesLoaded(ModuleBaseAddresses);
        MessageBox(NULL,L"Press ok to Debug",L"Alert!",MB_OK);//Used for Attching Debugger to IE



        PROC* pPROC=(PROC*)GetAddress(ModuleBaseAddresses,"MessageBoxW");
        _MessageBoxW ddd=&MessageBoxW;
        mbOrig=(_MessageBoxW)*pPROC;
        _MessageBoxW mbNew=&AshishMessageBox;
        if(ManipulateAddressPointer(pPROC,(PROC)mbNew)){
        }else{
            MessageBeep(!0x0);
        }

        //////////////////////////////////////////////////////////////////////////

        MessageBoxW(NULL,L"Dummy",L"Heeeeee",MB_OK);
        isProcessed=TRUE;
    }else{
        //A test function Comes Here, to check if the hook has been set or not;
    }
    //MessageBox(NULL,L"Hello World",L"Test",MB_OK);
    return CallNextHookEx(NULL,code,wParam,lParam);
}
}


int GetBaseAddressOfModulesLoaded(std::vector<HMODULE> &MBAddressVect){
int count=0;
size_t bytesRead=sizeof(MEMORY_BASIC_INFORMATION);
MEMORY_BASIC_INFORMATION mbi;
HMODULE hModIter;
wchar_t imageName[1024];
hModIter=GetModuleHandle(NULL);
if(hModIter==NULL){
    MessageBox(NULL,L"Failed to get Handle of Current Module",L"Error message",MB_OK);
}else{
    MBAddressVect.push_back(hModIter);
}
for(size_t lpAddress=0;bytesRead==sizeof(MEMORY_BASIC_INFORMATION);lpAddress+=mbi.RegionSize){
    memset(&mbi,0x00,sizeof(MEMORY_BASIC_INFORMATION));
    memset(&imageName,0x00,sizeof(imageName));
    bytesRead=VirtualQuery((LPCVOID)lpAddress,&mbi,sizeof(MEMORY_BASIC_INFORMATION));
    if(mbi.AllocationBase!=mbi.BaseAddress)continue;
    else if(!(mbi.State&MEM_COMMIT))continue;
    if(!mbi.BaseAddress)continue;
    hModIter=(HMODULE)mbi.BaseAddress;

    if(hModIter && GetModuleFileName(hModIter,imageName,1023)>0){

        /*
            Push all the module handles into the vector
        */

        if(!wcsstr(wcslwr(imageName),L"user32.dll")&&!wcsstr(wcslwr(imageName),L"gdi32.dll"))continue;
        else{
            MessageBox(NULL,imageName,L"Loaded DLL",MB_OK);
            gdi32Handle=hModIter;//This is messy
            MBAddressVect.push_back(hModIter);
            count++;
        }
    }
}
return count;
}

void* GetAddress(std::vector <HMODULE> BaseAddresses,char *OrignalFunctionName){
PIMAGE_THUNK_DATA pThunk = NULL, pOrigThunk = NULL;
PIMAGE_IMPORT_BY_NAME pAddressOfData = NULL;

for (std::vector<HMODULE>::iterator it = BaseAddresses.begin(); it!=BaseAddresses.end(); ++it) {
    HMODULE hMod=*it;
    PIMAGE_DOS_HEADER pDOSHeader = (PIMAGE_DOS_HEADER)hMod;
    if(pDOSHeader->e_magic!=0x5A4D){
        MessageBox(NULL,L"Not a Valid DOS Image",L"Error",MB_OK);
        return NULL;
    }else{
        //MessageBox(NULL,L"Valid DOS Image",L"Message",MB_OK);
    }
    PIMAGE_NT_HEADERS pNTHeader=MakePtr(PIMAGE_NT_HEADERS,pDOSHeader,pDOSHeader->e_lfanew);
    if(pNTHeader->Signature!=0x00004550){
        MessageBox(NULL,L"Not a Valid NT Image",L"Error",MB_OK);
        return NULL;
    }else{
        //MessageBox(NULL,L"Valid NT Image",L"Error",MB_OK);
    }
    PIMAGE_IMPORT_DESCRIPTOR pImportDesc = MakePtr( PIMAGE_IMPORT_DESCRIPTOR, hMod,pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
    if(!pImportDesc){
        MessageBox(NULL,L"Failed to get Import Descriptors",L"Error",MB_OK);
    }else{
        //MessageBox(NULL,L"Obtained Import Descriptors",L"Success",MB_OK);
    }
    while (pImportDesc->FirstThunk){
        pThunk = MakePtr(PIMAGE_THUNK_DATA, hMod, pImportDesc->FirstThunk); // updated by loader
        pOrigThunk = MakePtr(PIMAGE_THUNK_DATA, hMod, pImportDesc->OriginalFirstThunk); // unmodified by loader

        while (pOrigThunk->u1.Function){
            pAddressOfData = MakePtr(PIMAGE_IMPORT_BY_NAME, hMod, pOrigThunk->u1.AddressOfData);
            if (!IsBadReadPtr(pAddressOfData, sizeof(IMAGE_IMPORT_BY_NAME))) {
                char* funcName=(char*)pAddressOfData->Name;
                if(funcName){
                    if(strstr(funcName,OrignalFunctionName)){
                        MessageBox(NULL,L"Found",L"Success",MB_OK);
                        return(&(pThunk->u1.Function));
                    }
                }
            }
            pThunk++;
            pOrigThunk++;
        }
        pImportDesc++;
    }
}
}
int WINAPI AshishMessageBox(HWND wHandle,LPCWSTR text,LPCWSTR title,UINT type){
return mbOrig(NULL,L"you will always see this",L"No Matter What you try",MB_OK);
}


BOOL AshishExtTextOut(HDC hdc,int X,int Y,UINT fuOptions,const RECT *lprc,LPCWSTR lpString,UINT cbCount,const INT *lpDx){
MessageBox(NULL,lpString,L"Intercepted Text",MB_OK);
return etoOrig(hdc,X,Y,fuOptions,lprc,lpString,cbCount,lpDx);
}


BOOL ManipulateAddressPointer(PROC* pAddress,PROC location){
BOOL rv=FALSE;
if(IsBadWritePtr(pAddress,sizeof(PROC))){
    DWORD oldProtect;
    if(VirtualProtect(pAddress,sizeof(PROC),PAGE_READWRITE,&oldProtect)){
        *pAddress=location;
        DWORD test;
        if(VirtualProtect(pAddress,sizeof(PROC),oldProtect,&test)){
            rv=TRUE;
        }
    }else{
        MessageBox(NULL,L"Fail",L"Error message",MB_OK);
    }
}
else{
    //MessageBox(NULL,L"Memory Is Writable",L"Error message",MB_OK);
    *pAddress=location;
}
return rv;
}

最佳答案

有可能IE7会调用原始的MessageBoxW,而不会进入您的手中。以下是其中一些:
IE7运行在由几个模块(EXE+dll)组成的进程中IAT挂钩通过修补特定模块的导入表来工作。你确定你毫无例外地修补了所有的模块吗?顺便说一句,有些模块可能会在以后加载(在您的补丁之后)。
可以调用MessageBoxW而不使用导入的符号。你可以通过GetProcAddress获得它的地址。
可以将MessageBoxW的地址存储在某个变量中,然后使用它调用它。所以如果你在收到它的地址后修补它,它就不能工作了。
人们可以避免使用MessageBoxW。相反,它可以从User32/64.dll调用另一个低级函数,而User32/64.dll通常由MessageBoxW调用。
首先你应该知道为什么你没有被打电话。运行IE7,当它显示一个消息框时-通过调试器连接到它,并执行“全部中断”。然后看看调用堆栈。如果你在某个点上看到,第四种情况是不相关的。然后看看它是怎么叫的。也许您会发现哪个模块(EXE或DLL)调用了它,以及如何调用。
此外,为了避免1和2-您应该始终修补以下功能:
MessageBoxW
LoadLibraryA
LoadLibraryW
LoadLibraryExA
LoadLibraryExW

09-15 20:45