OK, I'd call this one a prelim:
Randomizer.h
[php]#pragma once
#include <Windows.h>
#include "UTIL_Memory.h"
#define RANDOMIZE_BLOCK __asm _emit 0xEB \
__asm _emit 0x10 \
__asm _emit 0xAA \
__asm _emit 0xAB \
__asm _emit 0xBB \
__asm _emit 0xCB \
__asm _emit 0xDB \
__asm _emit 0xEB \
__asm _emit 0xFB \
__asm _emit 0xFB \
__asm _emit 0xEB \
__asm _emit 0xDB \
__asm _emit 0xCB \
__asm _emit 0xBB \
__asm _emit 0xAB \
__asm _emit 0x9B \
__asm _emit 0x8B \
__asm _emit 0x7B
class CRandomizer
{
public:
CRandomizer(void)
{
}
void AddFn(LPVOID function)
{
PDWORD addr = (PDWORD)Util::Memory::FindPattern((BYTE*)"\xAA\xAB \xBB\xCB\xDB\xEB\xFB\xFB\xEB\xDB\xCB\xBB\xAB\x9b\x 8b\x7b","xxxxxxxxxxxxxxxx",
(unsigned long)function, 0x30);
if (!addr)
return;
DWORD oldProtect = 0;
VirtualProtect(addr, 0x10, PAGE_EXECUTE_READWRITE, &oldProtect);
srand(GetTickCount());
memset(addr, rand(), 0x10);
VirtualProtect(addr, 0x10, oldProtect, &oldProtect);
}
};
[/php]
I'm sure you have your own findpattern equivalents.
Usage:
[php]CRandomizer g_Randomizer;
void FunctionToRandomize();
int _tmain(int argc, _TCHAR* argv[])
{
g_Randomizer.AddFn(FunctionToRandomize);
FunctionToRandomize();
getchar();
return 0;
}
void FunctionToRandomize()
{
RANDOMIZE_BLOCK
printf("OK\n");
}
[/php]
Output is "OK".
Verified in IDA that it does write over those bytes. There are problems with some debug builds that use jumps to go into your functions. This code will not follow those jumps. You can only use one RANDOMIZE_BLOCK per function and it should be at the beginning of that function.
Adding more RANDOMIZE_BLOCKs won't hurt, it'll just be kind of silly.
/
