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"
  5min_stack_comments = "New fields added: required_fields, related_integrations, setup"
  6min_stack_version = "8.3.0"
  7updated_date = "2023/09/05"
  8
  9[rule]
 10author = ["Elastic"]
 11description = """
 12Identifies the abuse of a Linux binary to break out of a restricted shell or environment by spawning an interactive 
 13system shell. The activity of spawning a shell from a binary is not common behavior for a user or system administrator, 
 14and may indicate an attempt to evade detection, increase capabilities or enhance the stability of an adversary.
 15"""
 16from = "now-9m"
 17index = ["logs-endpoint.events.*"]
 18language = "eql"
 19license = "Elastic License v2"
 20name = "Linux Restricted Shell Breakout via Linux Binary(s)"
 21note = """## Triage and analysis
 22
 23### Investigating Shell Evasion via Linux Utilities
 24Detection alerts from this rule indicate that a Linux utility has been abused to breakout of restricted shells or
 25environments by spawning an interactive system shell.
 26Here are some possible avenues of investigation:
 27- Examine the entry point to the host and user in action via the Analyse View.
 28  - Identify the session entry leader and session user
 29- Examine the contents of session leading to the abuse via the Session View.
 30  - Examine the command execution pattern in the session, which may lead to suspricous activities
 31- Examine the execution of commands in the spawned shell.
 32  - Identify imment threat to the system from the executed commands
 33  - Take necessary incident response actions to contain any malicious behviour caused via this execution.
 34
 35### Related rules
 36
 37- A malicious spawned shell can execute any of the possible MITTRE ATT&CK vectors mainly to impair defences.
 38- Hence its adviced to enable defence evasion and privilige escalation rules accordingly in your environment
 39
 40### Response and remediation
 41
 42Initiate the incident response process based on the outcome of the triage.
 43
 44- If the triage releaved suspicious netwrok activity from the malicious spawned shell,
 45  - Isolate the involved host to prevent further post-compromise behavior.
 46- If the triage identified malware execution via the maliciously spawned shell,
 47  - Search the environment for additional compromised hosts.
 48  - Implement temporary network rules, procedures, and segmentation to contain the malware.
 49  - Stop suspicious processes.
 50  - Immediately block the identified indicators of compromise (IoCs).
 51  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
 52- If the triage revelaed defence evasion for imparing defenses
 53  - Isolate the involved host to prevent further post-compromise behavior.
 54  - Identified the disabled security guard components on the host and take necessary steps in renebaling the same.
 55  - If any tools have been disbaled / uninstalled or config tampered work towards reenabling the same.
 56- If the triage revelaed addition of persistence mechanism exploit like auto start scripts
 57  - Isolate further login to the systems that can initae auto start scripts.
 58  - Identify the auto start scripts and disable and remove the same from the systems
 59- If the triage revealed data crawling or data export via remote copy
 60  - Investigate credential exposure on systems compromised / used / decoded by the attacker during the data crawling
 61  - Intiate compromised credential deactivation and credential rotation process for all exposed crednetials.
 62  - Investiagte if any IPR data was accessed during the data crawling and take appropriate actions.
 63- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 64- 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).
 65
 66## Setup
 67
 68The session view analysis for the command alerted is avalible in versions 8.2 and above.
 69"""
 70references = [
 71    "https://gtfobins.github.io/gtfobins/apt/",
 72    "https://gtfobins.github.io/gtfobins/apt-get/",
 73    "https://gtfobins.github.io/gtfobins/nawk/",
 74    "https://gtfobins.github.io/gtfobins/mawk/",
 75    "https://gtfobins.github.io/gtfobins/awk/",
 76    "https://gtfobins.github.io/gtfobins/gawk/",
 77    "https://gtfobins.github.io/gtfobins/busybox/",
 78    "https://gtfobins.github.io/gtfobins/c89/",
 79    "https://gtfobins.github.io/gtfobins/c99/",
 80    "https://gtfobins.github.io/gtfobins/cpulimit/",
 81    "https://gtfobins.github.io/gtfobins/crash/",
 82    "https://gtfobins.github.io/gtfobins/env/",
 83    "https://gtfobins.github.io/gtfobins/expect/",
 84    "https://gtfobins.github.io/gtfobins/find/",
 85    "https://gtfobins.github.io/gtfobins/flock/",
 86    "https://gtfobins.github.io/gtfobins/gcc/",
 87    "https://gtfobins.github.io/gtfobins/mysql/",
 88    "https://gtfobins.github.io/gtfobins/nice/",
 89    "https://gtfobins.github.io/gtfobins/ssh/",
 90    "https://gtfobins.github.io/gtfobins/vi/",
 91    "https://gtfobins.github.io/gtfobins/vim/",
 92    "https://gtfobins.github.io/gtfobins/capsh/",
 93    "https://gtfobins.github.io/gtfobins/byebug/",
 94    "https://gtfobins.github.io/gtfobins/git/",
 95    "https://gtfobins.github.io/gtfobins/ftp/",
 96]
 97risk_score = 47
 98rule_id = "52376a86-ee86-4967-97ae-1a05f55816f0"
 99severity = "medium"
