Linux Restricted Shell Breakout via Linux Binary(s)

Identifies the abuse of a Linux binary to break out of a restricted shell or environment by spawning an interactive system shell. The activity of spawning a shell from a binary is not common behavior for a user or system administrator, and may indicate an attempt to evade detection, increase capabilities or enhance the stability of an adversary.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2022/05/06"
  3integration = ["endpoint"]
  4maturity = "production"
  5updated_date = "2024/09/23"
  6
  7[rule]
  8author = ["Elastic"]
  9description = """
 10Identifies the abuse of a Linux binary to break out of a restricted shell or environment by spawning an interactive
 11system shell. The activity of spawning a shell from a binary is not common behavior for a user or system administrator,
 12and may indicate an attempt to evade detection, increase capabilities or enhance the stability of an adversary.
 13"""
 14from = "now-9m"
 15index = ["logs-endpoint.events.*"]
 16language = "eql"
 17license = "Elastic License v2"
 18name = "Linux Restricted Shell Breakout via Linux Binary(s)"
 19note = """## Triage and analysis
 20
 21### Investigating Shell Evasion via Linux Utilities
 22Detection alerts from this rule indicate that a Linux utility has been abused to breakout of restricted shells or
 23environments by spawning an interactive system shell.
 24Here are some possible avenues of investigation:
 25- Examine the entry point to the host and user in action via the Analyse View.
 26  - Identify the session entry leader and session user
 27- Examine the contents of session leading to the abuse via the Session View.
 28  - Examine the command execution pattern in the session, which may lead to suspricous activities
 29- Examine the execution of commands in the spawned shell.
 30  - Identify imment threat to the system from the executed commands
 31  - Take necessary incident response actions to contain any malicious behviour caused via this execution.
 32
 33### Related rules
 34
 35- A malicious spawned shell can execute any of the possible MITTRE ATT&CK vectors mainly to impair defences.
 36- Hence its adviced to enable defence evasion and privilige escalation rules accordingly in your environment
 37
 38### Response and remediation
 39
 40Initiate the incident response process based on the outcome of the triage.
 41
 42- If the triage releaved suspicious netwrok activity from the malicious spawned shell,
 43  - Isolate the involved host to prevent further post-compromise behavior.
 44- If the triage identified malware execution via the maliciously spawned shell,
 45  - Search the environment for additional compromised hosts.
 46  - Implement temporary network rules, procedures, and segmentation to contain the malware.
 47  - Stop suspicious processes.
 48  - Immediately block the identified indicators of compromise (IoCs).
 49  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
 50- If the triage revelaed defence evasion for imparing defenses
 51  - Isolate the involved host to prevent further post-compromise behavior.
 52  - Identified the disabled security guard components on the host and take necessary steps in renebaling the same.
 53  - If any tools have been disbaled / uninstalled or config tampered work towards reenabling the same.
 54- If the triage revelaed addition of persistence mechanism exploit like auto start scripts
 55  - Isolate further login to the systems that can initae auto start scripts.
 56  - Identify the auto start scripts and disable and remove the same from the systems
 57- If the triage revealed data crawling or data export via remote copy
 58  - Investigate credential exposure on systems compromised / used / decoded by the attacker during the data crawling
 59  - Intiate compromised credential deactivation and credential rotation process for all exposed crednetials.
 60  - Investiagte if any IPR data was accessed during the data crawling and take appropriate actions.
 61- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 62- 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).
 63"""
 64references = [
 65    "https://gtfobins.github.io/gtfobins/apt/",
 66    "https://gtfobins.github.io/gtfobins/apt-get/",
 67    "https://gtfobins.github.io/gtfobins/nawk/",
 68    "https://gtfobins.github.io/gtfobins/mawk/",
 69    "https://gtfobins.github.io/gtfobins/awk/",
 70    "https://gtfobins.github.io/gtfobins/gawk/",
 71    "https://gtfobins.github.io/gtfobins/busybox/",
 72    "https://gtfobins.github.io/gtfobins/c89/",
 73    "https://gtfobins.github.io/gtfobins/c99/",
 74    "https://gtfobins.github.io/gtfobins/cpulimit/",
 75    "https://gtfobins.github.io/gtfobins/crash/",
 76    "https://gtfobins.github.io/gtfobins/env/",
 77    "https://gtfobins.github.io/gtfobins/expect/",
 78    "https://gtfobins.github.io/gtfobins/find/",
 79    "https://gtfobins.github.io/gtfobins/flock/",
 80    "https://gtfobins.github.io/gtfobins/gcc/",
 81    "https://gtfobins.github.io/gtfobins/mysql/",
 82    "https://gtfobins.github.io/gtfobins/nice/",
 83    "https://gtfobins.github.io/gtfobins/ssh/",
 84    "https://gtfobins.github.io/gtfobins/vi/",
 85    "https://gtfobins.github.io/gtfobins/vim/",
 86    "https://gtfobins.github.io/gtfobins/capsh/",
 87    "https://gtfobins.github.io/gtfobins/byebug/",
 88    "https://gtfobins.github.io/gtfobins/git/",
 89    "https://gtfobins.github.io/gtfobins/ftp/",
 90    "https://www.elastic.co/security-labs/sequel-on-persistence-mechanisms",
 91]
 92risk_score = 47
 93rule_id = "52376a86-ee86-4967-97ae-1a05f55816f0"
 94setup = """## Setup
 95
 96This rule requires data coming in from Elastic Defend.
 97
 98### Elastic Defend Integration Setup
 99Elastic Defend is integrated into the Elastic Agent using Fleet. Upon configuration, the integration allows the Elastic Agent to monitor events on your host and send data to the Elastic Security app.
100
101#### Prerequisite Requirements:
102- Fleet is required for Elastic Defend.
103- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
104
105#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
106- Go to the Kibana home page and click "Add integrations".
107- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
108- Click "Add Elastic Defend".
109- Configure the integration name and optionally add a description.
110- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
111- Select a configuration preset. Each preset comes with different default settings for Elastic Agent, you can further customize these later by configuring the Elastic Defend integration policy. [Helper guide](https://www.elastic.co/guide/en/security/current/configure-endpoint-integration-policy.html).
112- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
113- Enter a name for the agent policy in "New agent policy name". If other agent policies already exist, you can click the "Existing hosts" tab and select an existing policy instead.
114For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
115- Click "Save and Continue".
116- To complete the integration, select "Add Elastic Agent to your hosts" and continue to the next section to install the Elastic Agent on your hosts.
117For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
118
119Session View uses process data collected by the Elastic Defend integration, but this data is not always collected by default. Session View is available on enterprise subscription for versions 8.3 and above.
120#### To confirm that Session View data is enabled:
121- Go to “Manage → Policies”, and edit one or more of your Elastic Defend integration policies.
122- Select the” Policy settings” tab, then scroll down to the “Linux event collection” section near the bottom.
123- Check the box for “Process events”, and turn on the “Include session data” toggle.
124- If you want to include file and network alerts in Session View, check the boxes for “Network and File events”.
125- If you want to enable terminal output capture, turn on the “Capture terminal output” toggle.
126For more information about the additional fields collected when this setting is enabled and the usage of Session View for Analysis refer to the [helper guide](https://www.elastic.co/guide/en/security/current/session-view.html).
127"""
128severity = "medium"
129tags = [
130    "Domain: Endpoint",
131    "OS: Linux",
132    "Use Case: Threat Detection",
133    "Tactic: Execution",
134    "Data Source: Elastic Endgame",
135    "Data Source: Elastic Defend",
136]
137timestamp_override = "event.ingested"
138type = "eql"
139
140query = '''
141process where host.os.type == "linux" and event.type == "start" and
142(
143  /* launching shell from capsh */
144  (process.name == "capsh" and process.args == "--") or
145  
146  /* launching shells from unusual parents or parent+arg combos */
147  (process.name in ("bash", "dash", "ash", "sh", "tcsh", "csh", "zsh", "ksh", "fish") and (
148    (process.parent.name : "*awk" and process.parent.args : "BEGIN {system(*)}") or
149    (process.parent.name == "git" and process.parent.args : ("*PAGER*", "!*sh", "exec *sh") or 
150     process.args : ("*PAGER*", "!*sh", "exec *sh") and not process.name == "ssh" ) or
151    (process.parent.name : ("byebug", "ftp", "strace", "zip", "tar") and 
152    (
153      process.parent.args : "BEGIN {system(*)}" or
154      (process.parent.args : ("*PAGER*", "!*sh", "exec *sh") or process.args : ("*PAGER*", "!*sh", "exec *sh")) or
155      (
156        (process.parent.args : "exec=*sh" or (process.parent.args : "-I" and process.parent.args : "*sh")) or
157        (process.args : "exec=*sh" or (process.args : "-I" and process.args : "*sh"))
158        )
159      )
160    ) or
161    
162    /* shells specified in parent args */
163    /* nice rule is broken in 8.2 */
164    (process.parent.args : "*sh" and
165      (
166        (process.parent.name == "nice") or
167        (process.parent.name == "cpulimit" and process.parent.args == "-f") or
168        (process.parent.name == "find" and process.parent.args == "." and process.parent.args == "-exec" and 
169         process.parent.args == ";" and process.parent.args : "/bin/*sh") or
170        (process.parent.name == "flock" and process.parent.args == "-u" and process.parent.args == "/")
171      )
172    )
173  )) or
174
175  /* shells specified in args */
176  (process.args : "*sh" and (
177    (process.parent.name == "crash" and process.parent.args == "-h") or
178    (process.name == "sensible-pager" and process.parent.name in ("apt", "apt-get") and process.parent.args == "changelog")
179    /* scope to include more sensible-pager invoked shells with different parent process to reduce noise and remove false positives */
180    
181  )) or
182  (process.name == "busybox" and event.action == "exec" and process.args_count == 2 and process.args : "*sh" and not 
183   process.executable : "/var/lib/docker/overlay2/*/merged/bin/busybox" and not (process.parent.args == "init" and
184   process.parent.args == "runc") and not process.parent.args in ("ls-remote", "push", "fetch") and not process.parent.name == "mkinitramfs") or
185  (process.name == "env" and process.args_count == 2 and process.args : "*sh") or
186  (process.parent.name in ("vi", "vim") and process.parent.args == "-c" and process.parent.args : ":!*sh") or
187  (process.parent.name in ("c89", "c99", "gcc") and process.parent.args : "*sh,-s" and process.parent.args == "-wrapper") or
188  (process.parent.name == "expect" and process.parent.args == "-c" and process.parent.args : "spawn *sh;interact") or
189  (process.parent.name == "mysql" and process.parent.args == "-e" and process.parent.args : "\\!*sh") or
190  (process.parent.name == "ssh" and process.parent.args == "-o" and process.parent.args : "ProxyCommand=;*sh 0<&2 1>&2")
191)
192'''
193
194
195[[rule.threat]]
196framework = "MITRE ATT&CK"
197[[rule.threat.technique]]
198id = "T1059"
199name = "Command and Scripting Interpreter"
200reference = "https://attack.mitre.org/techniques/T1059/"
201[[rule.threat.technique.subtechnique]]
202id = "T1059.004"
203name = "Unix Shell"
204reference = "https://attack.mitre.org/techniques/T1059/004/"
205
206
207
208[rule.threat.tactic]
209id = "TA0002"
210name = "Execution"
211reference = "https://attack.mitre.org/tactics/TA0002/"

