1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <windows.h> #include <stdio.h>
void Demo() { printf("Demo***************\n"); } int main() { DWORD oldProtect; LPVOID address = (LPVOID)&Demo; printf("%p\n", Demo); unsigned char* new_target = (unsigned char*)VirtualAlloc(NULL, 1024, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); if (new_target == NULL) { printf("Memory allocation failed.\n"); return 1; } unsigned char code[] = { 0x48, 0x31, 0xC0, 0x48, 0xFF, 0xC0, 0xC3, 0x90 }; memcpy(new_target, code, 8); void (*pnew_target)() = (void(*)())new_target;
unsigned char recover[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; unsigned char data[] = { 0xCC,0xCC,0xCC,0xCC,0x90,0x90,0x90,0x90 };
SIZE_T size = sizeof(data); if (VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &oldProtect)) { printf("Memory protection changed successfully.\n"); memcpy(recover, address, sizeof(recover)); memcpy(address, data, sizeof(data)); } else { printf("Failed to change memory protection. Error: %lu\n", GetLastError()); } VirtualProtect(address, size, oldProtect, &oldProtect); __try { Demo(); } __except (EXCEPTION_EXECUTE_HANDLER) { SIZE_T size = sizeof(data); if (VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &oldProtect)) { printf("Memory protection changed successfully.\n");
pnew_target(); memcpy(address, recover, sizeof(data)); Demo();
} else { printf("Failed to change memory protection. Error: %lu\n", GetLastError()); } VirtualProtect(address, size, oldProtect, &oldProtect); }
getchar(); return 0; }
|