100tags = ["Domain: Endpoint", "OS: Linux", "Use Case: Threat Detection", "Tactic: Execution", "Data Source: Elastic Endgame", "Data Source: Elastic Defend"]
101timestamp_override = "event.ingested"
102type = "eql"
103query = '''
104process where host.os.type == "linux" and event.type == "start" and
105(
106  /* launching shell from capsh */
107  (process.name == "capsh" and process.args == "--") or
108  
109  /* launching shells from unusual parents or parent+arg combos */
110  (process.name : "*sh" and (
111    (process.parent.name : "*awk" and process.parent.args : "BEGIN {system(*)}") or
112    (process.parent.name == "git" and process.parent.args : ("*PAGER*", "!*sh", "exec *sh") or 
113     process.args : ("*PAGER*", "!*sh", "exec *sh") and not process.name == "ssh" ) or
114    (process.parent.name : ("byebug", "ftp", "strace", "zip", "tar") and 
115    (
116      process.parent.args : "BEGIN {system(*)}" or
117      (process.parent.args : ("*PAGER*", "!*sh", "exec *sh") or process.args : ("*PAGER*", "!*sh", "exec *sh")) or
118      (
119        (process.parent.args : "exec=*sh" or (process.parent.args : "-I" and process.parent.args : "*sh")) or
120        (process.args : "exec=*sh" or (process.args : "-I" and process.args : "*sh"))
121        )
122      )
123    ) or
124    
125    /* shells specified in parent args */
126    /* nice rule is broken in 8.2 */
127    (process.parent.args : "*sh" and
128      (
129        (process.parent.name == "nice") or
130        (process.parent.name == "cpulimit" and process.parent.args == "-f") or
131        (process.parent.name == "find" and process.parent.args == "." and process.parent.args == "-exec" and 
132         process.parent.args == ";" and process.parent.args : "/bin/*sh") or
133        (process.parent.name == "flock" and process.parent.args == "-u" and process.parent.args == "/")
134      )
135    )
136  )) or
137
138  /* shells specified in args */
139  (process.args : "*sh" and (
140    (process.parent.name == "crash" and process.parent.args == "-h") or
141    (process.name == "sensible-pager" and process.parent.name in ("apt", "apt-get") and process.parent.args == "changelog")
142    /* scope to include more sensible-pager invoked shells with different parent process to reduce noise and remove false positives */
143    
144  )) or
145  (process.name == "busybox" and event.action == "exec" and process.args_count == 2 and process.args : "*sh" and not 
146   process.executable : "/var/lib/docker/overlay2/*/merged/bin/busybox" and not (process.parent.args == "init" and
147   process.parent.args == "runc") and not process.parent.args in ("ls-remote", "push", "fetch") and not process.parent.name == "mkinitramfs") or
148  (process.name == "env" and process.args_count == 2 and process.args : "*sh") or
149  (process.parent.name in ("vi", "vim") and process.parent.args == "-c" and process.parent.args : ":!*sh") or
150  (process.parent.name in ("c89", "c99", "gcc") and process.parent.args : "*sh,-s" and process.parent.args == "-wrapper") or
151  (process.parent.name == "expect" and process.parent.args == "-c" and process.parent.args : "spawn *sh;interact") or
152  (process.parent.name == "mysql" and process.parent.args == "-e" and process.parent.args : "\\!*sh") or
153  (process.parent.name == "ssh" and process.parent.args == "-o" and process.parent.args : "ProxyCommand=;*sh 0<&2 1>&2")
154)
155'''
156
157[[rule.threat]]
158framework = "MITRE ATT&CK"
159
160[[rule.threat.technique]]
161id = "T1059"
162name = "Command and Scripting Interpreter"
163reference = "https://attack.mitre.org/techniques/T1059/"
164
165[[rule.threat.technique.subtechnique]]
166id = "T1059.004"
167name = "Unix Shell"
168reference = "https://attack.mitre.org/techniques/T1059/004/"
169
170[rule.threat.tactic]
171id = "TA0002"
172name = "Execution"
173reference = "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).

Setup

The session view analysis for the command alerted is avalible in versions 8.2 and above.

References

Related rules

to-top