Logrotate Exploitation for Privilege Escalation
Logrotate runs as root via cron or a systemd timer to rotate log files. It reads configuration files that define what to rotate and optionally run scripts before or after rotation via postrotate/prerotate directives. Two exploitation paths exist: directly modifying writable config files to inject commands, or exploiting a race condition in logrotate's create mode using the logrotten tool.
Logrotate Privilege Escalation on Linux
Two main attack paths exist:
- Writable config file: inject a
postrotatecommand that runs as root - Race condition (logrotten): exploit the window between log rename and creation in
createmode - Writable log directory: manually or automatically win the race to plant a symlink
Confirm logrotate is installed and check its configuration:
Check for writable config files: this is the direct path:
Check write access to log files or directories that logrotate processes: required for the logrotten race condition:
Technique 1: Writable Config File
If any file in /etc/logrotate.d/ is writable, inject a postrotate command that runs as root:
Step 1: Confirm write access:
Step 2: Read the existing config to understand the structure:
Step 3: Inject a postrotate payload:
Or replace the entire config file:
/etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
postrotate
cp /bin/bash /tmp/.rootbash; chmod u+s /tmp/.rootbash
endscript
}
Step 4: Wait for logrotate to run, or trigger it manually if you have permission:
After it fires:
Technique 2: logrotten Race Condition
logrotten exploits a race condition in logrotate's create mode. When logrotate rotates a log file, there is a brief window between renaming the old file and creating the new one. If the log directory is writable, you can replace the new log file with a symlink to a logrotate config file before logrotate creates it: causing logrotate to write a malicious config and then read it.
Affected versions: logrotate <= 3.15.1
Step 1: Check version:
Step 2: Confirm you can write to the log directory logrotate processes:
Step 3: Get logrotten:
Step 4: Create the payload:
/tmp/payload
#!/bin/bash
cp /bin/bash /tmp/.rootbash
chmod u+s /tmp/.rootbash
Step 5: Run logrotten against a writable log file:
In a second terminal, trigger log rotation:
Or simply wait for the scheduled rotation.
Step 6: After successful race:
Technique 3: Writable Log Directory + create Mode
If logrotate uses create mode (the default) and you have write access to the log directory, the race window is exploitable even without logrotten in some configurations:
When logrotate renames access.log to access.log.1 and before it creates the new access.log, replace the path with a symlink:
Timing this manually is unreliable: use logrotten for reliable exploitation.
References
-
whotwagner Race condition exploit tool for logrotate create mode.
-
logrotate(8) ManualMAN: logrotate(8) Manual, man7.org/linux/man-pages/man8/logrotate.8.html (opens in new tab)
Configuration directives including postrotate, create mode, and script execution.