Registry Exploits
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 hives
HKEY_CURRENT_USER (HKCU) - Contains user-specific configuration
HKEY_LOCAL_MACHINE (HKLM) - Contains system-wide settings
HKEY_CLASSES_ROOT (HKCR) - File associations and COM objects
HKEY_USERS (HKU) - All user profiles on the system
HKEY_CURRENT_CONFIG (HKCC) - Hardware profile informationAlwaysInstallElevated 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):
HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer\AlwaysInstallElevatedUser-specific setting:
HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer\AlwaysInstallElevated
Note: If either of these registry keys is missing or disabled (set to 0), the exploit will not work.
Checking for Vulnerability
If both queries return a value of 0x1, the system is vulnerable.
PowerShell Alternative
Exploitation Steps
Create a malicious MSI package on your attack machine:
Transfer the MSI package to the target machine (using wget, certutil, etc.)
Set up a listener (if using reverse shell):
Install the malicious MSI package with elevated privileges:
Alternative Methods
Using MSI with Custom Actions
For more complex payloads, you can create a custom MSI package using tools like WiX Toolset:
Using PowerShell Empire
PowerShell Empire includes a module for creating malicious MSI packages:
Automated Enumeration Tools
Several tools can automatically check for this vulnerability:
PowerUp.ps1:
Invoke-AllCheckswill identify if AlwaysInstallElevated is enabledWinPEAS: Checks for AlwaysInstallElevated registry keys
Metasploit:
exploit/windows/local/always_install_elevated
Mitigation
To prevent this vulnerability:
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.
Last updated