SSH authorized key persistence
| Class | Persistence |
|---|---|
| Also known as | SSH key injection |
| MITRE ATT&CK | T1098.004 Account Manipulation: SSH Authorized Keys |
| Typical severity | Critical |
| Affects | Linux and macOS servers, ESXi hosts, cloud virtual machines and network devices that accept SSH key login |
An attacker who has taken control of a server writes one line, their own public key, into an account's authorized_keys file. From then on they can sign in to that account over SSH with the matching private key, without a password.1 The technique is called SSH authorized key persistence because the line outlives the fix for the weakness that let them in, and any change of passwords.
Description
SSH key login uses a key pair. The user keeps the private key and copies the public key, which is not secret, to the server. The SSH server then lets in anyone who proves they hold the private key matching one of the listed public keys.2
The OpenSSH server, sshd, reads the list from files in each user's home directory. By default it reads both ~/.ssh/authorized_keys and ~/.ssh/authorized_keys2, the second a legacy location from the move from SSH1 to SSH2.34 Each line holds one key, in up to four fields separated by spaces: optional options, the key type, the key itself in base64, and a comment. The comment is only a label and plays no part in login.5 The setting AuthorizedKeysFile in sshd_config can point to other files, and AuthorizedKeysCommand can name a program that returns keys; that program is consulted only when no listed file holds a matching key.3
Mechanism
The technique needs a first way in: a flaw that runs commands on the server, a stolen password, or access to the cloud account that manages the machine. With command execution as root, one line is enough:
echo "ssh-ed25519 AAAAC3Nz...attacker deploy@build" >> /root/.ssh/authorized_keysRecorded attacks write keys in this way, with scripts or shell commands such as echo redirected into the file.1 Some also change sshd_config to set PubkeyAuthentication and PermitRootLogin to yes.1 The comment field can carry any text, so a planted key can be labelled to look like a deploy key or a colleague's laptop.
A second path, through the cloud provider, needs no shell on the machine at all. The Google Cloud CLI command add-metadata and a PATCH request to the Azure virtual machine API can both place keys on an instance.1 When a cloud user adds a key to an account with more rights on the machine than the cloud user has, the technique also raises their privileges.1
Impact
The planted key carries the rights of the account it sits in: root's file gives root, a normal user's file gives that user. Because the key is checked at each login and needs no password, the attacker keeps access through password changes, software updates and the closing of the original entry point.
Notable incidents
On 13 March 2019 an attacker took over the public Jenkins server of Matrix.org through a remote code execution flaw, CVE-2019-1003000.4 From there they set a trap on a build machine that used the forwarded SSH credentials of anyone who logged in to add the attacker's key to root on other hosts. A team member triggered it on 4 April. On 10 April the team found an unknown key in /root/.ssh/authorized_keys2 on the Jenkins server; it was missing from their key database and sat in the legacy file. The same key was present on 19 hosts. The team removed the keys around 2am on 11 April, then rebuilt the hosts and replaced every secret. Their write-up states that monitoring of the SSH configuration could have caught the breach at once.4
Malware and campaigns that have planted keys this way include Skidmap, TeamTNT, Earth Lusca and Salt Typhoon; Salt Typhoon added them under root on compromised network devices.1
Detection
Print the effective server configuration to see which key files and commands sshd uses, then read each of those files for root and every user with a login shell, including authorized_keys2:5
sudo sshd -T | grep -i authorizedkeys
sudo cat /root/.ssh/authorized_keys /root/.ssh/authorized_keys2Compare every line with the keys you hold. ssh-keygen -l -f prints the fingerprint of a public key file, which gives a short value to compare.2 Judge a key by its type and key data, never by its comment. Look also at the options at the start of each line: a command= option runs a fixed command whenever that key logs in.5 On cloud machines, check who holds the permission to change instance metadata, since keys can arrive that way.1
Prevention
Keep key files under root
After the 2019 breach, Matrix.org stopped users from managing their own keys and moved every account's keys to files written only by their configuration tool, using a token that stands for the user name.4 A setting with one path also stops sshd from reading authorized_keys2, since the default lists both files.3 On Ubuntu, settings go in a file under /etc/ssh/sshd_config.d/:6
# /etc/ssh/sshd_config.d/10-keys.conf
AuthorizedKeysFile /etc/ssh/authorized_keys/%u
PermitRootLogin noCheck the file with sudo sshd -t before restarting with sudo systemctl restart ssh.service; a mistake on a server reached only by SSH can lock you out.6 Setting PermitRootLogin to no stops root logging in over SSH at all, which also makes a key planted in root's own file useless.1
File permissions
Only the owner should be able to write an authorized_keys file; Ubuntu gives chmod go-w .ssh/authorized_keys to remove write access for others.6 With StrictModes on, the default, sshd refuses a key file when it, the .ssh directory or the home directory can be written by other users.5
Narrow each key
Options in front of a key limit what it can do. restrict turns off forwarding, terminal allocation and the user's ~/.ssh/rc script; from= accepts the key only from listed hosts or addresses; expiry-time= sets a date after which the key stops working; command= ties the key to one command, such as a backup.5
restrict,from="203.0.113.10",expiry-time="20270101" ssh-ed25519 AAAAC3Nz... deployIn a cloud account, give the right to change instance metadata only to the people and tools that need it, in line with Principle of least privilege.1
Prompt for a coding agent
On this server, list every authorized_keys and authorized_keys2 file for root and for each user with a login shell, and the AuthorizedKeysFile and AuthorizedKeysCommand values from "sudo sshd -T". For each key line, print the user, the file, the key type, the fingerprint, the comment and any options such as command= or from=. Do not change or delete anything. Mark lines that appear in more than one account.
In SecFlash
SecFlash reports a key added since the last server audit as SF-SRV-208 on the Server page, and a key added to an account's authorized keys as SF-DET-101 on the Detections page. A change to root's SSH keys also counts toward SF-SRV-701, changed system files, on the Server page.
See also
- Responding to an unrecognised server login
- SSH brute-force attack
- Scheduled task persistence
- Rogue administrator account
- Securing a Docker application on a self-managed server
- Category: Persistence
References
- a b c d e f g h i MITRE (version 1.4, 12 May 2026). "Account Manipulation: SSH Authorized Keys, T1098.004". MITRE ATT&CK.
- a b c OpenSSH (n.d.). "sshd_config(5)". OpenBSD manual pages.
- a b c d Matrix.org team (8 May 2019). "Post-mortem and remediations for Apr 11 security incident". Matrix.org blog.
- a b c d e OpenSSH (n.d.). "sshd(8)". OpenBSD manual pages.
- a b c Canonical (n.d.). "OpenSSH server". Ubuntu Server documentation.
External links
- AUTHORIZED_KEYS FILE FORMAT, sshd(8), OpenBSD manual pages