NtCompressKey
Forces defragmentation / compaction of a loaded registry hive backing file.
Prototype
NTSTATUS NtCompressKey( HANDLE Key );
Arguments
| Name | Type | Dir | Description |
|---|---|---|---|
| Key | HANDLE | in | Handle to a registry key inside the hive to be compacted; the entire hive containing this key is rewritten. |
Syscall IDs by Windows version
| Windows version | Syscall ID | Build |
|---|---|---|
| Win10 1507 | 0x98 | win10-1507 |
| Win10 1607 | 0x99 | win10-1607 |
| Win10 1703 | 0x9B | win10-1703 |
| Win10 1709 | 0x9C | win10-1709 |
| Win10 1803 | 0x9D | win10-1803 |
| Win10 1809 | 0x9D | win10-1809 |
| Win10 1903 | 0x9D | win10-1903 |
| Win10 1909 | 0x9D | win10-1909 |
| Win10 2004 | 0x9F | win10-2004 |
| Win10 20H2 | 0x9F | win10-20h2 |
| Win10 21H1 | 0x9F | win10-21h1 |
| Win10 21H2 | 0x9F | win10-21h2 |
| Win10 22H2 | 0x9F | win10-22h2 |
| Win11 21H2 | 0xA1 | win11-21h2 |
| Win11 22H2 | 0xA1 | win11-22h2 |
| Win11 23H2 | 0xA1 | win11-23h2 |
| Win11 24H2 | 0xA3 | win11-24h2 |
| Server 2016 | 0x99 | winserver-2016 |
| Server 2019 | 0x9D | winserver-2019 |
| Server 2022 | 0xA1 | winserver-2022 |
| Server 2025 | 0xA3 | winserver-2025 |
Kernel module
Related APIs
Syscall stub
4C 8B D1 mov r10, rcx B8 A3 00 00 00 mov eax, 0xA3 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
`NtCompressKey` is a maintenance primitive that rewrites the on-disk hive file containing the supplied key to reclaim space left by deleted cells. It is the kernel side of what `regedit` does when it offers to compact a hive on import. The implementation in `ntoskrnl.exe` (`CmpCompressKey` → `CmpDoCompressKey`) walks the cell map, builds a new contiguous hive file, fsyncs it, and atomically swaps it in. No public Win32 wrapper exists — consumers must `GetProcAddress("NtCompressKey")` from `ntdll.dll`.
Common malware usage
Effectively no offensive signal. The syscall does not read, write or expose hive contents in any way that helps an attacker, and it requires a writable handle to an already-loaded hive — anyone with that already has full read/write to the data. Occasionally seen in red-team tooling as part of *cleanup* — after an attacker has loaded an offline hive, harvested data, and dropped temporary keys, a final `NtCompressKey` shrinks the file back to a normal size so post-incident timeline reviewers don't notice an oversized hive. That is at most an anti-forensics curiosity.
Detection opportunities
Not worth detecting on its own — false-positive volume from legitimate registry-maintenance tools (`regedit /a`, third-party hive-compactors, some OEM imaging utilities) is essentially the entire signal. If you must, the ETW `Microsoft-Windows-Kernel-Registry` provider emits a HiveCompression event and `Microsoft-Windows-Kernel-File` will show the temporary `.compress` / `.tmp` hive file being written next to the original. Sysmon does not surface this directly.
Direct syscall examples
asmx64 direct stub (Win11 24H2)
; Direct syscall stub for NtCompressKey (SSN 0xA3 on Win11 24H2)
NtCompressKey PROC
mov r10, rcx ; Key
mov eax, 0A3h ; SSN — drifts per build
syscall
ret
NtCompressKey ENDPcCompact a hive after offline editing
// No public Win32 wrapper — resolve from ntdll directly.
#include <windows.h>
#include <winternl.h>
typedef NTSTATUS (NTAPI *pNtCompressKey)(HANDLE);
NTSTATUS CompactHive(HKEY hMountedHiveRoot) {
pNtCompressKey fn = (pNtCompressKey)GetProcAddress(
GetModuleHandleA("ntdll.dll"), "NtCompressKey");
return fn ? fn((HANDLE)hMountedHiveRoot) : STATUS_NOT_IMPLEMENTED;
}MITRE ATT&CK mappings
Last verified: 2026-05-20