Account Manipulation: SSH Authorized Keys

Adversaries may modify the SSH authorized_keys file to maintain persistence on a victim host. Linux distributions and macOS commonly use key-based authentication to secure the authentication process of SSH sessions for remote management. The authorized_keys file in SSH specifies the SSH keys that can be used for logging into the user account for which the file is configured. This file is usually found in the user's home directory under <user-home>/.ssh/authorized_keys.[1] Users may edit the system’s SSH config file to modify the directives PubkeyAuthentication and RSAAuthentication to the value "yes" to ensure public key and RSA authentication are enabled. The SSH config file is usually located under /etc/ssh/sshd_config.

Adversaries may modify SSH authorized_keys files directly with scripts or shell commands to add their own adversary-supplied public keys. In cloud environments, adversaries may be able to modify the SSH authorized_keys file of a particular virtual machine via the command line interface or rest API. For example, by using the Google Cloud CLI’s "add-metadata" command an adversary may add SSH keys to a user account.[2][3] Similarly, in Azure, an adversary may update the authorized_keys file of a virtual machine via a PATCH request to the API.[4] This ensures that an adversary possessing the corresponding private key may log in as an existing user via SSH.[5][6] It may also lead to privilege escalation where the virtual machine or instance has distinct permissions from the requesting user.

Where authorized_keys files are modified via cloud APIs or command line interfaces, an adversary may achieve privilege escalation on the target virtual machine if they add a key to a higher-privileged user.

SSH keys can also be added to accounts on network devices, such as with the ip ssh pubkey-chain Network Device CLI command.[7]

ID: T1098.004
Sub-technique of:  T1098
Platforms: IaaS, Linux, Network, macOS
Contributors: Arad Inbar, Fidelis Security; Austin Clark, @c2defense; Dror Alon, Palo Alto Networks; Or Kliger, Palo Alto Networks; Tony Lambert, Red Canary
Version: 1.3
Created: 24 June 2020
Last Modified: 03 October 2023

Procedure Examples

ID Name Description
S0482 Bundlore

Bundlore creates a new key pair with ssh-keygen and drops the newly created user key in authorized_keys to enable remote login.[8]

G1006 Earth Lusca

Earth Lusca has dropped an SSH-authorized key in the /root/.ssh folder in order to access a compromised server with SSH.[9]

S0468 Skidmap

Skidmap has the ability to add the public key of its handlers to the authorized_keys file to maintain persistence on an infected host.[10]

G0139 TeamTNT

TeamTNT has added RSA keys in authorized_keys.[11][12]

S0658 XCSSET

XCSSET will create an ssh key if necessary with the ssh-keygen -t rsa -f $HOME/.ssh/id_rsa -P command. XCSSET will upload a private key file to the server to remotely access the host without a password.[13]

Mitigations

ID Mitigation Description
M1042 Disable or Remove Feature or Program

Disable SSH if it is not necessary on a host or restrict SSH access for specific users/groups using /etc/ssh/sshd_config.

M1022 Restrict File and Directory Permissions

Restrict access to the authorized_keys file.

M1018 User Account Management

In cloud environments, ensure that only users who explicitly require the permissions to update instance metadata or configurations can do so.

Detection

ID Data Source Data Component Detects
DS0017 Command Command Execution

Monitor executed commands and arguments to modify the authorized_keys or /etc/ssh/sshd_config files.

DS0022 File File Modification

Monitor for changes made to detect changes made to the authorized_keys file for each user on a system. Monitor for changes to and suspicious processes modifiying /etc/ssh/sshd_config.

DS0009 Process Process Creation

Monitor for suspicious processes modifying the authorized_keys or /etc/ssh/sshd_config files.

References