Responding to an unrecognised server login
An unrecognised server login shows up as a line in the login records, an open session or a new SSH key that the owner cannot match to anyone allowed on the server. Whoever made it had a shell on the machine, with the rights of the account it used. Intruders at that point commonly add their own public key to an account's authorized_keys file, so that they can come back after the first way in is closed.1
Keep the evidence first
Collect logs and other artifacts before any repair. Cleaning a system first loses what is held only in memory, and an attacker who notices the response may change method.2 A disk snapshot taken at the hosting provider keeps the state of the server for later reading. At Hetzner Cloud a snapshot is made by hand and stays until it is deleted, but attached volumes are not part of it.3 Keep a copy of the systemd journal as well, since its records are harder to alter than the plain files in /var/log, and discuss the incident only on a channel the attacker cannot read.2
Read the login records
The command last lists sign-ins from /var/log/wtmp, newest first, and lastb reads failed attempts from /var/log/btmp. A system writes these files only if they exist, so on some images they are missing.4 On Ubuntu the SSH server runs as ssh.service, and its log is in the journal;5 --since limits the output to a period and accepts words such as yesterday.6
last # sign-ins, newest first
sudo lastb # failed sign-ins
loginctl list-sessions # sessions open now
sudo journalctl -u ssh.service --since yesterdayA login stands out when its time does not fit the user's habits, or when it comes from an IP address that user does not normally sign in from.2
Close every way back in
Changing passwords one at a time, as problems appear, gives the attacker time to adapt; the reset has to cover every affected account and device in one planned step, once the evidence is saved.2
loginctl terminate-user ends every session of a user and kills all of that user's processes.7 usermod -L disables only the password, by putting ! in front of the stored hash. Locking the whole account also needs the expiry date set to 1.8 For an account the attacker created or used and the owner does not need:
sudo loginctl terminate-user anna
sudo usermod -L -e 1 annaRead the key files of every account, root included. When AuthorizedKeysFile is not set, the SSH server reads both ~/.ssh/authorized_keys and the older ~/.ssh/authorized_keys2, one key per line.9 Delete each key that does not belong to a known person and device. The server also runs ~/.ssh/rc or /etc/ssh/sshrc at each login when they exist, so an unknown file there is code that runs on every sign-in.9
Intruders also edit /etc/ssh/sshd_config to set PermitRootLogin to yes.1 Its default is prohibit-password, which refuses passwords for root.10 After correcting the file, sudo sshd -t tests it and sudo systemctl restart ssh.service applies it; a mistake found only after the restart can lock the owner out.5
Find out what was changed
The host artifacts worth collecting include running processes and services, scheduled tasks, logs and, on Linux, the cron entries of every host.2 These are the places where scheduled task persistence, a rogue administrator account or a crypto miner shows up. Blocking the attacker's IP address without finding how their program arrived treats a symptom and leaves the root cause in place, so they can return another way.2
Treat every secret that was stored on the server as known to the attacker: database passwords, API keys in environment variables, and private keys used to reach other machines. Responding to a leaked API key has the steps for replacing each one.
Rebuild when the attacker had root
Matrix.org, in its 2019 post-mortem, wrote that once an attacker has root the responsible course is to "salt the earth and start over afresh". It replaced every secret, isolated all old hosts, including those that looked untouched, and redeployed on new ones.11 A snapshot can be turned into a duplicate server for investigation, apart from production.3
Notable incident
On 13 March 2019 an attacker took over Matrix.org's public Jenkins server through a remote code execution flaw, CVE-2019-1003000. From there they reached a build machine and left a trap: when an engineer signed in with SSH agent forwarding on, it used the forwarded keys to add the attacker's key to root on other hosts. That happened on 4 April, and again through another developer on 5 April. On 10 April the team found an unknown key in /root/.ssh/authorized_keys2 on the Jenkins server, and the same key on 19 hosts. They took snapshots before acting, in case the attacker noticed and tried to destroy data or hide their tracks.11
The attacker had left a setuid root shell as a backdoor and had likely copied password hashes. The team rebuilt and replaced every secret, but missed one: clicking Change API Key while viewing the Cloudflare admin account changed their personal key instead. On 12 April the attacker used the old admin key to point the matrix.org DNS at a defacement page.11
Prevent a repeat
OpenSSH allows password login (PasswordAuthentication) and agent forwarding (AllowAgentForwarding) unless they are set to no.10 Matrix.org's own changes were to turn agent forwarding off on the servers, restrict SSH access, add two-factor sign-in for SSH and keep all keys in /etc/ssh/authorized_keys/%u, written only by their configuration tool, so users cannot add keys themselves.11 Shell history and all logs belong on a separate server, kept for at least a year.2 Further hardening is in SSH brute-force attack and Securing a Docker application on a self-managed server.
In SecFlash
SecFlash reports a key added since the previous audit as SF-SRV-208, an account other than root with user ID 0 as SF-SRV-301, and changes to /etc, system programs, /boot or root's SSH keys as SF-SRV-701, all on the Server page. Password login over SSH appears there as SF-SRV-201.
See also
- SSH authorized key persistence
- Scheduled task persistence
- Rogue administrator account
- SSH brute-force attack
- Responding to a leaked API key
- Securing a Docker application on a self-managed server
- Category: Incident response
References
- a b MITRE (version 1.4, 12 May 2026). "Account Manipulation: SSH Authorized Keys, T1098.004". MITRE ATT&CK.
- a b c d e f g ACSC, CCCS, NZ NCSC, CERT NZ, UK NCSC and CISA (1 September 2020). "Technical Approaches to Uncovering and Remediating Malicious Activity" (AA20-245A). Canadian Centre for Cyber Security.
- a b Hetzner (changed 3 September 2026). "Backups/Snapshots: Overview". Hetzner Docs.
- back util-linux (n.d.). "last(1)". Linux manual page.
- a b Canonical (n.d.). "OpenSSH server". Ubuntu Server documentation.
- back systemd (n.d.). "journalctl(1)". Linux manual page.
- back systemd (n.d.). "loginctl(1)". Linux manual page.
- back shadow-utils (n.d.). "usermod(8)". Linux manual page.
- a b OpenSSH (n.d.). "sshd(8)". OpenBSD manual pages.
- a b 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.
External links
- sshd_config(5), OpenBSD manual pages
- CISA Releases Joint Advisory on Approaches to Uncovering and Remediating Malicious Activity, CISA