Repository navigation
[New Rule] SSH Reverse or Dynamic Tunnel on macOS #7127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
litemars
wants to merge
9
commits into
main
Choose a base branch
from
new_rule/ssh_reverse_tunnel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9a0ab69
adding new rule reverse tunnel macos
litemars 7ba5b46
Merge branch 'main' into new_rule/ssh_reverse_tunnel
litemars 0cfb6c6
Update rules/macos/command_and_control_ssh_reverse_or_dynamic_tunnel.…
litemars 2ee840e
Update rules/macos/command_and_control_ssh_reverse_or_dynamic_tunnel.…
litemars 5363709
Update rules/macos/command_and_control_ssh_reverse_or_dynamic_tunnel.…
litemars 45acd75
Update rules/macos/command_and_control_ssh_reverse_or_dynamic_tunnel.…
litemars 0c3fc34
Update rules/macos/command_and_control_ssh_reverse_or_dynamic_tunnel.…
litemars 90b6f83
Apply suggestion from @bryans3c
litemars 7dd536c
Apply suggestion from @bryans3c
litemars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
rules/macos/command_and_control_ssh_reverse_or_dynamic_tunnel.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| [metadata] | ||
| creation_date = "2026/11/03" | ||
| integration = ["endpoint"] | ||
| maturity = "production" | ||
| updated_date = "2026/11/03" | ||
|
|
||
| [rule] | ||
| author = ["Elastic"] | ||
| description = """ | ||
| Detects SSH or autossh on macOS establishing a reverse (-R) or dynamic SOCKS (-D) tunnel. Attackers use SSH tunneling | ||
| from compromised macOS hosts to expose local services to attacker infrastructure, proxy command and control traffic or | ||
| pivot into internal networks through outbound connections. Reverse and dynamic tunnels are uncommon on most macOS | ||
| endpoints, so alerts are suppressed per host and user to surface each new tunnel owner for review. | ||
| """ | ||
| from = "now-9m" | ||
| index = ["logs-endpoint.events.process-*"] | ||
| language = "eql" | ||
| license = "Elastic License v2" | ||
| name = "SSH Reverse or Dynamic Tunnel on macOS" | ||
| note = """## Triage and analysis | ||
|
|
||
| ### Investigating SSH Reverse or Dynamic Tunnel on macOS | ||
|
|
||
| SSH reverse (`-R`) and dynamic (`-D`) forwarding let a host expose local services to a remote server, or proxy traffic | ||
| through it. Attackers abuse these options to keep remote access to a compromised Mac, for example by forwarding the | ||
| local SSH port to attacker infrastructure, often from a persistence loop or LaunchAgent. This rule matches the | ||
| forwarding flag only when it is directly followed by a forwarding specification, so flags passed to a remote command | ||
| (for example `rsync --server -D` or `chown -R`) do not trigger it. Alerts are suppressed per host and user for 24 hours. | ||
|
|
||
| ### Possible investigation steps | ||
|
|
||
| - Review `process.command_line` for the forwarding specification. Reverse forwards of remote access ports such as 22 | ||
| (SSH), 5900 (Screen Sharing), 3283 (Apple Remote Desktop) or 3389 (RDP), and reverse SOCKS proxies (`-R <port>` | ||
| without a target), are higher risk than forwards of application or development ports. | ||
| - Check for options that indicate unattended use, such as `StrictHostKeyChecking=no`, `UserKnownHostsFile=/dev/null`, | ||
| `BatchMode=yes`, `ExitOnForwardFailure=yes` or `-N`. | ||
| - Check the private key passed with `-i`. Keys stored in hidden files or temporary directories such as `/var/folders`, | ||
| `/tmp` or `/Users/Shared` are suspicious. | ||
| - Examine the parent process and `process.Ext.effective_parent`. Shell loops (`while true; do ... ssh ...; done`), | ||
| LaunchAgents, LaunchDaemons or unsigned binaries starting SSH are suspicious. | ||
| - Investigate the destination host or IP address and its reputation, and whether the connection is expected for the user. | ||
| Public destinations are more suspicious than hosts on the local network. | ||
| - Look for related persistence (LaunchAgents, shell profile modifications) and changes to `~/.ssh/authorized_keys` | ||
| around the same time. | ||
|
|
||
| ### False positive analysis | ||
|
|
||
| - Developers and administrators may use reverse (`-R`) or dynamic (`-D`) forwarding to share local development | ||
| servers, reach lab machines or run SOCKS proxies. These commonly forward application ports to hosts on the local | ||
| network using keys stored in `~/.ssh`. Validate the activity with the user, and add exceptions for known destination | ||
| hosts or parent processes. | ||
| - Remote development and container tooling may start SSH tunnels on behalf of the user. Exclude known tooling by | ||
| parent executable once validated. | ||
|
litemars marked this conversation as resolved.
Resolved
|
||
| - Persistent tunnels managed by a LaunchAgent with `KeepAlive` restart frequently and alert once per suppression window. Once validated, exclude them by the key path passed with `-i` or the destination host. | ||
|
|
||
| ### Response and remediation | ||
|
|
||
| - If the tunnel is unauthorized, terminate the SSH process and any parent loop or script that restarts it. | ||
| - Remove associated persistence such as LaunchAgents, LaunchDaemons or shell profile entries, and delete the private key | ||
| used for the tunnel. | ||
| - Block the remote host or IP address at the network perimeter. | ||
| - Review `~/.ssh/authorized_keys` and rotate credentials that may have been exposed through the tunnel. | ||
| """ | ||
| references = [ | ||
| "https://man.openbsd.org/ssh", | ||
| ] | ||
| risk_score = 21 | ||
| rule_id = "85339249-73ac-485a-aaa3-218cca00c1df" | ||
| setup = """## Setup | ||
|
|
||
| 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](https://www.elastic.co/guide/en/fleet/current/fleet-server.html). | ||
|
|
||
| #### 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](https://www.elastic.co/guide/en/security/current/configure-endpoint-integration-policy.html). | ||
| - 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](https://www.elastic.co/guide/en/fleet/current/agent-policy.html). | ||
| - 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](https://www.elastic.co/guide/en/security/current/install-endpoint.html). | ||
| """ | ||
| severity = "low" | ||
| tags = [ | ||
| "Domain: Endpoint", | ||
| "OS: macOS", | ||
| "Use Case: Threat Detection", | ||
| "Tactic: Command and Control", | ||
| "Data Source: Elastic Defend", | ||
| "Resources: Investigation Guide", | ||
| "Threat: Protocol Tunneling", | ||
|
litemars marked this conversation as resolved.
Resolved
|
||
| "Platform: macOS", | ||
| ] | ||
| timestamp_override = "event.ingested" | ||
| type = "eql" | ||
| query = ''' | ||
| process where host.os.type == "macos" and event.type == "start" and event.action == "exec" and | ||
| process.name in ("ssh", "autossh") and | ||
| ( | ||
| process.command_line regex """.* -[A-NP-Za-np-z]*R ?([^ ]+:)?[0-9]{1,5}:(([^ :]+|\[[^ ]+\]):[0-9]{1,5}|/[^ ]+)( .*)?""" or | ||
| process.command_line regex """.* -[A-NP-Za-np-z]*R ?(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|localhost|\*|\[[^ ]+\]):)?[0-9]{1,5}( .*)?""" or | ||
| process.command_line regex """.* -[A-NP-Za-np-z]*D ?([^ ]+:)?[0-9]{1,5}( .*)?""" or | ||
| (process.args == "-o" and process.args : ("RemoteForward*", "DynamicForward*")) or | ||
| process.args : ("-oRemoteForward*", "-oDynamicForward*") | ||
| ) and | ||
| not process.parent.name == "autossh" | ||
| ''' | ||
|
|
||
| [[rule.threat]] | ||
| framework = "MITRE ATT&CK" | ||
|
|
||
| [[rule.threat.technique]] | ||
| id = "T1090" | ||
| name = "Proxy" | ||
| reference = "https://attack.mitre.org/techniques/T1090/" | ||
|
|
||
| [[rule.threat.technique]] | ||
| id = "T1572" | ||
| name = "Protocol Tunneling" | ||
| reference = "https://attack.mitre.org/techniques/T1572/" | ||
|
|
||
| [rule.threat.tactic] | ||
| id = "TA0011" | ||
| name = "Command and Control" | ||
| reference = "https://attack.mitre.org/tactics/TA0011/" | ||
|
|
||
| [rule.alert_suppression] | ||
| group_by = ["host.id", "user.name"] | ||
| duration = {value = 24, unit = "h"} | ||
| missing_fields_strategy = "suppress" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.