Rob-- / memoryjs

Read and write process memory in Node.js (Windows API functions exposed via Node bindings)
MIT License
636 stars 88 forks source link

Issue writing a string #33

Closed dustinrouillard closed 5 years ago

dustinrouillard commented 6 years ago

Alright, so I've tried multiple times to write a string back to memory, the string is the same length just the original text but reversed to make it a noticeable change.

This is the code I'm using to read the address and I know it is working perfectly to get me the value of the address.

let processName = "game.exe";

memory.openProcess(processName, (error, processObject) => {
    console.log(`Game PID: ${processObject.th32ProcessID}`);

    let exe = memory.findModule(processName, processObject.th32ProcessID);

    console.log(`Module PID: ${exe.th32ProcessID}`);

    let addr = `0x${(exe.modBaseAddr + 0x2829BFC).toString(16)}`;

    console.log(`Username Address: ${addr}`);

    let processMemory = memory.readMemory(processObject.handle, addr, 'string');

    console.log(`Current Value: ${processMemory}`);
});

I was going through some trouble at first reading until I changed the handle in the readMemory spot to the one from the original process instead of the module.

I've tried writting like this with both handles and none of them seem to have any effect of the memory.

setTimeout(() => {
    console.log(`Attempting to change the value now.`);

    setTimeout(() => {
        memory.writeMemory(processObject.handle, addr, updatedUsername, 'string');
    }, 100);

    setTimeout(() => {
        let processMemorynew = memory.readMemory(processObject.handle, addr, 'string');

        console.log(`This is the value now: ${processMemorynew}`);
    }, 500);
}, 1000);

The value never changes, I originally thought this was a permissions issue with windows but even in the Administrator CMD nothing seems to happen. I've also tried using setProtection before the writeMemory on the same address and it still seems to have no effect.

memory.setProtection(processObject.handle, addr, updatedUsername.length, memory.PAGE_EXECUTE_READWRITE);

If anyone happens to know what I can do to fix this or if I'm doing anything wrong. I'm able to change all the memory values I'm trying to change with this in other memory editors perfectly fine.

(Note: I would like to add this is a 64-bit process and I'm not sure if this matters or not with how it's done on the C++ side of it.)

Rob-- commented 5 years ago

Just tested this and I encountered no problems.

Here was my test application:

// print the value of the string and its memory location
string _string = "robert";
cout << "string\t0x" << hex << (DWORD64)_string.c_str() << dec << "\t" << _string << endl;

// pause, during this pause I use memoryjs to write to the string
// after I've written to the string, reprint the value
getchar();
cout << "string\t0x" << hex << (DWORD64)_string.c_str() << dec << "\t" << _string << endl;

And I used the following code to read the string (ensure the correct address) and then write:

> processObject = memoryjs.openProcess("ConsoleApplication2.exe")
{ dwSize: 304,
  th32ProcessID: 6076,
  cntThreads: 3,
  th32ParentProcessID: 5524,
  pcPriClassBase: 8,
  szExeFile: 'ConsoleApplication2.exe',
  handle: 744,
  modBaseAddr: 20774912 }
> memoryjs.readMemory(processObject.handle, 0x4ff940, 'string');
'robert'
> memoryjs.writeMemory(processObject.handle, 0x4ff940, 'rogerd', 'string');
undefined
> memoryjs.readMemory(processObject.handle, 0x4ff940, 'string');
'rogerd'