> Windows Syscalls
ntoskrnl.exeT1106

NtCompressKey

Forces defragmentation / compaction of a loaded registry hive backing file.

Prototype

NTSTATUS NtCompressKey(
  HANDLE Key
);

Arguments

NameTypeDirDescription
KeyHANDLEinHandle to a registry key inside the hive to be compacted; the entire hive containing this key is rewritten.

Syscall IDs by Windows version

Windows versionSyscall IDBuild
Win10 15070x98win10-1507
Win10 16070x99win10-1607
Win10 17030x9Bwin10-1703
Win10 17090x9Cwin10-1709
Win10 18030x9Dwin10-1803
Win10 18090x9Dwin10-1809
Win10 19030x9Dwin10-1903
Win10 19090x9Dwin10-1909
Win10 20040x9Fwin10-2004
Win10 20H20x9Fwin10-20h2
Win10 21H10x9Fwin10-21h1
Win10 21H20x9Fwin10-21h2
Win10 22H20x9Fwin10-22h2
Win11 21H20xA1win11-21h2
Win11 22H20xA1win11-22h2
Win11 23H20xA1win11-23h2
Win11 24H20xA3win11-24h2
Server 20160x99winserver-2016
Server 20190x9Dwinserver-2019
Server 20220xA1winserver-2022
Server 20250xA3winserver-2025

Kernel module

ntoskrnl.exeNtCompressKey

Related APIs

NtLoadKeyNtLoadKeyExNtSaveKeyNtSaveKeyExNtFlushKey

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 ENDP

cCompact 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