TExcelTAD
Home › Windows & Active Directory Security › Services and Service Accounts

Services and Service Accounts

A Windows service is a program the Service Control Manager (SCM) starts without anyone signing in: at boot, on demand, or when a trigger fires. Services run backups, databases, agents and print queues, and they run with an identity: often LocalSystem, more powerful than any administrator, and in domains often a domain user account with a password that someone typed into a dialog years ago. That makes services a double target. Whoever can change a service's program runs code as its identity, and whoever learns a service account's password can use it everywhere the account has rights. This lesson reviews both, using a synthetic export of a server's services.

Service basics (states, start types, sc.exe, Get-Service) are in Windows Services and PowerShell on Windows; the Registry location HKLM\SYSTEM\CurrentControlSet\Services is in The Windows Registry and Event Log.

Service identities, from most to least dangerous

Identity (StartName) Network identity Risk
a member of Domain Admins itself, everywhere a compromise of this one server is a compromise of the domain; never acceptable
a domain user account with a password (CORP\svc-sql) itself password often old, never rotated, reused on several servers; if it has an SPN it is Kerberoastable (Kerberoasting)
LocalSystem the computer account (SQL01$) full control of the machine; fine for OS components, too much for most applications
NT AUTHORITY\NetworkService / LocalService computer account / anonymous low privilege, but shared by many services and holding SeImpersonatePrivilege
virtual account NT SERVICE\<name> computer account one identity per service, no password to manage: the default for local-only needs
group managed service account CORP\gmsa-sql$ itself long random password generated and rotated by AD automatically: the right choice for domain services (LAPS and gMSA)

Misconfigurations a reviewer looks for

  1. Unquoted path with spaces. For C:\Program Files\Lab Agent\agent.exe without quotes, the SCM tries C:\Program.exe, then C:\Program Files\Lab.exe, then the real file. If any of those earlier locations is writable by users, a planted program runs as the service. Quoting the path removes the ambiguity.
  2. Writable program or folder. If ordinary users can modify the service's executable or its folder, they can replace it. Typical cause: software installed into C:\Tools or C:\Users\Public, which inherit user-writable ACLs.
  3. Weak service ACL. The service object itself has a security descriptor; if Authenticated Users may change its configuration (SERVICE_CHANGE_CONFIG), anyone can point it at another program. Sysinternals AccessChk and sc.exe sdshow show it.
  4. Over-privileged identity: a domain admin or a reused user account, as in the table.

Reviewing an export

The input below is shaped like Get-CimInstance Win32_Service | Select-Object Name, PathName, StartName, StartMode | ConvertTo-Json run on the lab's SQL01.corp.lab.test, plus a list of folders that the review found writable by BUILTIN\Users (in a real review that list comes from AccessChk or Get-Acl). The script applies the four checks and prints one line per finding.

powershell · try it yourself
$services = @'
[
 {"Name":"MSSQLSERVER","PathName":"\"C:\\Program Files\\Microsoft SQL Server\\sqlservr.exe\" -sMSSQLSERVER","StartName":"CORP\\svc-sql","StartMode":"Auto"},
 {"Name":"LabAgent","PathName":"C:\\Program Files\\Lab Agent\\agent.exe --service","StartName":"LocalSystem","StartMode":"Auto"},
 {"Name":"ReportSync","PathName":"C:\\Tools\\reportsync.exe","StartName":"CORP\\adm-reports","StartMode":"Auto"},
 {"Name":"Spooler","PathName":"C:\\Windows\\System32\\spoolsv.exe","StartName":"LocalSystem","StartMode":"Auto"},
 {"Name":"WebCache","PathName":"C:\\Windows\\System32\\svchost.exe -k webcache","StartName":"NT SERVICE\\WebCache","StartMode":"Manual"},
 {"Name":"BackupAgent","PathName":"\"C:\\Program Files\\Backup\\bkagent.exe\"","StartName":"CORP\\gmsa-backup
quot;,"StartMode":"Auto"} ] '@ | ConvertFrom-Json $userWritable = @('C:\Tools', 'C:\Program Files\Lab Agent') # from the ACL review $domainAdmins = @('CORP\adm-reports') # from the group export function Get-Exe([string]$path) { if ($path.StartsWith('"')) { return $path.Substring(1, $path.IndexOf('"', 1) - 1) } if ($path -match '^(.+?\.exe)') { return $Matches[1] } return $path } foreach ($s in $services | Sort-Object Name) { $exe = Get-Exe $s.PathName $findings = @() if (-not $s.PathName.StartsWith('"') -and $exe.Contains(' ')) { $findings += 'unquoted path with spaces' } $dir = $exe.Substring(0, $exe.LastIndexOf('\')) # Split-Path would use '/' on Linux if ($userWritable -contains $dir) { $findings += "program folder writable by Users ($dir)" } if ($domainAdmins -contains $s.StartName) { $findings += 'runs as a Domain Admin' } elseif ($s.StartName -like 'CORP\*' -and -not $s.StartName.EndsWith(' )) { $findings += 'domain user account with a password' } $status = if ($findings) { 'FINDING' } else { 'ok' } "{0,-12} {1,-20} {2,-7} {3}" -f $s.Name, $s.StartName, $status, ($findings -join '; ') }
BackupAgent  CORP\gmsa-backup$    ok
LabAgent     LocalSystem          FINDING unquoted path with spaces; program folder writable by Users (C:\Program Files\Lab Agent)
MSSQLSERVER  CORP\svc-sql         FINDING domain user account with a password
ReportSync   CORP\adm-reports     FINDING program folder writable by Users (C:\Tools); runs as a Domain Admin
Spooler      LocalSystem          ok
WebCache     NT SERVICE\WebCache  ok

