Token Impersonation — Identity Theft on Windows

The Token-Based Security Model

On Windows, every process and thread has an associated access token that defines its security identity: user, groups, enabled privileges, and integrity level. When a process accesses a resource (file, registry key, kernel object), the system compares that process's token against the Security Descriptor (DACL/SACL) of the resource.

There are two types of tokens:

  • Primary Token: Associated with the process. Represents the default identity of the process.

  • Impersonation Token: Used by individual threads to temporarily assume another identity.

┌──────────────────────────────────────────────────────────────────────┐
│                  Access Token Structure                              │
│                                                                      │
│  ┌─────────────────────────────────────────────────────────────┐    │
│  │                    ACCESS TOKEN                             │    │
│  │  TokenUser:        S-1-5-21-...-1001 (DOMAIN\User)         │    │
│  │  TokenGroups:      [Administrators, Users, Everyone...]     │    │
│  │  TokenPrivileges:  [SeDebugPrivilege, SeImpersonatePriv...] │    │
│  │  TokenIntegrity:   High (0x3000) / System (0x4000)          │    │
│  │  TokenSessionId:   1                                        │    │
│  │  ImpersonationLevel: SecurityImpersonation (3)              │    │
│  └─────────────────────────────────────────────────────────────┘    │
│                                                                      │
│  Token stolen from SYSTEM process:                                   │
│  ┌─────────────────────────────────────────────────────────────┐    │
│  │  TokenUser:        S-1-5-18 (NT AUTHORITY\SYSTEM)           │    │
│  │  TokenPrivileges:  [SeTcbPrivilege, SeAssignPrimaryToken...] │   │
│  │  TokenIntegrity:   System (0x4000)                          │    │
│  └─────────────────────────────────────────────────────────────┘    │
└──────────────────────────────────────────────────────────────────────┘

Token Impersonation is the technique of obtaining a token from another process (typically one with higher privileges) and using it to execute operations under that process's identity.


Prerequisites: Required Privileges

Privilege
Purpose

SeDebugPrivilege

Opens handles to processes of other users (including SYSTEM)

SeImpersonatePrivilege

Allows impersonating other tokens

SeAssignPrimaryTokenPrivilege

Allows assigning a primary token to a process

SeTcbPrivilege

Allows creating tokens with any SID (TCB = Trusted Computing Base)

A user in the Administrators group typically has SeDebugPrivilege and SeImpersonatePrivilege. Network services and local services have SeImpersonatePrivilege by default.


Technique 1: Token Stealing from SYSTEM Process


Technique 2: Spawn Process with Stolen Token (CreateProcessWithTokenW)

Instead of only impersonating on the current thread, we can create a child process running with the stolen token:


Technique 3: Named Pipe Impersonation

This technique creates a fake named pipe and tricks a privileged process (usually a SYSTEM service) into connecting to it. When the service connects and writes to the pipe, the server can call ImpersonateNamedPipeClient() to assume the client's identity.


Typical Red Team Token Escalation Chain


Red Team Tooling

  • Incognito (integrated in Meterpreter): list_tokens -u / impersonate_token "NT AUTHORITY\SYSTEM"

  • Cobalt Strike: steal_token <pid> / make_token <domain>\<user> <pass>

  • Mimikatz: token::elevate / token::impersonate


References

  • James Forshaw, "Abusing Token Privileges for LPE" — Google Project Zero (2019)

  • harmj0y, "Token Impersonation and UAC Bypass" — harmj0y.net

  • ired.team, "Access Token Manipulation" — ired.team/offensive-security/privilege-escalation/

  • MITRE ATT&CK, "T1134 — Access Token Manipulation" — attack.mitre.org

  • Microsoft Docs, "Access Tokens" — docs.microsoft.com/en-us/windows/win32/secauthz/access-tokens

  • decoder-it, "Juicy Potato — Token Impersonation via SeImpersonatePrivilege" — github.com/ohpe/juicy-potato

  • foxglovesecurity, "Rotten Potato — Privilege Escalation from Service Account to SYSTEM"

  • itm4n, "PrintSpoofer — Impersonating the PrintSpooler" — itm4n.github.io (2020)

Last updated