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
| Name | Type | Dir | Description |
|---|---|---|---|
| KeyHandle | PHANDLE | out | Receives the handle to the opened key on success. |
| DesiredAccess | ACCESS_MASK | in | Access mask, e.g. KEY_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, KEY_ALL_ACCESS. |
| ObjectAttributes | POBJECT_ATTRIBUTES | in | OBJECT_ATTRIBUTES with the NT registry path, e.g. \Registry\Machine\SAM\SAM. |
Syscall IDs by Windows version
| Windows version | Syscall ID | Build |
|---|---|---|
| Win10 1507 | 0x12 | win10-1507 |
| Win10 1607 | 0x12 | win10-1607 |
| Win10 1703 | 0x12 | win10-1703 |
| Win10 1709 | 0x12 | win10-1709 |
| Win10 1803 | 0x12 | win10-1803 |
| Win10 1809 | 0x12 | win10-1809 |
| Win10 1903 | 0x12 | win10-1903 |
| Win10 1909 | 0x12 | win10-1909 |
| Win10 2004 | 0x12 | win10-2004 |
| Win10 20H2 | 0x12 | win10-20h2 |
| Win10 21H1 | 0x12 | win10-21h1 |
| Win10 21H2 | 0x12 | win10-21h2 |
| Win10 22H2 | 0x12 | win10-22h2 |
| Win11 21H2 | 0x12 | win11-21h2 |
| Win11 22H2 | 0x12 | win11-22h2 |
| Win11 23H2 | 0x12 | win11-23h2 |
| Win11 24H2 | 0x12 | win11-24h2 |
| Server 2016 | 0x12 | winserver-2016 |
| Server 2019 | 0x12 | winserver-2019 |
| Server 2022 | 0x12 | winserver-2022 |
| Server 2025 | 0x12 | winserver-2025 |
Kernel module
Related APIs
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 ENDPrustOpen 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