Establishing SSH key-based persistence; detecting it via logs and file changes; and responding with containment and mitigation.
After gaining access (Lab 2) and escalating privileges (Lab 3), the attacker establishes persistence on the NAS server. This lab demonstrates adding an SSH public key for re-entry, then shows how to detect and respond on defense.
Generate an SSH key pair on Kali and place the public key on the target NAS. Then configure ~/.ssh/authorized_keys
with proper permissions to enable key-based login.
~/.ssh
, write authorized_keys
, set secure perms.Test the persistence by reconnecting via SSH using the private key.
Note: You may still need to repeat environment setup/escalation steps from Lab 3 to regain root in a new session, but the key ensures durable access post-reboot.
Audit ~/.ssh/authorized_keys
for unexpected entries, strange comments, or keys referencing external systems (e.g., @kali
).
authorized_keys
.Correlate logins in /var/log/auth.log
(or platform equivalent) for key-based acceptances from unfamiliar sources.
Find recently modified authorized_keys
files (7-day window example):
sudo find / -type f -name authorized_keys -mtime -7 2>/dev/null
Check who is currently logged in and from where, then terminate suspicious sessions.
who
shows session origin.Kill related processes (example using kill -9
on PIDs tied to the compromised user):
Manually inspect and remove unknown keys from authorized_keys
; restrict permissions to minimum required.
Reset affected user passwords in case credentials were also compromised.
SSH key-based persistence is effective and stealthy. Even with passwords disabled (keys-only mode), an attacker who obtains write access can add a key and regain entry after reboot. If SSH isn’t required, disable the service entirely. Otherwise, harden: monitor authorized_keys
, restrict which users may use SSH, consider 2FA, use IDS/IPS rules for key-file changes, and alert on anomalous logins.