Suspicious Child Process of Homebrew’s Bundled Ruby Interpreter
editSuspicious Child Process of Homebrew’s Bundled Ruby Interpreter
editDetects a suspicious child process spawned by Homebrew’s bundled Ruby interpreter ("vendor/portable-ruby") during a "brew install", "reinstall" or "upgrade". Cask "preflight" and "postflight" blocks run arbitrary Ruby at install time, including shelling out through "system_command", so a malicious cask from a third-party tap, or a compromised or typosquatted one, can use them to execute code with the installing user’s privileges. The rule looks for what such a hook would plausibly run: network and scripting tools, "osascript" prompts or shell-outs, "launchctl" persistence, keychain and directory-service credential access, shells running download, decode or persistence commands, binaries executed from temporary or shared directories, and untrusted or unsigned binaries.
Rule type: eql
Rule indices:
- logs-endpoint.events.process*
Severity: medium
Risk score: 47
Runs every: 5m
Searches indices from: now-9m (Date Math format, see also Additional look-back time)
Maximum alerts per execution: 100
References:
Tags:
- Domain: Endpoint
- OS: macOS
- Use Case: Threat Detection
- Tactic: Execution
- Tactic: Initial Access
- Data Source: Elastic Defend
- Resources: Investigation Guide
- Rule Type: Event Correlation (EQL)
- Platform: macOS
Version: 1
Rule authors:
- clivoa
Rule license: Elastic License v2
Investigation guide
editTriage and analysis
Investigating Suspicious Child Process of Homebrew’s Bundled Ruby Interpreter
This rule flags a process spawned by Homebrew’s bundled Ruby interpreter
(.../Homebrew/vendor/portable-ruby/.../bin/ruby) during brew install, reinstall or upgrade that matches a list
of tools and behaviours a malicious cask hook would plausibly use. A match most often means a cask’s preflight or
postflight Ruby block executed a command. Shells match when they run an inline command, including combined option
forms such as -xc or -lc. The list does not cover uninstall hooks, hooks that run a script file instead of an
inline command, or exfiltration with tools outside the list such as ditto, zip, scp or rsync.
Possible investigation steps
-
Identify which cask was being installed from the parent’s command line (
brew.rb install --cask <tap>/<cask>) and whether it is a known first-party cask or comes from a third-party or community tap. -
Review the spawned child’s full command line for network activity (
curl,wget,nc), a scripting interpreter invocation (bash -c,python3 -c,osascript), or anything writing outside the cask’s own Caskroom directory. - If the cask is from a third-party tap, check the tap’s Git history/commit author for the cask file for recent, suspicious changes.
- Correlate with any subsequent network connections, persistence (LaunchAgent/LaunchDaemon creation), or credential file access from the same process tree.
False positive analysis
-
A vendor helper run by a legitimate cask’s install step can match when it is unsigned or ad-hoc signed, or when
the cask runs
launchctl,osascriptor an interpreter as part of its setup. Confirm the cask, its tap and the helper’s origin, then exclude that cask’s executable path rather than the tool name.
Response and remediation
- Determine the cask and its tap. Treat a hook that spawned a shell, an interpreter, or a network client as execution under the installing user and move to containment while you confirm intent.
-
Capture the full process tree and command lines, and preserve the cask file from the tap and from the cask’s
Caskroom/<cask>/.metadatadirectory for analysis. -
Do not run
brew uninstallorreinstallon the suspect cask. Homebrew evaluates the cask’s Ruby code again for both commands and runs its uninstall hooks, which gives the malicious code another execution. Remove the installed artifacts, the cask’sCaskroomdirectory and the tap directory with ordinary file system tools instead, with the host isolated. - If the hook reached the network or wrote outside the Caskroom, treat the host as potentially compromised: hunt for persistence (LaunchAgents/LaunchDaemons), rotate any credentials reachable from the user’s session, and review outbound connections from the process tree.
- Report a malicious first-party cask to Homebrew, and for a third-party tap warn other users of that tap.
Setup
editSetup
This rule requires data coming in from Elastic Defend.
Elastic Defend Integration Setup
Elastic 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.
Prerequisite Requirements:
- Fleet is required for Elastic Defend.
- To configure Fleet Server refer to the documentation.
The following steps should be executed in order to add the Elastic Defend integration on a macOS System:
- Go to the Kibana home page and click "Add integrations".
- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
- Click "Add Elastic Defend".
- Configure the integration name and optionally add a description.
- Select the type of environment you want to protect, for MacOS it is recommended to select "Traditional Endpoints".
- 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.
- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
- 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. For more details on Elastic Agent configuration settings, refer to the helper guide.
- Click "Save and Continue".
- 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. For more details on Elastic Defend refer to the helper guide.
Rule query
editprocess where event.action == "exec" and host.os.type == "macos" and
process.parent.executable like~ "*/Homebrew/vendor/portable-ruby/*" and
process.parent.args in ("install", "reinstall", "upgrade") and
(
process.name in ("nc", "ncat", "perl", "node", "sqlite3") or
process.name like~ "python*" or
(process.name == "osascript" and
process.command_line like~ ("*do shell script*", "*display dialog*", "*hidden answer*")) or
(process.name == "launchctl" and process.args in ("load", "bootstrap", "submit", "enable")) or
(process.name == "security" and
process.args in ("find-generic-password", "find-internet-password", "dump-keychain", "export")) or
(process.name == "dscl" and process.args == "-authonly") or
(process.name in ("bash", "sh", "zsh", "dash") and process.args regex "-[a-zA-Z]*c[a-zA-Z]*" and
process.command_line like~ ("*curl *", "*wget *", "*osascript*", "*base64*", "*python -c*", "*python3 -c*",
"*perl -e*", "*eval *", "*launchctl load*", "*launchctl bootstrap*", "*nohup*",
"*LaunchAgents*", "*LaunchDaemons*", "*| sh*", "*|sh*", "*| bash*", "*|bash*",
"*| zsh*", "*chmod +x*", "*com.apple.quarantine*", "*dscl*", "*security find-*",
"*sqlite3*")) or
process.executable like~ ("/tmp/*", "/private/tmp/*", "/var/folders/*", "/private/var/folders/*", "/Users/Shared/*") or
((process.code_signature.trusted == false or process.code_signature.exists == false) and
not process.executable like~ "*/Homebrew/vendor/portable-ruby/*/bin/ruby")
)
Framework: MITRE ATT&CKTM
-
Tactic:
- Name: Initial Access
- ID: TA0001
- Reference URL: https://attack.mitre.org/tactics/TA0001/
-
Technique:
- Name: Supply Chain Compromise
- ID: T1195
- Reference URL: https://attack.mitre.org/techniques/T1195/
-
Sub-technique:
- Name: Compromise Software Supply Chain
- ID: T1195.002
- Reference URL: https://attack.mitre.org/techniques/T1195/002/
-
Tactic:
- Name: Execution
- ID: TA0002
- Reference URL: https://attack.mitre.org/tactics/TA0002/
-
Technique:
- Name: Command and Scripting Interpreter
- ID: T1059
- Reference URL: https://attack.mitre.org/techniques/T1059/