Skip to content

At SSH login

sshd_config: macOS Persistence via SSH Server Settings

How sshd configuration enables persistence on macOS: sshd_config, sshd_config.d drop-ins, ~/.ssh/rc and Remote Login, and how to check and fix a Mac.

MITRE ATT&CK
T1098.004
Tactics
Persistence, Defense evasion
Runs
At SSH login
Set up by
root
Locations
  • /etc/ssh/sshd_config
  • /etc/ssh/sshd_config.d/*
  • /etc/ssh/sshrc
  • ~/.ssh/rc
  • ~/.ssh/environment

How it works

The SSH server on macOS is OpenSSH's sshd, started by launchd. When Remote Login is on (System Settings › General › Sharing on macOS 13 Ventura and later), the job com.openssh.sshd from /System/Library/LaunchDaemons/ssh.plist listens on port 22. For each connection it starts /usr/libexec/sshd-keygen-wrapper, which creates host keys if they are missing and runs sshd. The plist ships Disabled, and turning Remote Login on writes an override in /var/db/com.apple.xpc.launchd/disabled.plist. Because every connection gets a fresh sshd, a configuration change applies to the next login without any restart.

The configuration is /etc/ssh/sshd_config. On recent releases its first active line is Include /etc/ssh/sshd_config.d/*, and Apple keeps its own changes in a drop-in, 100-macos.conf. On a clean macOS 27 install that drop-in sets UsePAM yes and AcceptEnv LANG LC_* and includes /etc/ssh/crypto.conf. The main file adds only AuthorizedKeysFile .ssh/authorized_keys and the sftp subsystem. Older releases kept Apple's settings in sshd_config itself, so compare with a clean Mac of the same release.

Three OpenSSH rules decide what is effective:

  • The first value wins. For most keywords, sshd uses the first value it reads.
  • Drop-ins come first. The Include sits at the top and its files are read in lexical order, so a drop-in overrides the main file, and a file named 000-… overrides Apple's 100-macos.conf.
  • Match blocks are conditional. A Match block applies its settings only to matching users, groups or addresses, until the next Match or the end of the file.

Two per-login scripts complete the picture:

  • ~/.ssh/rc runs with sh after authentication and before the user's shell or command, when PermitUserRC is on (the default). /etc/ssh/sshrc runs instead when the user has no ~/.ssh/rc.
  • ~/.ssh/environment sets variables for the session, but only when PermitUserEnvironment allows it.

Authentication itself also goes through PAM (/etc/pam.d/sshd), covered under PAM modules.

Where it lives

PathWhatWho can write it
/System/Library/LaunchDaemons/ssh.plistcom.openssh.sshd, socket-activated; Disabled by defaultApple only (sealed system volume)
/usr/sbin/sshd, /usr/libexec/sshd-keygen-wrapperServer and launcherApple only (sealed)
/var/db/com.apple.xpc.launchd/disabled.plistRemote Login on or offroot
/etc/ssh/sshd_configMain configurationroot
/etc/ssh/sshd_config.d/Drop-ins, read first in lexical order (100-macos.conf)root
/etc/ssh/sshrcLogin script for users without ~/.ssh/rcroot
~/.ssh/rc, ~/.ssh/environmentPer-user login script and variablesthe user
/etc/pam.d/sshdPAM stack for SSH loginsroot

How attackers use it

A drop-in that wins. With root, a new file sorted before Apple's overrides settings without touching the main file, which is the one most people read:

# /etc/ssh/sshd_config.d/000-tuning.conf
PermitRootLogin yes
PasswordAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys /usr/local/share/.k/%u

The extra AuthorizedKeysFile path holds keys outside every home folder, where a review of authorized_keys files misses them.

A key oracle. AuthorizedKeysCommand runs a program that prints acceptable keys for the user logging in. A backdoored helper prints the attacker's key for any account:

AuthorizedKeysCommand /usr/local/libexec/keyhelper %u
AuthorizedKeysCommandUser nobody

Conditional access. A Match block opens root or password logins only from the attacker's addresses, so tests from inside the network see nothing unusual:

Match Address 203.0.113.0/24
    PermitRootLogin yes
    PasswordAuthentication yes

Code at every login. ~/.ssh/rc starts an implant whenever its owner logs in. PermitUserEnvironment combined with ~/.ssh/environment pushes a library into the unprotected programs the session runs:

# ~/.ssh/rc
(/Users/alice/Library/.x/agent >/dev/null 2>&1 &)
# ~/.ssh/environment (needs PermitUserEnvironment yes)
DYLD_INSERT_LIBRARIES=/Users/Shared/.u/libx.dylib

Empty passwords. PermitEmptyPasswords yes paired with a passwordless local account gives a login that needs nothing at all.

How to check a host

On a live Mac (read-only):

launchctl print-disabled system | grep com.openssh.sshd    # "enabled" = Remote Login on
ls -le@ /etc/ssh /etc/ssh/sshd_config.d
grep -Ev '^[[:space:]]*(#|$)' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
sudo sshd -T | grep -iE '^(permitrootlogin|passwordauthentication|permitemptypasswords|permituserenvironment|permituserrc|authorizedkeysfile|authorizedkeyscommand|forcecommand|usepam|port|listenaddress) '
sudo sshd -T -C user=alice,host=client,addr=203.0.113.5 | grep -iE '^(permitrootlogin|passwordauthentication) '
sudo ls -la /Users/*/.ssh/rc /Users/*/.ssh/environment /var/root/.ssh/rc /etc/ssh/sshrc 2>/dev/null
stat -f '%SB %Sm %N' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*

sshd -T prints the effective configuration after includes and first-value-wins, and -C evaluates Match blocks for a given connection. It reads host keys, so it needs root. Compare every active line with a clean Mac of the same release. Any drop-in other than 100-macos.conf, or an Include pointing elsewhere, needs an explanation. Run codesign -dv --verbose=4 on any helper an AuthorizedKeysCommand names. Read sshd's unified log entries (log show --predicate 'process == "sshd"') for logins around the change.

Offline, read private/etc/ssh/, private/etc/pam.d/sshd, users' .ssh folders and private/var/db/com.apple.xpc.launchd/disabled.plist from the collection. The macOS Persistence tool parses them in the browser. The collection command takes all of /etc/ssh, every .ssh folder and the launchd overrides.

What macOS Persistence flags

For sshd_config and each file in sshd_config.d, the tool lists only the settings that open or widen a way in. Each becomes one entry, with the criteria of an enclosing Match block as a fact. Apple's stock files produce no entries.

  • Custom AuthorizedKeysFile: the setting names anything other than .ssh/authorized_keys, .ssh/authorized_keys2 (optionally prefixed with %h/) or none.
  • AuthorizedKeysCommand set: any value other than none.
  • Root login allowed: PermitRootLogin yes. OpenSSH's default prohibit-password and forced-commands-only are not flagged.
  • PermitUserEnvironment on: any value other than no, including a list of variable names.
  • Empty passwords allowed: PermitEmptyPasswords yes.
  • ForceCommand set: listed at information level, since management tools use it legitimately.
  • SSH rc script: ~/.ssh/rc or /etc/ssh/sshrc exists. SSH environment file: ~/.ssh/environment exists.
  • Each value and each rc or environment file also goes through the command heuristics, for example Runs from a hidden folder for a keys file in a dot-folder, or Injects a library (DYLD_) and Runs from a temporary or shared folder in an environment file.
  • Changed date: the file was modified within the 7 days before the newest file in the collection.
  • program also runs from mechanisms: a helper or rc path that another mechanism also starts.

Limits:

  • The tool does not compute the effective configuration (first value wins, include order, Match evaluation), so use sshd -T.
  • It does not flag PasswordAuthentication, Port, ListenAddress, AllowUsers, GatewayPorts, PermitTunnel or AcceptEnv.
  • It does not follow Include paths outside sshd_config.d.
  • It does not report whether Remote Login is on.

Cleaning up

  1. Preserve first: sudo cp -Rp /etc/ssh to case storage, save sudo sshd -T output, the rc and environment files and disabled.plist, and hash the copies.
  2. Remove the planted drop-ins and lines, restoring Apple's files from a clean Mac of the same release, then validate with sudo sshd -t. New connections use the fixed configuration at once. Kill the attacker's live sessions (pgrep -lf 'sshd:').
  3. Delete planted ~/.ssh/rc, /etc/ssh/sshrc and ~/.ssh/environment files, and any AuthorizedKeysCommand helper and the keys files it served.
  4. Review every account's authorized_keys, /etc/pam.d/sshd and the account list. Turn Remote Login off if the Mac does not need it.
  5. An intruder with root could read the host private keys. Regenerate them (remove /etc/ssh/ssh_host_*_key*; sshd-keygen-wrapper recreates them at the next connection), warn users about the changed host key, and reset the passwords of accounts that could log in with weakened settings.

References

See also

On Mac Forensics

The artifacts behind this technique, in the Mac Forensics reference: