System Shells via Services

Windows services typically run as SYSTEM and can be used as a privilege escalation opportunity. Malware or penetration testers may run a shell as a service to gain SYSTEM permissions.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2020/02/18"
  3integration = ["endpoint", "windows", "system"]
  4maturity = "production"
  5min_stack_comments = "New fields added: required_fields, related_integrations, setup"
  6min_stack_version = "8.3.0"
  7updated_date = "2024/01/16"
  8
  9[transform]
 10[[transform.osquery]]
 11label = "Osquery - Retrieve All Services"
 12query = "SELECT description, display_name, name, path, pid, service_type, start_type, status, user_account FROM services"
 13
 14[[transform.osquery]]
 15label = "Osquery - Retrieve Services Running on User Accounts"
 16query = """
 17SELECT description, display_name, name, path, pid, service_type, start_type, status, user_account FROM services WHERE
 18NOT (user_account LIKE '%LocalSystem' OR user_account LIKE '%LocalService' OR user_account LIKE '%NetworkService' OR
 19user_account == null)
 20"""
 21
 22[[transform.osquery]]
 23label = "Osquery - Retrieve Service Unsigned Executables with Virustotal Link"
 24query = """
 25SELECT concat('https://www.virustotal.com/gui/file/', sha1) AS VtLink, name, description, start_type, status, pid,
 26services.path FROM services JOIN authenticode ON services.path = authenticode.path OR services.module_path =
 27authenticode.path JOIN hash ON services.path = hash.path WHERE authenticode.result != 'trusted'
 28"""
 29
 30[rule]
 31author = ["Elastic"]
 32description = """
 33Windows services typically run as SYSTEM and can be used as a privilege escalation opportunity. Malware or penetration
 34testers may run a shell as a service to gain SYSTEM permissions.
 35"""
 36from = "now-9m"
 37index = ["winlogbeat-*", "logs-endpoint.events.*", "logs-windows.*", "endgame-*", "logs-system.security*"]
 38language = "eql"
 39license = "Elastic License v2"
 40name = "System Shells via Services"
 41note = """## Triage and analysis
 42
 43### Investigating System Shells via Services
 44
 45Attackers may configure existing services or create new ones to execute system shells to elevate their privileges from administrator to SYSTEM. They can also configure services to execute these shells with persistence payloads.
 46
 47This rule looks for system shells being spawned by `services.exe`, which is compatible with the above behavior.
 48
 49> **Note**:
 50> This investigation guide uses the [Osquery Markdown Plugin](https://www.elastic.co/guide/en/security/master/invest-guide-run-osquery.html) introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display unrendered Markdown in this guide.
 51
 52#### Possible investigation steps
 53
 54- Investigate the process execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
 55- Identify how the service was created or modified. Look for registry changes events or Windows events related to service activities (for example, 4697 and/or 7045).
 56  - Examine the created and existent services, the executables or drivers referenced, and command line arguments for suspicious entries.
 57    - $osquery_0
 58    - $osquery_1
 59    - $osquery_2
 60  - Retrieve the referenced files' SHA-256 hash values using the PowerShell `Get-FileHash` cmdlet and search for the existence and reputation of the hashes in resources like VirusTotal, Hybrid-Analysis, CISCO Talos, Any.run, etc.
 61- Identify the user account that performed the action and whether it should perform this kind of action.
 62- Contact the account owner and confirm whether they are aware of this activity.
 63- Investigate other alerts associated with the user/host during the past 48 hours.
 64- Assess whether this behavior is prevalent in the environment by looking for similar occurrences across hosts.
 65- Check for commands executed under the spawned shell.
 66
 67### False positive analysis
 68
 69- This activity should not happen legitimately. The security team should address any potential benign true positive (B-TP), as this configuration can put the user and the domain at risk.
 70
 71### Response and remediation
 72
 73- Initiate the incident response process based on the outcome of the triage.
 74- Isolate the involved host to prevent further post-compromise behavior.
 75- Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
 76- Delete the service or restore it to the original configuration.
 77- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
 78- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 79- Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
 80"""
 81risk_score = 47
 82rule_id = "0022d47d-39c7-4f69-a232-4fe9dc7a3acd"
 83severity = "medium"
 84tags = ["Domain: Endpoint", "OS: Windows", "Use Case: Threat Detection", "Tactic: Persistence", "Tactic: Execution", "Resources: Investigation Guide", "Data Source: Elastic Endgame", "Data Source: Elastic Defend"]
 85timestamp_override = "event.ingested"
 86type = "eql"
 87
 88query = '''
 89process where host.os.type == "windows" and event.type == "start" and
 90  process.parent.name : "services.exe" and
 91  process.name : ("cmd.exe", "powershell.exe", "pwsh.exe", "powershell_ise.exe") and
 92
 93  /* Third party FP's */
 94  not process.args : "NVDisplay.ContainerLocalSystem"
 95'''
 96
 97
 98[[rule.threat]]
 99framework = "MITRE ATT&CK"
