Windows.Remediation.RemoveService
Type: Client
Stops and deletes Windows services whose name or binary path matches a regex, for example a malicious persistence service.
Without ReallyDoIt the artifact only lists the services it would
remove. The agent’s own service is never touched.
WARNING: deleting the wrong service can break the host. Test the regex with a dry run before deploying widely.
Required permissions: EXECVE
Precondition: SELECT OS From info() where OS = 'windows'
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
ServiceRegex |
regex | ThisIsAUniqueName |
Matched against the service name and its binary path. |
ReallyDoIt |
bool | Stop and delete the matching services. Otherwise only list them. |
Source
Defined in artifacts/definitions/Windows/Remediation/RemoveService.yaml.
name: Windows.Remediation.RemoveService
description: |
Stops and deletes Windows services whose name or binary path matches
a regex, for example a malicious persistence service.
Without `ReallyDoIt` the artifact only lists the services it would
remove. The agent's own service is never touched.
WARNING: deleting the wrong service can break the host. Test the regex
with a dry run before deploying widely.
type: CLIENT
required_permissions:
- EXECVE
precondition: SELECT OS From info() where OS = 'windows'
parameters:
- name: ServiceRegex
type: regex
default: ThisIsAUniqueName
description: Matched against the service name and its binary path.
- name: ReallyDoIt
type: bool
description: Stop and delete the matching services. Otherwise only list them.
sources:
- query: |
// Only the service hosting the running agent process is exempt,
// matched by process ID. Names and paths are never trusted, so a
// look-alike service cannot hide from this action.
LET agent_pid <= getpid()
LET services = SELECT Name, DisplayName, PathName, State, StartMode,
ProcessId = agent_pid AS IsAgent
FROM wmi(query="SELECT Name, DisplayName, PathName, State, StartMode, ProcessId FROM Win32_Service",
namespace="ROOT/CIMV2")
WHERE Name =~ ServiceRegex OR PathName =~ ServiceRegex
LET run(argv) = SELECT ReturnCode, Stdout FROM execve(argv=argv)
SELECT Name, DisplayName, PathName, State, StartMode,
if(condition=ReallyDoIt AND NOT IsAgent,
then=run(argv=["sc.exe", "stop", Name])[0].ReturnCode) AS StopCode,
if(condition=IsAgent, then="Skipped (agent)",
else=if(condition=ReallyDoIt,
then=if(condition=run(argv=["sc.exe", "delete", Name])[0].ReturnCode = 0,
then="Deleted", else="Failed"),
else="Dry run")) AS Result
FROM services