Summary
.NET implementation of the Service Control Manager binarysc.exe for querying, starting, stopping, creating, deleting, and modifying Windows services on local and remote systems. Provides comprehensive service management capabilities with detailed service information and interactive UI features.
- Needs Admin: False (some operations require elevated privileges)
- Version: 3
- Author: @djhohnstein
Arguments
Thesc command supports multiple operation modes through parameter groups:
Query Mode
- query (Boolean) - Query services on target system
- computer (String, Optional) - Target computer name
- service (String, Optional) - Specific service name to query
- display_name (String, Optional) - Service display name filter
Start Mode
- start (Boolean) - Start specified service
- service (String, Required) - Service name to start
- computer (String, Optional) - Target computer name
Stop Mode
- stop (Boolean) - Stop specified service
- service (String, Required) - Service name to stop
- computer (String, Optional) - Target computer name
Create Mode
- create (Boolean) - Create new service
- service (String, Required) - Service name
- display_name (String, Required) - Service display name
- binpath (String, Required) - Path to service executable
- computer (String, Optional) - Target computer name
Delete Mode
- delete (Boolean) - Delete specified service
- service (String, Required) - Service name to delete
- computer (String, Optional) - Target computer name
Modify Mode
- modify (Boolean) - Modify existing service
- service (String, Required) - Service name to modify
- computer (String, Optional) - Target computer name
- binpath (String, Optional) - New binary path
- display_name (String, Optional) - New display name
- description (String, Optional) - Service description
- run_as (String, Optional) - Service account username
- password (String, Optional) - Service account password
- service_type (String, Optional) - Service type
- start_type (String, Optional) - Service start type
- dependencies (String Array, Optional) - Service dependencies
Usage
Detailed Summary
Agent Execution Flow
1. Parameter Processing and Validation
- Deserializes parameters from JSON input
- Validates required parameters for each operation mode
- Sets default computer name to local machine if not specified
- Ensures mutual exclusivity of operation modes
2. Parameter Validation Logic
- Validates required parameters for each operation mode
- Prevents execution with incomplete parameter sets
- Provides clear error messages for missing requirements
3. Service Control Manager Connection
- Opens connection to Service Control Manager on target system
- Uses appropriate access rights based on operation type
- Handles connection failures with detailed error messages
- Supports both local and remote service management
4. Service Enumeration (Query Mode)
- Enumerates all services on target system
- Uses two-phase approach: size calculation then data retrieval
- Handles memory allocation and deallocation properly
- Supports filtering by service name when specified
5. Service Configuration Retrieval
- Retrieves detailed service configuration information
- Gets service description through separate API call
- Properly manages memory allocation for variable-length data
- Handles configuration retrieval failures gracefully
6. Service State Management
- Uses .NET ServiceController class for state management
- Implements asynchronous waiting for state changes
- Supports cancellation during state transitions
- Provides feedback on current service state
7. Service Creation and Installation
- Creates new Windows service with specified parameters
- Automatically removes existing service with same name
- Uses standard service configuration defaults
- Provides comprehensive error handling
8. Service Modification
- Modifies existing service configuration
- Supports partial updates using SERVICE_NO_CHANGE constant
- Handles dependencies as null-terminated string array
- Updates service description through separate API call
9. Interactive UI Features
- Generates interactive table with action buttons
- Buttons are enabled/disabled based on service state
- Supports direct service management from UI
- Provides detailed service information in popup dialogs
API Function Resolution
Required Windows APIs
- Dynamically resolves all required service management APIs
- Uses both ANSI and Unicode variants as appropriate
- Leverages Apollo’s API resolution framework
- Provides comprehensive service management capabilities
Data Structures
ServiceResult Output Structure
Memory Management
- Implements proper handle management through SafeHandle
- Ensures automatic cleanup of service handles
- Provides constrained execution region guarantees
- Prevents handle leaks in error scenarios
Service Type and State Enumerations
Service Types
Service States
Service Start Types
APIs Used
| API | Purpose | DLL |
|---|---|---|
OpenSCManagerA | Open Service Control Manager | advapi32.dll |
OpenServiceA | Open handle to specific service | advapi32.dll |
CreateServiceA | Create new service | advapi32.dll |
DeleteService | Delete existing service | advapi32.dll |
StartServiceA | Start service | advapi32.dll |
ControlService | Send control codes to service | advapi32.dll |
EnumServicesStatusExW | Enumerate services with detailed info | advapi32.dll |
QueryServiceConfigW | Query service configuration | advapi32.dll |
QueryServiceConfig2W | Query extended service configuration | advapi32.dll |
ChangeServiceConfigA | Modify service configuration | advapi32.dll |
ChangeServiceConfig2W | Modify extended service configuration | advapi32.dll |
CloseServiceHandle | Close service handles | advapi32.dll |
MITRE ATT&CK Mapping
- T1106 - Native API
- T1543.003 - Create or Modify System Process: Windows Service
- T1569.002 - System Services: Service Execution
Security Considerations
- Privilege Escalation: Service creation/modification may require admin rights
- Persistence: Services can be used for maintaining persistence
- Service Hijacking: Modification of existing services for malicious purposes
- Remote Access: Can manage services on remote systems
- Credential Exposure: Service account credentials may be visible in memory
Limitations
- Some operations require administrative privileges
- Remote service management depends on network connectivity and permissions
- Service state changes may take time to complete
- Some services cannot be stopped due to system dependencies
- Service creation requires valid executable path
- Password changes may require service restart
Error Conditions
- Access Denied: Insufficient privileges for requested operation
- Service Not Found: Specified service does not exist
- Invalid Parameter: Missing required parameters for operation
- SCM Connection Failed: Cannot connect to Service Control Manager
- Service Start/Stop Timeout: Service fails to change state within timeout period
- Handle Creation Failed: Cannot obtain handle to service or SCM
- Configuration Error: Invalid service configuration parameters
- Network Error: Remote service management fails due to network issues