Triage and analysis

Investigating Shell Evasion via Linux Utilities

Detection alerts from this rule indicate that a Linux utility has been abused to breakout of restricted shells or environments by spawning an interactive system shell. Here are some possible avenues of investigation:

  • Examine the entry point to the host and user in action via the Analyse View.
    • Identify the session entry leader and session user
  • Examine the contents of session leading to the abuse via the Session View.
    • Examine the command execution pattern in the session, which may lead to suspricous activities
  • Examine the execution of commands in the spawned shell.
    • Identify imment threat to the system from the executed commands
    • Take necessary incident response actions to contain any malicious behviour caused via this execution.
  • A malicious spawned shell can execute any of the possible MITTRE ATT&CK vectors mainly to impair defences.
  • Hence its adviced to enable defence evasion and privilige escalation rules accordingly in your environment

Response and remediation

Initiate the incident response process based on the outcome of the triage.

  • If the triage releaved suspicious netwrok activity from the malicious spawned shell,
    • Isolate the involved host to prevent further post-compromise behavior.
  • If the triage identified malware execution via the maliciously spawned shell,
    • Search the environment for additional compromised hosts.
    • Implement temporary network rules, procedures, and segmentation to contain the malware.
    • Stop suspicious processes.
    • Immediately block the identified indicators of compromise (IoCs).
    • Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
  • If the triage revelaed defence evasion for imparing defenses
    • Isolate the involved host to prevent further post-compromise behavior.
    • Identified the disabled security guard components on the host and take necessary steps in renebaling the same.
    • If any tools have been disbaled / uninstalled or config tampered work towards reenabling the same.
  • If the triage revelaed addition of persistence mechanism exploit like auto start scripts
    • Isolate further login to the systems that can initae auto start scripts.
    • Identify the auto start scripts and disable and remove the same from the systems
  • If the triage revealed data crawling or data export via remote copy
    • Investigate credential exposure on systems compromised / used / decoded by the attacker during the data crawling
    • Intiate compromised credential deactivation and credential rotation process for all exposed crednetials.
    • Investiagte if any IPR data was accessed during the data crawling and take appropriate actions.
  • 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).

References

Related rules

to-top