Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use uintptr_t type for addresses to avoid data loss in x64 #14

Merged
merged 3 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ The `handle` and `modBaseAddr` properties are only available when opening a proc

When using the write or read functions, the data type (dataType) parameter can either be a string and be one of the following:

`"int", "dword", "long", "float", "double", "bool", "boolean", "str", "string", "vec3", "vector3", "vec4", "vector4"`
`"int", "dword", "long", "float", "double", "bool", "boolean", "ptr", "pointer", "str", "string", "vec3", "vector3", "vec4", "vector4"`

or can reference constants from within the library:

`memoryjs.INT, memoryjs.DWORD, memoryjs.LONG, memoryjs.FLOAT, memoryjs.DOUBLE, memoryjs.BOOL, memoryjs.BOOLEAN, memoryjs.STR, memoryjs.STRING, memoryjs.VEC3, memoryjs.VECTOR3, memoryjs.VEC4, memoryjs.VECTOR4`
`memoryjs.INT, memoryjs.DWORD, memoryjs.LONG, memoryjs.FLOAT, memoryjs.DOUBLE, memoryjs.BOOL, memoryjs.BOOLEAN, memoryjs.PTR, memoryjs.POINTER, memoryjs.STR, memoryjs.STRING, memoryjs.VEC3, memoryjs.VECTOR3, memoryjs.VEC4, memoryjs.VECTOR4`