100[[rule.threat.technique]]
101id = "T1543"
102name = "Create or Modify System Process"
103reference = "https://attack.mitre.org/techniques/T1543/"
104[[rule.threat.technique.subtechnique]]
105id = "T1543.003"
106name = "Windows Service"
107reference = "https://attack.mitre.org/techniques/T1543/003/"
108
109
110
111[rule.threat.tactic]
112id = "TA0003"
113name = "Persistence"
114reference = "https://attack.mitre.org/tactics/TA0003/"
115
116[[rule.threat]]
117framework = "MITRE ATT&CK"
118[[rule.threat.technique]]
119id = "T1059"
120name = "Command and Scripting Interpreter"
121reference = "https://attack.mitre.org/techniques/T1059/"
122[[rule.threat.technique.subtechnique]]
123id = "T1059.001"
124name = "PowerShell"
125reference = "https://attack.mitre.org/techniques/T1059/001/"
126[[rule.threat.technique.subtechnique]]
127id = "T1059.003"
128name = "Windows Command Shell"
129reference = "https://attack.mitre.org/techniques/T1059/003/"
130
131
132
133[rule.threat.tactic]
134id = "TA0002"
135name = "Execution"
136reference = "https://attack.mitre.org/tactics/TA0002/"

Triage and analysis

Investigating System Shells via Services

Attackers may configure existing services or create new ones to execute system shells to elevate their privileges from administrator to SYSTEM. They can also configure services to execute these shells with persistence payloads.

This rule looks for system shells being spawned by services.exe, which is compatible with the above behavior.

Note: This investigation guide uses the Osquery Markdown Plugin introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display unrendered Markdown in this guide.

Possible investigation steps

  • Investigate the process execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
  • Identify how the service was created or modified. Look for registry changes events or Windows events related to service activities (for example, 4697 and/or 7045).
    • Examine the created and existent services, the executables or drivers referenced, and command line arguments for suspicious entries.
      • $osquery_0
      • $osquery_1
      • $osquery_2
    • Retrieve the referenced files' SHA-256 hash values using the PowerShell Get-FileHash cmdlet and search for the existence and reputation of the hashes in resources like VirusTotal, Hybrid-Analysis, CISCO Talos, Any.run, etc.
  • Identify the user account that performed the action and whether it should perform this kind of action.
  • Contact the account owner and confirm whether they are aware of this activity.
  • Investigate other alerts associated with the user/host during the past 48 hours.
  • Assess whether this behavior is prevalent in the environment by looking for similar occurrences across hosts.
  • Check for commands executed under the spawned shell.

False positive analysis

  • This activity should not happen legitimately. The security team should address any potential benign true positive (B-TP), as this configuration can put the user and the domain at risk.

Response and remediation

  • Initiate the incident response process based on the outcome of the triage.
  • Isolate the involved host to prevent further post-compromise behavior.
  • Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
  • Delete the service or restore it to the original configuration.
  • Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
  • Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
  • Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).

Related rules

to-top