NtProtectVirtualMemory
Changes the protection on a region of committed virtual memory in a target process.
Prototype
NTSTATUS NtProtectVirtualMemory( HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T NumberOfBytesToProtect, ULONG NewAccessProtection, PULONG OldAccessProtection );
Arguments
| Name | Type | Dir | Description |
|---|---|---|---|
| ProcessHandle | HANDLE | in | Handle to the target process. Use NtCurrentProcess() ((HANDLE)-1) for self. |
| BaseAddress | PVOID* | in/out | Pointer to the base address of the region. Aligned down to a page boundary on return. |
| NumberOfBytesToProtect | PSIZE_T | in/out | Pointer to the size in bytes. Rounded up to a page boundary on return. |
| NewAccessProtection | ULONG | in | New memory protection constant, e.g. PAGE_EXECUTE_READ, PAGE_READWRITE. |
| OldAccessProtection | PULONG | out | Receives the previous protection value of the first page in the region. |
Syscall IDs by Windows version
| Windows version | Syscall ID | Build |
|---|---|---|
| Win10 1507 | 0x50 | win10-1507 |
| Win10 1607 | 0x50 | win10-1607 |
| Win10 1703 | 0x50 | win10-1703 |
| Win10 1709 | 0x50 | win10-1709 |
| Win10 1803 | 0x50 | win10-1803 |
| Win10 1809 | 0x50 | win10-1809 |
| Win10 1903 | 0x50 | win10-1903 |
| Win10 1909 | 0x50 | win10-1909 |
| Win10 2004 | 0x50 | win10-2004 |
| Win10 20H2 | 0x50 | win10-20h2 |
| Win10 21H1 | 0x50 | win10-21h1 |
| Win10 21H2 | 0x50 | win10-21h2 |
| Win10 22H2 | 0x50 | win10-22h2 |
| Win11 21H2 | 0x50 | win11-21h2 |
| Win11 22H2 | 0x50 | win11-22h2 |
| Win11 23H2 | 0x50 | win11-23h2 |
| Win11 24H2 | 0x50 | win11-24h2 |
| Server 2016 | 0x50 | winserver-2016 |
| Server 2019 | 0x50 | winserver-2019 |
| Server 2022 | 0x50 | winserver-2022 |
| Server 2025 | 0x50 | winserver-2025 |
Kernel module
Related APIs
Syscall stub
4C 8B D1 mov r10, rcx B8 50 00 00 00 mov eax, 0x50 F6 04 25 08 03 FE 7F 01 test byte ptr [0x7FFE0308], 1 75 03 jne short +3 0F 05 syscall C3 ret CD 2E int 2Eh C3 ret
Undocumented notes
NtProtectVirtualMemory has held SSN `0x50` from Windows 10 1507 through Windows 11 24H2 — one of the most stable syscall numbers on the platform. It is the kernel entry point behind `VirtualProtect`/`VirtualProtectEx` and ultimately reaches `MiProtectVirtualMemory`. Note that the function silently rounds `BaseAddress` down and `NumberOfBytesToProtect` up to page boundaries, so a request that touches even one byte of an adjacent page will reprotect that entire page.
Common malware usage
This is the second half of the canonical loader pattern: a region is allocated as `PAGE_READWRITE`, the payload is written, then `NtProtectVirtualMemory` flips it to `PAGE_EXECUTE_READ` (or `PAGE_EXECUTE_READWRITE` for self-modifying stubs). Switching from RW to RX after a write is the textbook way to defeat naive RWX-only memory scanners while still ending up with executable code. Used in Cobalt Strike beacon loaders, Sliver shellcode runners, Brute Ratel, and most module-stomping and thread-stack-spoofing implementations.
Detection opportunities
RX or RWX transitions on private (non-image) memory are a high-signal indicator. Sysmon does not have a dedicated event for it, but ETW Threat Intelligence (`Microsoft-Windows-Threat-Intelligence`) emits `EtwTiLogProtectExecVm` whenever protection is set to an executable value on private memory — this is exactly the event EDRs subscribe to. Userland ntdll hooks on `NtProtectVirtualMemory` are bypassed by direct or indirect syscalls, but the ETW-TI kernel-side telemetry still fires. PE-Sieve and Moneta detect private RX regions backed by no image as a post-mortem signal.
Direct syscall examples
asmx64 direct stub
; Direct syscall stub for NtProtectVirtualMemory (SSN 0x50, stable across all Win10/11)
NtProtectVirtualMemory PROC
mov r10, rcx ; syscall convention
mov eax, 50h ; SSN
syscall
ret
NtProtectVirtualMemory ENDPcRW -> RX flip after shellcode copy
// Classic two-phase loader: allocate RW, write, flip to RX.
SIZE_T size = payload_len;
PVOID base = NULL;
NtAllocateVirtualMemory(NtCurrentProcess(), &base, 0, &size,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
memcpy(base, payload, payload_len);
ULONG old = 0;
NTSTATUS st = NtProtectVirtualMemory(NtCurrentProcess(), &base, &size,
PAGE_EXECUTE_READ, &old);
if (NT_SUCCESS(st)) ((void(*)())base)();cHell's Gate dynamic lookup
// Resolve SSN at runtime from a non-hooked ntdll copy.
typedef NTSTATUS (NTAPI *pNtProtect)(HANDLE, PVOID*, PSIZE_T, ULONG, PULONG);
DWORD ssn = GetSyscallNumber(GetProcAddress(GetModuleHandleA("ntdll.dll"),
"NtProtectVirtualMemory"));
// hand SSN to your indirect-syscall trampoline; ssn is 0x50 on every supported build
set_ssn(ssn);
indirect_syscall_invoke(/* args */);MITRE ATT&CK mappings
- T1055Process Injection
- T1055.002Portable Executable Injection
- T1620Reflective Code Loading
Last verified: 2026-05-20