VirtualProtect
BOOL VirtualProtect(
[in] LPVOID lpAddress,
[in] SIZE_T dwSize,
[in] DWORD flNewProtect,
[out] PDWORD lpflOldProtect
);인자
설명
보편적인 값
Example
#include <windows.h>
#include <iostream>
int main() {
DWORD oldProtect = 0;
unsigned char* buffer = (unsigned char*)VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
buffer[0] = 0x41;
std::wcout << "----- Before memory protection -----" << std::endl;
std::cout << "buffer[0] : " << buffer[0] << std::endl;
__try {
unsigned char v1 = 0x42;
RtlMoveMemory(buffer, &v1, sizeof(v1));
std::cout << "[+] memory change success" << std::endl;
}
__except (EXCEPTION_EXECUTE_HANDLER){
std::cout << "[-] memory change failed\n";
}
std::cout << "buffer[0] : " << buffer[0] << std::endl;
std::wcout << "\n\n----- After memory protection -----" << std::endl;
VirtualProtect(buffer, 0x1000, PAGE_READONLY, &oldProtect);
std::cout << "[*] Writing after protect change...\n";
__try {
unsigned char v2 = 0x43;
RtlMoveMemory(buffer, &v2, sizeof(v2));
std::cout << "[-] Unexpected: write succeeded\n";
}
__except (EXCEPTION_EXECUTE_HANDLER) {
std::cout << "[+] Access violation caught -> VirtualProtect works\n";
}
return 0;
}
References
Last updated