At SSH login
SSH Authorized Keys: macOS Persistence via Remote Login
How SSH authorized_keys persistence works on macOS: Remote Login, ~/.ssh/authorized_keys, root keys and key options, and how to check and clean a Mac.
- MITRE ATT&CK
- T1098.004
- Tactics
- Persistence
- Runs
- At SSH login
- Set up by
- Users and root
- Locations
- ~/.ssh/authorized_keys
- /var/root/.ssh/authorized_keys
How it works
macOS ships OpenSSH's server, switched off. Turning on Remote Login enables the launchd job com.openssh.sshd. Remote Login lives in System Settings › General › Sharing on macOS 13 Ventura and later, and in System Preferences › Sharing before that. Admins can also run systemsetup -setremotelogin on.
The job's plist, /System/Library/LaunchDaemons/ssh.plist, ships with Disabled = true. Enabling Remote Login records an override in /var/db/com.apple.xpc.launchd/disabled.plist. launchd then listens on port 22 and, for each connection, starts /usr/libexec/sshd-keygen-wrapper, which creates host keys if they are missing and runs sshd. Remote Login's "Allow access for" setting restricts logins to the members of the com.apple.access_ssh group when "Only these users" is selected.
For public-key logins, sshd reads the files named by AuthorizedKeysFile. Apple's /etc/ssh/sshd_config sets it to .ssh/authorized_keys, relative to the home folder. OpenSSH's second default, authorized_keys2, is therefore not read on a stock Mac. Root's file is /var/root/.ssh/authorized_keys. The root account is disabled on a default install, but OpenSSH's default PermitRootLogin prohibit-password still accepts keys. Whether a root key actually works also depends on the Remote Login access list and the account's state, so test assumptions on the host.
Each line has optional options, the key type, the base64 key and a comment. The options attackers care about:
from="…"limits source addresses.command="…"forces a command.environment="…"sets variables, but only whenPermitUserEnvironmentis on.no-ptyandrestrictreduce what the session can do.expiry-timeends a key's validity.
With StrictModes (the default), sshd ignores the file if the home folder, ~/.ssh or the file itself is writable by other users, which is why attackers set modes 700 and 600.
A key login does not use the account password. Changing the password leaves the key working.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/System/Library/LaunchDaemons/ssh.plist | com.openssh.sshd; Disabled by default | Apple only (sealed system volume) |
/var/db/com.apple.xpc.launchd/disabled.plist | com.openssh.sshd → false while Remote Login is on | root (System Settings, launchctl, systemsetup) |
/var/db/dslocal/nodes/Default/groups/com.apple.access_ssh.plist | Remote Login access list | root |
/etc/ssh/sshd_config, /etc/ssh/sshd_config.d/ | AuthorizedKeysFile and other server settings | root |
~/.ssh/authorized_keys | Keys that log in as the user | the user (and root) |
/var/root/.ssh/authorized_keys | Keys that log in as root | root |
How attackers use it
Adding a key to a user's file. This needs no admin rights once Remote Login is on. The comment is often copied from a legitimate key so the line blends in:
mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ8w… alice@Alices-MacBook-Pro' >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Root access that survives password changes. With root, the intruder turns Remote Login on and plants a root key:
sudo systemsetup -setremotelogin on
sudo mkdir -p /var/root/.ssh
echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK2q… backup@mgmt' | sudo tee -a /var/root/.ssh/authorized_keys
Options that run code. A forced command runs a payload at each login before handing over a shell. With PermitUserEnvironment enabled in the sshd configuration, environment= can inject a library:
command="/Users/Shared/.u/run; exec /bin/zsh -l" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPq… deploy@ci
environment="DYLD_INSERT_LIBRARIES=/Users/Shared/.u/libx.dylib" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPq… alice@laptop
One key, many doors. The same attacker key goes into several accounts, sometimes including a hidden admin account created for the purpose (see local accounts). Every SSH login also runs the account's login shell files, so the key often comes paired with a shell startup payload.
How to check a host
On a live Mac (read-only):
sudo systemsetup -getremotelogin # needs Full Disk Access for Terminal on recent releases
launchctl print-disabled system | grep com.openssh.sshd
sudo lsof -nP -iTCP:22 -sTCP:LISTEN
dscl . -read /Groups/com.apple.access_ssh GroupMembership 2>/dev/null
sudo sh -c 'for f in /Users/*/.ssh/authorized_keys* /var/root/.ssh/authorized_keys*; do [ -f "$f" ] && { ls -le@ "$f"; ssh-keygen -lf "$f"; }; done'
grep -hi authorizedkeys /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* 2>/dev/null
log show --last 7d --predicate 'process == "sshd"' | grep -E 'Accepted (publickey|password)'
ssh-keygen -lf prints the SHA256 fingerprint of every key in a file. sshd logs the same fingerprint in its "Accepted publickey for alice from 198.51.100.20 … ED25519 SHA256:…" lines, so you can tie each login to a specific line, as long as the unified log still holds the period. Compare each key with your inventory: who issued it, from which machine, and when. A key nobody can account for is the finding, whatever its options. Compare file times with the account's other activity.
Offline, read Users/*/.ssh/authorized_keys*, private/var/root/.ssh/, private/etc/ssh/ and private/var/db/com.apple.xpc.launchd/disabled.plist from the collection. The macOS Persistence tool parses the key files in the browser. The collection command takes every user's .ssh, root's .ssh, /etc/ssh and the launchd overrides. Add com.apple.access_ssh.plist from the dslocal groups if you need the access list.
What macOS Persistence flags
Each key line becomes one entry. It is named after the comment, or the fingerprint when there is none, and carries the key type, the SHA256 fingerprint, the options, the from= restriction (or "any") and any forced command as facts. Both authorized_keys and authorized_keys2 are read.
- Key opens a root login: the key is in
/var/root/.ssh. - Key sets environment variables: the line uses an
environment=option. - Weak or obsolete key type: a DSA key, or an RSA key under 2048 bits.
- A forced command (
command="…") goes through the command heuristics: Downloads and runs code, Reverse or bind shell, Decodes and runs a payload, Interpreter with inline code, Injects a library (DYLD_), Runs from a temporary or shared folder, Runs from a hidden folder, Detaches from the session and the others. - Same key for users: the same fingerprint appears in more than one account's file on the host.
- Another key uses the comment “comment”: two different keys carry the same comment, the usual sign of a planted key imitating a legitimate one.
- program also runs from mechanisms: a forced command's program is also started by another mechanism.
- Changed date: the file was modified within the 7 days before the newest file in the collection.
Limits:
- The tool reads files only. It does not report whether Remote Login is on, who is in
com.apple.access_ssh, or whether an account can log in. - It does not check permissions against
StrictModes. - It reads no keys from a custom
AuthorizedKeysFilelocation, although the sshd configuration parser flags such a setting. - It cannot tell a legitimate key from a planted one without your inventory.
- A key without
from=is not flagged by itself.
Cleaning up
- Preserve first: copy the key files with their times (
sudo cp -p), save thessh-keygen -lfoutput,disabled.plistand the sshd log range, and hash the copies. - Delete the planted lines, or the whole file if no key in it is legitimate. End live sessions: list them with
pgrep -lf 'sshd:'and kill the attacker's. - If the Mac does not need Remote Login, turn it off in System Settings or with
sudo systemsetup -setremotelogin off. Otherwise limit "Allow access for" to named users. - Check every account and root,
sshd_configforAuthorizedKeysFile,AuthorizedKeysCommandandPermitUserEnvironment, and~/.ssh/rc. - Search the rest of the fleet for the attacker's fingerprint. Rotate the private keys stored on this Mac (
~/.ssh/id_*), since an intruder who could writeauthorized_keyscould also read them.
References
See also
Related techniques
On Mac Forensics
The artifacts behind this technique, in the Mac Forensics reference: