This is for a homework assignment.
I am supposed to change the contents of a dll function and put an error in it (0xCC). After loading the dll function and changing its protection to PAGE_EXECUTE_READWRITE.
I am having a trouble planting the error code in it. I tried the following (I removed the non interesting things):
typedef BOOL (*pfn)();
HMODULE hModule=LoadLibrary("somedll");
pfn somefunc=(pfn)GetProcAddress(hModule,"somefunction");
VirtualProtect (somefunc,dwSize,PAGE_EXECUTE_READWRITE,&dwOldProtect);
BYTE *p = (BYTE*)somefunc;
*p = 0xCC;
When I execute somefunc, instead of getting an exception, it is running without any problems. Why is that and am I doing wrong?
How do I overwrite the function?
Probably VirtualProtect returned an error. Check its (BOOL) return value and, if false, use GetLastError to check what was the cause.
Notice that VirtualProtect must be compatible with the access given at the call to VirtualAlloc that allocated the memory. Now, that’s something you can’t control, since VirtualAlloc is called internally in LoadLibrary.
Another thing is that the pointer provided to VirtualProtect must be the starting address to that memory page, and the function pointer you provided most probably is not. Your function can be anywhere inside the page, you’ll have to adjust it to the nearest page start before.
Of interest:
http://support.microsoft.com/kb/127904
(basically what you’re doing, but a few more steps required)
http://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/
(may be helpful for details on DLL loading mechanism)