LabAgent combines two findings into a direct path to SYSTEM: the folder C:\Program Files\Lab Agent is user-writable and the path is unquoted. ReportSync is worse for the domain: anyone who can replace C:\Tools\reportsync.exe runs code as a domain admin, and the account's password sits in the machine's LSA secrets (LSASS and stored credentials). MSSQLSERVER is not broken, but its user account is a Kerberoasting candidate. BackupAgent (a gMSA, name ending in $) and WebCache (virtual account) are the patterns to copy.

How it is detected

  • 7045 in the System log (Service Control Manager): a new service was installed, with its name, path, start type and account. 4697 in the Security log says the same when "Audit Security System Extension" is on. A new service whose path points into C:\Users\, C:\ProgramData\, \\host\share or contains cmd.exe /c or powershell -enc deserves an alert.
  • Remote administration tools leave typical services: Sysinternals PsExec installs a service called PSEXESVC (renameable with an option), and the Impacket library's psexec-style examples install a service with a random name that runs a command and is removed again. To a defender both look like a 7045 for a short-lived service, created over the network (a 4624 logon type 3 from the same source just before), often with the ADMIN$ share written (5145).
  • Changes to existing services: Sysmon event 13 (registry value set) on ...\Services\<name>\ImagePath, or 4657 with a registry SACL. sc.exe config against a remote host shows up the same way.
  • Service account logons where they don't belong: 4624 for svc-sql with logon type 2 or 10 (interactive, RDP) or from a workstation is a strong signal, because service accounts should only ever log on as type 5 (service) on their own servers.

How it is fixed or prevented

  • Quote every service path; remove write access for Users from program folders (install under C:\Program Files, whose default ACL is admin-only).
  • Replace user accounts with gMSAs or virtual accounts. Where a user account must stay, give it a long random password, rotate it, deny it interactive and RDP logon (User Rights Assignment: "Deny log on locally", "Deny log on through Remote Desktop Services") and remove it from every admin group.
  • Never run a service as a domain admin. If a tool "needs" domain admin, find the exact rights it needs and delegate those.
  • Disable services you don't need. On domain controllers that includes the Print Spooler, which has a long record of vulnerabilities and is a coercion vector (NTLM Relay, Coercion and Signing).
  • Allow-list software (App Control for Business) so a planted Program.exe can't run even if it lands.

Pitfalls

  • Checking only PathName. Service DLLs (ServiceDll under ...\Parameters for svchost services) and the folders on the DLL search path matter too.
  • Fixing the ACL but not the account. A writable folder is a local escalation; a domain admin service account is a domain compromise. Fix the second first.
  • Assuming $ means gMSA. Computer accounts end with $ too, and attackers create user accounts with a trailing $ to blend in. Check the object class (msDS-GroupManagedServiceAccount).
  • Alert fatigue on 7045. Software updates install services all the time. Baseline per host and alert on unusual paths and accounts rather than on every event.

Practice

1. For the unquoted path C:\Program Files\Lab Agent\agent.exe, list the executables the SCM tries, in order.

C:\Program.exe, C:\Program Files\Lab.exe, then C:\Program Files\Lab Agent\agent.exe. In the lab only the folder C:\Program Files\Lab Agent is writable, so the second candidate (C:\Program Files\Lab.exe) is not, but the real program itself can be replaced.

2. A 7045 shows the service xKq3pTzA with path %COMSPEC% /Q /c echo ..., created five seconds after a logon type 3 from 10.10.0.5. What do you suspect and what do you check next?

A remote execution tool creating a throw-away service. Check the 4624 (which account, NTLM or Kerberos, from where), 5145 for ADMIN$/C$ writes, the 4688 process tree under services.exe, and whether 10.10.0.5 is an authorised tester's machine (in the lab, it is kali.lab.test).

3. Why does `BackupAgent` pass the check, and what would make it fail?

Its path is quoted, its folder is not user-writable, and it runs as a gMSA. It would fail if the gMSA were made a domain admin or if C:\Program Files\Backup became writable by Users.

Test yourself

Which event ID in the System log records the installation of a new service?

  1. 4624
  2. 7045
  3. 4720
  4. 4769

Why is an unquoted service path with spaces a risk?

  1. It slows boot
  2. The SCM tries shorter paths first, so a program planted at an earlier candidate runs as the service's account
  3. It disables the service
  4. It leaks the password

What is the best identity for a domain service that must authenticate to other servers?

  1. A Domain Admin
  2. A group managed service account (gMSA)
  3. A shared user account with a password that never expires
  4. Guest

Which logon type should a service account normally show in 4624 events?

  1. 2 (interactive)
  2. 5 (service)
  3. 10 (RDP)
  4. 11 (cached interactive)