您好,登錄后才能下訂單哦!
結(jié)合網(wǎng)上資料、使用IAT HOOK截獲MessageBox函數(shù)、、、
步驟如下
1..寫一個自己的MessageBox函數(shù)注意調(diào)用約定為__stdcall、、
2..定義一MessageBox函數(shù)指針如下
typedef int (__stdcall *pOldMBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,UINT uType);
3..遍歷本進程的導(dǎo)入表尋找MessageBox的地址、、
4..修改MessageBox所在THUNK的地址為自己寫的函數(shù)地址、、代碼如下:
#include <windows.h>
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函數(shù) ****************************/
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 不是以序號導(dǎo)入
{
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開始的內(nèi)存的信息并將其保存到MEMORY_BASIC_INFORMATION結(jié)構(gòu)中
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開始的內(nèi)存的的保護屬性為PAGE_READWRITE并將原保護屬性保存到&dwOLD中
DWORD dwOLD;
VirtualProtect(pFirstThunk,sizeof(DWORD),PAGE_READWRITE,&dwOLD);
//更改真正MessageBoxA的地址為自己寫的HookMBox函數(shù)的地址、、
//將真正的地址付給先前定義的函數(shù)指針
//結(jié)果正確的話就是當本程序調(diào)用messagebox的時候程序轉(zhuǎn)去執(zhí)行咱的HookMBox函數(shù)
//并且在咱的HookMBox函數(shù)中咱還調(diào)用了真正的messagebox函數(shù)、、
pMBox =(pOldMBox)(pFirstThunk->u1.Function);
pFirstThunk->u1.Function = (PDWORD)HookMBox;
//恢復(fù)之前更改的內(nèi)存的保護屬性為人家自己的、、
VirtualProtect(pFirstThunk,sizeof(DWORD),dwOLD,0);
break;
}
}
pOriginalThunk++;
pFirstThunk++;
}
/****************************************************************************/
return 0;
}
int main()
{
ReAPI("User32.dll","MessageBoxA");
MessageBox(NULL,"沒有HOOK到","HOOK",MB_OK);
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。