At authentication
PAM Modules: macOS Persistence via /etc/pam.d
How PAM stacks in /etc/pam.d and sudo_local are abused on macOS for backdoor passwords and credential theft, and how to check and clean a Mac.
- MITRE ATT&CK
- T1556.003
- Tactics
- Persistence, Credential access, Defense evasion
- Runs
- At authentication
- Set up by
- root
- Locations
- /etc/pam.d/*
- /etc/pam.d/sudo_local
- /etc/pam.conf
- /usr/local/etc/pam.d/*
How it works
macOS authenticates many services through OpenPAM. A program that uses PAM names a service; OpenPAM reads that service's policy and runs its modules in order for four classes: auth (prove who you are), account (may you log in now), password (change it) and session (set up the session). Each line reads class control module [arguments]. The control flag (required, requisite, sufficient, binding, optional) decides how a module's result counts, and class include other-service pulls in another service's chain.
The pam.conf(5) page on macOS gives the search order: /etc/pam.d/<service>, then /etc/pam.conf, then /usr/local/etc/pam.d/<service> and /usr/local/etc/pam.conf. A service with no policy falls back to other, which denies everything. A bare module name such as pam_opendirectory.so resolves to Apple's copy in /usr/lib/pam (on disk pam_opendirectory.so.2); a full path loads any library, anywhere.
Modules are libraries loaded into the calling process, so they run with its privileges and see what it sees: root inside sudo, su and sshd, and the password the user types. The services that matter most on a Mac:
sudo,su,login(Terminal logins);sshd, because macOS enablesUsePAM yesin/etc/ssh/sshd_config.d/100-macos.conf;authorizationand its_la,_ctk,_aksvariants (password checks made through Authorization Services);screensaver*(screen unlock),checkpw,passwd,cups,smbd,rvncserver(Screen Sharing).
Two releases changed the picture. macOS 11 Big Sur put /usr/lib/pam on the sealed system volume, so nobody can add or replace Apple's modules; a foreign module must be referenced by full path. macOS 14 Sonoma added /etc/pam.d/sudo_local: the stock sudo file starts with auth include sudo_local, Apple ships sudo_local.template, and sudo_local survives system updates, while edits to the stock files can be reverted by an update. That makes sudo_local the natural place for a durable change, legitimate (Touch ID for sudo) or not.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/usr/lib/pam/pam_*.so.2 | Apple's modules (pam_opendirectory, pam_tid, pam_permit, pam_deny…) | Nobody: sealed system volume |
/etc/pam.d/<service> | Per-service stacks shipped by macOS (/etc is /private/etc) | root |
/etc/pam.d/sudo_local | Local additions to sudo, kept across updates (Sonoma and later) | root |
/etc/pam.conf | Single-file policy, read when /etc/pam.d has no file for the service | root |
/usr/local/etc/pam.d/<service>, /usr/local/etc/pam.conf | Lower-priority policy locations | root, or whoever owns /usr/local |
/usr/local/lib/pam/, /opt/homebrew/lib/pam/ | Third-party modules such as pam_reattach or pam-u2f | root, or the account that owns the Homebrew prefix |
There are no per-user PAM files. Homebrew's prefix belongs to the user who installed it, so a module referenced from there is only as trustworthy as that account.
How attackers use it
Every change here needs root. Attackers use it to keep a way back to root that survives a password reset, or to harvest passwords.
Accept any password for sudo. One line in sudo_local is enough, and it outlives macOS updates:
# /etc/pam.d/sudo_local
auth sufficient pam_permit.so
Any account allowed by sudoers now gets root with any password. The same line at the top of another service's auth chain opens that service.
Capture passwords with a planted module. A small library that calls pam_get_authtok and writes the result somewhere is placed outside the sealed volume and added as optional, so authentication still works normally:
# /etc/pam.d/sshd, line added at the top
auth optional /usr/local/lib/pam/pam_audit_helper.so
auth optional pam_krb5.so use_kcminit
auth optional pam_ntlm.so try_first_pass
Run a program at every authentication. macOS does not ship pam_exec, so a line naming it fails unless the attacker brings a module. A build that passes the password on standard input (FreeBSD's pam_exec has an expose_authtok option) turns any script into a credential logger:
auth optional /usr/local/lib/pam/pam_exec.so expose_authtok /Users/Shared/.cache/collect.sh
Loosen checks without adding anything. Deleting the pam_group.so … root_only line from su (it limits who may become root to members of admin and wheel) or turning required into optional changes who gets in without a single foreign file. Only a comparison with stock files catches edits like these.
PAM changes pair naturally with sudoers rules and SSH server settings: a responder who restores only one leaves the other.
How to check a host
On a live Mac, read the stacks and look for anything that is not a bare Apple module name:
ls -le@ /etc/pam.d /usr/local/etc/pam.d /etc/pam.conf 2>/dev/null
grep -nEv '^[[:space:]]*(#|$)' /etc/pam.d/*
grep -nE '^[[:space:]]*-?[a-z]+[[:space:]]+[^[:space:]]+[[:space:]]+/' /etc/pam.d/* # modules by full path
stat -f '%Sm %Sc %N' /etc/pam.d/* # modified / changed
cat /etc/pam.d/sudo_local 2>/dev/null
For every module named by path, check who built it and whether an installer put it there:
codesign -dv --verbose=4 /usr/local/lib/pam/pam_example.so
pkgutil --file-info /usr/local/lib/pam/pam_example.so
shasum -a 256 /usr/local/lib/pam/pam_example.so
Compare /etc/pam.d file by file with a clean Mac on the same build (sw_vers): stock stacks reference only modules in /usr/lib/pam, and sudo_local does not exist until someone creates it. For timing, log show --last 7d --predicate 'process == "sudo"' lists sudo use, and file times on the stacks tell you when they last changed (an OS update rewrites them all at once).
Offline, the collection command already archives /private/etc/pam.d; add /private/etc/pam.conf, /usr/local/etc/pam.d and every module path a stack names. Then compare against a reference set from the same macOS build, hash the modules, check their signatures on an analysis Mac, and line up the file times with installer receipts and unified logs. macOS Persistence reads the stacks from the collection for you, offline.
What macOS Persistence flags
The tool reads every file in /etc/pam.d and reports one entry per service, named pam: <file>, with the stack (class, control and module of each line) and the line numbers of anything flagged.
- pam_exec runs command (high): a line whose module path contains
pam_exec.so; the parameter is the module's arguments. - Module loaded from module (high): a module named by an absolute path outside
/usr/lib/pam/. Legitimate Homebrew modules referenced fromsudo_localtrigger it too; that is intended, since they run insidesudo. - pam_permit accepts anyone (high): an
authline loadingpam_permit.sowithsufficient(or a Linux-stylesuccess=control). The stockaccount required pam_permit.soinsudois not flagged. - Command and path heuristics on the module arguments, for example Downloads and runs code, Reverse or bind shell, Runs from a temporary or shared folder and Runs from a hidden folder.
- Changed date (low): the file was modified in the 7 days before the newest file in the collection. A macOS update in that window explains it for every stock file at once.
- Differs from package package (medium): only when an installer receipt lists the file and its checksum no longer matches. macOS's own
/etcfiles have no receipts, so stock stacks get no package verdict and never Not installed by any package.
Limits: it reads /etc/pam.d, /etc/pam.conf and /usr/local/etc/pam.d (the collection command takes the first two; add the third if it exists), and pam_permit accepts anyone covers binding as well as sufficient. It does not compare files with Apple's stock versions, so a deleted line or required turned into optional raise nothing. It does not hash or verify module binaries, and it reads files, not what a running sshd or sudo has loaded.
Cleaning up
- Preserve first: copy
/etc/pam.d,/etc/pam.conf,/usr/local/etc/pam.dand every module named by path with their times (dittoorcp -Rp), hash them, and export the unified log forsudo,suandsshdaround the change. - Open a root shell (
sudo -s) in a second window and keep it open while you edit: a brokensudoorsustack can lock you out of root. - Restore stock files from a clean Mac on the same build, remove lines in
sudo_localyou cannot account for, and delete foreign modules. Testsudo,suand SSH logins in new sessions before closing the root shell. - Check the neighbours: authorization plugins,
sudoers, SSH keys and local accounts. - Treat every password typed into an affected service since the change as exposed: rotate local and directory passwords, and any that were reused elsewhere.
References
See also
Related techniques
On Mac Forensics
The artifacts behind this technique, in the Mac Forensics reference: