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
Includesits at the top and its files are read in lexical order, so a drop-in overrides the main file, and a file named000-…overrides Apple's100-macos.conf. - Match blocks are conditional. A
Matchblock applies its settings only to matching users, groups or addresses, until the nextMatchor the end of the file.
Two per-login scripts complete the picture:
~/.ssh/rcruns withshafter authentication and before the user's shell or command, whenPermitUserRCis on (the default)./etc/ssh/sshrcruns instead when the user has no~/.ssh/rc.~/.ssh/environmentsets variables for the session, but only whenPermitUserEnvironmentallows it.
Authentication itself also goes through PAM (/etc/pam.d/sshd), covered under PAM modules.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/System/Library/LaunchDaemons/ssh.plist | com.openssh.sshd, socket-activated; Disabled by default | Apple only (sealed system volume) |
/usr/sbin/sshd, /usr/libexec/sshd-keygen-wrapper | Server and launcher | Apple only (sealed) |
/var/db/com.apple.xpc.launchd/disabled.plist | Remote Login on or off | root |
/etc/ssh/sshd_config | Main configuration | root |
/etc/ssh/sshd_config.d/ | Drop-ins, read first in lexical order (100-macos.conf) | root |
/etc/ssh/sshrc | Login script for users without ~/.ssh/rc | root |
~/.ssh/rc, ~/.ssh/environment | Per-user login script and variables | the user |
/etc/pam.d/sshd | PAM stack for SSH logins | root |
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/) ornone. - AuthorizedKeysCommand set: any value other than
none. - Root login allowed:
PermitRootLogin yes. OpenSSH's defaultprohibit-passwordandforced-commands-onlyare 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/rcor/etc/ssh/sshrcexists. SSH environment file:~/.ssh/environmentexists. - 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,
Matchevaluation), so usesshd -T. - It does not flag
PasswordAuthentication,Port,ListenAddress,AllowUsers,GatewayPorts,PermitTunnelorAcceptEnv. - It does not follow
Includepaths outsidesshd_config.d. - It does not report whether Remote Login is on.
Cleaning up
- Preserve first:
sudo cp -Rp /etc/sshto case storage, savesudo sshd -Toutput, the rc and environment files anddisabled.plist, and hash the copies. - 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:'). - Delete planted
~/.ssh/rc,/etc/ssh/sshrcand~/.ssh/environmentfiles, and anyAuthorizedKeysCommandhelper and the keys files it served. - Review every account's
authorized_keys,/etc/pam.d/sshdand the account list. Turn Remote Login off if the Mac does not need it. - An intruder with root could read the host private keys. Regenerate them (remove
/etc/ssh/ssh_host_*_key*;sshd-keygen-wrapperrecreates 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
Related techniques
On Mac Forensics
The artifacts behind this technique, in the Mac Forensics reference: