L23. Hunting Persistence on Linux
Video generating
Check back soon for the video lesson on Hunting Persistence on Linux
Course outlineLesson 23 of 23
Attackers rarely stop at one login. They install a way back in: a cron job, a systemd service, an SSH key or a line in a shell profile. Learn where Linux persistence hides, the commands that find it, and how to make each location alert you next time with auditd.
What Persistence Means
Persistence is anything an attacker sets up so they keep access after a reboot, a password change or a patch. On Linux it almost always means adding something to a place the system runs automatically. MITRE ATT&CK lists these as techniques under the Persistence tactic (TA0003), and the same handful of locations come up in nearly every real incident.Before you hunt on a system you think is compromised, collect evidence first (see the incident response lesson): your commands change timestamps, and a careful attacker may be watching.
Where Persistence Hides
| Location | ATT&CK technique | First command to run |
|---|---|---|
| Cron jobs | T1053.003 Cron | ls -la /etc/cron.* /etc/crontab /var/spool/cron |
| systemd services and timers | T1543.002 Systemd Service, T1053.006 Systemd Timers | systemctl list-timers --all |
| SSH authorized_keys | T1098.004 SSH Authorized Keys | sudo find / -name 'authorized_keys*' 2>/dev/null |
| Shell startup files | T1546.004 Unix Shell Configuration Modification | ls -la /etc/profile.d/ |
| New or modified accounts | T1136.001 Local Account | awk -F: '$3 == 0' /etc/passwd |
| Dynamic linker preload | T1574.006 Dynamic Linker Hijacking | cat /etc/ld.so.preload |
Cron
# System-wide cron files and directories
cat /etc/crontab
ls -la /etc/cron.d /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly
# Every user's personal crontab
for user in $(cut -d: -f1 /etc/passwd); do
sudo crontab -l -u "$user" 2>/dev/null | sed "s/^/$user: /"
doneLook for jobs that download and run something (curl, wget), decode data (base64 -d), open network connections (nc, /dev/tcp/), or run files from /tmp, /var/tmp or /dev/shm.
systemd Services and Timers
# Enabled services and all timers
systemctl list-unit-files --type=service --state=enabled
systemctl list-timers --all
# Unit files changed in the last 7 days
sudo find /etc/systemd/system /usr/lib/systemd/system -name '*.service' -mtime -7 -ls
# Per-user units, which run without root
sudo find /home /root -path '*/.config/systemd/user/*' -type f -ls
# What a suspicious unit actually runs
systemctl cat suspicious.serviceA service whose ExecStart points into /tmp or a home directory, or whose name imitates a real service with a small spelling change, deserves a closer look.
SSH Keys
# Every authorized_keys file on the system
sudo find / -name 'authorized_keys*' -type f 2>/dev/null -exec ls -la {} \;
# Where sshd actually looks for keys (it can be changed)
sudo sshd -T | grep -i authorizedkeysCompare each key with the people who should have access. A key added to root's or a service account's authorized_keys survives every password change.
Shell Startup Files and Accounts
# Commands that run at every login
ls -la /etc/profile /etc/profile.d/ /etc/bash.bashrc
sudo grep -nE 'curl|wget|nc |base64|/dev/tcp' /etc/profile /etc/profile.d/* /etc/bash.bashrc /root/.bashrc /home/*/.bashrc /home/*/.profile 2>/dev/null
# Any account other than root with user ID 0 is a backdoor
awk -F: '$3 == 0 {print $1}' /etc/passwd
# Recently added accounts and sudo rights
sudo grep -E 'useradd|usermod' /var/log/auth.log /var/log/secure 2>/dev/null
sudo ls -la /etc/sudoers.d/
Library Preloading and Changed Binaries
/etc/ld.so.preload makes every program load a library first. On a normal system the file does not exist or is empty; a rootkit uses it to hide files and processes.cat /etc/ld.so.preload 2>/dev/null
# Compare installed files with the package database
sudo dpkg --verify # Debian and Ubuntu
sudo rpm -Va # RHEL familyPackage verification flags system binaries whose checksum no longer matches the package, which is how replaced tools such as a backdoored sshd or ps show up. Configuration files you edited yourself will also appear, so read the list rather than panicking at its length.
Make Each Location Alert You
Hunting finds what is already there. Audit rules (see the auditd lesson) tell you the moment someone adds more:
# /etc/audit/rules.d/60-persistence.rules
-w /etc/crontab -p wa -k persistence_cron
-w /etc/cron.d/ -p wa -k persistence_cron
-w /var/spool/cron/ -p wa -k persistence_cron
-w /etc/systemd/system/ -p wa -k persistence_systemd
-w /etc/ld.so.preload -p wa -k persistence_preload
-w /etc/profile.d/ -p wa -k persistence_shell
-w /etc/sudoers.d/ -p wa -k persistence_sudoLoad them with sudo augenrules --load, ship the audit log to your SIEM, and alert on the keys. Watch the authorized_keys file of every privileged account the same way.
References
- MITRE ATT&CK: Persistence (TA0003)
- MITRE ATT&CK: Scheduled Task/Job: Cron (T1053.003)
- MITRE ATT&CK: Create or Modify System Process: Systemd Service (T1543.002)
- MITRE ATT&CK: Account Manipulation: SSH Authorized Keys (T1098.004)
- MITRE ATT&CK: Hijack Execution Flow: Dynamic Linker Hijacking (T1574.006)
Continue Learning
- ✓Common Linux persistence locations: cron, systemd services and timers, SSH authorized_keys, shell startup files, new accounts and ld.so.preload
- ✓Collect evidence before hunting on a suspected compromised host, because hunting commands change timestamps
- ✓Any account other than root with UID 0 is effectively a root backdoor
- ✓dpkg --verify and rpm -Va compare installed files with the package database to find replaced binaries
- ✓auditd watch rules on persistence locations turn a one-time hunt into ongoing detection
1. Which file, if it exists and is not empty, makes every program load a library first and is a common rootkit technique?
2. Why does a key added to root's authorized_keys file survive a password reset?
3. Which command lists systemd timers, including inactive ones?