If you want to dynamically check whether a process has specific values present in its memory and then log these, you're venturing into memory reading, which involves scanning the process's memory space. This is a sensitive task, usually associated with debugging or memory inspection tools, and it can have serious legal and ethical implications—especially if the target processes are games or proprietary software.
Exploring Memory Reading in C++:
We'll need to refine our approach to incorporate reading memory. To do this safely and effectively, follow the steps below, understanding this example focuses on educational purposes.
#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <string>
#include <fstream>
#include <ctime>
#include <vector>
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "version.lib")
DWORD FindProcessId(const std::wstring& processName) {
DWORD processIds[1024], cbNeeded, processCount;
if (!EnumProcesses(processIds, sizeof(processIds), &cbNeeded)) {
return 0;
}
processCount = cbNeeded / sizeof(DWORD);
for (DWORD i = 0; i < processCount; ++i) {
if (processIds[i] != 0) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processIds[i]);
if (hProcess) {
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
WCHAR szProcessName[MAX_PATH] = L"<unknown>";
GetModuleBaseNameW(hProcess, hMod, szProcessName, sizeof(szProcessName) / sizeof(WCHAR));
if (!_wcsicmp(szProcessName, processName.c_str())) {
CloseHandle(hProcess);
return processIds[i];
}
}
CloseHandle(hProcess);
}
}
}
return 0;
}
uintptr_t GetBaseAddress(DWORD processID) {
uintptr_t baseAddress = 0;
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
if (processHandle) {
HMODULE hMods[1024];
DWORD cbNeeded;
if (EnumProcessModules(processHandle, hMods, sizeof(hMods), &cbNeeded)) {
baseAddress = (uintptr_t)hMods[0];
}
}
CloseHandle(processHandle);
return baseAddress;
}
std::string GetCurrentTime() {
std::time_t t = std::time(nullptr);
char buffer[100];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
return buffer;
}
std::string GetFileVersion(const std::wstring& filePath) {
DWORD verHandle = 0;
DWORD verSize = GetFileVersionInfoSizeW(filePath.c_str(), &verHandle);
if (verSize != 0) {
std::vector<BYTE> verData(verSize);
if (GetFileVersionInfoW(filePath.c_str(), verHandle, verSize, verData.data())) {
VS_FIXEDFILEINFO* fileInfo = nullptr;
UINT size = 0;
if (VerQueryValueW(verData.data(), L"\\", reinterpret_cast<LPVOID*>(&fileInfo), &size)) {
if (fileInfo) {
return std::to_string((fileInfo->dwFileVersionMS >> 16) & 0xffff) + "." +
std::to_string((fileInfo->dwFileVersionMS >> 0) & 0xffff) + "." +
std::to_string((fileInfo->dwFileVersionLS >> 16) & 0xffff) + "." +
std::to_string((fileInfo->dwFileVersionLS >> 0) & 0xffff);
}
}
}
}
return "Unknown";
}
bool CheckMemoryValues(HANDLE hProcess, const std::vector<uint8_t>& pattern) {
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
MEMORY_BASIC_INFORMATION mbi;
uintptr_t address = reinterpret_cast<uintptr_t>(sysInfo.lpMinimumApplicationAddress);
uintptr_t maxAddress = reinterpret_cast<uintptr_t>(sysInfo.lpMaximumApplicationAddress);
while (address < maxAddress) {
if (VirtualQueryEx(hProcess, reinterpret_cast<LPCVOID>(address), &mbi, sizeof(mbi))) {
if (mbi.State == MEM_COMMIT && (mbi.Protect == PAGE_READWRITE || mbi.Protect == PAGE_WRITECOPY || mbi.Protect == PAGE_READONLY)) {
std::vector<uint8_t> buffer(mbi.RegionSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, reinterpret_cast<LPCVOID>(address), buffer.data(), mbi.RegionSize, &bytesRead)) {
for (size_t i = 0; i < bytesRead - pattern.size(); ++i) {
if (std::equal(pattern.begin(), pattern.end(), buffer.begin() + i)) {
return true;
}
}
}
}
address += mbi.RegionSize;
}
}
return false;
}
void LogProcessInfo(const std::wstring& processName, uintptr_t baseAddress, const std::string& version, bool hasPattern) {
std::ofstream logFile("process_info.log", std::ios::app);
logFile << GetCurrentTime() << ": Process '" << std::string(processName.begin(), processName.end())
<< "' - Base Address: 0x" << std::hex << baseAddress
<< ", Version: " << version
<< ", Has Pattern: " << (hasPattern ? "Yes" : "No") << "\n";
}
int main() {
std::wcout << L"Enter the process name (including .exe): ";
std::wstring processName;
std::getline(std::wcin, processName);
DWORD pid = FindProcessId(processName);
if (pid == 0) {
std::wcout << L"Process " << processName << L" not found." << std::endl;
} else {
uintptr_t baseAddress = GetBaseAddress(pid);
if (baseAddress == 0) {
std::cout << "Could not retrieve the base address." << std::endl;
} else {
WCHAR szProcessPath[MAX_PATH] = {0};
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (hProcess) {
if (GetModuleFileNameExW(hProcess, NULL, szProcessPath, MAX_PATH)) {
std::string version = GetFileVersion(szProcessPath);
// Define a pattern to search for in memory
std::vector<uint8_t> pattern = { 0xDE, 0xAD, 0xBE, 0xEF }; // Example pattern
bool hasPattern = CheckMemoryValues(hProcess, pattern);
LogProcessInfo(processName, baseAddress, version, hasPattern);
std::cout << "Data logged successfully." << std::endl;
} else {
std::cout << "Failed to retrieve process path." << std::endl;
}
CloseHandle(hProcess);
}
}
}
return 0;
}
Breakdown:
-
Memory Pattern Check:
CheckMemoryValues scans the process's memory for a specific byte pattern (0xDE, 0xAD, 0xBE, 0xEF in this example). This pattern should be replaced with whatever value you seek.
- It iterates over memory regions that are committed, readable, and acceptably protected.
-
ReadProcessMemory:
- This function reads a block of memory from the target process, checking if the pattern exists.
- Depending on the permissions needed and the target system, you might need admin privileges.
-
Logging:
- It logs whether the pattern was found in the process's memory.
Key Considerations:
-
Security and Legal Implications: Interacting with process memory can contravene terms of service and simulate hacking behavior; ensure you have explicit permission and a clear legal basis to do so.
-
Performance and Stability: Reading memory this way, especially in large quantities, can significantly affect performance and stability for large applications.
-
Further Error Handling: The code should include careful handling of potential errors and exceptions to avoid crashes and ensure robust behavior.