Skip to content

Privilege

Sudoers: macOS Privilege Persistence via sudo Rules

How sudoers rules give lasting root on macOS: /etc/sudoers, /etc/sudoers.d drop-ins, NOPASSWD and Defaults abuse, and how to check and restore a Mac.

MITRE ATT&CK
T1548.003
Tactics
Privilege escalation, Persistence, Defense evasion
Runs
Privilege
Set up by
root
Locations
  • /etc/sudoers
  • /etc/sudoers.d/*

How it works

macOS ships sudo 1.9 (/usr/bin/sudo, setuid root; 1.9.17p2 on macOS 27). Its policy lives in /etc/sudoers (/private/etc/sudoers, mode 0440, owned by root:wheel), edited with visudo.

Apple's stock file has three parts:

  • Defaults lines: env_reset, a list of env_keep locale and terminal variables, and a lecture file.
  • Two rules: root ALL = (ALL) ALL and %admin ALL = (ALL) ALL. Every member of the admin group can therefore run anything as root with their own password, so promoting an account to administrator is already full sudo.
  • A final #includedir /private/etc/sudoers.d line. The # is not a comment there. /private/etc/sudoers.d exists and is empty on a clean install.

A rule reads who host = (runas) TAGS: commands. The rules that matter most:

  • The last match wins. When several rules match, sudo applies the last one. Drop-ins are read at the position of the include line, at the end of the file, so they override the main file.
  • Drop-in names. sudo reads sudoers.d files in lexical order and skips names that end in ~ or contain a ..
  • The timestamp. After a successful authentication, sudo does not ask again for a few minutes (timestamp_timeout), per terminal by default. Defaults entries can lengthen or widen that window.

Authentication itself is PAM's job (/etc/pam.d/sudo). Since macOS 14 Sonoma that stack includes sudo_local, the update-safe place to enable Touch ID for sudo, covered under PAM modules. sudo logs every command to the unified log, as process sudo.

Where it lives

PathWhatWho can write it
/usr/bin/sudosetuid-root binaryApple only (sealed system volume)
/etc/sudoersMain policy, mode 0440root (with visudo)
/etc/sudoers.d/Drop-ins, read in lexical order at the include lineroot
/etc/pam.d/sudo, /etc/pam.d/sudo_localHow sudo authenticatesroot
/var/db/dslocal/nodes/Default/groups/admin.plistMembers of %admin, who get full sudoroot
/etc/sudo_lectureFirst-use lectureroot

How attackers use it

Writing a sudoers rule needs root, so this is not a way to get root the first time. It is a way to keep root cheaply: a rule turns a normal-looking account, or any process running as that user, into root without a prompt.

Passwordless root for an account the intruder controls. A drop-in is less conspicuous than an edit to the main file:

echo 'alice ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/zz-maint
sudo chmod 440 /etc/sudoers.d/zz-maint

MITRE ATT&CK lists OSX/Dok as adding a NOPASSWD: ALL rule to /etc/sudoers.

A rule that looks narrow. Permission to run one editor, pager or file tool as root is root, because the tool can spawn a shell (sudo find . -exec /bin/sh \; -quit):

bob ALL=(root) NOPASSWD: /usr/bin/find, /usr/bin/less

Weakening the defaults instead of adding rules. These lines let the account's processes reuse a password the user typed elsewhere, or skip authentication outright. MITRE ATT&CK lists OSX/Proton as modifying the tty_tickets setting so that one sudo authentication covers every terminal:

Defaults !tty_tickets
Defaults timestamp_timeout=-1
Defaults:alice !authenticate
Defaults env_keep += "DYLD_INSERT_LIBRARIES"

No sudoers change at all. Adding an account to the admin group gives full sudo through Apple's own %admin line (see local accounts). A shell startup function named sudo captures the password instead of changing the policy.

How to check a host

On a live Mac (read-only; reading the policy needs root):

sudo visudo -c                          # parses sudoers and every included file it actually reads
sudo ls -la@ /etc/sudoers.d/
sudo cat /etc/sudoers /etc/sudoers.d/*
sudo -l -U alice                        # what a given account may run
dscl . -read /Groups/admin GroupMembership
stat -f '%SB %Sm %Sp %Su:%Sg %N' /etc/sudoers /etc/sudoers.d/*
log show --last 7d --predicate 'process == "sudo"' | grep -E 'COMMAND=|incorrect password'

visudo -c prints one "parsed OK" line per file it reads, which tells you whether a suspicious drop-in is live or skipped by sudo's file-name rule. Compare /etc/sudoers with a clean Mac of the same release: anything beyond the root and %admin rules and Apple's Defaults was added. The sudo log lines (USER=root ; COMMAND=…) show what the rules were used for and when. Check every account named in a rule against the account list and admin membership.

Offline, read private/etc/sudoers, private/etc/sudoers.d/*, private/etc/pam.d/sudo* and the admin group record from the collection. The macOS Persistence tool parses them in the browser. The collection command takes all of them.

What macOS Persistence flags

Each rule becomes one entry named "who → runas", with the tags as a fact. Defaults lines are listed only when they weaken sudo as described below.

  • Passwordless sudo to anything: a NOPASSWD rule whose command list is ALL.
  • Passwordless sudo: a NOPASSWD rule for specific commands.
  • Full sudo: ALL commands with a password.
  • Can run command as root: a rule that is not ALL lists a program that can spawn a shell, such as vi, vim, less, find, awk, python, perl, env, tar, zip, git, cp, tee, dd, rsync or ssh.
  • sudo asks no password (!authenticate): a Defaults line contains !authenticate.
  • sudo keeps a loader variable: an env_keep line keeps a DYLD_ variable, LD_PRELOAD, LD_LIBRARY_PATH, PYTHONPATH or PERL5LIB.

Apple's stock lines are not findings. In /etc/sudoers itself, root ALL = (ALL) ALL and %admin ALL = (ALL) ALL (whitespace ignored) are treated as macOS's baseline. Their Full sudo drops to information level and they are marked as installed by macOS. The same lines in a sudoers.d file, or any other line in /etc/sudoers such as %admin ALL=(ALL) NOPASSWD: ALL, keep their full severity.

Related layers:

  • The accounts layer marks administrators (UID 500 and above) with Member of group, which on a stock Mac means full sudo through the %admin line.
  • Changed date: the file was modified within the 7 days before the newest file in the collection.
  • program also runs from mechanisms: a permitted non-system program is also started by another mechanism.

Limits:

  • Aliases are not expanded. *_Alias lines are skipped, and a rule naming an alias is judged on the alias name.
  • timestamp_timeout, !tty_tickets and timestamp_type=global are not flagged, so read every Defaults line yourself.
  • A sudoers.d file sudo skips (a . in its name, or a trailing ~) is still listed, with Ignored by sudo (file name) and its other flags shown as info: the rules never apply, but someone wrote them. visudo -c confirms what sudo loads.
  • @include targets outside /etc/sudoers.d are not followed, and file ownership and modes are not checked.

Cleaning up

  1. Preserve first: sudo cp -p /etc/sudoers /etc/sudoers.d/* /path/to/case/, hash the copies, and save sudo -l -U <user> for each account named in a rule, plus the sudo log range.
  2. Keep a root shell open while you edit. Delete planted drop-ins with sudo rm, and change /etc/sudoers only through sudo visudo, which refuses to save a broken file. Restore Apple's two rules and Defaults, then confirm with sudo visudo -c.
  3. Expire cached authentications: sudo -K clears the invoking user's timestamp, and rebooting clears them all. Kill processes started through the rules.
  4. Review admin membership, /etc/pam.d/sudo and sudo_local, and shell startup files for sudo wrappers.
  5. Writing sudoers took root. Treat the Mac as root-compromised: rotate the passwords of every account in a rule or in admin, and look for the intruder's other persistence before you call it clean.

References

See also

On Mac Forensics

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