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 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:

  1. Machine-wide setting (requires administrative access to view):

    HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer\AlwaysInstallElevated
  2. User-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

  1. Create a malicious MSI package on your attack machine:

  2. Transfer the MSI package to the target machine (using wget, certutil, etc.)

  3. Set up a listener (if using reverse shell):

  4. 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-AllChecks will identify if AlwaysInstallElevated is enabled

  • WinPEAS: Checks for AlwaysInstallElevated registry keys

  • Metasploit: exploit/windows/local/always_install_elevated

Mitigation

To prevent this vulnerability:

  1. Disable the AlwaysInstallElevated policy by setting both registry keys to 0 or removing them:

  2. 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

  1. Identify a writable autorun program

  2. Replace it with a malicious executable or modify it to execute your payload

  3. 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

  1. Identify a service with vulnerable registry permissions

  2. Modify the ImagePath value to point to your malicious executable

  3. 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

  1. Identify a service with an unquoted path containing spaces

  2. Check write permissions in the directories along the path

  3. 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

  1. Limit registry write permissions for standard users

  2. Use properly quoted service paths

  3. Disable AlwaysInstallElevated policy

  4. Audit registry changes for sensitive keys

  5. Use secure boot and execute prevention mechanisms

  6. Avoid storing credentials in the registry

  7. 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:

  1. Replace the executable with a malicious one

  2. 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:

  1. Always check for AlwaysInstallElevated - it's a quick win if enabled

  2. Autorun entries are another common vector

  3. Remember that registry-based privilege escalation often requires a system reboot or user login, so plan accordingly

  4. 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