In x86, I get the address of the function with GetProcAddress()and write a simple one XOR EAX,EAX; RET 4;in it. Simple and efficient. How to do the same in x64?
bool DisableSetUnhandledExceptionFilter()
{
const BYTE PatchBytes[5] = { 0x33, 0xC0, 0xC2, 0x04, 0x00 };
HMODULE hLib = GetModuleHandle( _T("kernel32.dll") );
if( hLib == NULL )
return false;
BYTE* pTarget = (BYTE*)GetProcAddress( hLib, "SetUnhandledExceptionFilter" );
if( pTarget == 0 )
return false;
if( !WriteMemory( pTarget, PatchBytes, sizeof(PatchBytes) ) )
return false;
FlushInstructionCache(GetCurrentProcess(), pTarget, sizeof(PatchBytes));
return true;
}
static bool WriteMemory( BYTE* pTarget, const BYTE* pSource, DWORD Size )
{
if( pTarget == 0 )
return false;
if( pSource == 0 )
return false;
if( Size == 0 )
return false;
if( IsBadReadPtr( pSource, Size ) )
return false;
DWORD OldProtect = 0;
if( !VirtualProtect( pTarget, Size, PAGE_EXECUTE_READWRITE, &OldProtect ) )
return false;
memcpy( pTarget, pSource, Size );
DWORD Temp = 0;
if( !VirtualProtect( pTarget, Size, OldProtect, &Temp ) )
return false;
return true;
}
This example is adapted from the code found here: http://www.debuginfo.com/articles/debugfilters.html#overwrite .
source
share