MCPcopy Create free account
hub / github.com/changeofpace/Self-Remapping-Code / main

Function main

Self-Remapping Code/main.cpp:111–151  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

109}
110
111int main(int argc, char* argv[])
112{
113 const PVOID imagebase = GetModuleHandleA(NULL);
114 if (ValidateSectionAlignment(imagebase))
115 {
116 const DWORD imageSize = GetSizeOfImage(imagebase);
117
118 // Allocate an executable / writable memory region where the remapping code will execute.
119 if (PVOID remapperRegion = VirtualAlloc(NULL, imageSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE))
120 {
121 // Copy SelfRemappingCode.exe's image to this region.
122 memcpy(remapperRegion, imagebase, imageSize);
123
124 // Calculate remap::RemapSelfImage's VA in this region.
125 typedef void(*Remap_t)(PVOID);
126 Remap_t Remap = Remap_t(SIZE_T(remapperRegion) + SIZE_T(remap::RemapSelfImage) - SIZE_T(imagebase));
127
128 // Process execution continues in the new region.
129 Remap(remapperRegion);
130 // Process execution returns to the original (remapped) region.
131
132 VirtualFree(remapperRegion, 0, MEM_RELEASE);
133 TestVirtualProtect(imagebase);
134 for (;;)
135 {
136 printf("Zzz...\n");
137 Sleep(3000);
138 }
139 }
140 else
141 printf("VirtualAlloc failed for remapper region: %d.\n", GetLastError());
142 }
143 else
144 printf("Error: .rdata or .data are not aligned to system allocation granularity.\n");
145
146 // Force-include filler code / data.
147 if (SIZE_T(imagebase) == 1) { filler::text(); const double zxcv = filler::rdata[3]; }
148
149 getchar();
150 return 0;
151}

Callers

nothing calls this directly

Calls 3

ValidateSectionAlignmentFunction · 0.85
GetSizeOfImageFunction · 0.85
TestVirtualProtectFunction · 0.85

Tested by

no test coverage detected