RoboDBG
Loading...
Searching...
No Matches
debugger.memory.cpp
1#include "debugger.h"
2
3namespace RoboDBG {
4
5bool Debugger::writeMemory(LPVOID address, const void* buffer, SIZE_T size) {
6 SIZE_T bytesWritten = 0;
7 if (!WriteProcessMemory(hProcessGlobal,address, buffer, size, &bytesWritten) || bytesWritten != size) {
8 std::cerr << "WriteProcessMemory failed: " << GetLastError() << std::endl;
9 return false;
10 }
11 FlushInstructionCache(hProcessGlobal, address, size);
12 return true;
13}
14
15bool Debugger::readMemory( LPVOID address, void* buffer, SIZE_T size) {
16 SIZE_T bytesRead = 0;
17 if (!ReadProcessMemory(hProcessGlobal, address, buffer, size, &bytesRead) || bytesRead != size) {
18 std::cerr << "ReadProcessMemory failed: " << GetLastError() << std::endl;
19 return false;
20 }
21 return true;
22}
23
25 SYSTEM_INFO sysInfo;
26 GetSystemInfo(&sysInfo);
27
28 LPVOID addr = sysInfo.lpMinimumApplicationAddress;
29 MEMORY_BASIC_INFORMATION mbi;
30
31 while (addr < sysInfo.lpMaximumApplicationAddress) {
32 if (VirtualQueryEx(hProcessGlobal, addr, &mbi, sizeof(mbi)) == 0)
33 break;
34
35 BYTE* regionStart = static_cast<BYTE*>(mbi.BaseAddress);
36 BYTE* regionEnd = regionStart + mbi.RegionSize;
37 BYTE* target = static_cast<BYTE*>(baseAddress);
38
39 if (target >= regionStart && target < regionEnd) {
40 return MemoryRegion_t{
41 mbi.BaseAddress,
42 mbi.RegionSize,
43 mbi.State,
44 mbi.Protect,
45 mbi.Type
46 };
47 }
48
49 addr = regionEnd;
50 }
51
52 // Return invalid result
53 return MemoryRegion_t{ nullptr, 0, 0, 0, 0 };
54}
55
56bool Debugger::changeMemoryProtection(MemoryRegion_t page, DWORD newProtect) { //TODO: Use AccessRights enum instead of Memory Protection Constants (https://learn.microsoft.com/en-us/windows/win32/Memory/memory-protection-constants)
57 return changeMemoryProtection(page.BaseAddress, page.RegionSize, newProtect);
58}
59
60bool Debugger::changeMemoryProtection(LPVOID baseAddress, SIZE_T regionSize, DWORD newProtect) {
61 DWORD oldProtect;
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;
66 return true;
67 } else {
68 std::cerr << "[-] Failed to change protection at " << baseAddress
69 << " - Error: " << GetLastError() << std::endl;
70 return false;
71 }
72}
73
74
75// -------------------------------------------------------------
76// Returns all readable, committed memory regions
77// -------------------------------------------------------------
78std::vector<MemoryRegion_t> Debugger::getMemoryPages() {
79 SYSTEM_INFO sysInfo;
80 GetSystemInfo(&sysInfo);
81
82 LPVOID addr = sysInfo.lpMinimumApplicationAddress;
83 MEMORY_BASIC_INFORMATION mbi;
84 std::vector<MemoryRegion_t> regions;
85
86 while (addr < sysInfo.lpMaximumApplicationAddress) {
87 if (VirtualQueryEx(hProcessGlobal, addr, &mbi, sizeof(mbi)) == 0)
88 break;
89
90 MemoryRegion_t region;
91 region.BaseAddress = mbi.BaseAddress;
92 region.RegionSize = mbi.RegionSize;
93 region.State = mbi.State;
94 region.Protect = mbi.Protect;
95 region.Type = mbi.Type;
96
97 regions.push_back(region);
98
99 addr = static_cast<BYTE*>(mbi.BaseAddress) + mbi.RegionSize;
100 }
101
102 return regions;
103}
104
105// -------------------------------------------------------------
106// Searches for a binary pattern in all valid memory regions
107// -------------------------------------------------------------
108std::vector<uintptr_t> Debugger::searchInMemory(const std::vector<BYTE>& pattern) {
109 std::vector<uintptr_t> matches;
110 auto regions = getMemoryPages(); // Ensure correct capitalization if needed
111
112 for (const auto& region : regions) {
113 // Skip regions that are not committed or inaccessible
114 if (region.State != MEM_COMMIT || (region.Protect & PAGE_GUARD) || (region.Protect == PAGE_NOACCESS))
115 continue;
116
117 std::vector<BYTE> buffer(region.RegionSize);
118 SIZE_T bytesRead;
119
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);
124 }
125 }
126 }
127 }
128
129 return matches;
130}
131
132// -------------------------------------------------------------
133// prints all memory pages for debugging reasonss
134// -------------------------------------------------------------
136 SYSTEM_INFO sysInfo;
137 GetSystemInfo(&sysInfo);
138
139 LPVOID addr = sysInfo.lpMinimumApplicationAddress;
140 MEMORY_BASIC_INFORMATION mbi;
141
142 while (addr < sysInfo.lpMaximumApplicationAddress) {
143 if (VirtualQueryEx(hProcessGlobal, addr, &mbi, sizeof(mbi)) == 0)
144 break;
145
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;
151
152 addr = static_cast<BYTE*>(mbi.BaseAddress) + mbi.RegionSize;
153 }
154}
155
156}
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.
Main Debugger file.
Represents a memory region in a process.
Definition debugger.h:103
DWORD Protect
Protection flags (e.g., PAGE_READWRITE).
Definition debugger.h:107
SIZE_T RegionSize
Size of the memory region in bytes.
Definition debugger.h:105
LPVOID BaseAddress
Base address of the memory region.
Definition debugger.h:104
DWORD Type
Type (MEM_IMAGE, MEM_MAPPED, MEM_PRIVATE).
Definition debugger.h:108
DWORD State
State (MEM_COMMIT, MEM_FREE, MEM_RESERVE).
Definition debugger.h:106