Service Exploitation
Windows services often run with high privileges and can be exploited in various ways for privilege escalation. This document covers common techniques to identify and exploit vulnerable services.
Service Commands
Basic Service Management Commands
# Query the configuration of a service
sc qc <service_name>
# Query the current status of a service
sc query <service_name>
# Modify a configuration option of a service
sc config <service_name> <option>= <value>
# Start or stop a service
net start <service_name>
net stop <service_name>
# Alternative start/stop commands
sc start <service_name>
sc stop <service_name>
# List all services
sc query type= service state= all
# List running services
sc queryex type= service state= active
# Display all service dependencies
sc qc <service_name> | findstr "DEPENDENCIES"
# Get the security descriptor of a service
sc sdshow <service_name>PowerShell Service Commands
Understanding Windows Services
Services are managed by the Service Control Manager (SCM) and typically run with SYSTEM, LocalService, NetworkService, or custom service account privileges. Each service has:
An executable path (BINARY_PATH_NAME)
A service account (SERVICE_START_NAME)
A Discretionary Access Control List (DACL) controlling who can modify the service
Service Permission Types
Each service has an Access Control List (ACL) which defines service-specific permissions:
SERVICE_QUERY_CONFIG: Allows querying service configuration (innocuous)
SERVICE_QUERY_STATUS: Allows checking service status (innocuous)
SERVICE_STOP: Allows stopping the service (potentially useful)
SERVICE_START: Allows starting the service (potentially useful)
SERVICE_CHANGE_CONFIG: Allows changing service configuration (dangerous)
SERVICE_ALL_ACCESS: Provides full control over the service (dangerous)
Exploitation Potential
The exploitation potential depends on the combination of permissions you have:
Ideal scenario: Having SERVICE_CHANGE_CONFIG and either SERVICE_STOP or SERVICE_START (or both)
Limited scenario: Having SERVICE_CHANGE_CONFIG but no ability to stop/start
Potential Rabbit Hole: If you can change a service configuration but cannot stop/start the service, you may not be able to escalate privileges immediately. The changes will only take effect when the service is restarted, which might require:
Waiting for a system reboot
Waiting for an automated service restart
Finding another vulnerability to trigger a service restart
Checking Service Configuration
Key information to look for:
BINARY_PATH_NAME: The path to the executable
SERVICE_START_NAME: The account used to run the service
Method 1: Insecure Permissions on Service Executable
If the service's executable has weak permissions allowing modification, we can replace it with a malicious executable.
Identifying Vulnerable Services
Exploitation Steps
Generate a malicious executable:
Create a listener:
Transfer the executable to Windows target (using wget, certutil, etc.)
Replace the service executable:
Restart the service:
Method 2: Unquoted Service Paths
When a service's path contains spaces and isn't enclosed in quotes, Windows will try to execute each valid path with spaces.
Identifying Vulnerable Services
How It Works
For a service with path C:\Program Files\Vulnerable Service\service.exe, Windows tries to execute:
C:\Program.exeC:\Program Files\Vulnerable.exeC:\Program Files\Vulnerable Service\service.exe
Exploitation Steps
Identify a writable directory in the service path:
Generate a malicious executable:
Create a listener:
Place the malicious executable in the path with proper name:
Restart the service:
Method 3: Insecure Service Permissions
If a service's DACL allows modification, we can reconfigure the service to run any executable as SYSTEM.
Identifying Vulnerable Services
Exploitation Steps
Generate a malicious executable:
Create a listener:
Transfer and prepare the executable:
Reconfigure the service:
Restart the service:
Dealing with Start/Stop Restrictions
If you can change the service configuration but cannot start or stop it:
Option 1: Modify the executable to execute at the next system reboot
Option 2: Target a service that automatically restarts when it crashes
Option 3: Look for services that are frequently restarted by the system or users
Real-World Example 1: Exploiting Unquoted Service Path
For a service with configuration like:
Check if we can write to the directory:
The BUILTIN\Users group has write permissions. We can exploit this:
Real-World Example 2: Exploiting Insecure Service Permissions
For a service with insecure DACL:
We can reconfigure it:
Detection and Prevention
To prevent service-based privilege escalation:
Use quotes for service paths with spaces
Restrict write access to service executables and directories
Set proper DACLs on services to prevent reconfiguration
Run services with least privilege accounts
Regularly audit service configurations and permissions
Alternative Payload Generation (Without msfvenom)
If you don't have access to msfvenom, you can create a simple service executable with C# or PowerShell:
Additional Tools For Enumeration
PowerUp.ps1:
Invoke-AllChecksidentifies many common service vulnerabilitiesWinPEAS: Checks for service misconfigurations automatically
ServicePermissionsChecker.ps1: Custom PowerShell script to check service permissions
Last updated