Super bored so I decided to do this, now I'm bored again. >_>
The example below shows how forcing an access violation and setting up an exception handler to catch the exception could be used to hook a function. Yep...
Remember, I'm always looking for criticism to improve my code. |:
Well, enjoy?
[php]#include <windows.h>
/*
xor eax,eax
mov eax,[eax]
*/
BYTE bytes[] = { 0x33,0xC0,0x8B,0x00,0x90 }; //force access violation
BYTE origBytes[5];
BYTE *addy;
int __stdcall Func(HWND hwnd,LPCTSTR text,LPCTSTR caption,UINT code)
{
text = "Hooked";
caption = "Hooked";
return MessageBox(hwnd,text,caption,code);
}
LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS pException)
{
pException->ContextRecord->Eip = (DWORD)Func;
for(int i=0;i<sizeof(bytes)+1;i++)
{
*(addy+i) = origBytes[i];
}
return EXCEPTION_CONTINUE_EXECUTION;
}
void Main()
{
while(!GetModuleHandle("user32.dll"))
{
Sleep(10);
}
AddVectoredExceptionHandler(1,&ExceptionHandler);
addy = (BYTE*)GetProcAddress( LoadLibrary("user32.dll"),"MessageBoxA" );
DWORD old;
VirtualProtect(addy,4096,PAGE_EXECUTE_READWRITE,&o ld);
for(int i=0;i<sizeof(bytes)+1;i++)
{
origBytes[i] = *(addy+i);
*(addy+i) = bytes[i];
}
}
bool __stdcall DllMain(HINSTANCE hInst,DWORD dwReason,void* useless)
{
if(dwReason == DLL_PROCESS_ATTACH)
{
CreateThread(0,0,(LPTHREAD_START_ROUTINE)Main,0,0, 0);
}
return true;
}[/php]