我觉得用detours库hook api比较简单
因为我现在还不会改汇编指令来hook api...
因为我现在还不会改汇编指令来hook api...
///////////////////////////////////////////////////////////////////////////////////// // HOOKAPI DEMO PROGRAM //作者:东海一鱼 //时间: 2010.7.22 //使用编译器: VC2003 //使用第三方库: NULL //Bug修复纪录: //////////////////////////////////////////////////////////////////////////////////// #include <stdlib.h> #include <windows.h> #define FLATJMPCODE_LENGTH 5 //我的新函数 int __stdcall MyHookFn(HWND hwnd,char* sztext,char* szTitle,int stly) { const char* szHookTxt = "原函数已被HOOKD!"; //用来替换原显示内容 return MessageBoxEx(hwnd,szHookTxt,szTitle,stly,0); //调用另外的API } //HOOK函数 BOOL HookApi(LPVOID ApiFun,LPVOID HookFun) { BOOL IsSuccess = FALSE; DWORD TempVar; //临时变量 MEMORY_BASIC_INFORMATION MemInfo; //内存分页属性信息 VirtualQuery(ApiFun,&MemInfo,sizeof(MEMORY_BASIC_INFORMATION)); //查询信息 if(VirtualProtect(MemInfo.BaseAddress,MemInfo.RegionSize, PAGE_READWRITE,&MemInfo.Protect)) //修改页面为可写 { __asm { mov eax,ApiFun mov byte ptr[eax],0xe9 mov edx,HookFun sub edx,eax sub edx,FLATJMPCODE_LENGTH mov dword ptr[eax + 1],edx } VirtualProtect(MemInfo.BaseAddress,MemInfo.RegionSize, MemInfo.Protect,&TempVar); //改回原属性 IsSuccess = TRUE; } return IsSuccess; } int main(int argc,char** argv) { HMODULE hDll; LPVOID OldFun; hDll = GetModuleHandle("User32.dll"); OldFun = GetProcAddress(hDll,"MessageBoxA"); //要HOOK的对象 if(OldFun) { if(HookApi(OldFun,MyHookFn)) //如果HOOK成功 MessageBoxA(0,"call Api MessageBox","Is Hookd?",MB_OK); //调用原API } if(hDll) FreeLibrary(hDll); return 0; }
BOOL (WINAPI *pCloseWindow)(HWND hwnd) = CloseWindow; BOOL MyCloseWindow(HWND hwnd); if(DetourTransactionBegin() == NO_ERROR) MessageBox( NULL, "DetourTransactionBegin()无错误", "信息", MB_ICONINFORMATION ); if(DetourUpdateThread(GetCurrentThread()) == NO_ERROR) MessageBox( NULL, "DetourUpdateThread()无错误", "信息", MB_ICONINFORMATION ); else MessageBox( NULL, "DetourUpdateThread()失败", "信息", MB_ICONINFORMATION ); if(DetourAttach(&(PVOID&)pCloseWindow, MyCloseWindow) == NO_ERROR) MessageBox( NULL, "DetourAttach()无错误", "信息", MB_ICONINFORMATION ); if(DetourTransactionCommit() == NO_ERROR) OutputDebugString("send() detoured successfully"); BOOL MyCloseWindow(HWND hwnd){ MessageBox(NULL,"hooked CloseWindow() sucessfully.","hook",MB_ICONINFORMATION); return TRUE; }因为我我注入dll到大海战时,它会关闭程序,所以最终找到了是调用的CloseWindow()函数