Recent Microsoft linkers have the option of stack randomisation. This technique performs the task in a different manner, it adjusts the stack pointer dynamically at the application's entry point across a random range so that the stack pointer is different every time the app starts.
It uses GLOBAL scope variables as LOCAL variable are created on the stack and this code MUST be placed at the start of the entry point. All of the application code must come after it.
The virtue of this approach is it protects the app from certain types of stack exploits.
Code:
GLOBAL tcnt as DWORD GLOBAL seed as DWORD GLOBAL rand as DWORD GLOBAL lcnt as DWORD GLOBAL bstp as DWORD ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ FUNCTION PBmain() AS LONG tcnt = GetTickCount ' get a number sample ! mov eax, tcnt ! bswap eax ' invert byte order ! mov seed, eax ' store it as a seed ! mov lcnt, 64 ' loop iteration count RANDOMIZE seed ' set seed as base for rnd lbl: rand = RND(1, 32) ' random numbers in the range of 1 to 32 ! sub lcnt, 1 ! jnz lbl ! mov eax, rand ' load rnd result into eax ! shl eax, 4 ' mul by 16 original side comment had not been updated but the code is correct ! sub esp, eax ' subtract result from stack pointer ! mov bstp, esp ' a copy of ESP after mod Startup END FUNCTION ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
The virtue of this approach is it protects the app from certain types of stack exploits.
Comment