diff --git a/include/readmem.h b/include/readmem.h index 0fca83b..e8918af 100644 --- a/include/readmem.h +++ b/include/readmem.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "lasprint.h" @@ -21,6 +23,8 @@ using std::cerr; using std::exception; using std::this_thread::sleep_for; using std::chrono::microseconds; +using std::array; +using std::stringstream; extern int pid; @@ -28,6 +32,6 @@ int processID(lua_State* L); int readAddress(lua_State* L); template -T readMem(int pid, uint64_t memAddress); +T readMem(int pid, uintptr_t memAddress); #endif \ No newline at end of file diff --git a/src/readmem.cpp b/src/readmem.cpp index 1e6727e..cc08109 100644 --- a/src/readmem.cpp +++ b/src/readmem.cpp @@ -1,12 +1,45 @@ #include "readmem.h" string processName; +string newProcessName; +uintptr_t memoryOffset = 0; struct iovec memLocal; struct iovec memRemote; int pid = 0; +void setMemoryOffset() +{ + string command = "cat /proc/" + to_string(pid) + "/maps | grep " + newProcessName; + array buffer; + string result; + + // Open the command for reading + FILE* pipe = popen(command.c_str(), "r"); + if (!pipe) + { + std::cout << "Error executing command: " << command << std::endl; + } + + // Read the command output line by line + while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) + { + result += buffer.data(); + } + + // Close the pipe + pclose(pipe); + + size_t dashPos = result.find_first_of("-"); + + if (dashPos != string::npos) + { + string firstNumber = result.substr(0, dashPos); + memoryOffset = stoull(firstNumber, nullptr, 16); + } +} + struct StockPid { pid_t pid; @@ -40,7 +73,7 @@ void Func_StockPid(const char *processtarget) int processID(lua_State* L) { processName = lua_tostring(L, 1); - string newProcessName = processName.substr(0, 15); + newProcessName = processName.substr(0, 15); string command = "pidof " + newProcessName; const char *cCommand = command.c_str(); @@ -54,11 +87,13 @@ int processID(lua_State* L) } lasPrint("\n"); + setMemoryOffset(); + return 0; } template -T readMem(int pid, uint64_t memAddress) +T readMem(int pid, uintptr_t memAddress) { T value; // Variable to store the read value @@ -99,8 +134,8 @@ template string readMem(int pid, uint64_t memAddress); int readAddress(lua_State* L) { + uintptr_t address = memoryOffset; string valueType = lua_tostring(L, 1); - uint64_t address = 0; for (int i = 2; i <= lua_gettop(L); i++) { address += lua_tointeger(L, i); // Calculate the final memory address by summing the Lua arguments.