Read and write process memory in Node.js (Windows API functions exposed via Node bindings)
Read and write process memory in Node.js (Windows API functions exposed via Node bindings)
memoryjs is a an NPM package to read and write process memory!
Features • Getting Started • Usage • Documentation • Debug
TODO:
This is a Node add-on (last tested to be working on v14.15.0) and therefore requires node-gyp to use.
You may also need to follow these steps to install and setup node-gyp.
npm install memoryjsWhen using memoryjs, the target process should match the platform architecture of the Node version running. For example if you want to target a 64 bit process, you should try and use a 64 bit version of Node.
You also need to recompile the library and target the platform you want. Head to the memoryjs node module directory, open up a terminal and run one of the following compile scripts:
# will automatically compile based on the detected Node architecture
npm run build
# compile to target 32 bit processes
npm run build32
# compile to target 64 bit processes
npm run build64If you are planning to use this module with Node Webkit or Electron, take a look at Liam Mitchell's build notes here.
const memoryjs = require('memoryjs');
const processName = "csgo.exe";// sync: open a process
const processObject = memoryjs.openProcess(processName);
// async: open a process
memoryjs.openProcess(processName, (error, processObject) => {});
// sync: get all processes
const processes = memoryjs.getProcesses();
// async: get all processes
memoryjs.getProcesses((error, processes) => {});
// close a process (release handle)
memoryjs.closeHandle(handle);See the Documentation section of this README to see what a process object looks like.
// sync: find a module
const moduleObject = memoryjs.findModule(moduleName, processId);
// async: find a module
memoryjs.findModule(moduleName, processId, (error, moduleObject) => {});
// sync: get all modules
const modules = memoryjs.getModules(processId);
// async: get all modules
memoryjs.getModules(processId, (error, modules) => {});See the Documentation section of this README to see what a module object looks like.
…See the Documentation section of this README to see what values dataType can be.
// sync: open a named file mapping object
const fileHandle = memoryjs.openFileMapping(fileName);
// sync: map entire file into a specified process
const baseAddress = memoryjs.mapViewOfFile(processHandle, fileName);
// sync: map portion of a file into a specified process
const baseAddress = memoryjs.mapViewOfFile(processHandle, fileName, offset, viewSize, pageProtection);
// sync: close handle to a file mapping object
const success = memoryjs.closeHandle(fileHandle);See the Documentation section of this README to see details on the parameters and return values for these functions.
// sync: change/set the protection on a region of memory
const oldProtection = memoryjs.virtualProtectEx(handle, address, size, protection);See the Documentation section of this README to see what values protection can be.
…// sync: execute a function in a remote process
const result = memoryjs.callFunction(handle, args, returnType, address);
// async: execute a function in a remote process
memoryjs.callFunction(handle, args, returnType, address, (error, result) => {});Click here to see what a result object looks like.
Click here for details about how to format the arguments and the return type.
……Note: this documentation is currently being updated, refer to the Wiki for more information.
{ dwSize: 304,
th32ProcessID: 10316,
cntThreads: 47,
th32ParentProcessID: 7804,
pcPriClassBase: 8,
szExeFile: "csgo.exe",
modBaseAddr: 1673789440,
handle: 808 }The handle and modBaseAddr properties are only available when opening a process and not when listing processes.
{ modBaseAddr: 468123648,
modBaseSize: 80302080,
szExePath: 'c:\\program files (x86)\\steam\\steamapps\\common\\counter-strike global offensive\\csgo\\bin\\client.dll',
szModule: 'client.dll',
th32ProcessID: 10316,
GlblcntUsage: 2 }{ returnValue: 1.23,
exitCode: 2 }This object is returned when a function is executed in a remote process:
returnValue is the value returned from the function that was calledexitCode is the termination status of the threadWhen using the write or read functions, the data type (dataType) parameter should reference a constant from within the library:
| Constant | Bytes | Aliases | Range |
|---|---|---|---|
memoryjs.BOOL |
1 | memoryjs.BOOLEAN |
0 to 1 |
memoryjs.INT8 |
1 | memoryjs.BYTE, memoryjs.CHAR |
-128 to 127 |
memoryjs.UINT8 |
1 | memoryjs.UBYTE, memoryjs.UCHAR |
0 to 255 |
memoryjs.INT16 |
2 | memoryjs.SHORT |
-32,768 to 32,767 |
memoryjs.UINT16 |
2 | memoryjs.USHORT, memoryjs.WORD |
0 to 65,535 |
memoryjs.INT32 |
4 | memoryjs.INT, memoryjs.LONG |
-2,147,483,648 to 2,147,483,647 |
memoryjs.UINT32 |
4 | memoryjs.UINT, memoryjs.ULONG, memoryjs.DWORD |
0 to 4,294,967,295 |
memoryjs.INT64 |
8 | n/a | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
memoryjs.UINT64 |
8 | n/a | 0 to 18,446,744,073,709,551,615 |
memoryjs.FLOAT |
4 | n/a | 3.4E +/- 38 (7 digits) |
memoryjs.DOUBLE |
8 | n/a | 1.7E +/- 308 (15 digits) |
memoryjs.PTR |
4/8 | memoryjs.POINTER |
n/a |
memoryjs.UPTR |
4/8 | memoryjs.UPOINTER |
n/a |
memoryjs.STR |
n/a | memoryjs.STRING |
n/a |
memoryjs.VEC3 |
12 | memoryjs.VECTOR3 |
n/a |
memoryjs.VEC4 |
16 | memoryjs.VECTOR4 |
n/a |
Notes:
_BE to the data type. For example: memoryjs.DOUBLE_BE.INT64, UINT64, INT64_BE, UINT64_BE) you will need to supply a BigInt. When reading a 64 bit integer, you will receive a BigInt.These data types are to used to denote the type of data being read or written.
64 bit integer example:
const value = memoryjs.readMemory(handle, address, memoryjs.INT64);
console.log(typeof value); // bigint
memoryjs.writeMemory(handle, address, value + 1n, memoryjs.INT64);Vector3 is a data structure of three floats:
const vector3 = { x: 0.0, y: 0.0, z: 0.0 };
memoryjs.writeMemory(handle, address, vector3, memoryjs.VEC3);Vector4 is a data structure of four floats:
const vector4 = { w: 0.0, x: 0.0, y: 0.0, z: 0.0 };
memoryjs.writeMemory(handle, address, vector4, memoryjs.VEC4);If you have a structure you want to write to memory, you can use buffers. For an example on how to do this, view the buffers example.
To write/read a structure to/from memory, you can use structron to define your structures and use them to write or parse buffers.
If you want to read a std::string using structron, the library exposes a custom type that can be used to read/write strings:
// To create the type, we need to pass the process handle, base address of the
// structure, and the target process architecture (either "32" or "64").
const stringType = memoryjs.STRUCTRON_TYPE_STRING(processObject.handle, structAddress, '64');
// Create a custom structure using the custom type, full example in /examples/buffers.js
const Struct = require('structron');
const Player = new Struct()
.addMember(string, 'name');Alternatively, you can use the concentrate and dissolve libraries to achieve the same thing. An old example of this is here.
Protection type is a bit flag DWORD value.
This parameter should reference a constant from the library:
memoryjs.PAGE_NOACCESS, memoryjs.PAGE_READONLY, memoryjs.PAGE_READWRITE, memoryjs.PAGE_WRITECOPY, memoryjs.PAGE_EXECUTE, memoryjs.PAGE_EXECUTE_READ, memoryjs.PAGE_EXECUTE_READWRITE, memoryjs.PAGE_EXECUTE_WRITECOPY, memoryjs.PAGE_GUARD, memoryjs.PAGE_NOCACHE, memoryjs.PAGE_WRITECOMBINE, memoryjs.PAGE_ENCLAVE_THREAD_CONTROL, memoryjs.PAGE_TARGETS_NO_UPDATE, memoryjs.PAGE_TARGETS_INVALID, memoryjs.PAGE_ENCLAVE_UNVALIDATED
Refer to MSDN's Memory Protection Constants for more information.
Memory allocation type is a bit flag DWORD value.
This parameter should reference a constat from the library:
memoryjs.MEM_COMMIT, memoryjs.MEM_RESERVE, memoryjs.MEM_RESET, memoryjs.MEM_RESET_UNDO
Refer to MSDN's VirtualAllocEx documentation for more information.
You can use this library to read either a "string", or "char*" and to write a string.
In both cases you
No open issues yet, or sync has not completed.