6 SIZE_T bytesWritten = 0;
7 if (!WriteProcessMemory(hProcessGlobal,address, buffer, size, &bytesWritten) || bytesWritten != size) {
8 std::cerr <<
"WriteProcessMemory failed: " << GetLastError() << std::endl;
11 FlushInstructionCache(hProcessGlobal, address, size);
17 if (!ReadProcessMemory(hProcessGlobal, address, buffer, size, &bytesRead) || bytesRead != size) {
18 std::cerr <<
"ReadProcessMemory failed: " << GetLastError() << std::endl;
26 GetSystemInfo(&sysInfo);
28 LPVOID addr = sysInfo.lpMinimumApplicationAddress;
29 MEMORY_BASIC_INFORMATION mbi;
31 while (addr < sysInfo.lpMaximumApplicationAddress) {
32 if (VirtualQueryEx(hProcessGlobal, addr, &mbi,
sizeof(mbi)) == 0)
35 BYTE* regionStart =
static_cast<BYTE*
>(mbi.BaseAddress);
36 BYTE* regionEnd = regionStart + mbi.RegionSize;
37 BYTE* target =
static_cast<BYTE*
>(baseAddress);
39 if (target >= regionStart && target < regionEnd) {
62 if (VirtualProtectEx(hProcessGlobal, baseAddress, regionSize, newProtect, &oldProtect)) {
63 std::cout <<
"[+] Changed protection at " << baseAddress
64 <<
" from 0x" << std::hex << oldProtect
65 <<
" to 0x" << newProtect << std::endl;
68 std::cerr <<
"[-] Failed to change protection at " << baseAddress
69 <<
" - Error: " << GetLastError() << std::endl;
80 GetSystemInfo(&sysInfo);
82 LPVOID addr = sysInfo.lpMinimumApplicationAddress;
83 MEMORY_BASIC_INFORMATION mbi;
84 std::vector<MemoryRegion_t> regions;
86 while (addr < sysInfo.lpMaximumApplicationAddress) {
87 if (VirtualQueryEx(hProcessGlobal, addr, &mbi,
sizeof(mbi)) == 0)
93 region.
State = mbi.State;
95 region.
Type = mbi.Type;
97 regions.push_back(region);
99 addr =
static_cast<BYTE*
>(mbi.BaseAddress) + mbi.RegionSize;
109 std::vector<uintptr_t> matches;
112 for (
const auto& region : regions) {
114 if (region.State != MEM_COMMIT || (region.Protect & PAGE_GUARD) || (region.Protect == PAGE_NOACCESS))
117 std::vector<BYTE> buffer(region.RegionSize);
120 if (ReadProcessMemory(hProcessGlobal, region.BaseAddress, buffer.data(), region.RegionSize, &bytesRead)) {
121 for (SIZE_T i = 0; i + pattern.size() <= bytesRead; ++i) {
122 if (memcmp(buffer.data() + i, pattern.data(), pattern.size()) == 0) {
123 matches.push_back(
reinterpret_cast<uintptr_t
>(region.BaseAddress) + i);
137 GetSystemInfo(&sysInfo);
139 LPVOID addr = sysInfo.lpMinimumApplicationAddress;
140 MEMORY_BASIC_INFORMATION mbi;
142 while (addr < sysInfo.lpMaximumApplicationAddress) {
143 if (VirtualQueryEx(hProcessGlobal, addr, &mbi,
sizeof(mbi)) == 0)
146 std::cout <<
"BaseAddr: " << mbi.BaseAddress
147 <<
" | RegionSize: " << mbi.RegionSize
148 <<
" | State: " << std::hex << mbi.State
149 <<
" | Protect: " << mbi.Protect
150 <<
" | Type: " << mbi.Type << std::endl;
152 addr =
static_cast<BYTE*
>(mbi.BaseAddress) + mbi.RegionSize;
MemoryRegion_t getPageByAddress(LPVOID baseAddress)
Gets information for the page containing an address.
std::vector< uintptr_t > searchInMemory(const std::vector< BYTE > &pattern)
Scans process memory for a byte pattern.
std::vector< MemoryRegion_t > getMemoryPages()
Enumerates readable/committed pages of the process.
void PrintMemoryPages()
Prints a formatted list of memory pages (debug helper).
bool readMemory(LPVOID address, void *buffer, SIZE_T size)
Reads raw bytes from target memory.
bool writeMemory(LPVOID address, const void *buffer, SIZE_T size)
Writes raw bytes to target memory.
bool changeMemoryProtection(LPVOID baseAddress, SIZE_T regionSize, DWORD newProtect)
Changes memory protection on a region.
Represents a memory region in a process.
DWORD Protect
Protection flags (e.g., PAGE_READWRITE).
SIZE_T RegionSize
Size of the memory region in bytes.
LPVOID BaseAddress
Base address of the memory region.
DWORD Type
Type (MEM_IMAGE, MEM_MAPPED, MEM_PRIVATE).
DWORD State
State (MEM_COMMIT, MEM_FREE, MEM_RESERVE).