Static memory overwriting (machine code) on runtime with c++

Viewed 108

I want to modify a single-player game A which will be done with application B. When application B is launched it finds the game A process and overwrites some assembly code. Without launched application B game should be modified. In short, I want to be able to edit the machine code of any application, as it is possible in cheat engine, in application runtime

OS I am working on is Windows 10 x64. Game A is written in assembly x86.

I know where the code is located A.exe + pointer, and the length of the code 5 bytes

The code I have currently does not work, because of an exception "Access violation reading location"

Writing to address of assembly code

DWORD oldPermissions;
BYTE* whereToWrite = (BYTE*) aExeBaseAddress + pointer;
BYTE* replacementCode = {0x90, 0x90, 0x90, 0x90, 0x90}; // Nop's in assembly, this is just example value
VirtualProtect(whereToWrite, 5, PAGE_EXECUTE_READWRITE, &oldPermissions);
memcpy(whereToWrite, replacementCode, 5); // This place throws exception, because I do not have permissions
VirtualProtect(whereToWrite, 5, oldPermissions, &oldPermissions);

Reading from address of assembly code

DWORD oldPermissions;
BYTE* fromWhereToRead = (BYTE*) aExeBaseAddress + pointer;
BYTE* whereToWrite = new BYTE[5];
VirtualProtect(fromWhereToRead, 5, PAGE_EXECUTE_READWRITE, &oldPermissions);
memcpy(whereToWrite, fromWhereToRead, 5); // This place throws exception, because I do not have permissions
VirtualProtect(fromWhereToRead, 5, oldPermissions, &oldPermissions);

The Assembly code that I am trying to overwrite is a function call. I would like the game A to call a function on application B. So even if I am able to get byte code to be overwritten, I still need a way how to get a pointer to a function on application B, turn it into bytecode for application A to call

Is it even possible, what I am trying to achieve? Thanks for suggestions and help

0 Answers
Related