#include "stdafx.h" #include <iostream> #include "windows.h" #include "process.h" #include "tlhelp32.h" #include <Winsock2.h> #include "stdio.h"
using namespace std;
#pragma region 全局变量
HMODULE Current_Handle; PBYTE pfile; PIMAGE_DOS_HEADER Dos_Header; PIMAGE_NT_HEADERS Nt_Header; DWORD IATSection_Base; DWORD IATSection_Size; DWORD oldFuncAddress; DWORD newFuncAddress;
#pragma endregion
#pragma region 依赖函数
BOOL str_cmp(char *a,char *b){ while(*a==*b && *a!='\0' && *b!='\0'){ a++; b++; } if(*a=='\0' && *b=='\0') return true; return false; }
int WINAPI NewMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) { return MessageBoxW(NULL,L"IAT HOOK",L"HOOK", NULL); }
#pragma endregion
#pragma region 功能函数
bool IatHook(LPCSTR DllName,LPCSTR ProcName,DWORD NewFuncAddress) { DWORD oldprotect = 0; oldFuncAddress = (DWORD)GetProcAddress( GetModuleHandleA(DllName), ProcName );
PIMAGE_THUNK_DATA pthunk = NULL;
PIMAGE_IMPORT_DESCRIPTOR Current_IID = (PIMAGE_IMPORT_DESCRIPTOR)(pfile + IATSection_Base);
while (Current_IID){ if (str_cmp((char *)DllName,(char *)(pfile + Current_IID->Name))){ pthunk = (PIMAGE_THUNK_DATA)(pfile+Current_IID->FirstThunk); while (pthunk->u1.Function){ if (pthunk->u1.Function == (DWORD)oldFuncAddress){ VirtualProtect((LPVOID)&pthunk->u1.Function, 4, PAGE_EXECUTE_READWRITE, &oldprotect); pthunk->u1.Function = NewFuncAddress;
newFuncAddress = pthunk->u1.Function;
VirtualProtect((LPVOID)&pthunk->u1.Function, 4, oldprotect, &oldprotect);
return true; } pthunk++; } } Current_IID++; }
return false; }
bool IatUnHook(LPCSTR DllName,DWORD OldFuncAddress) {
DWORD oldprotect = 0; PIMAGE_THUNK_DATA pthunk = NULL;
PIMAGE_IMPORT_DESCRIPTOR Current_IID = (PIMAGE_IMPORT_DESCRIPTOR)(pfile + IATSection_Base);
while (Current_IID){ if (str_cmp((char *)DllName,(char *)(pfile + Current_IID->Name))){ pthunk = (PIMAGE_THUNK_DATA)(pfile+Current_IID->FirstThunk); while (pthunk->u1.Function){ if (pthunk->u1.Function == newFuncAddress){ VirtualProtect((LPVOID)&pthunk->u1.Function, 4, PAGE_EXECUTE_READWRITE, &oldprotect); pthunk->u1.Function = OldFuncAddress;
VirtualProtect((LPVOID)&pthunk->u1.Function, 4, oldprotect, &oldprotect);
return true; } pthunk++; } } Current_IID++; }
return false; }
void PeInit(){ Current_Handle = GetModuleHandle(NULL); pfile = (PBYTE)Current_Handle; Dos_Header = (PIMAGE_DOS_HEADER)pfile;
if (Dos_Header->e_magic != IMAGE_DOS_SIGNATURE){ OutputDebugString("Is Not PE"); return; }
Nt_Header = (PIMAGE_NT_HEADERS)(pfile + Dos_Header->e_lfanew);
if (Nt_Header->Signature != IMAGE_NT_SIGNATURE) { OutputDebugString("Is Not PE"); return; }
IMAGE_DATA_DIRECTORY IAT_Section = (IMAGE_DATA_DIRECTORY)(Nt_Header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]); IATSection_Base = IAT_Section.VirtualAddress; IATSection_Size = IAT_Section.Size; }
#pragma endregion
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: { Current_Handle = NULL; pfile = NULL; Dos_Header = NULL; Nt_Header = NULL; IATSection_Base = 0; IATSection_Size = 0; oldFuncAddress = 0; newFuncAddress = 0;
PeInit();
if(IatHook("USER32.dll","MessageBoxW",(DWORD)NewMessageBoxW)){MessageBox(NULL,"HOOK成功","LYSM",NULL);} else{MessageBox(NULL,"HOOK失败","LYSM",NULL);}
break; } case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: { if(IatUnHook("USER32.dll",oldFuncAddress)){MessageBox(NULL,"UNHOOK成功","LYSM",NULL);} else{MessageBox(NULL,"UNHOOK失败","LYSM",NULL);} break; } } return TRUE; }
|