1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| PROC m_FuncAddress; BYTE m_OldBytes[5]; BYTE m_NewBytes[5];
BOOL Hook(const char* pszModuleName, const char* pszFuncName, PROC pfnHookFunc) { m_FuncAddress = (PROC)GetProcAddress(GetModuleHandle(pszModuleName), pszFuncName); if(m_FuncAddress == NULL) { return FALSE; } SIZE_T dwSize = 0; ReadProcessMemory(GetCurrentProcess(), m_FuncAddress, m_OldBytes, 5, &dwSize); m_NewBytes[0] = '\xE9'; *(DWORD*)(m_NewBytes + 1) = (DWORD)pfnHookFunc - (DWORD)m_FuncAddress - 5; WriteProcessMemory(GetCurrentProcess(), m_FuncAddress, m_NewBytes, 5, &dwSize); return TRUE; }
BOOL UnHook() { if (m_FuncAddress != 0) { SIZE_T dwSize = 0; WriteProcessMemory(GetCurrentProcess(), m_FuncAddress, m_OldBytes, 5, &dwSize); } return TRUE; }
BOOL ReHook() { if (m_FuncAddress != 0) { SIZE_T dwSize = 0; WriteProcessMemory(GetCurrentProcess(), m_FuncAddress, m_NewBytes, 5, &dwSize); } return TRUE; }
int WINAPI MyMessageBoxA( _In_opt_ HWND hWnd, _In_opt_ LPCSTR lpText, _In_opt_ LPCSTR lpCaption, _In_ UINT uType) { UnHook(); int nRet = MessageBoxA(hWnd, "Hello Dokey", "Hello Dokey", uType); ReHook(); return nRet; }
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: m_FuncAddress = NULL; memset(m_OldBytes, 0, 5); memset(m_NewBytes, 0, 5); Hook("user32.dll", "MessageBoxA", (PROC)MyMessageBoxA); break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: UnHook(); break; } return TRUE; }
|