Rob-- / memoryjs

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

Error reading BigInt pointer #114

Open alorodri opened 1 year ago

alorodri commented 1 year ago

Hello!

In this code, it fails to read the memory from the first pointer, when it sums the offset and tries to readMemory again the new address. Elden Ring is a x64 game, I compiled memoryjs with build64 and I had no problem getting the first ptr value. The problem comes with the second readMemory call, because the first returns a BigInt, then when I call again I must pass an address that is a BigInt, and then it shows me that error:

C:\Users\alope\source\repos\electronTestWindows\node_modules\memoryjs\index.js:49
    return memoryjs.readMemory(handle, address, dataType.toLowerCase());
                    ^

Error: Error in native callback
    at Object.readMemory (C:\Users\alope\source\repos\electronTestWindows\node_modules\memoryjs\index.js:49:21)
    at readMemory (C:\Users\alope\source\repos\electronTestWindows\test.js:16:26)
    at Object.<anonymous> (C:\Users\alope\source\repos\electronTestWindows\test.js:24:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.17.1

Some useful data:

Object process

{
  dwSize: 304,
  th32ProcessID: 30796,
  cntThreads: 97,
  th32ParentProcessID: 32248,
  pcPriClassBase: 8,
  szExeFile: 'eldenring.exe',
  handle: 564,
  modBaseAddr: 2061870039768
}

That's my code:

const memoryjs = require("memoryjs");

function readMemory() {
  const processName = "eldenring.exe";
  const offset1 = 0x3cd4d88;
  const offset2 = 0x94;

  const processObject = memoryjs.openProcess(processName);
  const baseAddress = processObject.modBaseAddr;
  const ptrToValue = memoryjs.readMemory(
    processObject.handle,
    baseAddress + offset1,
    memoryjs.PTR
  );

  const value = memoryjs.readMemory(
    processObject.handle,
    ptrToValue + BigInt(offset2),
    memoryjs.INT64
  );
  console.log(value);
}

readMemory();

Thanks in advance!

Danovich commented 10 months ago

Hey @alorodri,

I have the same error, did you manage to figure it out? If I pass BigInt to func it fails even though it should support it @Rob-- .

all functions that accept an address also accept the address as a BigInt

alorodri commented 10 months ago

Hey @alorodri,

I have the same error, did you manage to figure it out? If I pass BigInt to func it fails even though it should support it @Rob-- .

all functions that accept an address also accept the address as a BigInt

Hey @Danovich ! Besides idk if it's the correct solution, I've been able to manage it casting the address to Number before passing it again to the readMemory function.

Danovich commented 10 months ago

@alorodri and the data you get from readMemory is correct? Converting bigint to number is sketchy as you can go over the MAX_SAFE_INTEGER and lose accuracy...

alorodri commented 10 months ago

@alorodri and the data you get from readMemory is correct? Converting bigint to number is sketchy as you can go over the MAX_SAFE_INTEGER and lose accuracy...

In my case it worked and I got the value I was searching for, but seems it loses accuracy and can result in errors if the Number is > 2^54 or something like that.

Danovich commented 10 months ago

@Rob-- any updates on this issue?

Rob-- commented 5 months ago

Hi, will fix this soon.