Appending an attacker’s public key to ~/.ssh/authorized_keys gives durable, password-less access that survives credential resets and looks like ordinary SSH. Cloud-focused and cryptomining campaigns do this routinely, and so do operators who want a fallback once the first foothold is lost. Detection combines file-change monitoring, inventory of authorised keys and logging of which key was used.
Signals at a Glance
| Signal | Where | ATT&CK |
|---|---|---|
| Write or append to ~/.ssh/authorized_keys or authorized_keys2 | auditd, EDR file events, FIM | T1098.004 |
| New .ssh directory created in a service or system account home | auditd, osquery | T1098.004 |
| Public key fingerprint in sshd logs not present in your key inventory | sshd auth log (LogLevel VERBOSE) | T1078 |
| sshd_config changes: AuthorizedKeysFile, PermitRootLogin, PubkeyAuthentication | auditd on /etc/ssh/ | T1098.004 |
| Key added by a non-interactive process (curl, bash -c, a web shell parent) | EDR process + file correlation | T1098.004 |
| Login by key from a source address never seen for that account | auth log, VPN and flow data | T1021.004 |
Watching the Key Files
Start with write detection on the files that grant access. auditd cannot wildcard home directories, so generate per-user watches from your inventory or configuration tooling, and always include root and system accounts such as www-data, postgres and git:
# /etc/audit/rules.d/ssh-keys.rules -w /root/.ssh/ -p wa -k ssh_keys -w /etc/ssh/sshd_config -p wa -k sshd_config -w /etc/ssh/sshd_config.d/ -p wa -k sshd_config # Per-user watches: generate these for each account that has a home directory, for example # -w /home/alice/.ssh/authorized_keys -p wa -k ssh_keys # -w /var/lib/postgresql/.ssh/ -p wa -k ssh_keys # Where kernel and EDR coverage allow, prefer a path regex over per-user rules: # TargetFilename matches /\.ssh/authorized_keys2?$ with a write or rename action # tune/validate: legitimate writers are provisioning tools and cloud-init; filter on exe and auid.
Real-time rules miss keys planted before monitoring began. Inventory the keys on every host with osquery and compare them with the keys you expect. The query returns the owner, key comment and file modification time, so a key added since the last review stands out:
-- osquery: authorized keys per user, with file modification time
SELECT u.username,
ak.key_file,
ak.algorithm,
ak.comment,
substr(ak.key, 1, 24) AS key_prefix,
datetime(f.mtime, 'unixepoch') AS file_mtime
FROM users u
JOIN authorized_keys ak USING (uid)
JOIN file f ON f.path = ak.key_file
ORDER BY f.mtime DESC;
-- tune/validate: key comments are attacker-controlled; compare key material or fingerprints, not comments.
Detecting Use of a Planted Key
- Set
LogLevel VERBOSEinsshd_config. sshd then logs the fingerprint of the key used on each successful public-key login, for exampleAccepted publickey for user from ip ... SHA256:.... - Maintain an allowlist of expected fingerprints per host or role, and alert on any successful key login with a fingerprint outside it.
- Alert on the combination of a recent authorized_keys write and a first-seen source IP for that account within the following hours.
- Review
AuthorizedKeysCommandandAuthorizedKeysFilein sshd_config, since an attacker who controls those can bypass per-user file monitoring entirely. - Remove the root cause during response: rotate the exposed credentials, remove unrecognised keys on every host, and check for other persistence listed in the cron and systemd guide.
- In cloud environments, enforce key management centrally (instance metadata, certificate-based SSH or a bastion) so local authorized_keys edits become an exception worth alerting on.
Use the SigmaHQ Linux file-event and SSH rules, Elastic’s SSH authorized-keys modification detections, Splunk ESCU content, MITRE CAR analytics and Atomic Red Team T1098.004 tests, along with the osquery authorized_keys table for inventory. Baseline your own environment, because provisioning tools and cloud-init legitimately write these files.
Build Detections That Fire
Mjolnir Security helps blue teams turn threat intelligence into high-fidelity detections across endpoint, network, identity and cloud telemetry.
- Detection Engineering We build and tune detections mapped to ATT&CK from your own telemetry.
- Threat Hunting as a Service Hypothesis-driven hunts for the behaviours on this page.
- 24/7 Incident Response Containment when a detection fires. Call +1 833 403 5875.
Stay ahead of emerging threats. Get notified when we publish new intelligence reports and advisories.
Subscribe to Alerts