Windows Services

Service Binary Hijacking

Steps

  1. Check the services (detect which are in unusual folders)

  2. Check our permissions in those folders

  3. Create a malicious.exe

  4. Remplace the .exe

  5. Restart the service

To get a list of all installed Windows services, we can choose various methods such as the GUI snap-in services.msc, the Get-Service Cmdlet, or the Get-CimInstance Cmdlet (superseding Get-WmiObject).

List of services with binary path

Get-CimInstance -ClassName win32_service | Select Name,State,PathName | Where-Object {$_.State -like 'Running'}

Enumerate the permissions

Next, let's enumerate the permissions on both service binaries. We can choose between the traditional icacls Windows utility or the PowerShell Cmdlet Get-ACL. For this example, we'll use icacls since it usable both in PowerShell and the Windows command line.

Permissions Table

Mask
Permissions

F

Full access

M

Modify access

RX

Read and execute access

R

Read-only access

W

Write-only access

Permissions Abuse

Let's create a small binary on Kali, which we'll use to replace the original .exe

adduser.c

Compiling

Copy the file

Execution

or

If the service StartMode is auto:

Verify that the user have the SeShutdown privilege:

Automated Tools

PowerView

SharpUp

We can use SharpUp from the GhostPack suite of tools to check for service binaries suffering from weak ACLs.

DLL Hijacking

If there is an uncommon path for any service check the permissions after

Check the file locally

  1. Download the .exe of the service

  2. Create the service and start it locally

  1. Open Process Monitor and use this filters:

If .dll is found it is vulnerable

customdll.cpp

Compiling customdll.cpp

Copy the .dll file to the desired path.

Unquoted Service Path

When a service is installed, the registry configuration specifies a path to the binary that should be executed on service start. If this binary is not encapsulated within quotes, Windows will attempt to locate the binary in different folders. Take the example binary path below.

Service Binary Path

Windows will decide the execution method of a program based on its file extension, so it's not necessary to specify it. Windows will attempt to load the following potential executables in order on service start, with a .exe being implied:

  • C:\Program

  • C:\Program Files

  • C:\Program Files (x86)\System

  • C:\Program Files (x86)\System Explorer\service\SystemExplorerService64

Enumerate running and stopped services

Searching for Unquoted Paths (cmd)

Check if we can start / stop the service

Check the permissions to write

Craft the malicious.exe

Use the adduser.c file created in Permissions Abuse

Copy the malicious .exe

Start the service

After the error, check if succesfull. The error stems from the fact that our cross-compiled C code does not accept the parameters that are a leftover of the original service binary path. However, Current.exe was still executed

Automated Tools

PowerView

Scheduled tasks

Check the permissions

Copy the file

Last updated