查看文章 |
注入进程实现hook API -- 拦截并修改API函数参数
2009-02-25 13:23
// MainProcessDll.cpp : Defines the entry point for the DLL application. // #include <windows.h> #include <stdlib.h> #include <stdio.h> char g_waiguaFullPath[0x100] = {0}; DWORD g_FunAddr = 0; DWORD g_PatchFunc = 0; DWORD g_PatchAllHookFun = 0; DWORD g_CmdLen = 5; BYTE g_Cmd[0x14] = {0}; LPVOID g_mem = NULL; DWORD g_hookflag = 0; WCHAR tt[] = L"tt"; typedef HANDLE (CALLBACK *PCreateEventW)( LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName ); PCreateEventW PCreateEventWFun = NULL; BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } void __stdcall FreeMem() { if(g_mem) { VirtualFree(g_mem,0x14,0x8000); g_mem = NULL; return; } } void __stdcall RestoreHookFunction() { if(g_mem == NULL) return; if(!g_hookflag) return; memcpy((void *)g_FunAddr,g_Cmd,g_CmdLen); FreeMem(); } void __stdcall ModifyEventName(WCHAR *EventName) { _asm { pushad mov ebx,rand mov edi,EventName xor esi,esi GOON: call ebx cdq mov ecx,14h idiv ecx add esi,2 add dl,61h cmp esi,0Ah mov [esi + edi - 2],dl jl GOON popad } } void __stdcall CreateEventWHookFunction(DWORD pEsp) { _asm { pushad mov eax,pEsp mov ecx,[eax + 0ch] mov ebx,[ecx + 14h] test ebx,ebx jz QUIT mov ecx,4 lea edi,tt mov esi,ebx xor eax,eax repne cmpsb jz PIPEI sbb eax,eax sbb eax,0ffffffffh PIPEI: test eax,eax jnz QUIT call RestoreHookFunction push ebx call ModifyEventName QUIT: popad } } void __stdcall PatchFunctionForAllHook(DWORD pEsp) { _asm { mov eax,g_PatchFunc mov ecx,pEsp push ecx call eax } } int __stdcall AddCmdToBuff() { if(g_mem == NULL) return 1; _asm { pushad mov eax,g_mem mov byte PTR[eax],60h mov byte PTR[eax + 1],9ch inc eax mov byte PTR[eax + 1],54h inc eax mov byte PTR[eax + 1],0B9h inc eax mov edx,g_PatchFunc mov [eax + 1],edx mov ecx,g_PatchAllHookFun inc eax mov byte PTR[eax + 4],0E8h add eax,4 sub ecx,eax sub ecx,5 mov [eax + 1],ecx inc eax mov byte PTR[eax + 4],9Dh add eax, 4 mov byte PTR[eax + 1],61h mov ecx,g_CmdLen inc eax mov ebx, ecx shr ecx,2 inc eax lea esi,g_Cmd mov edi,eax rep movsd mov ecx,ebx and ecx,3 rep movsb mov ecx,g_CmdLen mov edx,g_FunAddr add eax,ecx sub edx,eax lea ecx,[edx + ecx - 5] mov byte PTR[eax],0E9h mov [eax + 1],ecx popad } return 0; } int __stdcall InlineHook() { memcpy(g_Cmd,(void *)g_FunAddr, g_CmdLen); _asm { push ecx mov ecx,g_FunAddr mov eax,g_mem mov byte ptr[ecx],0E9h sub eax,ecx sub eax,5 mov ecx,g_FunAddr mov [ecx+1],eax pop ecx } g_hookflag = 1; g_PatchAllHookFun = (DWORD)PatchFunctionForAllHook; AddCmdToBuff(); return 0; } int __stdcall HookFunction(DWORD FunAddr,DWORD PatchFunc) { DWORD OldProtect; if(!(FunAddr && PatchFunc)) return 1; g_FunAddr = FunAddr; g_PatchFunc = PatchFunc; if(!VirtualProtect((void *)g_FunAddr,5,PAGE_EXECUTE_READWRITE,&OldProtect)) return 2; g_mem = VirtualAlloc(0,0x14,MEM_COMMIT,PAGE_EXECUTE_READWRITE); if(g_mem == NULL) return 3; InlineHook(); return 0; } int __stdcall Runxxx(char *waiguafullPath) { char EventName[0x104] = {0}; if(waiguafullPath == NULL) return 2; lstrcpy(g_waiguaFullPath,waiguafullPath); PCreateEventWFun = (PCreateEventW)GetProcAddress( GetModuleHandle("kernel32.dll"), "CreateEventW"); if(!PCreateEventWFun) return 1; lstrcpy(EventName,"tty_btn0"); HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS,TRUE,EventName); if(hEvent) { CloseHandle(hEvent); HookFunction((DWORD)PCreateEventWFun + 5,(DWORD)CreateEventWHookFunction); } return 0; } ////MainProcessDll.def LIBARARY MainProcessDll EXPORTS Runxxx @1 // xxxxhook.cpp : Defines the entry point for the DLL application. // #include <windows.h> #include <stdio.h> #pragma data_seg("Shared") char g_waiguaFullPath[0x104] = {0}; HHOOK g_hHook = NULL; #pragma data_seg() #pragma comment(linker,"/SECTION:Shared,RWS") BOOL g_IsHook = FALSE; char g_MainProcessDll[0x104] = {0}; HANDLE g_hmod_xxxxhook_dll = NULL; HMODULE g_hModuleMainProcess = NULL; typedef int (CALLBACK *pRunxxx)(char *waiguaFullPath); pRunxxx RunxxxFun = NULL; BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { g_hmod_xxxxhook_dll = hModule; return TRUE; } LRESULT CALLBACK CallWndProc( int nCode, WPARAM wParam, LPARAM lParam ) { try { if(!g_IsHook) { if(GetModuleHandle("pg_guiwnd.dll")) { g_IsHook = TRUE; lstrcpy(g_MainProcessDll,g_waiguaFullPath); lstrcat(g_MainProcessDll,"MainProcessDll.dll"); g_hModuleMainProcess = LoadLibrary(g_MainProcessDll); if(g_hModuleMainProcess) { RunxxxFun = (pRunxxx)GetProcAddress(g_hModuleMainProcess,"Runxxx"); if(RunxxxFun != NULL) { RunxxxFun(g_waiguaFullPath); } else { FreeLibrary(g_hModuleMainProcess); g_hModuleMainProcess = NULL; } } } } } catch (...) { } return CallNextHookEx(g_hHook,nCode,wParam,lParam); } void Install_InitPath() { char Dest[0x100] = {0}; GetModuleFileName(0,Dest,0x100); char *pChar = Dest + lstrlen(Dest) - 1; while( *pChar != '\\') pChar--; pChar++; *pChar = 0; lstrcpy(g_waiguaFullPath,Dest); } int __stdcall Install2() { Install_InitPath(); g_hHook = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, (HINSTANCE)g_hmod_xxxxhook_dll, 0 ); if(g_hHook == NULL) return 2; return 0; } int __stdcall UnInstall2() { if(g_IsHook) { if(g_hModuleMainProcess) { FreeLibrary(g_hModuleMainProcess); g_hModuleMainProcess = NULL; } g_IsHook = FALSE; } if(g_hHook) { if(UnhookWindowsHookEx(g_hHook)) g_hHook = 0; else return 2; } return 0; } //xxxxhook.def LIBRARY xxxxhook EXPORTS Install2 @1 CallWndProc @2 UnInstall2 @3 另注: 明天就要离开公司,离开了与自己共事多年的朋友们,临别写一句《功夫熊猫》中的一句话勉励自己吧。“过去的,已经过去了;未来的,还未可知。现在却是上苍的礼赠。我们可以把握的,是当下。” |
最近读者: