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
|
#include <windows.h>
#include <iostream>
#include <conio.h>
PBYTE DetourFunction( PBYTE pFunc, PBYTE pHook ) //returns oFunc
{
if( (pFunc == NULL) || (pHook == NULL) )
return NULL;
PBYTE oFunc = (PBYTE) VirtualAlloc(NULL, 32, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
DWORD oldProtect;
VirtualProtect( (PBYTE)pFunc, 5, PAGE_EXECUTE_READWRITE, &oldProtect );
for( unsigned int i = 0; i < 5; i ++ )
*(BYTE *) &oFunc[i] = *(BYTE *) (pFunc + i);
*(BYTE *) &oFunc[5] = (BYTE) 0xE9;
*(DWORD *) &oFunc[6] = (DWORD) (pFunc - oFunc - 5);
*(BYTE *) &pFunc[0] = (BYTE) 0xE9;
*(DWORD *) &pFunc[1] = (DWORD) ( pHook - pFunc - 5 );
VirtualProtect( (PBYTE)pFunc, 5, oldProtect, &oldProtect );
return oFunc;
}
PBYTE oMsgBox;
int myMessageBoxA( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType )
{
for(;;){ printf("\nHooked Function is called");
_getch();MessageBox(0,"Hello","Sup",MB_OK);
}
return ((int(*)(int, int, int, int))oMsgBox)(0,0, 0, 0);
}
int main()
{ FARPROC pFunc = GetProcAddress( GetModuleHandle("user32.dll"), "MessageBoxA");
oMsgBox = DetourFunction( (PBYTE)pFunc, (PBYTE)myMessageBoxA );
MessageBox(0, "Hello World!", 0, 0);
return 0;
}
|