I want to create self erasing code - function to erase others. I tried to do it - but sometimes (most of times) I run it - it isn't working as it's suposed to work. I want to do something like this:
int func(int a)
{
return a+1;
}
int main()
{
func(5);
erase(func);
...
//execute other things
...
}
I already created function to do this, but it is kinda buggy - it's nopping everything until it finds ret opcode, I know that func begin address is (DWORD)func, but how do I find end of function address? I've seen example on web with something like:
void func()
_asm __volatile__ beg
{
...
...
...
_asm __volatile__ end
}
but this does not work for me - I'm using VS 2013. Here's my code:
void destruct(BYTE *pAddress, DWORD dwLen)
{
DWORD dwOldProtect, dwBkup;
VirtualProtect(pAddress, dwLen, PAGE_EXECUTE_READWRITE, &dwOldProtect);
for(DWORD x = 0x0; x < dwLen; x++)
*(pAddress + x) = 0x90;
VirtualProtect(pAddress, dwLen, dwOldProtect, &dwBkup);
}
as dwLen I pass length calculated in other function - from begin to next ret statement. As delete function I mean fill it's body with NOP or junk - I want to "erase" function, which will be used at startup of program only one time, I don't need it later, I just don't want anybody to dump my app and reverse it etc.