This is simply used to denote the type of data being read or written.

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module.exports = {
DOUBLE: 'double',
BOOL: 'bool',
BOOLEAN: 'boolean',
PTR: 'ptr',
POINTER: 'pointer',
STR: 'str',
STRING: 'string',
VEC3: 'vec3',
Expand Down
39 changes: 23 additions & 16 deletions lib/memoryjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void openProcess(const FunctionCallbackInfo<Value>& args) {
processInfo->Set(String::NewFromUtf8(isolate, "handle"), Number::New(isolate, (int)Process.hProcess));

DWORD base = Module.getBaseAddress((char*) *(processName), process.th32ProcessID);
processInfo->Set(String::NewFromUtf8(isolate, "modBaseAddr"), Number::New(isolate, (int)base));
processInfo->Set(String::NewFromUtf8(isolate, "modBaseAddr"), Number::New(isolate, (uintptr_t)base));

/* openProcess can either take one argument or can take
two arguments for asychronous use (second argument is the callback) */
Expand Down Expand Up @@ -207,7 +207,7 @@ void getModules(const FunctionCallbackInfo<Value>& args) {
// Create a v8 object to store the current module's information
Local<Object> module = Object::New(isolate);

module->Set(String::NewFromUtf8(isolate, "modBaseAddr"), Number::New(isolate, (int)moduleEntries[i].modBaseAddr));
module->Set(String::NewFromUtf8(isolate, "modBaseAddr"), Number::New(isolate, (uintptr_t)moduleEntries[i].modBaseAddr));
module->Set(String::NewFromUtf8(isolate, "modBaseSize"), Number::New(isolate, (int)moduleEntries[i].modBaseSize));
module->Set(String::NewFromUtf8(isolate, "szExePath"), String::NewFromUtf8(isolate, moduleEntries[i].szExePath));
module->Set(String::NewFromUtf8(isolate, "szModule"), String::NewFromUtf8(isolate, moduleEntries[i].szModule));
Expand Down Expand Up @@ -271,12 +271,12 @@ void findModule(const FunctionCallbackInfo<Value>& args) {
// Create a v8 Object (JSON) to store the process information
Local<Object> moduleInfo = Object::New(isolate);

moduleInfo->Set(String::NewFromUtf8(isolate, "modBaseAddr"), Number::New(isolate, (int)module.modBaseAddr));
moduleInfo->Set(String::NewFromUtf8(isolate, "modBaseAddr"), Number::New(isolate, (uintptr_t)module.modBaseAddr));
moduleInfo->Set(String::NewFromUtf8(isolate, "modBaseSize"), Number::New(isolate, (int)module.modBaseSize));
moduleInfo->Set(String::NewFromUtf8(isolate, "szExePath"), String::NewFromUtf8(isolate, module.szExePath));
moduleInfo->Set(String::NewFromUtf8(isolate, "szModule"), String::NewFromUtf8(isolate, module.szModule));
moduleInfo->Set(String::NewFromUtf8(isolate, "th32ProcessID"), Number::New(isolate, (int)module.th32ProcessID));
moduleInfo->Set(String::NewFromUtf8(isolate, "hModule"), Number::New(isolate, (int)module.hModule));
moduleInfo->Set(String::NewFromUtf8(isolate, "hModule"), Number::New(isolate, (uintptr_t)module.hModule));

/* findModule can either take one or two arguments,
three arguments for asychronous use (third argument is the callback) */
Expand Down Expand Up @@ -319,44 +319,51 @@ void readMemory(const FunctionCallbackInfo<Value>& args) {
Local<Value> argv[argc];

// Define the error message that will be set if no data type is recognised
argv[1] = String::NewFromUtf8(isolate, "");
argv[0] = String::NewFromUtf8(isolate, "");

/* following if statements find the data type to read and then return the correct data type
args[0] -> Uint32Value() is the address to read, unsigned int is used because address needs to be positive */
if (!strcmp(dataType, "int")) {

int result = Memory.readMemory<int>(process::hProcess, args[0]->Uint32Value());
if (args.Length() == 3) argv[0] = Number::New(isolate, result);
if (args.Length() == 3) argv[1] = Number::New(isolate, result);
else args.GetReturnValue().Set(Number::New(isolate, result));

} else if (!strcmp(dataType, "dword")) {

DWORD result = Memory.readMemory<DWORD>(process::hProcess, args[0]->Uint32Value());
if (args.Length() == 3) argv[0] = Number::New(isolate, result);
if (args.Length() == 3) argv[1] = Number::New(isolate, result);
else args.GetReturnValue().Set(Number::New(isolate, result));

} else if (!strcmp(dataType, "long")) {

long result = Memory.readMemory<long>(process::hProcess, args[0]->Uint32Value());
if (args.Length() == 3) argv[0] = Number::New(isolate, result);
if (args.Length() == 3) argv[1] = Number::New(isolate, result);
else args.GetReturnValue().Set(Number::New(isolate, result));

} else if (!strcmp(dataType, "float")) {

float result = Memory.readMemory<float>(process::hProcess, args[0]->Uint32Value());
if (args.Length() == 3) argv[0] = Number::New(isolate, result);
if (args.Length() == 3) argv[1] = Number::New(isolate, result);
else args.GetReturnValue().Set(Number::New(isolate, result));

} else if (!strcmp(dataType, "double")) {

double result = Memory.readMemory<double>(process::hProcess, args[0]->Uint32Value());
if (args.Length() == 3) argv[0] = Number::New(isolate, result);
if (args.Length() == 3) argv[1] = Number::New(isolate, result);
else args.GetReturnValue().Set(Number::New(isolate, result));

}
else if (!strcmp(dataType, "ptr") || !strcmp(dataType, "pointer")) {

intptr_t result = Memory.readMemory<intptr_t>(process::hProcess, args[0]->Uint32Value());
if (args.Length() == 3) argv[1] = Number::New(isolate, result);
else args.GetReturnValue().Set(Number::New(isolate, result));

} else if (!strcmp(dataType, "bool") || !strcmp(dataType, "boolean")) {

bool result = Memory.readMemory<bool>(process::hProcess, args[0]->Uint32Value());
if (args.Length() == 3) argv[0] = Boolean::New(isolate, result);
if (args.Length() == 3) argv[1] = Boolean::New(isolate, result);
else args.GetReturnValue().Set(Boolean::New(isolate, result));

} else if (!strcmp(dataType, "string") || !strcmp(dataType, "str")) {
Expand All @@ -382,13 +389,13 @@ void readMemory(const FunctionCallbackInfo<Value>& args) {
}

if (chars.size() == 0) {
if (args.Length() == 3) argv[1] = String::NewFromUtf8(isolate, "unable to read string (no null-terminator found after 1 million chars)");
if (args.Length() == 3) argv[0] = String::NewFromUtf8(isolate, "unable to read string (no null-terminator found after 1 million chars)");
else return memoryjs::throwError("unable to read string (no null-terminator found after 1 million chars)", isolate);
} else {
// vector -> string
std::string str(chars.begin(), chars.end());

if (args.Length() == 3) argv[0] = String::NewFromUtf8(isolate, str.c_str());
if (args.Length() == 3) argv[1] = String::NewFromUtf8(isolate, str.c_str());
else args.GetReturnValue().Set(String::NewFromUtf8(isolate, str.c_str()));
}

Expand All @@ -400,7 +407,7 @@ void readMemory(const FunctionCallbackInfo<Value>& args) {
moduleInfo->Set(String::NewFromUtf8(isolate, "y"), Number::New(isolate, result.y));
moduleInfo->Set(String::NewFromUtf8(isolate, "z"), Number::New(isolate, result.z));

if (args.Length() == 3) argv[0] = moduleInfo;
if (args.Length() == 3) argv[1] = moduleInfo;
else args.GetReturnValue().Set(moduleInfo);

} else if (!strcmp(dataType, "vector4") || !strcmp(dataType, "vec4")) {
Expand All @@ -412,12 +419,12 @@ void readMemory(const FunctionCallbackInfo<Value>& args) {
moduleInfo->Set(String::NewFromUtf8(isolate, "y"), Number::New(isolate, result.y));
moduleInfo->Set(String::NewFromUtf8(isolate, "z"), Number::New(isolate, result.z));

if (args.Length() == 3) argv[0] = moduleInfo;
if (args.Length() == 3) argv[1] = moduleInfo;
else args.GetReturnValue().Set(moduleInfo);

} else {

if (args.Length() == 3) argv[1] = String::NewFromUtf8(isolate, "unexpected data type");
if (args.Length() == 3) argv[0] = String::NewFromUtf8(isolate, "unexpected data type");
else return memoryjs::throwError("unexpected data type", isolate);

}
Expand Down