it seems this pulling the offsets from data which is right but when you search it in in ida it should point to the string name in rdata which it dont it poinbts to data #include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include
#include <psapi.h>
#include
unsigned long long get_base_address(DWORD pid) {
MODULEENTRY32 me32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return 0;
}
unsigned long long base_address = (unsigned long long)me32.modBaseAddr;
CloseHandle(hSnapshot);
return base_address;
}
bool get_section_limits(HANDLE hProcess, unsigned long long base_addr, const char* section_name, unsigned long long& section_start, unsigned long long& section_end) {
IMAGE_DOS_HEADER dos_header;
if (!ReadProcessMemory(hProcess, (LPCVOID)base_addr, &dos_header, sizeof(dos_header), NULL)) {
return false;
}
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
IMAGE_NT_HEADERS nt_headers;
if (!ReadProcessMemory(hProcess, (LPCVOID)(base_addr + dos_header.e_lfanew), &nt_headers, sizeof(nt_headers), NULL)) {
return false;
}
if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
return false;
}
IMAGE_SECTION_HEADER* section_headers = new IMAGE_SECTION_HEADER[nt_headers.FileHeader.NumberOfSections];
if (!ReadProcessMemory(hProcess, (LPCVOID)((unsigned long long)(base_addr + dos_header.e_lfanew) + sizeof(nt_headers)), section_headers, sizeof(IMAGE_SECTION_HEADER) * nt_headers.FileHeader.NumberOfSections, NULL)) {
delete[] section_headers;
return false;
}
for (int i = 0; i < nt_headers.FileHeader.NumberOfSections; ++i) {
if (strncmp((char*)section_headers[i].Name, section_name, IMAGE_SIZEOF_SHORT_NAME) == 0) {
section_start = base_addr + section_headers[i].VirtualAddress;
section_end = section_start + section_headers[i].Misc.VirtualSize;
delete[] section_headers;
return true;
}
}
delete[] section_headers;
return false;
}
std::vector<std::pair<std::string, unsigned long long>> find_strings_in_section(HANDLE hProcess, unsigned long long section_start, unsigned long long section_end, size_t min_length) {
std::vector<std::pair<std::string, unsigned long long>> strings;
unsigned long long addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
code
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READONLY || mbi.Protect == PAGE_READWRITE)) {
char* buffer = new char[mbi.RegionSize];
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer, mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead;) {
if (isprint(buffer[i]) && isprint(buffer[i + 1]) && isprint(buffer[i + 2])) {
std::string possible_string;
SIZE_T j = i;
while (j < bytesRead && isprint(buffer[j]) && buffer[j] != '\0') {
possible_string += buffer[j];
j++;
}
if (possible_string.length() >= min_length) {
strings.emplace_back(possible_string, addr + i);
}
i = j; // Move i to the end of the found string
}
else {
i++;
}
}
}
delete[] buffer;
}
addr += mbi.RegionSize;
}
return strings;
}
unsigned long long find_reference_in_data_section(HANDLE hProcess, unsigned long long section_start, unsigned long long section_end, unsigned long long string_addr) {
unsigned long long addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
code
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE || mbi.Protect == PAGE_EXECUTE_WRITECOPY || mbi.Protect == PAGE_WRITECOPY)) {
char* buffer = new char[mbi.RegionSize];
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer, mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - sizeof(unsigned long long); ++i) {
unsigned long long* potential_ptr = (unsigned long long*)(buffer + i);
if (*potential_ptr == string_addr) {
unsigned long long reference_addr = addr + i;
delete[] buffer;
return reference_addr;
}
}
}
delete[] buffer;
}
addr += mbi.RegionSize;
}
return 0;
}
int main() {
const char* process_name = "Fallout76.exe";
const size_t minStringLength = 5; // Define the minimum string length you consider a valid string
code
FILE* log_file;
if (fopen_s(&log_file, "memscan.log", "w") != 0) {
printf("Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
log_message(log_file, "Could not find process ID for \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
unsigned long long base_addr = get_base_address(pid);
log_message(log_file, "Base Address: 0x%llx\n", base_addr);
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
log_message(log_file, "Could not open process \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
unsigned long long rdata_section_start, rdata_section_end;
if (!get_section_limits(hProcess, base_addr, ".rdata", rdata_section_start, rdata_section_end)) {
log_message(log_file, "Failed to get limits for .rdata section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
unsigned long long data_section_start, data_section_end;
if (!get_section_limits(hProcess, base_addr, ".data", data_section_start, data_section_end)) {
log_message(log_file, "Failed to get limits for .data section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
log_message(log_file, "$strings_with_offsets = [\n");
auto rdata_strings = find_strings_in_section(hProcess, rdata_section_start, rdata_section_end, minStringLength);
for (const auto& item : rdata_strings) {
unsigned long long string_addr = item.second;
unsigned long long reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr != 0) {
// Subtract 0x10 from reference address if needed depending on structure analysis
unsigned long long function_pointer = reference_addr - 0x10;
unsigned long long offset = function_pointer - base_addr;
// Log detailed addresses for debugging
log_message(log_file, "String: \"%s\"\n", item.first.c_str());
log_message(log_file, "String Address: 0x%llx\n", string_addr);
log_message(log_file, "Reference Address: 0x%llx\n", reference_addr);
log_message(log_file, "Function Pointer (Adjusted): 0x%llx\n", function_pointer);
log_message(log_file, "Offset from Base: 0x%llx\n", offset);
if (offset >= 0) {
log_message(log_file, " \"%s\" at offset: 0x%llx\n", item.first.c_str(), offset);
}
else {
log_message(log_file, "Error: Calculated offset is negative or out of bounds.\n");
}
}
}
log_message(log_file, "];\n");
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
Assistant
The issue you're encountering with identifying the correct references to string data in memory ultimately boils down to ensuring the integrity of memory reading operations and account for the specifics of executable file structures. Here are some considerations and steps you might take to troubleshoot and potentially resolve the problem:
Correct Section Identification: The first step is ensuring that you’re correctly identifying and reading sections like .rdata and .data. Verify the section parsing logic, particularly string comparison operations for section names, which should not depend on null termination beyond defined size limits (IMAGE_SIZEOF_SHORT_NAME).
CPU Architecture Specifics: Ensure that you are taking into account the architecture (32-bit vs 64-bit) of the target process, particularly when computing and comparing addresses. This is crucial because address size varies, and incorrect type sizes can lead to alignment issues.
Pointer and Address Calculation: Double-check all the math around address calculations, particularly when extracting pointers and calculating offsets. Round-off issues or incorrect type casting (especially with unsigned types) can commonly lead to misinterpretations of offset values.
Access Rights: Ensure that your process has the necessary permissions to read the memory of the target process. Use PROCESS_VM_READ | PROCESS_QUERY_INFORMATION when opening the process, as you already are, but watch out if DEP (Data Execution Prevention) or other Windows security features might interfere.
OS Compatibility: Make sure your reading logic is compatible with the OS and libraries in use. The tlhelp32.h methods are generally consistent across Windows versions, but consider different behavior or additional restrictions based on the Windows version running.
Logging and Debugging: Increase the verbosity of your logging particularly around where you suspect the offsets to be incorrectly calculated. Log every operation involving addresses extensively to identify at which point the discrepancy starts.
Error Handling: Given the complexity of memory scanning operations, strengthen your error handling to catch and log any potential failures (e.g., failed memory reads or invalid pointers) to ensure they aren't silently masking a deeper problem.
Assumptions about STD Strings: If you are assuming that strings will always reside in .rdata, verify this assumption against the actual application you are analyzing. Some applications manage their string data differently based on complexity, optimizations, and other considerations.
User
fix the code and print full program
Assistant
Certainly! Below is a revised version of your code with added corrections and improvements for clarity and proper address handling. I've also added comments to highlight the changes and assumptions throughout the code:
cpp
User
have it print what section it gets the offset from
Assistant
Certainly! To log which section each string reference is found in, you can modify the code to include that information. Below is the updated code to accomplish this:
cpp
User
its pulling everything from data the strig is in rdata and the offsets should be from data
Assistant
Understood, you want to correctly identify strings located in the .rdata section and then find offsets in the .data section where these strings are referenced. Let's correct and ensure the logic is properly aligned with this intent. Here's how you could adjust the code:
cpp
User
study how this was made and learn what i want from it it shouws how its meant to be #include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include
#include
#include <psapi.h>
void log_message(FILE* log_file, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
if (log_file != NULL) {
vfprintf(log_file, format, args);
fflush(log_file);
}
va_end(args);
}
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 0;
}
do {
char exeFile[MAX_PATH];
size_t converted_chars = 0;
wcstombs_s(&converted_chars, exeFile, MAX_PATH, pe32.szExeFile, _TRUNCATE);
if (_stricmp(exeFile, process_name) == 0) {
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return 0;
}
unsigned long long get_base_address(DWORD pid) {
MODULEENTRY32 me32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
code
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return 0;
}
unsigned long long base_address = (unsigned long long)me32.modBaseAddr;
CloseHandle(hSnapshot);
return base_address;
}
bool get_section_limits(HANDLE hProcess, unsigned long long base_addr, const char* section_name, unsigned long long& section_start, unsigned long long& section_end) {
IMAGE_DOS_HEADER dos_header;
if (!ReadProcessMemory(hProcess, (LPCVOID)base_addr, &dos_header, sizeof(dos_header), NULL)) {
return false;
}
code
Assistant
The code you've provided is a C++ program designed to scan the memory of a specific process (Fallout76.exe) and perform the following tasks:
Retrieve Process ID: It obtains the process ID of Fallout76.exe using Toolhelp32Snapshot and traverses through the running processes.
Get Base Address: It finds the base address of the specified process through module enumeration.
Determine Section Limits: It reads the PE headers of the process memory to determine the start and end addresses of the .rdata and .data sections.
Find Strings: For each string in the gameSettings array, it searches the .rdata section in the process memory to find the string address.
Find References: Once a string is found in the .rdata section, it looks for references to that string address in the .data section.
Calculate Offsets: It calculates the offset of the reference from a hardcoded base address (0x7FF68C9B0000) and logs these mappings.
Key Features and Mechanics:
Logging: The results (string and their computed offsets) are logged to a memscan.log file.
Memory Management: Uses VirtualQueryEx and ReadProcessMemory to safely traverse and read memory sections.
Hardcoded Base Address: The program uses a hardcoded base address for computation instead of dynamically querying it, which suggests a specific version or setup of the Fallout76.exe is assumed.
Memory Sections: Focuses specifically on .rdata for string data and .data for potential references/pointers.
What This Code Attempts to Accomplish:
The goal is likely to identify in-memory settings or functions pertinent to the game's configuration or internal mechanics by linking known strings to their in-memory representations and offsets. These offsets presumably relate to the game's configuration or runtime attributes, which might be adjusted or understood better with such inspection.
Potential Enhancements:
Dynamic Base Address: Instead of using a hardcoded base address which may vary with application updates or different environments, dynamically retrieve it to increase resilience.
User
now rewrite the other code for Memory Sections: Focuses specifically on .rdata for string data and .data for potential references/pointers.
Assistant
Certainly! Let's adapt the previous memory scanning code to focus specifically on finding strings in the .rdata section and then locating references or pointers to those strings in the .data section. The code will log the strings and their respective offsets from the process's base address.
cpp
User
what i intend to do first, search for strings
get the string address
then do a search for the addresses
so you can get the address that loads it
after that, address - 0x10
that should be the function pointer
get that address, subtract fo76 base address
Assistant
Got it! Your goal is to perform these specific steps for memory inspection within a process, such as Fallout76.exe:
Search for Strings: Locate strings in the .rdata section.
Obtain String Address: Retrieve the memory address where each string is stored.
Search for References to String Addresses: Look for where these string addresses are referenced in the .data section.
Calculate Function Pointer: From each reference address, subtract 0x10 to get the potential function pointer.
Calculate Offset: Subtract the base address of the process to obtain the function's offset within the executable.
Let me provide a detailed revised version of your program implementing these steps:
cpp
User
use these for the search adresses .rdata 0000000143DC64E0
.data 00000001457AF000
Assistant
Certainly! If you have specific addresses for the start of the .rdata and .data sections, you can modify the code to use these hardcoded addresses for the search. Here's how you could adapt the program:
If the problem persists after implementing these tips, conducting a side-by-side manual analysis using a debugger or more granular control tool (like IDA Pro) can be instrumental. Analyze manually identified offsets and discern differences with your program's output to further refine your approach.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include <vector>
#include <cstdarg>
// Function to log messages both to console and file
void log_message(FILE* log_file, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args); // Console output
if (log_file != NULL) {
vfprintf(log_file, format, args); // File output
fflush(log_file);
}
va_end(args);
}
// Function to get the process ID by name
DWORD get_process_id_by_name(const char* process_name) {
PROCESSENTRY32 pe32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 0;
}
do {
char exeFile[MAX_PATH];
size_t converted_chars = 0;
wcstombs_s(&converted_chars, exeFile, MAX_PATH, pe32.szExeFile, _TRUNCATE);
if (_stricmp(exeFile, process_name) == 0) {
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return 0;
}
// Function to get the base address of the process
uintptr_t get_base_address(DWORD pid) {
MODULEENTRY32 me32 = { sizeof(me32) };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
if (Module32First(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
CloseHandle(hSnapshot);
return 0;
}
// Function to get the start and end addresses of a specific section in the process
bool get_section_limits(HANDLE hProcess, uintptr_t base_addr, const char* section_name, uintptr_t& section_start, uintptr_t& section_end) {
IMAGE_DOS_HEADER dos_header;
if (!ReadProcessMemory(hProcess, (LPCVOID)base_addr, &dos_header, sizeof(dos_header), NULL)) {
return false;
}
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
IMAGE_NT_HEADERS nt_headers;
if (!ReadProcessMemory(hProcess, (LPCVOID)(base_addr + dos_header.e_lfanew), &nt_headers, sizeof(nt_headers), NULL)) {
return false;
}
if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
return false;
}
std::vector<IMAGE_SECTION_HEADER> section_headers(nt_headers.FileHeader.NumberOfSections);
if (!ReadProcessMemory(hProcess, (LPCVOID)((uintptr_t)(base_addr + dos_header.e_lfanew) + sizeof(nt_headers)), section_headers.data(), sizeof(IMAGE_SECTION_HEADER) * nt_headers.FileHeader.NumberOfSections, NULL)) {
return false;
}
for (const auto& section : section_headers) {
if (strncmp(reinterpret_cast<const char*>(section.Name), section_name, IMAGE_SIZEOF_SHORT_NAME) == 0) {
section_start = base_addr + section.VirtualAddress;
section_end = section_start + section.Misc.VirtualSize;
return true;
}
}
return false;
}
// Function to find strings in a section of the process's memory
std::vector<std::pair<std::string, uintptr_t>> find_strings_in_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, size_t min_length) {
std::vector<std::pair<std::string, uintptr_t>> strings;
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READONLY || mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READ || mbi.Protect == PAGE_EXECUTE_READWRITE)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead;) {
if (isprint(buffer[i]) && isprint(buffer[i + 1]) && isprint(buffer[i + 2])) {
std::string possible_string;
SIZE_T j = i;
while (j < bytesRead && isprint(buffer[j]) && buffer[j] != '\0') {
possible_string += buffer[j];
j++;
}
if (possible_string.length() >= min_length) {
strings.emplace_back(possible_string, addr + i);
}
i = j; // Move i to the end of the found string
}
else {
i++;
}
}
}
}
addr += mbi.RegionSize;
}
return strings;
}
// Function to find a reference to a string address within the .data section
uintptr_t find_reference_in_data_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, uintptr_t string_addr) {
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE || mbi.Protect == PAGE_EXECUTE_WRITECOPY || mbi.Protect == PAGE_WRITECOPY)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - sizeof(uintptr_t); ++i) {
uintptr_t* potential_ptr = reinterpret_cast<uintptr_t*>(buffer.data() + i);
if (*potential_ptr == string_addr) {
return addr + i;
}
}
}
}
addr += mbi.RegionSize;
}
return 0;
}
int main() {
const char* process_name = "Fallout76.exe";
const size_t minStringLength = 5; // Define the minimum string length you consider a valid string
FILE* log_file;
if (fopen_s(&log_file, "memscan.log", "w") != 0) {
printf("Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
log_message(log_file, "Could not find process ID for \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t base_addr = get_base_address(pid);
log_message(log_file, "Base Address: 0x%llx\n", base_addr);
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
log_message(log_file, "Could not open process \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t rdata_section_start, rdata_section_end;
if (!get_section_limits(hProcess, base_addr, ".rdata", rdata_section_start, rdata_section_end)) {
log_message(log_file, "Failed to get limits for .rdata section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
uintptr_t data_section_start, data_section_end;
if (!get_section_limits(hProcess, base_addr, ".data", data_section_start, data_section_end)) {
log_message(log_file, "Failed to get limits for .data section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
log_message(log_file, "$strings_with_offsets = [\n");
auto rdata_strings = find_strings_in_section(hProcess, rdata_section_start, rdata_section_end, minStringLength);
for (const auto& item : rdata_strings) {
uintptr_t string_addr = item.second;
uintptr_t reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr != 0) {
// Subtract offset as needed according to data structure specifics
uintptr_t function_pointer = reference_addr - 0x10;
uintptr_t offset = function_pointer - base_addr;
// Log detailed addresses for debugging
log_message(log_file, "String: \"%s\"\n", item.first.c_str());
log_message(log_file, "String Address: 0x%llx\n", string_addr);
log_message(log_file, "Reference Address: 0x%llx\n", reference_addr);
log_message(log_file, "Function Pointer (Adjusted): 0x%llx\n", function_pointer);
log_message(log_file, "Offset from Base: 0x%llx\n", offset);
log_message(log_file, " \"%s\" at offset: 0x%llx\n", item.first.c_str(), offset);
}
}
log_message(log_file, "];\n");
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
Key Changes and Considerations:
Address Handling: The address type is now explicitly defined using uintptr_t to handle pointers correctly across different architectures.
Cross-Section Protection: Updated section protection handling to consider execute-read scenarios as well, given that some sections might have different memory protection flags.
Consistent Offset Calculation: Ensured that the offsets and address calculations are consistent with each possible architecture while maintaining the logic you've defined.
Comprehensive Error Handling: Added checks and logs to catch potential failures or errors in memory reading, which should make debugging and addressing issues easier.
Code Simplification and Safety: Used std::vector for buffer management in sections to avoid manual memory allocation and increase code readability, safety, and maintainability.
This should address common pitfalls and improve the reliability of finding and referencing strings in memory. However, further customization might be needed based on the actual behavior of the target process and data layouts you encounter.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include <vector>
#include <cstdarg>
// Function to log messages both to console and a file
void log_message(FILE* log_file, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args); // Console output
if (log_file != NULL) {
vfprintf(log_file, format, args); // File output
fflush(log_file);
}
va_end(args);
}
// Function to get the process ID by name
DWORD get_process_id_by_name(const char* process_name) {
PROCESSENTRY32 pe32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 0;
}
do {
char exeFile[MAX_PATH];
size_t converted_chars = 0;
wcstombs_s(&converted_chars, exeFile, MAX_PATH, pe32.szExeFile, _TRUNCATE);
if (_stricmp(exeFile, process_name) == 0) {
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return 0;
}
// Function to get the base address of the process
uintptr_t get_base_address(DWORD pid) {
MODULEENTRY32 me32 = { sizeof(me32) };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
if (Module32First(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
CloseHandle(hSnapshot);
return 0;
}
// Function to get the start and end addresses of a specific section in the process
bool get_section_limits(HANDLE hProcess, uintptr_t base_addr, const char* section_name, uintptr_t& section_start, uintptr_t& section_end) {
IMAGE_DOS_HEADER dos_header;
if (!ReadProcessMemory(hProcess, (LPCVOID)base_addr, &dos_header, sizeof(dos_header), NULL)) {
return false;
}
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
IMAGE_NT_HEADERS nt_headers;
if (!ReadProcessMemory(hProcess, (LPCVOID)(base_addr + dos_header.e_lfanew), &nt_headers, sizeof(nt_headers), NULL)) {
return false;
}
if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
return false;
}
std::vector<IMAGE_SECTION_HEADER> section_headers(nt_headers.FileHeader.NumberOfSections);
if (!ReadProcessMemory(hProcess, (LPCVOID)((uintptr_t)(base_addr + dos_header.e_lfanew) + sizeof(nt_headers)), section_headers.data(), sizeof(IMAGE_SECTION_HEADER) * nt_headers.FileHeader.NumberOfSections, NULL)) {
return false;
}
for (const auto& section : section_headers) {
if (strncmp(reinterpret_cast<const char*>(section.Name), section_name, IMAGE_SIZEOF_SHORT_NAME) == 0) {
section_start = base_addr + section.VirtualAddress;
section_end = section_start + section.Misc.VirtualSize;
return true;
}
}
return false;
}
// Function to find strings in a section of the process's memory
std::vector<std::pair<std::string, uintptr_t>> find_strings_in_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, size_t min_length) {
std::vector<std::pair<std::string, uintptr_t>> strings;
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READONLY || mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READ || mbi.Protect == PAGE_EXECUTE_READWRITE)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead;) {
if (isprint(buffer[i]) && isprint(buffer[i + 1]) && isprint(buffer[i + 2])) {
std::string possible_string;
SIZE_T j = i;
while (j < bytesRead && isprint(buffer[j]) && buffer[j] != '\0') {
possible_string += buffer[j];
j++;
}
if (possible_string.length() >= min_length) {
strings.emplace_back(possible_string, addr + i);
}
i = j; // Move i to the end of the found string
} else {
i++;
}
}
}
}
addr += mbi.RegionSize;
}
return strings;
}
// Function to find a reference to a string address within the .data section
uintptr_t find_reference_in_data_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, uintptr_t string_addr) {
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE || mbi.Protect == PAGE_EXECUTE_WRITECOPY || mbi.Protect == PAGE_WRITECOPY)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - sizeof(uintptr_t); ++i) {
uintptr_t* potential_ptr = reinterpret_cast<uintptr_t*>(buffer.data() + i);
if (*potential_ptr == string_addr) {
return addr + i;
}
}
}
}
addr += mbi.RegionSize;
}
return 0;
}
int main() {
const char* process_name = "Fallout76.exe";
const size_t minStringLength = 5; // Define the minimum string length you consider a valid string
FILE* log_file;
if (fopen_s(&log_file, "memscan.log", "w") != 0) {
printf("Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
log_message(log_file, "Could not find process ID for \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t base_addr = get_base_address(pid);
log_message(log_file, "Base Address: 0x%llx\n", base_addr);
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
log_message(log_file, "Could not open process \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t rdata_section_start, rdata_section_end;
if (!get_section_limits(hProcess, base_addr, ".rdata", rdata_section_start, rdata_section_end)) {
log_message(log_file, "Failed to get limits for .rdata section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
uintptr_t data_section_start, data_section_end;
if (!get_section_limits(hProcess, base_addr, ".data", data_section_start, data_section_end)) {
log_message(log_file, "Failed to get limits for .data section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
log_message(log_file, "$strings_with_offsets = [\n");
// Process strings found in the .rdata section
auto rdata_strings = find_strings_in_section(hProcess, rdata_section_start, rdata_section_end, minStringLength);
for (const auto& item : rdata_strings) {
uintptr_t string_addr = item.second;
uintptr_t reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr != 0) {
// Adjust the address logic if necessary based on structure analysis
uintptr_t function_pointer = reference_addr - 0x10;
uintptr_t offset = function_pointer - base_addr;
// Log detailed addresses and the originating section
log_message(log_file, "String: \"%s\"\n", item.first.c_str());
log_message(log_file, "String Address: 0x%llx\n", string_addr);
log_message(log_file, "Reference Address: 0x%llx\n", reference_addr);
log_message(log_file, "Function Pointer (Adjusted): 0x%llx\n", function_pointer);
log_message(log_file, "Offset from Base: 0x%llx\n", offset);
log_message(log_file, " \"%s\" at offset: 0x%llx in section .data\n", item.first.c_str(), offset);
}
}
log_message(log_file, "];\n");
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
Key Modifications:
Section Logging: The program now explicitly logs where each offset is found, particularly specifying when it finds a reference in the .data section.
Memory Safety: Use of std::vector for automatic memory management to ensure buffer allocation and deallocation is handled cleanly.
This code aims to improve the traceability and logging of string references found in the different sections, which can significantly aid in debugging and analysis. Remember to test and validate with different executables as behavior can vary depending on compiler optimizations and other production build considerations.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include <vector>
#include <cstdarg>
// Function to log messages both to console and a file
void log_message(FILE* log_file, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args); // Console output
if (log_file != NULL) {
vfprintf(log_file, format, args); // File output
fflush(log_file);
}
va_end(args);
}
// Function to get the process ID by name
DWORD get_process_id_by_name(const char* process_name) {
PROCESSENTRY32 pe32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 0;
}
do {
char exeFile[MAX_PATH];
size_t converted_chars = 0;
wcstombs_s(&converted_chars, exeFile, MAX_PATH, pe32.szExeFile, _TRUNCATE);
if (_stricmp(exeFile, process_name) == 0) {
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return 0;
}
// Function to get the base address of the process
uintptr_t get_base_address(DWORD pid) {
MODULEENTRY32 me32 = { sizeof(me32) };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
if (Module32First(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
CloseHandle(hSnapshot);
return 0;
}
// Function to get the start and end addresses of a specific section in the process
bool get_section_limits(HANDLE hProcess, uintptr_t base_addr, const char* section_name, uintptr_t& section_start, uintptr_t& section_end) {
IMAGE_DOS_HEADER dos_header;
if (!ReadProcessMemory(hProcess, (LPCVOID)base_addr, &dos_header, sizeof(dos_header), NULL)) {
return false;
}
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
IMAGE_NT_HEADERS nt_headers;
if (!ReadProcessMemory(hProcess, (LPCVOID)(base_addr + dos_header.e_lfanew), &nt_headers, sizeof(nt_headers), NULL)) {
return false;
}
if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
return false;
}
std::vector<IMAGE_SECTION_HEADER> section_headers(nt_headers.FileHeader.NumberOfSections);
if (!ReadProcessMemory(hProcess, (LPCVOID)((uintptr_t)(base_addr + dos_header.e_lfanew) + sizeof(nt_headers)), section_headers.data(), sizeof(IMAGE_SECTION_HEADER) * nt_headers.FileHeader.NumberOfSections, NULL)) {
return false;
}
for (const auto& section : section_headers) {
if (strncmp(reinterpret_cast<const char*>(section.Name), section_name, IMAGE_SIZEOF_SHORT_NAME) == 0) {
section_start = base_addr + section.VirtualAddress;
section_end = section_start + section.SizeOfRawData;
return true;
}
}
return false;
}
// Function to find strings in a section of the process's memory
std::vector<std::pair<std::string, uintptr_t>> find_strings_in_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, size_t min_length) {
std::vector<std::pair<std::string, uintptr_t>> strings;
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READONLY || mbi.Protect == PAGE_EXECUTE_READ || mbi.Protect == PAGE_EXECUTE_READWRITE)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead;) {
if (isprint(buffer[i]) && isprint(buffer[i + 1]) && isprint(buffer[i + 2])) {
std::string possible_string;
SIZE_T j = i;
while (j < bytesRead && isprint(buffer[j]) && buffer[j] != '\0') {
possible_string += buffer[j];
j++;
}
if (possible_string.length() >= min_length) {
strings.emplace_back(possible_string, addr + i);
}
i = j; // Move i to the end of the found string
} else {
i++;
}
}
}
}
addr += mbi.RegionSize;
}
return strings;
}
// Function to find a reference to a string address within the .data section
uintptr_t find_reference_in_data_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, uintptr_t string_addr) {
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE || mbi.Protect == PAGE_EXECUTE_WRITECOPY || mbi.Protect == PAGE_WRITECOPY)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - sizeof(uintptr_t); ++i) {
uintptr_t* potential_ptr = reinterpret_cast<uintptr_t*>(buffer.data() + i);
if (*potential_ptr == string_addr) {
return addr + i;
}
}
}
}
addr += mbi.RegionSize;
}
return 0;
}
int main() {
const char* process_name = "Fallout76.exe";
const size_t minStringLength = 5; // Define the minimum string length you consider a valid string
FILE* log_file;
if (fopen_s(&log_file, "memscan.log", "w") != 0) {
printf("Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
log_message(log_file, "Could not find process ID for \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t base_addr = get_base_address(pid);
log_message(log_file, "Base Address: 0x%llx\n", base_addr);
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
log_message(log_file, "Could not open process \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t rdata_section_start, rdata_section_end;
if (!get_section_limits(hProcess, base_addr, ".rdata", rdata_section_start, rdata_section_end)) {
log_message(log_file, "Failed to get limits for .rdata section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
uintptr_t data_section_start, data_section_end;
if (!get_section_limits(hProcess, base_addr, ".data", data_section_start, data_section_end)) {
log_message(log_file, "Failed to get limits for .data section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
log_message(log_file, "$strings_with_offsets = [\n");
// Process strings found in the .rdata section
auto rdata_strings = find_strings_in_section(hProcess, rdata_section_start, rdata_section_end, minStringLength);
for (const auto& item : rdata_strings) {
uintptr_t string_addr = item.second;
uintptr_t reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr != 0) {
uintptr_t offset = reference_addr - base_addr;
// Log detailed addresses and the originating section
log_message(log_file, "String: \"%s\"\n", item.first.c_str());
log_message(log_file, "String Address (.rdata): 0x%llx\n", string_addr);
log_message(log_file, "Reference Address (.data): 0x%llx\n", reference_addr);
log_message(log_file, "Offset from Base: 0x%llx\n", offset);
log_message(log_file, " \"%s\" at offset: 0x%llx in .data section\n", item.first.c_str(), offset);
}
}
log_message(log_file, "];\n");
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
Key Modifications:
Section Identification: Correctly ensure you're reading strings from the .rdata section and references from the .data section.
Offset Calculation: Adjusted the offset calculation directly based on the reference address within .data.
The comments and logging have been updated to reflect this relationship more accurately, ensuring that you see the intended results in your logs. This setup should give you clarity over where each string and its reference are located.
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
IMAGE_NT_HEADERS nt_headers;
if (!ReadProcessMemory(hProcess, (LPCVOID)(base_addr + dos_header.e_lfanew), &nt_headers, sizeof(nt_headers), NULL)) {
return false;
}
if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
return false;
}
IMAGE_SECTION_HEADER* section_headers = new IMAGE_SECTION_HEADER[nt_headers.FileHeader.NumberOfSections];
if (!ReadProcessMemory(hProcess, (LPCVOID)((unsigned long long)(base_addr + dos_header.e_lfanew) + sizeof(nt_headers)), section_headers, sizeof(IMAGE_SECTION_HEADER) * nt_headers.FileHeader.NumberOfSections, NULL)) {
delete[] section_headers;
return false;
}
for (int i = 0; i < nt_headers.FileHeader.NumberOfSections; ++i) {
if (strncmp((char*)section_headers[i].Name, section_name, IMAGE_SIZEOF_SHORT_NAME) == 0) {
section_start = base_addr + section_headers[i].VirtualAddress;
section_end = section_start + section_headers[i].Misc.VirtualSize;
delete[] section_headers;
return true;
}
}
delete[] section_headers;
return false;
}
unsigned long long find_string_in_section(HANDLE hProcess, unsigned long long section_start, unsigned long long section_end, const char* string_to_search) {
unsigned long long string_addr = 0;
unsigned long long addr = section_start;
SIZE_T string_length = strlen(string_to_search);
unsigned long long find_reference_in_data_section(HANDLE hProcess, unsigned long long section_start, unsigned long long section_end, unsigned long long string_addr) {
unsigned long long addr = section_start;
code
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT &&
(mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE ||
mbi.Protect == PAGE_EXECUTE_WRITECOPY || mbi.Protect == PAGE_WRITECOPY)) {
char* buffer = new char[mbi.RegionSize];
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer, mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - sizeof(unsigned long long); i++) {
unsigned long long* potential_ptr = (unsigned long long*)(buffer + i);
if (*potential_ptr == string_addr) {
unsigned long long reference_addr = addr + i;
delete[] buffer;
return reference_addr;
}
}
}
delete[] buffer;
}
addr += mbi.RegionSize;
}
return 0;
FILE* log_file;
if (fopen_s(&log_file, "memscan.log", "w") != 0) {
printf("Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
fclose(log_file);
return 1;
}
unsigned long long base_addr = 0x7FF68C9B0000;
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
fclose(log_file);
return 1;
}
unsigned long long rdata_section_start, rdata_section_end;
if (!get_section_limits(hProcess, base_addr, ".rdata", rdata_section_start, rdata_section_end)) {
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
unsigned long long data_section_start, data_section_end;
if (!get_section_limits(hProcess, base_addr, ".data", data_section_start, data_section_end)) {
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
fprintf(log_file, "$gameSettings = [\n");
for (const char* string_to_search : gameSettings) {
unsigned long long string_addr = find_string_in_section(hProcess, rdata_section_start, rdata_section_end, string_to_search);
if (string_addr == 0) {
continue;
}
unsigned long long reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr == 0) {
continue;
}
unsigned long long function_pointer = reference_addr - 0x10;
unsigned long long offset = function_pointer - base_addr;
fprintf(log_file, " \"%s\" => 0x%llx,\n", string_to_search, offset);
}
fprintf(log_file, "];\n");
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
Error Handling: Enhance error handling and logging for improved diagnostic capability if operations like memory reads fail.
64-bit Compatibility: Ensure assumptions related to architecture and pointer sizes are consistent with the target application.
Performance Optimizations: Could include multithreading or asynchronous processing if scanning large memory sections becomes a bottleneck. This could also involve optimizing memory buffer reallocation and management.
Security Practices: Consider appropriate permissions and safeguards if deploying in a sensitive or constrained environment, respecting user-level security.
This code exemplifies a common pattern used in memory scanning/analysis tasks prevalent in debugging, reverse engineering, or game modding communities. Understanding how and why these mechanics work will give insights into program behavior and configuration management at runtime.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include <vector>
#include <cstdarg>
// Function to log messages to both console and a file
void log_message(FILE* log_file, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
if (log_file != NULL) {
vfprintf(log_file, format, args);
fflush(log_file);
}
va_end(args);
}
// Function to get the process ID by name
DWORD get_process_id_by_name(const char* process_name) {
PROCESSENTRY32 pe32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 0;
}
do {
char exeFile[MAX_PATH];
size_t converted_chars = 0;
wcstombs_s(&converted_chars, exeFile, MAX_PATH, pe32.szExeFile, _TRUNCATE);
if (_stricmp(exeFile, process_name) == 0) {
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return 0;
}
// Function to get the base address of the process
uintptr_t get_base_address(DWORD pid) {
MODULEENTRY32 me32 = { sizeof(me32) };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
if (Module32First(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
CloseHandle(hSnapshot);
return 0;
}
// Function to get the start and end addresses of a specific section in the process
bool get_section_limits(HANDLE hProcess, uintptr_t base_addr, const char* section_name, uintptr_t& section_start, uintptr_t& section_end) {
IMAGE_DOS_HEADER dos_header;
if (!ReadProcessMemory(hProcess, (LPCVOID)base_addr, &dos_header, sizeof(dos_header), NULL)) {
return false;
}
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
IMAGE_NT_HEADERS nt_headers;
if (!ReadProcessMemory(hProcess, (LPCVOID)(base_addr + dos_header.e_lfanew), &nt_headers, sizeof(nt_headers), NULL)) {
return false;
}
if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
return false;
}
std::vector<IMAGE_SECTION_HEADER> section_headers(nt_headers.FileHeader.NumberOfSections);
if (!ReadProcessMemory(hProcess, (LPCVOID)((uintptr_t)(base_addr + dos_header.e_lfanew) + sizeof(nt_headers)), section_headers.data(), sizeof(IMAGE_SECTION_HEADER) * nt_headers.FileHeader.NumberOfSections, NULL)) {
return false;
}
for (const auto& section : section_headers) {
if (strncmp(reinterpret_cast<const char*>(section.Name), section_name, IMAGE_SIZEOF_SHORT_NAME) == 0) {
section_start = base_addr + section.VirtualAddress;
section_end = section_start + section.Misc.VirtualSize; // Correct to use Misc.VirtualSize for the section size
return true;
}
}
return false;
}
// Function to find strings in a section of the process's memory
std::vector<std::pair<std::string, uintptr_t>> find_strings_in_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, size_t min_length) {
std::vector<std::pair<std::string, uintptr_t>> strings;
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READONLY || mbi.Protect == PAGE_EXECUTE_READ || mbi.Protect == PAGE_EXECUTE_READWRITE)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead;) {
if (isprint(buffer[i]) && isprint(buffer[i + 1]) && isprint(buffer[i + 2])) {
std::string possible_string;
SIZE_T j = i;
while (j < bytesRead && isprint(buffer[j]) && buffer[j] != '\0') {
possible_string += buffer[j];
j++;
}
if (possible_string.length() >= min_length) {
strings.emplace_back(possible_string, addr + i);
}
i = j; // Move i to the end of the found string
} else {
i++;
}
}
}
}
addr += mbi.RegionSize;
}
return strings;
}
// Function to find a reference to a string address within the .data section
uintptr_t find_reference_in_data_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, uintptr_t string_addr) {
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE || mbi.Protect == PAGE_EXECUTE_WRITECOPY || mbi.Protect == PAGE_WRITECOPY)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - sizeof(uintptr_t); ++i) {
uintptr_t* potential_ptr = reinterpret_cast<uintptr_t*>(buffer.data() + i);
if (*potential_ptr == string_addr) {
return addr + i;
}
}
}
}
addr += mbi.RegionSize;
}
return 0;
}
// Main function
int main() {
const char* process_name = "Fallout76.exe";
const size_t minStringLength = 5; // Define the minimum string length you consider a valid string
FILE* log_file;
if (fopen_s(&log_file, "memscan.log", "w") != 0) {
printf("Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
log_message(log_file, "Could not find process ID for \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t base_addr = get_base_address(pid);
log_message(log_file, "Base Address: 0x%llx\n", base_addr);
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
log_message(log_file, "Could not open process \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t rdata_section_start, rdata_section_end;
if (!get_section_limits(hProcess, base_addr, ".rdata", rdata_section_start, rdata_section_end)) {
log_message(log_file, "Failed to get limits for .rdata section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
uintptr_t data_section_start, data_section_end;
if (!get_section_limits(hProcess, base_addr, ".data", data_section_start, data_section_end)) {
log_message(log_file, "Failed to get limits for .data section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
log_message(log_file, "$strings_with_offsets = [\n");
// Process strings found in the .rdata section
auto rdata_strings = find_strings_in_section(hProcess, rdata_section_start, rdata_section_end, minStringLength);
for (const auto& item : rdata_strings) {
uintptr_t string_addr = item.second;
uintptr_t reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr != 0) {
uintptr_t offset = reference_addr - base_addr;
// Log detailed addresses and the originating section
log_message(log_file, "String: \"%s\"\n", item.first.c_str());
log_message(log_file, "String Address (.rdata): 0x%llx\n", string_addr);
log_message(log_file, "Reference Address (.data): 0x%llx\n", reference_addr);
log_message(log_file, "Offset from Base: 0x%llx\n", offset);
log_message(log_file, " \"%s\" at offset: 0x%llx in .data section\n", item.first.c_str(), offset);
}
}
log_message(log_file, "];\n");
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
Key Focus and Features:
String Search in .rdata: The code scans the .rdata section for printable strings, ensuring that these are potential candidates for strings used within the application.
Reference Search in .data: Once a string is found, the code scans the .data section to find where these strings are referenced through pointers.
Offset Calculation: The program calculates offsets from the base address for each reference found and logs these for later interpretation or debugging.
Resilient Base Address Management: Base addresses are dynamically retrieved and calculated using the system API instead of relying on hardcoded values, which increases the adaptability to different runtime environments or application versions.
By focusing on these sections, the code can more effectively identify the relationship between stored data and active or performance-critical references, which is the typical use-case in memory inspection or debugging scenarios. This setup facilitates the analysis of game settings, configurations, or inter-related function pointers as might exist within the memory space of Fallout76.exe.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include <vector>
// Function to log messages to both console and file
void log_message(FILE* log_file, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args); // Console output
if (log_file != NULL) {
vfprintf(log_file, format, args);
fflush(log_file);
}
va_end(args);
}
// Function to get the process ID by name
DWORD get_process_id_by_name(const char* process_name) {
PROCESSENTRY32 pe32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 0;
}
do {
char exeFile[MAX_PATH];
size_t converted_chars = 0;
wcstombs_s(&converted_chars, exeFile, MAX_PATH, pe32.szExeFile, _TRUNCATE);
if (_stricmp(exeFile, process_name) == 0) {
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return 0;
}
// Function to get the base address of the process
uintptr_t get_base_address(DWORD pid) {
MODULEENTRY32 me32 = { sizeof(me32) };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
if (Module32First(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
CloseHandle(hSnapshot);
return 0;
}
// Function to get the start and end addresses of a specific section in the process
bool get_section_limits(HANDLE hProcess, uintptr_t base_addr, const char* section_name, uintptr_t& section_start, uintptr_t& section_end) {
IMAGE_DOS_HEADER dos_header;
if (!ReadProcessMemory(hProcess, (LPCVOID)base_addr, &dos_header, sizeof(dos_header), NULL)) {
return false;
}
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
IMAGE_NT_HEADERS nt_headers;
if (!ReadProcessMemory(hProcess, (LPCVOID)(base_addr + dos_header.e_lfanew), &nt_headers, sizeof(nt_headers), NULL)) {
return false;
}
if (nt_headers.Signature != IMAGE_NT_SIGNATURE) {
return false;
}
std::vector<IMAGE_SECTION_HEADER> section_headers(nt_headers.FileHeader.NumberOfSections);
if (!ReadProcessMemory(hProcess, (LPCVOID)((uintptr_t)(base_addr + dos_header.e_lfanew) + sizeof(nt_headers)), section_headers.data(), sizeof(IMAGE_SECTION_HEADER) * nt_headers.FileHeader.NumberOfSections, NULL)) {
return false;
}
for (const auto& section : section_headers) {
if (strncmp(reinterpret_cast<const char*>(section.Name), section_name, IMAGE_SIZEOF_SHORT_NAME) == 0) {
section_start = base_addr + section.VirtualAddress;
section_end = section_start + section.Misc.VirtualSize;
return true;
}
}
return false;
}
// Function to find strings in a section of the process's memory
std::vector<std::pair<std::string, uintptr_t>> find_strings_in_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, size_t min_length) {
std::vector<std::pair<std::string, uintptr_t>> strings;
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READONLY || mbi.Protect == PAGE_EXECUTE_READ || mbi.Protect == PAGE_EXECUTE_READWRITE)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead;) {
if (isprint(buffer[i]) && isprint(buffer[i + 1]) && isprint(buffer[i + 2])) {
std::string possible_string;
SIZE_T j = i;
while (j < bytesRead && isprint(buffer[j]) && buffer[j] != '\0') {
possible_string += buffer[j];
j++;
}
if (possible_string.length() >= min_length) {
strings.emplace_back(possible_string, addr + i);
}
i = j; // Move i to the end of the found string
} else {
i++;
}
}
}
}
addr += mbi.RegionSize;
}
return strings;
}
// Function to find a reference to a string address within the .data section
uintptr_t find_reference_in_data_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, uintptr_t string_addr) {
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE || mbi.Protect == PAGE_EXECUTE_WRITECOPY || mbi.Protect == PAGE_WRITECOPY)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - sizeof(uintptr_t); ++i) {
uintptr_t* potential_ptr = reinterpret_cast<uintptr_t*>(buffer.data() + i);
if (*potential_ptr == string_addr) {
return addr + i;
}
}
}
}
addr += mbi.RegionSize;
}
return 0;
}
// Main function
int main() {
const char* process_name = "Fallout76.exe";
const size_t minStringLength = 5; // Define the minimum string length you consider a valid string
FILE* log_file;
if (fopen_s(&log_file, "memscan.log", "w") != 0) {
printf("Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
log_message(log_file, "Could not find process ID for \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t base_addr = get_base_address(pid);
log_message(log_file, "Base Address: 0x%llx\n", base_addr);
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
log_message(log_file, "Could not open process \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t rdata_section_start, rdata_section_end;
if (!get_section_limits(hProcess, base_addr, ".rdata", rdata_section_start, rdata_section_end)) {
log_message(log_file, "Failed to get limits for .rdata section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
uintptr_t data_section_start, data_section_end;
if (!get_section_limits(hProcess, base_addr, ".data", data_section_start, data_section_end)) {
log_message(log_file, "Failed to get limits for .data section\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
log_message(log_file, "$strings_with_offsets = [\n");
// Process strings found in the .rdata section
auto rdata_strings = find_strings_in_section(hProcess, rdata_section_start, rdata_section_end, minStringLength);
for (const auto& item : rdata_strings) {
uintptr_t string_addr = item.second;
uintptr_t reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr != 0) {
// Calculate potential function pointer address
uintptr_t function_pointer = reference_addr - 0x10;
uintptr_t offset = function_pointer - base_addr;
// Log detailed addresses and the originating section
log_message(log_file, "String: \"%s\"\n", item.first.c_str());
log_message(log_file, "String Address (.rdata): 0x%llx\n", string_addr);
log_message(log_file, "Reference Address (.data): 0x%llx\n", reference_addr);
log_message(log_file, "Function Pointer: 0x%llx\n", function_pointer);
log_message(log_file, "Offset from Base: 0x%llx\n", offset);
log_message(log_file, " \"%s\" at function pointer offset: 0x%llx\n", item.first.c_str(), offset);
}
}
log_message(log_file, "];\n");
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
Explanation of the Code:
Process and Base Address Initialization: The program starts by locating the process ID and base address of Fallout76.exe.
Section Limits: The code determines the limits of .rdata (for strings) and .data (for references) sections by parsing the executable's PE headers.
String Detection: It scans the .rdata section for printable strings meeting a minimum length requirement to find potential configuration or text data.
Reference Detection: The program searches for occurrences of the identified string addresses within the .data section, indicating where they are utilized or referenced.
Address and Offset Calculation: For each reference found, it calculates the function pointer by adjusting the address as per your instruction (address - 0x10) and computes its offset relative to the base address.
Logging: All results are logged to a file, which gives a complete map of strings, their references, and the calculated function pointers.
This approach will allow you to map out the relationships between data and code within the running process, which is crucial for understanding the program’s inner workings or debugging certain features.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include <vector>
#include <cstdarg>
// Function to log messages to both console and file
void log_message(FILE* log_file, const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args); // Console output
if (log_file != NULL) {
vfprintf(log_file, format, args);
fflush(log_file);
}
va_end(args);
}
// Function to get the process ID by name
DWORD get_process_id_by_name(const char* process_name) {
PROCESSENTRY32 pe32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 0;
}
do {
char exeFile[MAX_PATH];
size_t converted_chars = 0;
wcstombs_s(&converted_chars, exeFile, MAX_PATH, pe32.szExeFile, _TRUNCATE);
if (_stricmp(exeFile, process_name) == 0) {
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return 0;
}
// Function to get the base address of the process
uintptr_t get_base_address(DWORD pid) {
MODULEENTRY32 me32 = { sizeof(me32) };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
if (Module32First(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return reinterpret_cast<uintptr_t>(me32.modBaseAddr);
}
CloseHandle(hSnapshot);
return 0;
}
// Function to find strings in a section of the process's memory
std::vector<std::pair<std::string, uintptr_t>> find_strings_in_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, size_t min_length) {
std::vector<std::pair<std::string, uintptr_t>> strings;
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READONLY || mbi.Protect == PAGE_EXECUTE_READ || mbi.Protect == PAGE_EXECUTE_READWRITE)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead;) {
if (isprint(buffer[i]) && isprint(buffer[i + 1]) && isprint(buffer[i + 2])) {
std::string possible_string;
SIZE_T j = i;
while (j < bytesRead && isprint(buffer[j]) && buffer[j] != '\0') {
possible_string += buffer[j];
j++;
}
if (possible_string.length() >= min_length) {
strings.emplace_back(possible_string, addr + i);
}
i = j; // Move i to the end of the found string
} else {
i++;
}
}
}
}
addr += mbi.RegionSize;
}
return strings;
}
// Function to find a reference to a string address within the .data section
uintptr_t find_reference_in_data_section(HANDLE hProcess, uintptr_t section_start, uintptr_t section_end, uintptr_t string_addr) {
uintptr_t addr = section_start;
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_EXECUTE_READWRITE || mbi.Protect == PAGE_EXECUTE_WRITECOPY || mbi.Protect == PAGE_WRITECOPY)) {
std::vector<char> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer.data(), mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - sizeof(uintptr_t); ++i) {
uintptr_t* potential_ptr = reinterpret_cast<uintptr_t*>(buffer.data() + i);
if (*potential_ptr == string_addr) {
return addr + i;
}
}
}
}
addr += mbi.RegionSize;
}
return 0;
}
// Main function
int main() {
const char* process_name = "Fallout76.exe";
const size_t minStringLength = 5; // Define the minimum string length you consider a valid string
FILE* log_file;
if (fopen_s(&log_file, "memscan.log", "w") != 0) {
printf("Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
log_message(log_file, "Could not find process ID for \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
uintptr_t base_addr = get_base_address(pid);
log_message(log_file, "Base Address: 0x%llx\n", base_addr);
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
log_message(log_file, "Could not open process \"%s\"\n", process_name);
fclose(log_file);
return 1;
}
// Hardcoded section addresses for demonstration purposes
uintptr_t rdata_section_start = 0x0000000143DC64E0;
uintptr_t data_section_start = 0x00000001457AF000;
// Assumed end for sections, since only start is specific here
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)(data_section_start), &mbi, sizeof(mbi))) {
log_message(log_file, "Failed to query memory information\n");
CloseHandle(hProcess);
fclose(log_file);
return 1;
}
uintptr_t rdata_section_end = rdata_section_start + mbi.RegionSize;
uintptr_t data_section_end = data_section_start + mbi.RegionSize;
log_message(log_file, "$strings_with_offsets = [\n");
// Process strings found in the .rdata section
auto rdata_strings = find_strings_in_section(hProcess, rdata_section_start, rdata_section_end, minStringLength);
for (const auto& item : rdata_strings) {
uintptr_t string_addr = item.second;
uintptr_t reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr != 0) {
// Calculate potential function pointer address
uintptr_t function_pointer = reference_addr - 0x10;
uintptr_t offset = function_pointer - base_addr;
// Log detailed addresses and the originating section
log_message(log_file, "String: \"%s\"\n", item.first.c_str());
log_message(log_file, "String Address (.rdata): 0x%llx\n", string_addr);
log_message(log_file, "Reference Address (.data): 0x%llx\n", reference_addr);
log_message(log_file, "Function Pointer: 0x%llx\n", function_pointer);
log_message(log_file, "Offset from Base: 0x%llx\n", offset);
log_message(log_file, " \"%s\" at function pointer offset: 0x%llx\n", item.first.c_str(), offset);
}
}
log_message(log_file, "];\n");
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
Key Adjustments:
Static Section Start Addresses: The program uses the provided hardcoded addresses for the starts of the .rdata and .data sections. This assumes these boundaries are valid for the running instance of Fallout76.exe.
Dynamic Section Ends: The code uses VirtualQueryEx to determine the size of a memory region, using it to dynamically define the end of the sections.
Simplified Logic: The logic directly focuses on finding strings in the .rdata section and their references in the .data section, thereby aligning with your specific instructions.
Logging of Results: The program outputs detailed logs for each string found and its computed offset from the base address, assisting in debugging and analysis.
This setup ensures you can specifically target the memory areas of interest, operating under the assumption that memory layout and hard addresses correspond to your operational context.