Service Binary Hijacking
This attack involves replacing the service binary with a malicious version and restart the service.
Enumerate Services
PowerShell cmdlet
Using Get-CimInstance
with PowerShell to list running services and filter built in services:
When running Get-CimInstance
and Get-Service
via WinRM or a bind shell will result in a "Access denied" error when querying for services with a non-administrative user.
Using an interactive logon such as RDP solves this problem.
Registry
The following one-liner command was taken from evil-winrm source code and uses the registry to enumerate services.
Note: this is very useful when using low privileged user.
Enumerate Binary Permissions
Verifying the permission to modify the binary:
The table below describes the permissions definitions icacls
results:
F
Full access
M
Modify access
RX
Read and execute access
R
Read-only access
W
Write-only access
Creating The Malicious Binary
The C code you provided appears to add a new user <user>
with the password password123!
to the system and elevate it to administrator:
Compiling this C code to create an executable:
Moving the legitimate service's binary, in order to be able to replace with the malicious one:
Transferring the executable from the attacker machine:
Triggering the Execution
In order to trigger the malicious binary execution it's needed to either restart the service manually or If the service is set to start automatically, it possible to reboot the machine to force the service to start. Once the service restarts, the malicious binary will be executed.
Checking the Service Permissions:
Manual restart
Trying to restart the service:
Automatic restart
If restarting the service is not allowed, trying the another method as described above:
Checking if the service set to restart automatically:
Checking if the current user (foothold) has the
SeShutdownPrivilege
permission which allows to reboot the machine
Rebooting the machine
/r
- Reboot instead of shutdown.
/t 0
- Trigger in 0 seconds.
Automated Process
It is possible to automate the process using well known tool called PowerUp
from PowerSploit
Collection:
Downloading PowerUp.ps1:
Bypassing Execution Policy:
Importing PowerUp.ps1:
Finding Modifiable Services using Get-ModifiableServiceFile
is a PowerUp
function that looks for services on the machine whose executables are modifiable by the current user.
Exploiting:
References
Last updated