Skip to content

Windows Services: Persistence Through the SCM

How Windows services give SYSTEM persistence at boot, how attackers create or hijack them, and how to find, check and remove a malicious service.

MITRE ATT&CK
T1543.003
Tactics
Persistence, Privilege escalation
Runs
At boot
Set up by
Administrators
Locations
  • HKLM\SYSTEM\CurrentControlSet\Services
  • HKLM\SYSTEM\CurrentControlSet\Services\*\Parameters
  • HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost
  • HKLM\SYSTEM\Select

How it works

The Service Control Manager (services.exe) starts early in boot and reads every subkey of HKLM\SYSTEM\CurrentControlSet\Services. Each subkey with a Type value is a service or a driver. For a service, ImagePath is the command line SCM runs, ObjectName the account it runs as (LocalSystem when the value is absent), and Start says when:

StartMeaning
0, 1Boot and system start, for drivers only (see Kernel drivers)
2Automatic: started at boot, before anyone logs on; with DelayedAutostart = 1, shortly after the other automatic services
3Manual: started on request, by a service that depends on it, or by a trigger in the TriggerInfo subkey (device arrival, network, domain join, an ETW event…)
4Disabled

Type says how the service is hosted. 0x10 is a program in its own process. 0x20 is a DLL inside a shared svchost.exe: ImagePath is then %SystemRoot%\System32\svchost.exe -k <group>, the DLL is named by ServiceDll in the Parameters subkey (sometimes in the service key itself), and svchost calls its ServiceMain export. Since Windows 10 1703, machines with more than 3.5 GB of memory give most shared services their own svchost process; the registration does not change. 0x50 and 0x60 are per-user service templates: Windows creates an instance with a suffix (CDPUserSvc_3a5f1) for each user who signs in.

Recovery settings tell SCM what to do when the service stops unexpectedly: restart it, reboot, or run the program in FailureCommand (set with sc failure).

SCM builds its database from the registry at boot. A service created through SCM (sc create, New-Service, the CreateService API) exists at once and is logged; one written straight into the registry is only picked up at the next boot, and no "service installed" event is written.

Where it lives

LocationWhat
HKLM\SYSTEM\CurrentControlSet\Services\<name>ImagePath, Start, Type, ObjectName, DisplayName, Description, DelayedAutostart, FailureActions, FailureCommand; subkeys Parameters, TriggerInfo, Security
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SvchostOne multi-string value per svchost group, listing its services
HKLM\SYSTEM\SelectCurrent names the ControlSet00n that CurrentControlSet points to

Services are machine-wide only: there is no per-user or WOW6432Node variant. Offline, the SYSTEM hive has no CurrentControlSet; read Select\Current and open that control set.

A stock Windows 10 or 11 system has several hundred subkeys, services and drivers mixed. Windows' own service programs and DLLs sit in C:\Windows\System32; Defender's run from C:\ProgramData\Microsoft\Windows Defender\Platform. Third-party services normally run from C:\Program Files.

Administrators and SYSTEM can write the Services key; standard users can only read it, and creating a service through SCM needs administrator rights. A service whose key, program or folder a standard user can write is a local privilege escalation path rather than a persistence one.

How attackers use it

A new service that runs the attacker's program as SYSTEM at every boot is the classic form:

sc.exe create WinHostSvc binPath= "C:\ProgramData\WinHost\svchost.exe" start= auto obj= LocalSystem
New-Service -Name WinHostSvc -BinaryPathName 'C:\ProgramData\WinHost\svchost.exe' -StartupType Automatic

Remote execution tools create services too. PsExec installs PSEXESVC; Impacket's psexec and smbexec, Metasploit's psexec modules and Cobalt Strike's jump psexec register a service whose ImagePath is a program copied to ADMIN$ or a %COMSPEC% one-liner; for PowerShell payloads, %COMSPEC% /b /c start /b /min powershell -nop -w hidden -encodedcommand …. When the tool does not clean up, the service stays behind.

Quieter variants change what an existing service runs:

  • Repoint a rarely used Windows service: sc.exe config <name> binPath= "…" start= auto. No new service appears, and at most a start-type change (7040) is logged.
  • Register a ServiceDll service in an existing svchost group such as netsvcs: process listings show only svchost.exe, and the payload is a DLL.
  • Add a recovery command to a service that crashes on purpose: sc.exe failure <name> reset= 0 actions= run/60000 command= "C:\ProgramData\x.exe".

Disguise is mostly in the names: display names and descriptions copied from real Windows services, programs named svchost.exe or taskhostw.exe outside System32, or random names left by tooling.

