Privilege Escalation via Cron Jobs
Cron jobs are scheduled tasks that run automatically at specified intervals. When misconfigured, they can provide avenues for privilege escalation through writable scripts, wildcard misuse, environment variable manipulation, and path injection.
π Cron Jobs Privilege Escalation β Command Awareness
Comprehensive command reference observed during cron job security audits, investigations, and defensive monitoring. Shown for defensive awareness and security hardening purposes only.
π Cron Discovery & Enumeration
-
List system-wide cron jobs
Why used: View cron directories and files for initial assessment.ls -la /etc/cron* -
Check system crontab
Why used: Examine system-wide scheduled tasks and their configurations.cat /etc/crontab -
List cron directories
Why used: Discover all scheduled task locations.ls -la /etc/cron.d/ /etc/cron.daily/ /etc/cron.hourly/ /etc/cron.monthly/ /etc/cron.weekly/ -
Check user crontabs
Why used: Find user-specific cron jobs (requires appropriate permissions).ls -la /var/spool/cron/crontabs/ -
View specific user cron
Why used: Check scheduled tasks for specific users (requires privileges).crontab -l -u username -
Find cron jobs in logs
Why used: Review recent cron executions from system logs.grep -i "cron" /var/log/syslog 2>/dev/null | tail -20
π File Permission Analysis
-
Check writable cron scripts
Why used: Identify world-writable cron scripts that could be modified by any user.find /etc/cron* -type f -perm -o+w 2>/dev/null -
Find writable cron directories
Why used: Discover writable cron directories where malicious scripts could be placed.find /etc/cron* -type d -perm -o+w 2>/dev/null -
Check cron script ownership
Why used: Review file ownership to identify scripts running with elevated privileges.ls -l /etc/cron.d/ -
Find SUID/SGID scripts in cron
Why used: Identify scripts with special privileges that run via cron.find /etc/cron* -type f -perm /6000 2>/dev/null
π Script Content Inspection
-
Examine cron script content
Why used: Review script contents for security issues like command injection or path manipulation.cat /etc/cron.d/backup.sh -
Check for wildcard usage
Why used: Identify wildcard usage that could be exploited through file creation.grep -r "\*" /etc/cron* 2>/dev/null -
Search for relative paths
Why used: Find scripts using relative paths instead of absolute paths.grep -r "\./" /etc/cron* 2>/dev/null -
Look for environment variables
Why used: Identify environment variable usage that could be manipulated.grep -r "PATH\|HOME\|SHELL" /etc/cron* 2>/dev/null
π― Wildcard Exploitation Awareness
-
Example vulnerable cron entry
Why used: Demonstrates risky wildcard usage that could be exploited.# Vulnerable: Using wildcard without precautions * * * * * root tar -zcf /backups/backup.tar.gz /home/* -
Check for tar wildcard exploits
Why used: Find tar commands with wildcards that could be vulnerable to argument injection.grep -r "tar.*\*" /etc/cron* 2>/dev/null -
Example file creation test
Why used: Shows how specially named files could trigger command execution (for defensive understanding).touch /home/user/--checkpoint=1 touch /home/user/--checkpoint-action=exec=shell.elf -
Secure alternative
Why used: Demonstrates safer alternative to wildcard usage.# Secure: Using find instead of wildcard * * * * * root find /home -maxdepth 1 -type f -exec tar -zcf /backups/backup.tar.gz {} +
π£οΈ PATH & Environment Manipulation
-
Check cron environment variables
Why used: Review PATH and other environment variables set in crontab.grep -A5 -B5 "PATH=" /etc/crontab -
Find scripts without absolute paths
Why used: Identify commands called without absolute paths.grep -r "^[^#].*[[:space:]][a-z]" /etc/cron* 2>/dev/null | grep -v "/" -
Check for writable directories in cron PATH
Why used: Analyze PATH directories for writable entries.grep "^PATH=" /etc/crontab | cut -d= -f2 | tr ":" "\n" | xargs -I {} ls -ld {}
β‘ SUID/SGID Cron Scripts
-
Find SUID binaries called by cron
Why used: Identify SUID binaries executed via cron jobs.for file in $(grep -r "^[^#]" /etc/cron* 2>/dev/null | awk '{print $NF}' | grep "^/"); do if [ -f "$file" ]; then find "$file" -type f -perm -4000 2>/dev/null fi done -
Check script permissions
Why used: Review permissions on shell scripts executed by cron.ls -l $(grep -l "#!/bin/bash\|#!/bin/sh" /etc/cron* 2>/dev/null)
π File Overwrite & Symlink Attacks
-
Check for overwritable files
Why used: Find files that cron jobs write to, which could be symlink targets.grep -r ">\|>>" /etc/cron* 2>/dev/null | grep -v "#" -
Look for file creation patterns
Why used: Identify file creation operations that could be targeted.grep -r "touch\|echo.*>\|cat.*>" /etc/cron* 2>/dev/null -
Check log file permissions
Why used: Review permissions on log files written by cron.ls -l $(grep -r ">>.*log" /etc/cron* 2>/dev/null | awk '{print $NF}' | cut -d">" -f2)
π§ͺ Testing & Monitoring Commands
-
Monitor cron execution
Why used: Real-time monitoring of cron job executions.tail -f /var/log/syslog | grep -i cron -
Check running processes from cron
Why used: Identify currently running cron processes.ps aux | grep -E "(cron|CRON)" | grep -v grep -
Test cron script manually
Why used: Test cron script execution in controlled environment.sudo -u username /path/to/cron/script.sh -
Check script dependencies
Why used: Review shared library dependencies of cron binaries.ldd /path/to/cron/binary 2>/dev/null
π‘οΈ Defensive Hardening Commands
-
Set secure cron file permissions
Why used: Apply secure permissions to cron configuration files.chmod 644 /etc/crontab chmod 600 /etc/cron.d/* find /etc/cron* -type f -exec chmod 600 {} \; -
Remove world-writable permissions
Why used: Eliminate world-writable permissions from cron files.find /etc/cron* -type f -perm -o+w -exec chmod o-w {} \; -
Set proper ownership
Why used: Ensure cron files are owned by root.chown root:root /etc/crontab chown root:root /etc/cron.d/* -
Create cron audit script
Why used: Script to regularly audit cron security.#!/bin/bash echo "=== Cron Security Audit ===" echo "World-writable cron files:" find /etc/cron* -type f -perm -o+w 2>/dev/null echo "\nCron files not owned by root:" find /etc/cron* ! -user root -type f 2>/dev/null echo "\nSUID/SGID in cron directories:" find /etc/cron* -type f -perm /6000 2>/dev/null
π§ Secure Configuration Examples
-
Secure crontab PATH example
Why used: Demonstrates secure cron configuration with absolute paths.# Secure PATH configuration PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # Use absolute paths for all commands * * * * * root /usr/bin/logger "Cron job running" 0 2 * * * root /usr/bin/apt-get update && /usr/bin/apt-get upgrade -y -
Secure wildcard usage
Why used: Shows safe alternatives to dangerous wildcard patterns.# Secure: Using find with null separator 0 3 * * * root find /home -name "*.log" -type f -mtime +30 -delete # Secure: Explicit file listing 0 4 * * * root /usr/bin/tar -zcf /backup/home.tar.gz /home/user1 /home/user2 /home/user3 -
Secure script execution
Why used: Example of secure script execution with controlled environment.# Secure: Running script with full path and environment reset * * * * * root /bin/bash -c 'cd / && /usr/local/bin/backup.sh'
π Monitoring & Alerting Setup
-
Monitor cron file changes
Why used: Audit rules to detect modifications to cron configuration.# Using auditd -a always,exit -F path=/etc/crontab -F perm=wa -k cron_change -a always,exit -F dir=/etc/cron.d -F perm=wa -k cron_change -
Set up cron change alerts
Why used: Script to detect and alert on cron file modifications.#!/bin/bash # Monitor cron files for changes CRON_FILES="/etc/crontab /etc/cron.d/* /etc/cron.hourly/* /etc/cron.daily/* /etc/cron.monthly/* /etc/cron.weekly/*" for file in $CRON_FILES; do if [ -f "$file" ]; then if ! grep -q "$(md5sum "$file")" /var/log/cron_checksums.log 2>/dev/null; then echo "ALERT: Cron file $file has changed!" | mail -s "Cron Change Alert" admin@example.com md5sum "$file" >> /var/log/cron_checksums.log fi fi done -
Log cron execution details
Why used: Enhanced cron logging configuration.# In /etc/rsyslog.d/50-cron.conf cron.* /var/log/cron.log
π¨ Recovery & Incident Response
-
Check for malicious cron entries
Why used: Search for suspicious patterns indicating compromise.grep -E "(wget|curl|bash -i|sh -i|python -c|perl -e|nc.*-e|netcat.*-e)" /etc/cron* /var/spool/cron/* 2>/dev/null -
Disable all cron jobs temporarily
Why used: Emergency stop of cron service during incident response.systemctl stop cron pkill -9 cron -
Restore from backup
Why used: Restore clean cron configurations from backups.cp /backup/crontab.clean /etc/crontab cp -r /backup/cron.d.clean/* /etc/cron.d/
π€ Automated Security Tools
-
Use Lynis for cron auditing
Why used: Automated security audit tool with cron-specific checks.lynis audit system --tests-from-group "cron" -
Check with Tiger
Why used: Security scanner that includes cron configuration checks.tiger -c -
Custom cron auditor script
Why used: Example of automated cron security scanning.#!/bin/bash # Cron security scanner echo "Cron Security Scan - $(date)" echo "=================================" /usr/local/bin/cron_scanner.sh --check-permissions --check-wildcards --check-paths
β Key Security Rules for Cron
-
1. Always use absolute paths
# GOOD /usr/bin/logger "message" # BAD logger "message" -
2. Avoid dangerous wildcards
# GOOD find /home -name "*.log" -exec rm {} \; # BAD rm /home/*.log -
3. Set secure permissions
chmod 600 /etc/cron.d/* chown root:root /etc/cron.d/* -
4. Monitor for changes
auditctl -w /etc/crontab -p wa -k cron_change
π Quick Reference Checklist
- β Audit cron files monthly
- β Remove world-writable permissions
- β Use absolute paths exclusively
- β Avoid wildcards in dangerous commands
- β Monitor cron logs regularly
- β Keep backups of clean configurations
- β Implement change detection
- β Test cron scripts in safe environment