SSH Reverse or Dynamic Tunnel on macOS

edit
IMPORTANT: This documentation is no longer updated. Refer to Elastic's version policy and the latest documentation.

SSH Reverse or Dynamic Tunnel on macOS

edit

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.

Rule type: eql

Rule indices:

  • logs-endpoint.events.process-*

Severity: low

Risk score: 21

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: Command and Control
  • Data Source: Elastic Defend
  • Resources: Investigation Guide
  • Threat: Protocol Tunneling
  • Platform: macOS

Version: 1

Rule authors:

  • Elastic

Rule license: Elastic License v2

Investigation guide

edit

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.
  • 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.

Setup

edit

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.

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

edit
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"

Framework: MITRE ATT&CKTM