> Windows Syscalls
ntoskrnl.exeT1012T1082T1497

NtQueryKey

Returns metadata about an open registry key — name, class, subkey/value counts, last-write time.

Prototype

NTSTATUS NtQueryKey(
  HANDLE                KeyHandle,
  KEY_INFORMATION_CLASS KeyInformationClass,
  PVOID                 KeyInformation,
  ULONG                 Length,
  PULONG                ResultLength
);

Arguments

NameTypeDirDescription
KeyHandleHANDLEinHandle to an open key. Required access depends on KeyInformationClass; KEY_QUERY_VALUE suffices for most classes.
KeyInformationClassKEY_INFORMATION_CLASSinVariant requested: KeyBasicInformation, KeyNodeInformation, KeyFullInformation, KeyNameInformation, KeyCachedInformation, KeyVirtualizationInformation, etc.
KeyInformationPVOIDoutCaller-allocated buffer that receives the requested structure.
LengthULONGinSize of KeyInformation in bytes.
ResultLengthPULONGoutReceives the size actually returned (or required, on STATUS_BUFFER_TOO_SMALL).

Syscall IDs by Windows version

Windows versionSyscall IDBuild
Win10 15070x16win10-1507
Win10 16070x16win10-1607
Win10 17030x16win10-1703
Win10 17090x16win10-1709
Win10 18030x16win10-1803
Win10 18090x16win10-1809
Win10 19030x16win10-1903
Win10 19090x16win10-1909
Win10 20040x16win10-2004
Win10 20H20x16win10-20h2
Win10 21H10x16win10-21h1
Win10 21H20x16win10-21h2
Win10 22H20x16win10-22h2
Win11 21H20x16win11-21h2
Win11 22H20x16win11-22h2
Win11 23H20x16win11-23h2
Win11 24H20x16win11-24h2
Server 20160x16winserver-2016
Server 20190x16winserver-2019
Server 20220x16winserver-2022
Server 20250x16winserver-2025

Kernel module

ntoskrnl.exeNtQueryKey

Related APIs

RegQueryInfoKeyWNtEnumerateKeyNtQueryValueKeyNtSetInformationKeyNtQueryMultipleValueKey

Syscall stub

4C 8B D1            mov r10, rcx
B8 16 00 00 00      mov eax, 0x16
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

Notably, NtQueryKey's SSN has been pinned at `0x16` from Windows 10 1507 through Windows 11 24H2 — one of the most stable numbers in the SSDT, which makes a hardcoded SSN safe across builds. The KEY_FULL_INFORMATION variant returns LastWriteTime as a FILETIME, the count of subkeys / values, and the maximum lengths of each — the same numbers `reg query` and Sysinternals `regjump` rely on.

Common malware usage

Three flavors of abuse. (1) *Forensic timestomp detection avoidance*: an operator can read LastWriteTime before tampering with a value, then use NtSetInformationKey (KeyWriteTimeInformation) to restore it. (2) *Enumeration tooling*: registry-walking implants pair NtEnumerateKey + NtQueryKey to recurse a subtree and identify high-value targets (autoruns, services, AppCompat shims). (3) *Sandbox fingerprinting*: query keys like `HKLM\HARDWARE\DESCRIPTION\System\BIOS` and inspect LastWriteTime to spot freshly-built analysis VMs.

Detection opportunities

Standalone NtQueryKey is high-volume background noise — Explorer alone calls it tens of thousands of times an hour. The valuable signal is the *write* that follows: KeyWriteTimeInformation set via NtSetInformationKey is rare in legitimate workloads and almost diagnostic of timestomp. EDRs hooking CmRegisterCallbackEx see RegNtPostQueryKey but most ignore it for cost reasons. ETW Microsoft-Windows-Kernel-Registry RegistryQueryKey is available but disabled by default. Hunting tip: process-handle inventory showing a non-Explorer, non-svchost process holding hundreds of open registry handles is a better lead than any single NtQueryKey call.

Direct syscall examples

cRead LastWriteTime for timestomp staging

// Capture the original LastWriteTime before tampering with a value,
// so it can be restored via NtSetInformationKey afterward.
KEY_BASIC_INFORMATION* kbi = (KEY_BASIC_INFORMATION*)buf;
ULONG ret = 0;
NTSTATUS st = NtQueryKey(hKey, KeyBasicInformation, kbi, sizeof(buf), &ret);
if (NT_SUCCESS(st)) {
    g_savedWriteTime = kbi->LastWriteTime;
}

asmx64 direct stub (stable SSN 0x16)

NtQueryKey PROC
    mov  r10, rcx
    mov  eax, 16h
    syscall
    ret
NtQueryKey ENDP

rustFull information enumeration

// Cargo: ntapi = "0.4"
let mut needed: u32 = 0;
let status = unsafe {
    NtQueryKey(h_key,
        KeyFullInformation,
        null_mut(),
        0,
        &mut needed)
};
assert_eq!(status, STATUS_BUFFER_TOO_SMALL as i32);
let mut buf = vec![0u8; needed as usize];
let status = unsafe {
    NtQueryKey(h_key,
        KeyFullInformation,
        buf.as_mut_ptr() as _,
        buf.len() as u32,
        &mut needed)
};
assert!(status == 0);

MITRE ATT&CK mappings

Last verified: 2026-05-20