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:
Defaultslines:env_reset, a list ofenv_keeplocale and terminal variables, and a lecture file.- Two rules:
root ALL = (ALL) ALLand%admin ALL = (ALL) ALL. Every member of theadmingroup 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.dline. The#is not a comment there./private/etc/sudoers.dexists 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.dfiles 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.Defaultsentries 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
| Path | What | Who can write it |
|---|---|---|
/usr/bin/sudo | setuid-root binary | Apple only (sealed system volume) |
/etc/sudoers | Main policy, mode 0440 | root (with visudo) |
/etc/sudoers.d/ | Drop-ins, read in lexical order at the include line | root |
/etc/pam.d/sudo, /etc/pam.d/sudo_local | How sudo authenticates | root |
/var/db/dslocal/nodes/Default/groups/admin.plist | Members of %admin, who get full sudo | root |
/etc/sudo_lecture | First-use lecture | root |
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
NOPASSWDrule whose command list isALL. - Passwordless sudo: a
NOPASSWDrule for specific commands. - Full sudo:
ALLcommands with a password. - Can run command as root: a rule that is not
ALLlists a program that can spawn a shell, such asvi,vim,less,find,awk,python,perl,env,tar,zip,git,cp,tee,dd,rsyncorssh. - sudo asks no password (!authenticate): a
Defaultsline contains!authenticate. - sudo keeps a loader variable: an
env_keepline keeps aDYLD_variable,LD_PRELOAD,LD_LIBRARY_PATH,PYTHONPATHorPERL5LIB.
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
%adminline. - 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.
*_Aliaslines are skipped, and a rule naming an alias is judged on the alias name. timestamp_timeout,!tty_ticketsandtimestamp_type=globalare not flagged, so read everyDefaultsline yourself.- A
sudoers.dfile 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 -cconfirms what sudo loads. @includetargets outside/etc/sudoers.dare not followed, and file ownership and modes are not checked.
Cleaning up
- Preserve first:
sudo cp -p /etc/sudoers /etc/sudoers.d/* /path/to/case/, hash the copies, and savesudo -l -U <user>for each account named in a rule, plus the sudo log range. - Keep a root shell open while you edit. Delete planted drop-ins with
sudo rm, and change/etc/sudoersonly throughsudo visudo, which refuses to save a broken file. Restore Apple's two rules andDefaults, then confirm withsudo visudo -c. - Expire cached authentications:
sudo -Kclears the invoking user's timestamp, and rebooting clears them all. Kill processes started through the rules. - Review
adminmembership,/etc/pam.d/sudoandsudo_local, and shell startup files forsudowrappers. - 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
Related techniques
On Mac Forensics
The artifacts behind this technique, in the Mac Forensics reference: