> Windows Syscalls
ntoskrnl.exeT1003.002T1003.004T1012

NtOpenKey

Opens an existing registry key — the kernel entry behind RegOpenKeyEx, used to reach SAM, SECURITY and persistence hives.

Prototype

NTSTATUS NtOpenKey(
  PHANDLE            KeyHandle,
  ACCESS_MASK        DesiredAccess,
  POBJECT_ATTRIBUTES ObjectAttributes
);

Arguments

NameTypeDirDescription
KeyHandlePHANDLEoutReceives the handle to the opened key on success.
DesiredAccessACCESS_MASKinAccess mask, e.g. KEY_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, KEY_ALL_ACCESS.
ObjectAttributesPOBJECT_ATTRIBUTESinOBJECT_ATTRIBUTES with the NT registry path, e.g. \Registry\Machine\SAM\SAM.

Syscall IDs by Windows version

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

Kernel module

ntoskrnl.exeNtOpenKey

Related APIs

RegOpenKeyExWRegOpenCurrentUserNtOpenKeyExNtOpenKeyTransactedNtCreateKeyNtQueryValueKey

Syscall stub

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

The minimal-arity registry open — three arguments, SSN `0x12` since the dawn of NT. The NT registry namespace lives under `\Registry`, with `\Registry\Machine` mapping to HKLM and `\Registry\User\<SID>` to per-user hives; advapi32's `RegOpenKeyEx` rewrites `HKEY_*` predefined handles to these paths before calling NtOpenKey. There is no `OpenOptions` parameter — for that you need `NtOpenKeyEx`. Callers who need to read SAM/SECURITY must first impersonate SYSTEM (`NtOpenProcessToken` on lsass + `NtDuplicateToken` + `SetThreadToken`) because the hive ACL excludes Administrators by default.

Common malware usage

Gateway to **registry credential dumping**. The canonical chains are: (a) **T1003.002 SAM hive** — open `\Registry\Machine\SAM\SAM\Domains\Account\Users\Names\<user>` to enumerate users, then walk `\...\Users\<RID>\V` for the encrypted password hash, decrypting with the BootKey assembled from four `\Registry\Machine\System\CurrentControlSet\Control\Lsa\{JD,Skew1,GBG,Data}` `Class` attributes; (b) **T1003.004 LSA secrets** — open `\Registry\Machine\SECURITY\Policy\Secrets\*\CurrVal` for cached service-account passwords and DPAPI domain backup keys; (c) **persistence enumeration** — open Run keys, IFEO, AppInit_DLLs, Winlogon Notify, services hive. Mimikatz' `lsadump::sam` and `lsadump::secrets` execute exactly this path, as do secretsdump.py from Impacket and `reg.exe save HKLM\SAM <file>` followed by offline parsing.

Detection opportunities

Microsoft-Windows-Kernel-Registry ETW (`{70EB4F03-C1DE-4F73-A051-33D13D5413BD}`) emits an event per registry open with full path, requesting PID, and access mask. Sysmon Event ID 12 (RegistryEvent, CreateKey/SetKey) and Event ID 13 (SetValue) cover writes; the **read side requires explicit Sysmon RegistryEvent type=CreateKey filtering on `HKLM\SAM`, `HKLM\SECURITY`, `HKLM\Security\Policy\Secrets`** — these are the high-fidelity dump indicators. EDRs hook `NtOpenKey`/`NtOpenKeyEx` in the kernel via `CmRegisterCallbackEx` (RegNtPreOpenKeyEx / RegNtPostOpenKey notifications). Defender's ASR rule "Block credential stealing from the Windows local security authority subsystem (lsass.exe)" indirectly raises the cost of the LSA-token-theft prelude. Hunt for any non-LSASS/non-svchost process opening `\Registry\Machine\SAM` or `\Registry\Machine\SECURITY`.

Direct syscall examples

cOpen Run key for persistence read

UNICODE_STRING name;
RtlInitUnicodeString(&name,
    L"\\Registry\\Machine\\Software\\Microsoft\\Windows\\CurrentVersion\\Run");

OBJECT_ATTRIBUTES oa;
InitializeObjectAttributes(&oa, &name, OBJ_CASE_INSENSITIVE, NULL, NULL);

HANDLE hKey = NULL;
NTSTATUS s = NtOpenKey(&hKey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &oa);

asmDirect stub (SSN 0x12)

NtOpenKey PROC
    mov  r10, rcx
    mov  eax, 12h
    syscall
    ret
NtOpenKey ENDP

rustOpen key under impersonation context

use ntapi::ntregapi::NtOpenKey;
use ntapi::ntrtl::RtlInitUnicodeString;
use winapi::shared::ntdef::{HANDLE, OBJECT_ATTRIBUTES, OBJ_CASE_INSENSITIVE, UNICODE_STRING};

pub unsafe fn open_key(path: *const u16, access: u32) -> Option<HANDLE> {
    let mut us: UNICODE_STRING = core::mem::zeroed();
    RtlInitUnicodeString(&mut us, path);
    let mut oa = OBJECT_ATTRIBUTES {
        Length: core::mem::size_of::<OBJECT_ATTRIBUTES>() as u32,
        RootDirectory: core::ptr::null_mut(),
        ObjectName: &mut us,
        Attributes: OBJ_CASE_INSENSITIVE,
        SecurityDescriptor: core::ptr::null_mut(),
        SecurityQualityOfService: core::ptr::null_mut(),
    };
    let mut h: HANDLE = core::ptr::null_mut();
    if NtOpenKey(&mut h, access, &mut oa) == 0 { Some(h) } else { None }
}

MITRE ATT&CK mappings

Last verified: 2026-05-20