// Struct to ensure HANDLEs are properly closed
struct HandleCloser {
void operator()(HANDLE handle) const {
if (handle && handle != INVALID_HANDLE_VALUE) {
CloseHandle(handle);
}
}
};
using UniqueHandle = std::unique_ptr<std::remove_pointer::type, HandleCloser>;
// Function to log messages to a log file only
void log_message(std::ofstream& log_file, const char* format, ...) {
va_list args;
va_start(args, format);
char buffer[2048]; // Increased buffer size for longer messages
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
code
log_file << buffer;
log_file.flush();
}
// Function to retrieve the Process ID (PID) by process name
DWORD get_process_id_by_name(const std::string& process_name, std::ofstream& log_file) {
UniqueHandle hSnapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (!hSnapshot || hSnapshot.get() == INVALID_HANDLE_VALUE) {
log_message(log_file, "CreateToolhelp32Snapshot failed with error: %lu\n", GetLastError());
return 0;
}
code
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot.get(), &pe32)) {
log_message(log_file, "Process32First failed with error: %lu\n", GetLastError());
return 0;
}
do {
std::wstring ws(pe32.szExeFile);
std::string exeFile(ws.begin(), ws.end());
if (_stricmp(exeFile.c_str(), process_name.c_str()) == 0) {
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot.get(), &pe32));
return 0;
}
// Function to retrieve the base address of the main module of the process
uintptr_t get_base_address(DWORD pid, const std::string& process_name, std::ofstream& log_file) {
UniqueHandle hSnapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid));
if (!hSnapshot || hSnapshot.get() == INVALID_HANDLE_VALUE) {
log_message(log_file, "CreateToolhelp32Snapshot for modules failed with error: %lu\n", GetLastError());
return 0;
}
code
Assistant
MODULEENTRY32 me32;
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hSnapshot.get(), &me32)) {
log_message(log_file, "Module32First failed with error: %lu\n", GetLastError());
return 0;
}
do {
// Convert module name to std::string for comparison
std::wstring ws(me32.szModule);
std::string moduleName(ws.begin(), ws.end());
// Compare the module name with the process name
if (_stricmp(moduleName.c_str(), process_name.c_str()) == 0) {
log_message(log_file, "Main module %s found at base address 0x%llX\n", moduleName.c_str(),
reinterpret_cast<unsigned long long>(me32.modBaseAddr));
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
} while (Module32Next(hSnapshot.get(), &me32));
log_message(log_file, "Main module not found in the module list.\n");
return 0;
}
// Struct to store section information
struct Section {
std::string name;
uintptr_t start;
uintptr_t end;
};
// Function to parse PE headers and extract sections
bool get_sections(HANDLE hProcess, uintptr_t base_addr, std::vector& sections, std::ofstream& log_file) {
// Read the DOS header
IMAGE_DOS_HEADER dosHeader;
SIZE_T bytesRead;
if (!ReadProcessMemory(hProcess, reinterpret_cast(base_addr), &dosHeader, sizeof(dosHeader), &bytesRead) || bytesRead != sizeof(dosHeader)) {
log_message(log_file, "Failed to read DOS header. Error: %lu\n", GetLastError());
return false;
}
code
if (dosHeader.e_magic != IMAGE_DOS_SIGNATURE) {
log_message(log_file, "Invalid DOS signature.\n");
return false;
}
uintptr_t ntHeaderAddr = base_addr + dosHeader.e_lfanew;
DWORD signature;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(ntHeaderAddr), &signature, sizeof(signature), &bytesRead) || bytesRead != sizeof(signature)) {
log_message(log_file, "Failed to read NT headers signature. Error: %lu\n", GetLastError());
return false;
}
if (signature != IMAGE_NT_SIGNATURE) {
log_message(log_file, "Invalid NT signature.\n");
return false;
}
IMAGE_FILE_HEADER fileHeader;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(ntHeaderAddr + sizeof(DWORD)), &fileHeader, sizeof(fileHeader), &bytesRead) || bytesRead != sizeof(fileHeader)) {
log_message(log_file, "Failed to read IMAGE_FILE_HEADER. Error: %lu\n", GetLastError());
return false;
}
WORD sizeOfOptionalHeader = fileHeader.SizeOfOptionalHeader;
uintptr_t optionalHeaderAddr = ntHeaderAddr + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER);
WORD magic;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(optionalHeaderAddr), &magic, sizeof(magic), &bytesRead) || bytesRead != sizeof(magic)) {
log_message(log_file, "Failed to read Optional Header Magic. Error: %lu\n", GetLastError());
return false;
}
bool is64 = false;
if (magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
is64 = true;
}
else if (magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
is64 = false;
}
else {
log_message(log_file, "Unknown Optional Header Magic: 0x%X.\n", magic);
return false;
}
// Read the entire Optional Header
std::vector<BYTE> optionalHeaderData(sizeOfOptionalHeader);
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(optionalHeaderAddr), optionalHeaderData.data(), sizeOfOptionalHeader, &bytesRead) || bytesRead != sizeOfOptionalHeader) {
log_message(log_file, "Failed to read Optional Header. Error: %lu\n", GetLastError());
return false;
}
uintptr_t sectionHeaderAddr = optionalHeaderAddr + sizeOfOptionalHeader;
for (int i = 0; i < fileHeader.NumberOfSections; ++i) {
IMAGE_SECTION_HEADER sectionHeader;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(sectionHeaderAddr + i * sizeof(IMAGE_SECTION_HEADER)), §ionHeader, sizeof(sectionHeader), &bytesRead) || bytesRead != sizeof(sectionHeader)) {
log_message(log_file, "Failed to read IMAGE_SECTION_HEADER for section %d. Error: %lu\n", i + 1, GetLastError());
return false;
}
Section sec;
char secName[9] = { 0 };
memcpy(secName, sectionHeader.Name, 8);
sec.name = std::string(secName);
sec.start = base_addr + sectionHeader.VirtualAddress;
sec.end = sec.start + sectionHeader.Misc.VirtualSize;
sections.push_back(sec);
log_message(log_file, "Section %s: Start=0x%llX, End=0x%llX (Relative: Start=0x%llX, End=0x%llX)\n",
sec.name.c_str(),
static_cast<unsigned long long>(sec.start),
static_cast<unsigned long long>(sec.end),
static_cast<unsigned long long>(sec.start - base_addr),
static_cast<unsigned long long>(sec.end - base_addr));
}
return true;
}
// Function to search for a string within a specified memory section
uintptr_t find_string_in_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, const std::string& string_to_search, const std::string& section_name, std::ofstream& log_file, uintptr_t base_addr) {
uintptr_t string_addr = 0;
uintptr_t addr = section_start;
size_t string_length = string_to_search.length();
code
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, reinterpret_cast<LPCVOID>(addr), &mbi, sizeof(mbi))) {
log_message(log_file, "VirtualQueryEx failed at address 0x%llX with error: %lu\n",
static_cast<unsigned long long>(addr), GetLastError());
break;
}
if (mbi.State == MEM_COMMIT &&
(mbi.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY | PAGE_READONLY | PAGE_EXECUTE_READ))) {
SIZE_T regionSize = mbi.RegionSize;
if (addr + regionSize > section_end) {
regionSize = section_end - addr;
}
std::vector<char> buffer(regionSize);
SIZE_T bytesReadMem;
if (ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(addr), buffer.data(), regionSize, &bytesReadMem) && bytesReadMem > 0) {
for (SIZE_T i = 0; i < bytesReadMem - string_length + 1; ++i) {
if (std::memcmp(buffer.data() + i, string_to_search.c_str(), string_length) == 0) {
string_addr = addr + i;
return string_addr;
}
}
}
addr += regionSize;
}
else {
addr += mbi.RegionSize;
}
}
return 0;
}
int main() {
std::string process_name;
std::cout << "Enter the process name (e.g., notepad.exe): ";
std::getline(std::cin, process_name);
code
// Map of game settings and their types
const std::vector<std::string> gameSettings = {
"fVATSMaxChance", "fVATSChancePerceptionMultiplier", "fVATSMaxEngageDistance",
"fVATSChanceRaycastVisibilityMininmum", "fVATSPingCost", "fVATSCriticalChargeBase",
"fVATSCriticalChargeBonusMult", "fVATSCriticalChargeLimit", "fVATSCriticalChargeMult",
"fPlayerAutoReloadDelaySec", "bDisablePlayerCollision", "fAVDCarryWeightBase",
"fAbsolutePlayerInventoryWeightLimit", "fInvisibilityMaxRefraction",
"fInvisibilityMinRefraction", "fJetpackThrustInitial", "fJetpackThrustSustained",
"fJetpackTimeToSustained", "bJetpackInitialThrustOnlyOnTakeoff",
"fAimStabilityCrouchedScopeMult", "fPropModScaleWeapDurability",
"fLockpickSkillSweetSpotBase", "fSweetSpotEasy", "fSweetSpotAverage",
"fSweetSpotHard", "fSweetSpotVeryHard", "iHackingMinWords", "iHackingMaxWords",
"fCombatMusicNearCombatInnerRadius", "fCombatMusicNearCombatOuterRadius",
"fCombatMusicPlayerTargetedThreatRatio", "pkKeywords", "fAttackActionPointCost",
"fspeed", "fweight", "iDurability", "ihealth", "bHoldInputToPower",
"fMinPowerPerShot", "poAmmo", "paAimModel", "ppOverrideProjectile",
"uNumProjectiles", "vaActorValues"
};
std::map<std::string, std::string> settingTypes = {
{"fVATSMaxChance", "float"},
{"fVATSChancePerceptionMultiplier", "float"},
{"fVATSMaxEngageDistance", "float"},
{"fVATSChanceRaycastVisibilityMininmum", "float"},
{"fVATSPingCost", "float"},
{"fVATSCriticalChargeBase", "float"},
{"fVATSCriticalChargeBonusMult", "float"},
{"fVATSCriticalChargeLimit", "float"},
{"fVATSCriticalChargeMult", "float"},
{"fPlayerAutoReloadDelaySec", "float"},
{"bDisablePlayerCollision", "bool"},
{"fAVDCarryWeightBase", "float"},
{"fAbsolutePlayerInventoryWeightLimit", "float"},
{"fInvisibilityMaxRefraction", "float"},
{"fInvisibilityMinRefraction", "float"},
{"fJetpackThrustInitial", "float"},
{"fJetpackThrustSustained", "float"},
{"fJetpackTimeToSustained", "float"},
{"bJetpackInitialThrustOnlyOnTakeoff", "bool"},
{"fAimStabilityCrouchedScopeMult", "float"},
{"fPropModScaleWeapDurability", "float"},
{"fLockpickSkillSweetSpotBase", "float"},
{"fSweetSpotEasy", "float"},
{"fSweetSpotAverage", "float"},
{"fSweetSpotHard", "float"},
{"fSweetSpotVeryHard", "float"},
{"iHackingMinWords", "int"},
{"iHackingMaxWords", "int"},
{"fCombatMusicNearCombatInnerRadius", "float"},
{"fCombatMusicNearCombatOuterRadius", "float"},
{"fCombatMusicPlayerTargetedThreatRatio", "float"},
{"pkKeywords", "pointer"},
{"fAttackActionPointCost", "float"},
{"fspeed", "float"},
{"fweight", "float"},
{"iDurability", "int"},
{"ihealth", "int"},
{"bHoldInputToPower", "bool"},
{"fMinPowerPerShot", "float"},
{"poAmmo", "pointer"},
{"paAimModel", "pointer"},
{"ppOverrideProjectile", "pointer"},
{"uNumProjectiles", "unsigned int"},
{"ActorValues", "array"}
};
std::ofstream log_file("memscan_detailed.log", std::ios::out);
if (!log_file.is_open()) {
std::cerr << "Could not open log file for writing\n";
return 1;
}
DWORD pid = get_process_id_by_name(process_name, log_file);
if (pid == 0) {
log_message(log_file, "Process %s not found.\n", process_name.c_str());
std::cerr << "Process not found. Check the process name and ensure it's running.\n";
return 1;
}
log_message(log_file, "Found process %s with PID: %u\n", process_name.c_str(), pid);
std::cout << "Found process " << process_name << " with PID: " << pid << "\n";
uintptr_t base_addr = get_base_address(pid, process_name, log_file);
if (base_addr == 0) {
log_message(log_file, "Failed to get base address for process ID %u.\n", pid);
std::cerr << "Failed to get base address.\n";
return 1;
}
log_message(log_file, "Base address of %s: 0x%llX\n", process_name.c_str(), static_cast<unsigned long long>(base_addr));
std::cout << "Base address of " << process_name << ": 0x" << std::hex << base_addr << std::dec << "\n";
UniqueHandle hProcess(OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid));
if (!hProcess) {
log_message(log_file, "Failed to open process with PID %u. Error: %lu\n", pid, GetLastError());
std::cerr << "Failed to open process. Ensure you have the necessary permissions.\n";
return 1;
}
log_message(log_file, "Successfully opened process handle.\n");
std::vector<Section> all_sections;
if (!get_sections(hProcess.get(), base_addr, all_sections, log_file)) {
log_message(log_file, "Failed to retrieve sections.\n");
std::cerr << "Failed to retrieve sections.\n";
return 1;
}
for (const auto& section : all_sections) {
log_message(log_file, "Section %s: Start=0x%llX, End=0x%llX (Relative: Start=0x%llX, End=0x%llX)\n",
section.name.c_str(),
static_cast<unsigned long long>(section.start),
static_cast<unsigned long long>(section.end),
static_cast<unsigned long long>(section.start - base_addr),
static_cast<unsigned long long>(section.end - base_addr));
}
// Map to store found settings and their relative offsets
std::map<std::string, uintptr_t> foundSettings;
for (const auto& setting : gameSettings) {
foundSettings[setting] = 0; // Initialize with 0 offset
}
// For each game setting string, search in the sections
for (const auto& setting : gameSettings) {
bool found = false;
for (const auto& section : all_sections) {
uintptr_t address = find_string_in_section(hProcess.get(), section.start, section.end, setting, section.name, log_file, base_addr);
if (address != 0) {
uintptr_t relativeAddress = address - base_addr;
foundSettings[setting] = relativeAddress;
log_message(log_file, "Found \"%s\" at relative address 0x%llX (absolute address: 0x%llX) in section %s\n",
setting.c_str(),
static_cast<unsigned long long>(relativeAddress),
static_cast<unsigned long long>(address),
section.name.c_str());
found = true;
break; // If found in one section, no need to search other sections
}
}
if (!found) {
log_message(log_file, "Did not find \"%s\" in any section.\n", setting.c_str());
}
}
// Output the results in the desired format
log_message(log_file, "\n$gameSettings = [\n");
std::cout << "\n$gameSettings = [\n";
for (const auto& setting : gameSettings) {
uintptr_t offset = foundSettings[setting];
std::string settingType = settingTypes[setting];
if (offset != 0) {
log_message(log_file, " \"%s\" => 0x%llX, // %s\n", setting.c_str(), static_cast<unsigned long long>(offset), settingType.c_str());
std::cout << " \"" << setting << "\" => 0x" << std::hex << offset << ", // " << settingType << "\n";
}
else {
log_message(log_file, " // \"%s\" not found\n", setting.c_str());
std::cout << " // \"" << setting << "\" not found\n";
}
}
log_message(log_file, "];\n");
std::cout << "];\n";
std::cout << "Press Enter to exit...";
std::cin.get();
return 0;
}
Certainly! Based on the provided C++ code, there are a few areas that need fixing to ensure correct functionality and compilation. The primary issue revolves around the mismatch between keys in the gameSettings vector and the settingTypes map. Additionally, there are minor improvements and potential optimizations that can be addressed.
Primary Issue: Key Mismatch in settingTypes
In the gameSettings vector, one of the settings is "vaActorValues", but in the settingTypes map, it's mistakenly keyed as "ActorValues". This mismatch will cause the program to fail to find the type for "vaActorValues", leading to incorrect or missing type annotations in the output.
Other Potential Issues and Improvements
Format Specifiers in log_message:
The %llX specifier is used for formatting unsigned long long values. Ensure that the cast to unsigned long long is safe and appropriate for the target architecture (64-bit).
Handling Missing Keys:
If a setting isn't found in the settingTypes map, it might be beneficial to handle it gracefully or provide a default type.
Unused Variables:
In the get_sections function, the is64 variable is determined but not used. If you plan to handle both 32-bit and 64-bit processes differently in the future, you can retain it. Otherwise, consider removing it to clean up the code.
String Conversion:
Converting std::wstring to std::string using iterators assumes the executable names are ASCII. For better robustness, especially with non-ASCII characters, consider using proper Unicode handling.
Error Handling:
While the code logs errors, you might also want to provide more user-friendly error messages or handle specific exceptions.
Fixed and Improved Code
Below is the corrected and slightly improved version of your code addressing the primary issue and incorporating some of the improvements mentioned:
cpp
#define NOMINMAX
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <cstdarg>
#include <memory>
#include <cstdint>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <map>
// Struct to ensure HANDLEs are properly closed
struct HandleCloser {
void operator()(HANDLE handle) const {
if (handle && handle != INVALID_HANDLE_VALUE) {
CloseHandle(handle);
}
}
};
using UniqueHandle = std::unique_ptr<std::remove_pointer<HANDLE>::type, HandleCloser>;
// Function to log messages to a log file only
void log_message(std::ofstream& log_file, const char* format, ...) {
va_list args;
va_start(args, format);
char buffer[2048]; // Increased buffer size for longer messages
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
log_file << buffer;
log_file.flush();
}
// Function to retrieve the Process ID (PID) by process name
DWORD get_process_id_by_name(const std::string& process_name, std::ofstream& log_file) {
UniqueHandle hSnapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (!hSnapshot || hSnapshot.get() == INVALID_HANDLE_VALUE) {
log_message(log_file, "CreateToolhelp32Snapshot failed with error: %lu\n", GetLastError());
return 0;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot.get(), &pe32)) {
log_message(log_file, "Process32First failed with error: %lu\n", GetLastError());
return 0;
}
do {
std::wstring ws(pe32.szExeFile);
std::string exeFile(ws.begin(), ws.end());
if (_stricmp(exeFile.c_str(), process_name.c_str()) == 0) {
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot.get(), &pe32));
return 0;
}
// Function to retrieve the base address of the main module of the process
uintptr_t get_base_address(DWORD pid, const std::string& process_name, std::ofstream& log_file) {
UniqueHandle hSnapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid));
if (!hSnapshot || hSnapshot.get() == INVALID_HANDLE_VALUE) {
log_message(log_file, "CreateToolhelp32Snapshot for modules failed with error: %lu\n", GetLastError());
return 0;
}
MODULEENTRY32 me32;
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hSnapshot.get(), &me32)) {
log_message(log_file, "Module32First failed with error: %lu\n", GetLastError());
return 0;
}
do {
// Convert module name to std::string for comparison
std::wstring ws(me32.szModule);
std::string moduleName(ws.begin(), ws.end());
// Compare the module name with the process name
if (_stricmp(moduleName.c_str(), process_name.c_str()) == 0) {
log_message(log_file, "Main module %s found at base address 0x%llX\n", moduleName.c_str(),
reinterpret_cast<unsigned long long>(me32.modBaseAddr));
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
} while (Module32Next(hSnapshot.get(), &me32));
log_message(log_file, "Main module not found in the module list.\n");
return 0;
}
// Struct to store section information
struct Section {
std::string name;
uintptr_t start;
uintptr_t end;
};
// Function to parse PE headers and extract sections
bool get_sections(HANDLE hProcess, uintptr_t base_addr, std::vector<Section>& sections, std::ofstream& log_file) {
// Read the DOS header
IMAGE_DOS_HEADER dosHeader;
SIZE_T bytesRead;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(base_addr), &dosHeader, sizeof(dosHeader), &bytesRead) || bytesRead != sizeof(dosHeader)) {
log_message(log_file, "Failed to read DOS header. Error: %lu\n", GetLastError());
return false;
}
if (dosHeader.e_magic != IMAGE_DOS_SIGNATURE) {
log_message(log_file, "Invalid DOS signature.\n");
return false;
}
uintptr_t ntHeaderAddr = base_addr + dosHeader.e_lfanew;
DWORD signature;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(ntHeaderAddr), &signature, sizeof(signature), &bytesRead) || bytesRead != sizeof(signature)) {
log_message(log_file, "Failed to read NT headers signature. Error: %lu\n", GetLastError());
return false;
}
if (signature != IMAGE_NT_SIGNATURE) {
log_message(log_file, "Invalid NT signature.\n");
return false;
}
IMAGE_FILE_HEADER fileHeader;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(ntHeaderAddr + sizeof(DWORD)), &fileHeader, sizeof(fileHeader), &bytesRead) || bytesRead != sizeof(fileHeader)) {
log_message(log_file, "Failed to read IMAGE_FILE_HEADER. Error: %lu\n", GetLastError());
return false;
}
WORD sizeOfOptionalHeader = fileHeader.SizeOfOptionalHeader;
uintptr_t optionalHeaderAddr = ntHeaderAddr + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER);
WORD magic;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(optionalHeaderAddr), &magic, sizeof(magic), &bytesRead) || bytesRead != sizeof(magic)) {
log_message(log_file, "Failed to read Optional Header Magic. Error: %lu\n", GetLastError());
return false;
}
bool is64 = false;
if (magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
is64 = true;
}
else if (magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
is64 = false;
}
else {
log_message(log_file, "Unknown Optional Header Magic: 0x%X.\n", magic);
return false;
}
// Read the entire Optional Header
std::vector<BYTE> optionalHeaderData(sizeOfOptionalHeader);
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(optionalHeaderAddr), optionalHeaderData.data(), sizeOfOptionalHeader, &bytesRead) || bytesRead != sizeOfOptionalHeader) {
log_message(log_file, "Failed to read Optional Header. Error: %lu\n", GetLastError());
return false;
}
uintptr_t sectionHeaderAddr = optionalHeaderAddr + sizeOfOptionalHeader;
for (int i = 0; i < fileHeader.NumberOfSections; ++i) {
IMAGE_SECTION_HEADER sectionHeader;
if (!ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(sectionHeaderAddr + i * sizeof(IMAGE_SECTION_HEADER)), §ionHeader, sizeof(sectionHeader), &bytesRead) || bytesRead != sizeof(sectionHeader)) {
log_message(log_file, "Failed to read IMAGE_SECTION_HEADER for section %d. Error: %lu\n", i + 1, GetLastError());
return false;
}
Section sec;
char secName[9] = { 0 };
memcpy(secName, sectionHeader.Name, 8);
sec.name = std::string(secName);
sec.start = base_addr + sectionHeader.VirtualAddress;
sec.end = sec.start + sectionHeader.Misc.VirtualSize;
sections.push_back(sec);
log_message(log_file, "Section %s: Start=0x%llX, End=0x%llX (Relative: Start=0x%llX, End=0x%llX)\n",
sec.name.c_str(),
static_cast<unsigned long long>(sec.start),
static_cast<unsigned long long>(sec.end),
static_cast<unsigned long long>(sec.start - base_addr),
static_cast<unsigned long long>(sec.end - base_addr));
}
return true;
}
// Function to search for a string within a specified memory section
uintptr_t find_string_in_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, const std::string& string_to_search, const std::string& section_name, std::ofstream& log_file, uintptr_t base_addr) {
uintptr_t string_addr = 0;
uintptr_t addr = section_start;
size_t string_length = string_to_search.length();
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, reinterpret_cast<LPCVOID>(addr), &mbi, sizeof(mbi))) {
log_message(log_file, "VirtualQueryEx failed at address 0x%llX with error: %lu\n",
static_cast<unsigned long long>(addr), GetLastError());
break;
}
if (mbi.State == MEM_COMMIT &&
(mbi.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY | PAGE_READONLY | PAGE_EXECUTE_READ))) {
SIZE_T regionSize = mbi.RegionSize;
if (addr + regionSize > section_end) {
regionSize = section_end - addr;
}
std::vector<char> buffer(regionSize);
SIZE_T bytesReadMem;
if (ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(addr), buffer.data(), regionSize, &bytesReadMem) && bytesReadMem > 0) {
for (SIZE_T i = 0; i < bytesReadMem - string_length + 1; ++i) {
if (std::memcmp(buffer.data() + i, string_to_search.c_str(), string_length) == 0) {
string_addr = addr + i;
return string_addr;
}
}
}
addr += regionSize;
}
else {
addr += mbi.RegionSize;
}
}
return 0;
}
int main() {
std::string process_name;
std::cout << "Enter the process name (e.g., notepad.exe): ";
std::getline(std::cin, process_name);
// Map of game settings and their types
const std::vector<std::string> gameSettings = {
"fVATSMaxChance", "fVATSChancePerceptionMultiplier", "fVATSMaxEngageDistance",
"fVATSChanceRaycastVisibilityMininmum", "fVATSPingCost", "fVATSCriticalChargeBase",
"fVATSCriticalChargeBonusMult", "fVATSCriticalChargeLimit", "fVATSCriticalChargeMult",
"fPlayerAutoReloadDelaySec", "bDisablePlayerCollision", "fAVDCarryWeightBase",
"fAbsolutePlayerInventoryWeightLimit", "fInvisibilityMaxRefraction",
"fInvisibilityMinRefraction", "fJetpackThrustInitial", "fJetpackThrustSustained",
"fJetpackTimeToSustained", "bJetpackInitialThrustOnlyOnTakeoff",
"fAimStabilityCrouchedScopeMult", "fPropModScaleWeapDurability",
"fLockpickSkillSweetSpotBase", "fSweetSpotEasy", "fSweetSpotAverage",
"fSweetSpotHard", "fSweetSpotVeryHard", "iHackingMinWords", "iHackingMaxWords",
"fCombatMusicNearCombatInnerRadius", "fCombatMusicNearCombatOuterRadius",
"fCombatMusicPlayerTargetedThreatRatio", "pkKeywords", "fAttackActionPointCost",
"fspeed", "fweight", "iDurability", "ihealth", "bHoldInputToPower",
"fMinPowerPerShot", "poAmmo", "paAimModel", "ppOverrideProjectile",
"uNumProjectiles", "vaActorValues" // Corrected from "ActorValues" to "vaActorValues"
};
std::map<std::string, std::string> settingTypes = {
{"fVATSMaxChance", "float"},
{"fVATSChancePerceptionMultiplier", "float"},
{"fVATSMaxEngageDistance", "float"},
{"fVATSChanceRaycastVisibilityMininmum", "float"},
{"fVATSPingCost", "float"},
{"fVATSCriticalChargeBase", "float"},
{"fVATSCriticalChargeBonusMult", "float"},
{"fVATSCriticalChargeLimit", "float"},
{"fVATSCriticalChargeMult", "float"},
{"fPlayerAutoReloadDelaySec", "float"},
{"bDisablePlayerCollision", "bool"},
{"fAVDCarryWeightBase", "float"},
{"fAbsolutePlayerInventoryWeightLimit", "float"},
{"fInvisibilityMaxRefraction", "float"},
{"fInvisibilityMinRefraction", "float"},
{"fJetpackThrustInitial", "float"},
{"fJetpackThrustSustained", "float"},
{"fJetpackTimeToSustained", "float"},
{"bJetpackInitialThrustOnlyOnTakeoff", "bool"},
{"fAimStabilityCrouchedScopeMult", "float"},
{"fPropModScaleWeapDurability", "float"},
{"fLockpickSkillSweetSpotBase", "float"},
{"fSweetSpotEasy", "float"},
{"fSweetSpotAverage", "float"},
{"fSweetSpotHard", "float"},
{"fSweetSpotVeryHard", "float"},
{"iHackingMinWords", "int"},
{"iHackingMaxWords", "int"},
{"fCombatMusicNearCombatInnerRadius", "float"},
{"fCombatMusicNearCombatOuterRadius", "float"},
{"fCombatMusicPlayerTargetedThreatRatio", "float"},
{"pkKeywords", "pointer"},
{"fAttackActionPointCost", "float"},
{"fspeed", "float"},
{"fweight", "float"},
{"iDurability", "int"},
{"ihealth", "int"},
{"bHoldInputToPower", "bool"},
{"fMinPowerPerShot", "float"},
{"poAmmo", "pointer"},
{"paAimModel", "pointer"},
{"ppOverrideProjectile", "pointer"},
{"uNumProjectiles", "unsigned int"},
{"vaActorValues", "array"} // Corrected from "ActorValues" to "vaActorValues"
};
std::ofstream log_file("memscan_detailed.log", std::ios::out);
if (!log_file.is_open()) {
std::cerr << "Could not open log file for writing\n";
return 1;
}
DWORD pid = get_process_id_by_name(process_name, log_file);
if (pid == 0) {
log_message(log_file, "Process %s not found.\n", process_name.c_str());
std::cerr << "Process not found. Check the process name and ensure it's running.\n";
return 1;
}
log_message(log_file, "Found process %s with PID: %u\n", process_name.c_str(), pid);
std::cout << "Found process " << process_name << " with PID: " << pid << "\n";
uintptr_t base_addr = get_base_address(pid, process_name, log_file);
if (base_addr == 0) {
log_message(log_file, "Failed to get base address for process ID %u.\n", pid);
std::cerr << "Failed to get base address.\n";
return 1;
}
log_message(log_file, "Base address of %s: 0x%llX\n", process_name.c_str(), static_cast<unsigned long long>(base_addr));
std::cout << "Base address of " << process_name << ": 0x" << std::hex << base_addr << std::dec << "\n";
UniqueHandle hProcess(OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid));
if (!hProcess) {
log_message(log_file, "Failed to open process with PID %u. Error: %lu\n", pid, GetLastError());
std::cerr << "Failed to open process. Ensure you have the necessary permissions.\n";
return 1;
}
log_message(log_file, "Successfully opened process handle.\n");
std::vector<Section> all_sections;
if (!get_sections(hProcess.get(), base_addr, all_sections, log_file)) {
log_message(log_file, "Failed to retrieve sections.\n");
std::cerr << "Failed to retrieve sections.\n";
return 1;
}
for (const auto& section : all_sections) {
log_message(log_file, "Section %s: Start=0x%llX, End=0x%llX (Relative: Start=0x%llX, End=0x%llX)\n",
section.name.c_str(),
static_cast<unsigned long long>(section.start),
static_cast<unsigned long long>(section.end),
static_cast<unsigned long long>(section.start - base_addr),
static_cast<unsigned long long>(section.end - base_addr));
}
// Map to store found settings and their relative offsets
std::map<std::string, uintptr_t> foundSettings;
for (const auto& setting : gameSettings) {
foundSettings[setting] = 0; // Initialize with 0 offset
}
// For each game setting string, search in the sections
for (const auto& setting : gameSettings) {
bool found = false;
for (const auto& section : all_sections) {
uintptr_t address = find_string_in_section(hProcess.get(), section.start, section.end, setting, section.name, log_file, base_addr);
if (address != 0) {
uintptr_t relativeAddress = address - base_addr;
foundSettings[setting] = relativeAddress;
log_message(log_file, "Found \"%s\" at relative address 0x%llX (absolute address: 0x%llX) in section %s\n",
setting.c_str(),
static_cast<unsigned long long>(relativeAddress),
static_cast<unsigned long long>(address),
section.name.c_str());
found = true;
break; // If found in one section, no need to search other sections
}
}
if (!found) {
log_message(log_file, "Did not find \"%s\" in any section.\n", setting.c_str());
}
}
// Output the results in the desired format
log_message(log_file, "\n$gameSettings = [\n");
std::cout << "\n$gameSettings = [\n";
for (const auto& setting : gameSettings) {
uintptr_t offset = foundSettings[setting];
std::string settingType = "unknown"; // Default type
// Retrieve the type from the map if available
auto it = settingTypes.find(setting);
if (it != settingTypes.end()) {
settingType = it->second;
}
else {
log_message(log_file, "Type for setting \"%s\" not found. Using default type.\n", setting.c_str());
}
if (offset != 0) {
log_message(log_file, " \"%s\" => 0x%llX, // %s\n", setting.c_str(), static_cast<unsigned long long>(offset), settingType.c_str());
std::cout << " \"" << setting << "\" => 0x" << std::hex << offset << ", // " << settingType << "\n";
}
else {
log_message(log_file, " // \"%s\" not found\n", setting.c_str());
std::cout << " // \"" << setting << "\" not found\n";
}
}
log_message(log_file, "];\n");
std::cout << "];\n";
std::cout << "Press Enter to exit...";
std::cin.get();
return 0;
}
Key Changes and Explanations
Corrected Key in settingTypes:
Changed {"ActorValues", "array"} to {"vaActorValues", "array"} to match the entry in gameSettings.
Handling Missing Types Gracefully:
In the output loop, if a setting's type isn't found in the settingTypes map, it defaults to "unknown" and logs a message indicating the missing type. This prevents potential crashes or undefined behaviors.
Consistency in Logging:
Ensured that all log messages use consistent formatting and correct specifiers.
Comments for Clarity:
Added comments to highlight changes and improvements for better code readability and maintenance.
Potential Future Enhancements:
Consider implementing proper Unicode handling when converting std::wstring to std::string to support non-ASCII process or module names.
Implement additional error handling or user feedback mechanisms as needed.
Final Notes
After applying these fixes, ensure that:
The process name entered matches exactly (case-insensitive due to _stricmp) with an existing running process.
The application has the necessary permissions to read the target process's memory. You might need to run the program with administrative privileges.
The target process is compatible (e.g., 64-bit vs. 32-bit) with the scanning tool.
Always exercise caution when reading or manipulating other processes' memory to avoid unintended consequences or violations of software policies.