this program needs alot of adjutments as it dont do as it should offsets found i nthis otta point to the string in ida when searched i think but it dont #include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#include
#include
#include <psapi.h>
// Function to log messages
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 process ID by name
DWORD get_process_id_by_name(const char* process_name) {
PROCESSENTRY32 pe32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
code
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 0;
}
do {
char exeFile[MAX_PATH];
int result = WideCharToMultiByte(CP_ACP, 0, pe32.szExeFile, -1, exeFile, MAX_PATH, NULL, NULL);
if (result == 0) {
CloseHandle(hSnapshot);
return 0;
}
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 a module in a given process
unsigned long long get_base_address(DWORD pid, const char* module_name) {
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 = 0;
do {
char modName[MAX_PATH];
if (WideCharToMultiByte(CP_ACP, 0, me32.szModule, -1, modName, MAX_PATH, NULL, NULL) != 0 &&
_stricmp(modName, module_name) == 0) {
base_address = (unsigned long long)me32.modBaseAddr;
break;
}
} while (Module32Next(hSnapshot, &me32));
CloseHandle(hSnapshot);
return base_address;
Assistant
}
// Function to get the start and end addresses of a section
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
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;
}
// Function to find a string in a section
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);
code
while (addr < section_end) {
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQueryEx(hProcess, (LPCVOID)addr, &mbi, sizeof(mbi))) {
break;
}
char* buffer = new (std::nothrow) char[mbi.RegionSize];
if (!buffer) {
break;
}
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, (LPCVOID)addr, buffer, mbi.RegionSize, &bytesRead) && bytesRead > 0) {
for (SIZE_T i = 0; i < bytesRead - string_length; i++) {
if (memcmp(buffer + i, string_to_search, string_length) == 0) {
string_addr = addr + i;
delete[] buffer;
return string_addr;
}
}
}
delete[] buffer;
addr += mbi.RegionSize;
}
return 0;
}
// Function to find a reference to a string in the data section
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;
}
unsigned long long* buffer = new (std::nothrow) unsigned long long[mbi.RegionSize / sizeof(unsigned long long)];
if (!buffer) {
break;
}
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++) {
if (buffer[i] == string_addr) {
unsigned long long reference_addr = addr + i * sizeof(unsigned long long);
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) {
printf("Could not find process\n");
fclose(log_file);
return 1;
}
unsigned long long base_addr = get_base_address(pid, module_name);
if (base_addr == 0) {
printf("Could not find base address\n");
fclose(log_file);
return 1;
}
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
printf("Could not open process\n");
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)) {
printf("Could not find .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)) {
printf("Could not find .data section\n");
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) {
printf("String not found: %s\n", string_to_search);
continue;
}
unsigned long long reference_addr = find_reference_in_data_section(hProcess, data_section_start, data_section_end, string_addr);
if (reference_addr == 0) {
printf("Reference not found for string: %s\n", string_to_search);
continue;
}
// Adjust by 0x10 as originally intended
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;
}
It looks like you're trying to modify a program that scans a target process's memory (like a game) to find offsets for certain game settings. You mentioned that the program isn't working as expected and the offsets it finds don't match what you see in IDA Pro, which is a common disassembler and debugger.
There are several issues in your code that could be causing these problems. Let's go through them step by step and adjust your code accordingly.
Also, since we'll be working with Unicode strings (see next point), we're changing const char* to const wchar_t* and prefixing the strings with L to make them wide-character strings.
2. Consistency in Character Encoding and Function Versions
Your code mixes ANSI and Unicode function versions and character types, which can cause unexpected behavior. The Windows API provides both ANSI (functions ending with A) and Unicode (functions ending with W) versions of functions and structures. It's important to be consistent.
Adjustments:
Use wide-character versions of Windows API functions (those ending with W).
Use wchar_t instead of char for strings.
Use _wcsicmp instead of _stricmp for string comparisons.
Here's how you can adjust the get_process_id_by_name function:
unsigned long long get_base_address(DWORD pid, const wchar_t* module_name) {
MODULEENTRY32W me32;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
me32.dwSize = sizeof(MODULEENTRY32W);
if (!Module32FirstW(hSnapshot, &me32)) {
CloseHandle(hSnapshot);
return 0;
}
unsigned long long base_address = 0;
do {
if (_wcsicmp(me32.szModule, module_name) == 0) {
base_address = (unsigned long long)me32.modBaseAddr;
break;
}
} while (Module32NextW(hSnapshot, &me32));
CloseHandle(hSnapshot);
return base_address;
}
3. Correctly Handling 64-bit Processes and Structures
When dealing with 64-bit processes, you need to use the correct data structures. In your get_section_limits function, you're using IMAGE_NT_HEADERS, which is for 32-bit images. For 64-bit images, you should use IMAGE_NT_HEADERS64.
Also, when reading the optional headers, you need to determine whether the target process is 32-bit or 64-bit by checking the Magic field in the IMAGE_OPTIONAL_HEADER. Here's how you can adjust your get_section_limits function:
Ensure that when you're reading structures from the target process's memory, you're reading the correct sizes and that the structures align with those in the target process. Misalignment or incorrect sizes can lead to incorrect data being read.
For instance, when you read the optional header, make sure you're reading the correct header (IMAGE_OPTIONAL_HEADER32 for 32-bit processes and IMAGE_OPTIONAL_HEADER64 for 64-bit processes) and that you're adjusting pointer arithmetic accordingly.
5. Validating All Read and Write Operations
Always check the return values of ReadProcessMemory and other API functions. If a read fails, it could be because of insufficient permissions, the target address is invalid, or other issues. Handling these cases can help you debug why certain operations aren't working.
6. Computing Offsets Correctly
After finding the reference to a string in the .data section, you adjust the function_pointer by subtracting 0x10. Ensure that this adjustment is correct for your specific use case. The 0x10 might not be the correct value, or it might be unnecessary.
Also, when calculating the offset:
cpp
unsigned long long offset = function_pointer - base_addr;
Make sure that both function_pointer and base_addr are in the same address space and that they represent the correct addresses you're interested in.
7. Testing with Known Values
To ensure your code works correctly, test it with processes and modules where you know the expected results. For example, use a simple program that contains known strings at known offsets, and verify that your code can find and report the correct offsets.
8. Permissions and Process Architecture
Make sure that your program has the necessary permissions to read the target process's memory. Also, if you're building your program as a 32-bit application, you might have trouble reading memory from a 64-bit process due to pointer size differences and other architecture-related issues.
Consider compiling your program as a 64-bit application if you're targeting 64-bit processes.
9. Example Adjusted main Function
Here's how you might adjust your main function with the above considerations:
cpp
int main() {
const wchar_t* process_name = L"notepad.exe";
const wchar_t* module_name = L"notepad.exe";
// (Initialize your gameSettings array as before...)
FILE* log_file;
if (_wfopen_s(&log_file, L"memscan.log", L"w") != 0) {
wprintf(L"Could not open log file for writing\n");
return 1;
}
DWORD pid = get_process_id_by_name(process_name);
if (pid == 0) {
wprintf(L"Could not find process\n");
fclose(log_file);
return 1;
}
unsigned long long base_addr = get_base_address(pid, module_name);
if (base_addr == 0) {
wprintf(L"Could not find base address\n");
fclose(log_file);
return 1;
}
HANDLE hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess == NULL) {
wprintf(L"Could not open process\n");
fclose(log_file);
return 1;
}
// Proceed with adjusted get_section_limits and other functions...
CloseHandle(hProcess);
fclose(log_file);
return 0;
}
10. Consider Using Existing Libraries
Parsing PE files and reading process memory can be complex and error-prone. Consider using existing libraries that handle PE parsing, such as pe-parse or Microsoft's DbgHelp library, which can simplify these tasks.
Summary
By making these adjustments, your program should more accurately find the offsets that correspond to what's shown in IDA. Always ensure that you're consistent with character encoding, that you're using the correct data structures for the target process's architecture, and that you're validating all API calls.
If you continue to have issues, consider stepping through your code with a debugger to see exactly where it diverges from expected behavior.
Let me know if you have any questions or need further assistance!