| 119 | } |
| 120 | |
| 121 | void PrintMemorySegments() { |
| 122 | printf("\n=== MEMORY SEGMENTS ===\n\n"); |
| 123 | printf("%-12s %-18s %-8s %-18s\n", "Segment", "Address", "Perm", "Size"); |
| 124 | printf("========================================================================\n"); |
| 125 | |
| 126 | MEMORY_BASIC_INFORMATION mbi = { 0 }; |
| 127 | |
| 128 | // Get module handle for the executable |
| 129 | HMODULE hModule = GetModuleHandleA(NULL); |
| 130 | |
| 131 | // Query .text section details |
| 132 | if (hModule && VirtualQuery((LPVOID)hModule, &mbi, sizeof(mbi)) == sizeof(mbi)) { |
| 133 | printf("%-12s 0x%016p %-8s 0x%016zx (%.2f KB)\n", |
| 134 | ".text", |
| 135 | mbi.AllocationBase, |
| 136 | GetProtectionString(mbi.Protect), |
| 137 | mbi.RegionSize, |
| 138 | mbi.RegionSize / 1024.0); |
| 139 | } |
| 140 | |
| 141 | // Query .data segment details |
| 142 | if (VirtualQuery((LPVOID)&g_dummyShellcode, &mbi, sizeof(mbi)) == sizeof(mbi)) { |
| 143 | printf("%-12s 0x%016p %-8s 0x%016zx (%.2f KB)\n", |
| 144 | ".data", |
| 145 | mbi.AllocationBase, |
| 146 | GetProtectionString(mbi.Protect), |
| 147 | mbi.RegionSize, |
| 148 | mbi.RegionSize / 1024.0); |
| 149 | } |
| 150 | |
| 151 | // Query stack details |
| 152 | int stackVar = 0; |
| 153 | if (VirtualQuery((LPVOID)&stackVar, &mbi, sizeof(mbi)) == sizeof(mbi)) { |
| 154 | printf("%-12s 0x%016p %-8s 0x%016zx (%.2f KB)\n", |
| 155 | "Stack", |
| 156 | mbi.AllocationBase, |
| 157 | GetProtectionString(mbi.Protect), |
| 158 | mbi.RegionSize, |
| 159 | mbi.RegionSize / 1024.0); |
| 160 | } |
| 161 | |
| 162 | // Query heap details |
| 163 | LPVOID heapAlloc = HeapAlloc(GetProcessHeap(), 0, 64); |
| 164 | if (heapAlloc) { |
| 165 | if (VirtualQuery(heapAlloc, &mbi, sizeof(mbi)) == sizeof(mbi)) { |
| 166 | printf("%-12s 0x%016p %-8s 0x%016zx (%.2f KB)\n", |
| 167 | "Heap", |
| 168 | mbi.AllocationBase, |
| 169 | GetProtectionString(mbi.Protect), |
| 170 | mbi.RegionSize, |
| 171 | mbi.RegionSize / 1024.0); |
| 172 | } |
| 173 | HeapFree(GetProcessHeap(), 0, heapAlloc); |
| 174 | } |
| 175 | |
| 176 | // Query PEB (Process Environment Block) address |
| 177 | #ifdef _WIN64 |
| 178 | PVOID peb = (PVOID)__readgsqword(0x60); |
no test coverage detected