结合网上资料、使用IAT HOOK截获MessageBox函数、、、
步骤如下
1..写一个自己的MessageBox函数注意调用约定为__stdcall、、
2..定义一MessageBox函数指针如下
typedef int (__stdcall *pOldMBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,UINT uType);
3..遍历本进程的导入表寻找MessageBox的地址、、
4..修改MessageBox所在THUNK的地址为自己写的函数地址、、代码如下:
#include
HANDLE pBegin = GetModuleHandle(NULL);
PBYTE pBegin2 = (PBYTE)pBegin;
PIMAGE_DOS_HEADER DOS = PIMAGE_DOS_HEADER(pBegin2);
PIMAGE_NT_HEADERS NT = PIMAGE_NT_HEADERS(pBegin2+DOS->e_lfanew);
PIMAGE_OPTIONAL_HEADER OPTION = &(NT->OptionalHeader);
PIMAGE_IMPORT_DESCRIPTOR IMPORT = PIMAGE_IMPORT_DESCRIPTOR(OPTION->DataDirectory[1].VirtualAddress + pBegin2);
typedef int (__stdcall *pOldMBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,UINT uType);
pOldMBox pMBox = NULL;
int __stdcall HookMBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,UINT uType)
{
if (NULL == pMBox)
{
return MessageBox(hWnd,lpText,lpCaption,uType);
}
else
{
return pMBox(NULL,"哈哈! IAT HOOK到了","HOOK",MB_OK);
}
}
int ReAPI(const char* DllName, const char* FunName)
{
/************************** 找相同的DLL ****************************/
while (IMPORT->Name)
{
char* OurDllName = (char*)(IMPORT->Name + pBegin2);
if (0 == strcmpi(DllName , OurDllName))
{
break;
}
IMPORT++;
}
/****************************************************************************/
/************************* 找相同的API函数 ****************************/
PIMAGE_IMPORT_BY_NAME pImportByName = NULL;
PIMAGE_THUNK_DATA pOriginalThunk = NULL;
PIMAGE_THUNK_DATA pFirstThunk = NULL;
pOriginalThunk = (PIMAGE_THUNK_DATA)(IMPORT->OriginalFirstThunk + pBegin2);
pFirstThunk = (PIMAGE_THUNK_DATA)(IMPORT->FirstThunk + pBegin2);
while (pOriginalThunk->u1.Function) //记住是Function
{
DWORD u1 = pOriginalThunk->u1.Ordinal; //记住是Ordinal
if ((u1 & IMAGE_ORDINAL_FLAG) != IMAGE_ORDINAL_FLAG) //说明MSB不是1 不是以序号导入
{
pImportByName = (PIMAGE_IMPORT_BY_NAME)((DWORD)pOriginalThunk->u1.AddressOfData + pBegin2);
char* OurFunName = (char*)(pImportByName->Name); //下边的计算也可以
//char* OurFunName2 = (char*)((DWORD)pOriginalThunk->u1.AddressOfData + pBegin2 + 2);
if (0 == strcmpi(FunName,OurFunName))
{
//获取以pFirstThunk开始的内存的信息并将其保存到MEMORY_BASIC_INFORMATION结构中
MEMORY_BASIC_INFORMATION mbi_thunk;
VirtualQuery(pFirstThunk, &mbi_thunk, sizeof(MEMORY_BASIC_INFORMATION));
//VirtualProtect(mbi_thunk.BaseAddress,mbi_thunk.RegionSize, PAGE_READWRITE, &mbi_thunk.Protect);
//修改以pFirstThunk开始的内存的的保护属性为PAGE_READWRITE并将原保护属性保存到&dwOLD中
DWORD dwOLD;
VirtualProtect(pFirstThunk,sizeof(DWORD),PAGE_READWRITE,&dwOLD);
//更改真正MessageBoxA的地址为自己写的HookMBox函数的地址、、
//将真正的地址付给先前定义的函数指针
//结果正确的话就是当本程序调用messagebox的时候程序转去执行咱的HookMBox函数
//并且在咱的HookMBox函数中咱还调用了真正的messagebox函数、、
pMBox =(pOldMBox)(pFirstThunk->u1.Function);
pFirstThunk->u1.Function = (PDWORD)HookMBox;
//恢复之前更改的内存的保护属性为人家自己的、、
VirtualProtect(pFirstThunk,sizeof(DWORD),dwOLD,0);
break;
}
}
pOriginalThunk++;
pFirstThunk++;
}
/****************************************************************************/
return 0;
}
int main()
{
ReAPI("User32.dll","MessageBoxA");
MessageBox(NULL,"没有HOOK到","HOOK",MB_OK);
}