【发布时间】:2012-06-11 05:18:03
【问题描述】:
我正在尝试在 Internet Explorer 8(未在保护模式下运行)、Windows 7 x64 上使用 IAT Hooking。该代码也不适用于 WinXP 上的 IE7。 我已经获得了指向存储函数地址的位置的指针。 我正在添加我的代码的 sn-p,以使事情变得简单。
typedef int (WINAPI *_MessageBoxW)(HWND,LPCWSTR,LPCWSTR,UINT);
int WINAPI TestMessageBox(HWND,LPCWSTR,LPCWSTR,UINT);
I获取导入地址表和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;
}
【问题讨论】:
-
消息框调用可能来自IE exe加载的模块,因此您还需要IAT挂钩所有这些。
-
@Necrolis 哦!我不知道。我只是在 user32.dll 中寻找函数。
-
@Necrolis 但是你如何解释,获得相同的值 *pLocation 和 testVar1?
-
听起来你正在 IAT 挂钩 user32,这完全关闭,你需要 IAT 挂钩 IE 二进制文件中的 user32 条目。仅仅因为你得到了值并不意味着你正在挂钩正确的模块,尤其是像
MessageBoxA/W这样的常见函数可能仍然可以使用,但不是为了你所追求的。 -
@Necrolis:我通过注入一个正在运行的 IE 实例进行 IAT 挂钩。一旦进入 IE,我将获得 user32.dll 的 HMODULE,一旦我拥有它们,我正在读取 PE 标头并进行替换。通过这样做,我认为我是在 IAT 挂钩 IE 中的 user32 条目,而不是 user32,这不是我应该做的吗?
标签: c internet-explorer winapi hook portable-executable