Persisting in svchost.exe with a Service DLL
This is a quick lab that looks into a persistence mechanism that relies on installing a new Windows service, that will be hosted by an svchost.exe process.
Overview
At a high level, this is how the technique works:
Create a service
EvilSvc.dll
DLL (the DLL that will be loaded into ansvchost.exe
) with the code we want executed on each system rebootCreate a new service
EvilSvc
withbinPath= svchost.exe
Add the
ServiceDll
value toEvilSvc
service and point it to the service DLL compiled in step 1Modify
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost
to specify under which group your service should be loaded intoStart
EvilSvc
serviceThe
EvilSvc
is started and its service DLLEvilSvc.dll
is loaded into ansvchost.exe
Walkthrough
1. Compile Service DLL
First of, let's compile our service DLL as EvilSvc.dll. This DLL is going to be loaded into an svchost.exe
as part of our service EvilSvc
that we will register in a second:
2. Create EvilSvc Service
Let's now create a new service called EvilSvc
and specify the binPath
to be svchost.exe -k DcomLaunch
, which will tell Service Control Manager that we want our EvilSvc
to be hosted by svchost.exe
in a service group called DcomLaunch
:
3. Modify EvilSvc - Specify ServiceDLL Path
Next, inside HKLM\SYSTEM\CurrentControlSet\services\EvilSvc\
, create a new value called ServiceDll
and point it to the EvilSvc.dll service DLL compiled in step 1:
EvilSvc.dll
must exist in C:\Windows\system32\EvilSvc.dll
At this point, our EvilSvc
should be created with all the right parameters as seen in the registry:
4. Group EvilSvc with DcomLaunch
As a final step, we need to tell the Service Control Manager under which service group our EvilSvc
should load.
We want it to get loaded in the DcomLaunch
group, so we need to add our service name EvilSvc
in the list of services in the DcomLaunch
value in HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost
:
5. Start EvilSvc Service
We can now try loading our EvilSvc
service:
EvilSvc
is now loaded into svchost.exe as part of a DcomLauncher
services group:
Detection
Below are some initial thoughts on how one could start hunting for this technique:
Recently created services with
svchost.exe
as abinpath
Listing out ServiceDLL value for all system services and looking for DLLs that are loaded from suspicious locations (i.e non c:\windows\system32):
Get-ItemProperty hklm:\SYSTEM\ControlSet001\Services\*\Parameters | ? { $_.servicedll } | select psparentpath, servicedll
References
Last updated