Skip to main content
Be Bitwise

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
Service Control Manager

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 TypeDescription
SERVICE_WIN32_OWN_PROCESSRuns in its own process
SERVICE_WIN32_SHARE_PROCESSShares svchost.exe with others
SERVICE_KERNEL_DRIVERKernel-mode device driver
SERVICE_FILE_SYSTEM_DRIVERKernel-mode file system driver
SERVICE_INTERACTIVE_PROCESSCan interact with desktop (legacy)
SERVICE_USER_OWN_PROCESSPer-user service instance (Win10)
SERVICE_USER_SHARE_PROCESSPer-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:

text
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.dll

Startup Types

Services can be configured to start at different points:

ValueStartup TypeNotes
0x00Boot StartLoaded by kernel loader (device drivers only, before disk access)
0x01System StartStarted by I/O manager (device drivers, after boot drivers)
0x02AutomaticStarted by SCM at boot (most system services)
0x03Demand Start (Manual)Started on request (started when needed by dependent service)
0x04DisabledCannot 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)

text
Privileges: All local privileges (SeDebugPrivilege, etc.)
Network identity: Computer account (DOMAIN\MACHINENAME$)
Profile: %SystemRoot%\System32\config\systemprofile
Risk: Highest - compromise = full system control

LocalSystem 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)

text
Privileges: Minimal (similar to regular user)
Network identity: Anonymous (no network credentials)
Profile: %SystemRoot%\ServiceProfiles\LocalService
Risk: Low - limited local and no network access

NetworkService (NT AUTHORITY\NETWORK SERVICE)

text
Privileges: Minimal (similar to regular user)
Network identity: Computer account (DOMAIN\MACHINENAME$)
Profile: %SystemRoot%\ServiceProfiles\NetworkService
Risk: Medium - can authenticate on network as machine

Virtual Service Accounts (NT SERVICE<ServiceName>)

Windows 7 brought in virtual accounts: a unique, auto-managed identity per service:

text
NT SERVICE\wuauserv ← Windows Update service identity
NT SERVICE\Dnscache ← DNS Client service identity

Each one gets its own SID, so you can grant only the permissions a particular service genuinely needs — least privilege, applied at service granularity.

Key Concept
Principle of Least Privilege for Services

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:

text
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:

c
#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 SCM
void 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 point
int 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

c
// Install a new service
SC_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 service
StartService(hService, 0, NULL);
// Stop the service
SERVICE_STATUS status;
ControlService(hService, SERVICE_CONTROL_STOP, &status);

Or using the sc command-line tool:

text
sc create MyService binPath= "C:\Path\To\MyService.exe" start= auto
sc start MyService
sc stop MyService
sc query MyService
sc delete MyService

Service Security Considerations

Services are juicy targets — elevated privileges, always running, rarely watched closely:

  1. 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.exe falls to a planted C:\Program.exe.

  2. 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.

  3. 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.

  4. 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.