I'm attempting to scan the memory of a certain module in a certain process and find all ints in the memory, and output them.
I do this by getting the base address of the module (in this case the module is the name of the exe) and then using the module size to calculate the end address of the module.
I then iterate over every address and try to read an int. It works in some cases, but a good deal of the time, RPM returns error 299 (ERROR_PARTIAL_COPY). I'm not really sure as to why, because I check the page rights, and I check if the page has backing.
It seems to work on x86 processes (when I compile it in x86) but not x64 processes (when I compile in x64)? Any suggestions are appreciated, I'm kind of lost. My RPM call is correct as well, as it works on some processes. Thanks!
void ScanProcess(DWORD processID){
char szProcessName[MAX_PATH];
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
HMODULE hMod = NULL;
if (hProcess){
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName));
MODULEINFO lpm;
GetModuleInformation(hProcess, hMod, &lpm, sizeof(lpm));
DWORD start, end;
start = (DWORD)hMod;
end = start + lpm.SizeOfImage;
printf("Scanning module %s (PID: %u) Range: %x to %x\n", szProcessName, processID, start, end);
for (uintptr_t i = start; i < end; i++) {
PSAPI_WORKING_SET_EX_INFORMATION * pWSX = (PSAPI_WORKING_SET_EX_INFORMATION *)malloc(sizeof(PSAPI_WORKING_SET_EX_INFORMATION));
pWSX->VirtualAddress = reinterpret_cast<void*>(i);
QueryWorkingSetEx(hProcess, &pWSX, sizeof(PSAPI_WORKING_SET_EX_INFORMATION));
if (pWSX->VirtualAttributes.Valid == 1) {
if ((pWSX->VirtualAttributes.Win32Protection & PAGE_READWRITE) || (pWSX->VirtualAttributes.Win32Protection & PAGE_READONLY) || (pWSX->VirtualAttributes.Win32Protection & PAGE_EXECUTE_READWRITE) || (pWSX->VirtualAttributes.Win32Protection & PAGE_EXECUTE_READ)) {
printf("Page: %x. Page backing is %s. Page protection: %X\n", i, pWSX->VirtualAttributes.Valid ? "valid" : "invalid", pWSX->VirtualAttributes.Win32Protection);
int j = 0;
if (ReadProcessMemory(hProcess, (LPCVOID)i, &j, sizeof(int), 0))
printf("%s SUCCESS %i\n",szProcessName, j);
else
printf("Error: %x", GetLastError());
}
}
}
}
}
CloseHandle(hProcess);
}