Windows Services & SCM
The Windows Architecture Deep Dive · Windows Programming track
10 min read
In this module
Background Processes That Keep Windows Running
Windows services are the long-running background processes doing the unglamorous work — networking, security, event logging, printing. They run with no user session attached, can fire up at boot time, and answer to the Service Control Manager (SCM). If you do sysadmin, security hardening, or any low-level Windows work, services come up constantly.
The Service Control Manager (SCM)
The SCM (services.exe) is a system process that comes up early in the boot sequence and runs the lifecycle for every service:
The SCM provides these core functions:
- Starting and stopping services based on startup configuration
- Managing service state (running, stopped, paused, starting, stopping)
- Dependency resolution (starting services in the correct order)
- Communicating with services through a control interface
- Maintaining the service database in the registry
The SCM is the boss of Windows services. It pulls configs from the registry, walks the dependency graph, manages each service's lifecycle, and exposes the control interface that sc.exe, services.msc, and the Service Control API all talk to. It runs as services.exe under LocalSystem.
Service Types
Windows supports several types of services:
| Service Type | Description |
|---|---|
| SERVICE_WIN32_OWN_PROCESS | Runs in its own process |
| SERVICE_WIN32_SHARE_PROCESS | Shares svchost.exe with others |
| SERVICE_KERNEL_DRIVER | Kernel-mode device driver |
| SERVICE_FILE_SYSTEM_DRIVER | Kernel-mode file system driver |
| SERVICE_INTERACTIVE_PROCESS | Can interact with desktop (legacy) |
| SERVICE_USER_OWN_PROCESS | Per-user service instance (Win10) |
| SERVICE_USER_SHARE_PROCESS | Per-user shared svchost |
svchost.exe: The Service Host
A lot of Windows services don't ship as their own .exe. They're DLLs, and svchost.exe is what actually runs them:
Each svchost.exe hosts a related cluster of services. From Windows 10 onward, machines with enough RAM get one svchost per service for stronger isolation.
The service DLL paths are specified in the registry:
HKLM\SYSTEM\CurrentControlSet\Services\wuauserv\├── Type = 0x20 (SERVICE_WIN32_SHARE_PROCESS)├── Start = 3 (SERVICE_DEMAND_START)├── ImagePath = %SystemRoot%\system32\svchost.exe -k netsvcs└── Parameters\ └── ServiceDll = %SystemRoot%\system32\wuaueng.dllStartup Types
Services can be configured to start at different points:
| Value | Startup Type | Notes |
|---|---|---|
| 0x00 | Boot Start | Loaded by kernel loader (device drivers only, before disk access) |
| 0x01 | System Start | Started by I/O manager (device drivers, after boot drivers) |
| 0x02 | Automatic | Started by SCM at boot (most system services) |
| 0x03 | Demand Start (Manual) | Started on request (started when needed by dependent service) |
| 0x04 | Disabled | Cannot be started |
There's also Delayed Auto-Start, a flag layered on type 0x02 that holds startup back to keep boot snappy. Once the regular auto-start crowd is up, the SCM kicks the delayed ones into action.
Service Accounts
Services run under specific security identities:
LocalSystem (NT AUTHORITY\SYSTEM)
Privileges: All local privileges (SeDebugPrivilege, etc.)Network identity: Computer account (DOMAIN\MACHINENAME$)Profile: %SystemRoot%\System32\config\systemprofileRisk: Highest - compromise = full system controlLocalSystem is the most powerful built-in account on the box. Windows Installer (msiserver) and the Task Scheduler both live here. A bug in any LocalSystem service is, by default, a full machine takeover.
LocalService (NT AUTHORITY\LOCAL SERVICE)
Privileges: Minimal (similar to regular user)Network identity: Anonymous (no network credentials)Profile: %SystemRoot%\ServiceProfiles\LocalServiceRisk: Low - limited local and no network accessNetworkService (NT AUTHORITY\NETWORK SERVICE)
Privileges: Minimal (similar to regular user)Network identity: Computer account (DOMAIN\MACHINENAME$)Profile: %SystemRoot%\ServiceProfiles\NetworkServiceRisk: Medium - can authenticate on network as machineVirtual Service Accounts (NT SERVICE<ServiceName>)
Windows 7 brought in virtual accounts: a unique, auto-managed identity per service:
NT SERVICE\wuauserv ← Windows Update service identityNT SERVICE\Dnscache ← DNS Client service identityEach one gets its own SID, so you can grant only the permissions a particular service genuinely needs — least privilege, applied at service granularity.
Running services as LocalSystem grants them more privileges than they need, creating a larger attack surface. Best practice is to use the least-privileged account that provides the necessary functionality. Use LocalService for services that need no network identity, NetworkService for those that need machine-level network authentication, and virtual service accounts for per-service isolation.
Dependency Management
A service can declare it depends on other services, or on whole service groups:
HKLM\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\├── DependOnService = [ "Bowser", "MRxSmb20", "NSI" ]└── DependOnGroup = [ "NetworkProvider" ]The SCM builds a dependency graph and starts services in the correct order:
When a service fails to start, anything depending on it dies with it.
Writing a Windows Service
A Windows service has a specific program structure:
#include <windows.h>
SERVICE_STATUS_HANDLE g_StatusHandle;SERVICE_STATUS g_ServiceStatus;HANDLE g_StopEvent = NULL;
// Service entry point (called by SCM)void WINAPI ServiceMain(DWORD argc, LPTSTR *argv) { // Register the control handler g_StatusHandle = RegisterServiceCtrlHandler( "MyService", ServiceCtrlHandler);
// Report: starting g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
// Initialise your service g_StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
// Report: running g_ServiceStatus.dwCurrentState = SERVICE_RUNNING; g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
// Main service loop while (WaitForSingleObject(g_StopEvent, 1000) != WAIT_OBJECT_0) { // Do work... }
// Report: stopped g_ServiceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(g_StatusHandle, &g_ServiceStatus);}
// Handle control requests from SCMvoid WINAPI ServiceCtrlHandler(DWORD control) { switch (control) { case SERVICE_CONTROL_STOP: g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING; SetServiceStatus(g_StatusHandle, &g_ServiceStatus); SetEvent(g_StopEvent); // Signal the main loop to exit break; case SERVICE_CONTROL_INTERROGATE: SetServiceStatus(g_StatusHandle, &g_ServiceStatus); break; }}
// Process entry pointint main(void) { SERVICE_TABLE_ENTRY serviceTable[] = { { "MyService", ServiceMain }, { NULL, NULL } // End of table };
// Connect to the SCM and dispatch to ServiceMain StartServiceCtrlDispatcher(serviceTable); return 0;}Installing and Controlling Services
// Install a new serviceSC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);SC_HANDLE hService = CreateService( hSCManager, "MyService", // Service name (internal) "My Custom Service", // Display name SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, "C:\\Path\\To\\MyService.exe", // Binary path NULL, NULL, NULL, // Load order, dependencies, tag NULL, // Service account (NULL = LocalSystem) NULL // Password);
// Start the serviceStartService(hService, 0, NULL);
// Stop the serviceSERVICE_STATUS status;ControlService(hService, SERVICE_CONTROL_STOP, &status);Or using the sc command-line tool:
sc create MyService binPath= "C:\Path\To\MyService.exe" start= autosc start MyServicesc stop MyServicesc query MyServicesc delete MyServiceService Security Considerations
Services are juicy targets — elevated privileges, always running, rarely watched closely:
-
Service binary hijacking: an unquoted binary path with spaces lets Windows try a different executable than intended. The classic:
C:\Program Files\My Service\svc.exefalls to a plantedC:\Program.exe. -
Service DLL hijacking: for svchost-hosted services, a ServiceDll path that's relative or vulnerable to search order is an open door for a malicious DLL.
-
Weak service permissions: if the service's registry key or its binary has sloppy ACLs, a low-privileged user can rewrite the service config and run code as whatever account the service uses.
-
Unquoted service paths: spaces in an unquoted path get parsed as argument separators by the SCM. That's binary planting waiting to happen.
Services are the spine of Windows. They're also where a lot of incident response and hardening work lands, because their security config sets the ceiling for what an attacker who lands on the box can do.