How to check a host

On a live machine (use sc.exe in PowerShell, where sc is Set-Content):

sc.exe queryex type= service state= all
sc.exe qc WinHostSvc
sc.exe qfailure WinHostSvc
sc.exe qtriggerinfo WinHostSvc
reg query HKLM\SYSTEM\CurrentControlSet\Services\WinHostSvc /s
Get-CimInstance Win32_Service | Select-Object Name, StartMode, State, StartName, PathName
autorunsc.exe -accepteula -a s -h -s -c > services.csv

Win32_Service shows svchost.exe for shared services; read Parameters\ServiceDll for the DLL that really runs.

Offline, open the SYSTEM hive, follow Select\Current, and read Services. A service key's last-write time changes when any of its values changes. Windows updates rewrite many of Windows' own keys, so their times are noisy; on an unknown service the time often dates its installation or last reconfiguration.

Event logs:

  • System log 7045: a service was installed (name, ImagePath, type, start type, account). 7040: start type changed.
  • Security log 4697: a service was installed, when "Audit Security System Extension" is enabled.
  • 4657 when the key has an auditing SACL, or Sysmon events 12 and 13 for registry writes: the only trace of a service written straight into the registry.

Suspicious: a program in ProgramData, a profile, a temporary folder or a share; Windows program names outside System32; a ServiceDll outside System32; cmd, powershell, rundll32 or mshta in ImagePath; a FailureCommand with a "Run a program" recovery action; no description and no vendor; or a Windows service whose ImagePath or ServiceDll differs from a clean machine of the same build.

What Windows Persistence flags

The tool reads every subkey of Services in the current control set (the one Select\Current names) that has a Type, and an ImagePath or ServiceDll unless it is a driver. Kernel and file system driver types become Kernel drivers entries; the rest are services. Each entry shows the display name, start type (with "delayed" and "+ trigger"), type, account, ServiceDll, group, description and failure command. Start 0 to 2 counts as a boot trigger, a TriggerInfo subkey as an event, anything else as on demand; Start 4 marks the entry disabled.

The program is the ServiceDll when there is one, else the program ImagePath starts. On the service:

  • The command-line rules run on ImagePath in system context, and the path rules on ServiceDll: System-wide autostart from a writable folder, Runs from a temporary or public folder, Runs from a network share, Windows program name in the wrong place, Name one letter away from a Windows program, Windows program used to run outside code, Encoded PowerShell command, Downloads and runs code, Runs with a hidden window, Machine-made name and the others.
  • Shared-process service DLL outside Windows' folders: medium when the ServiceDll is under C:\Program Files or C:\ProgramData\Microsoft, high anywhere else outside Windows' folders. A DLL in a folder users can write raises System-wide autostart from a writable folder instead.
  • Unquoted service path with spaces: an unquoted ImagePath whose program path, environment variables expanded, starts with a drive letter and contains a space.
  • Runs a program when the service fails: medium when FailureCommand is set and one of the recovery actions in FailureActions is "Run a program". Otherwise the command is information only, shown with "(no recovery action runs it)".

Across mechanisms: Written date when the key changed in the 7 days before the newest time seen for that host, Inside a Defender exclusion (exclusion), and program also runs from mechanisms.

There is no list of Windows' service names. A service whose program sits in Windows' folders and raises no flag is treated as Windows' own: "Hide Windows entries" hides it, and its key time is not checked for recency.

Limits: no signature or hash check, and no running state. The tool does not read the Security subkey, the svchost group lists, or control sets other than the current one.

Cleaning up

  1. Preserve: reg export HKLM\SYSTEM\CurrentControlSet\Services\WinHostSvc svc.reg, copy the program or DLL, hash it with Get-FileHash, and keep the sc.exe qc and sc.exe qfailure output.
  2. Stop and delete: sc.exe stop WinHostSvc, then sc.exe delete WinHostSvc (or Remove-Service in PowerShell 6 and later). While a handle is open, the service is only marked for deletion; it goes once the handles close, or at reboot.
  3. For a hijacked Windows service, do not delete it: restore ImagePath, ServiceDll and Start from a clean machine of the same build, and remove any name added to a svchost group.
  4. Delete the payload and its folder. Look for the same program in Run keys, scheduled tasks, WMI subscriptions and other services.
  5. A service that ran as SYSTEM had full control of the machine. Change the passwords of accounts that services run as (SCM keeps them as LSA secrets) and of every account used on the host; consider rebuilding it.

See also

On Windows Forensics

The artifacts behind this technique, in the Windows Forensics reference: