Each new shell
Shell History Tampering on macOS: zsh_history
How attackers stop zsh recording commands on macOS (history linked to /dev/null, HISTFILE, SAVEHIST), what Terminal's zsh_sessions keep, and how to detect it.
- MITRE ATT&CK
- T1070.003
- Tactics
- Defense evasion
- Runs
- Each new shell
- Set up by
- Any user
- Locations
- ~/.zsh_history
- ~/.zsh_sessions/*.history
- ~/.bash_history
- /private/var/root/.zsh_history
- /private/etc/zshrc
How it works
zsh has been the default login shell for new accounts since macOS Catalina 10.15; older accounts may still use bash. Apple's /etc/zshrc sets the history file and its sizes:
HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
HISTSIZE=2000
SAVEHIST=1000
zsh keeps commands in memory and writes up to SAVEHIST of them to HISTFILE when the shell exits, unless options such as INC_APPEND_HISTORY or SHARE_HISTORY make it write as it goes. A shell that is killed with SIGKILL never writes.
Terminal.app adds a second layer. /etc/zshrc sources /etc/zshrc_Apple_Terminal, which keeps per-window history: while a session runs, commands go to ~/.zsh_sessions/<TERM_SESSION_ID>.historynew; when the shell exits they are appended both to ~/.zsh_history and to ~/.zsh_sessions/<TERM_SESSION_ID>.history, so a restored window gets its own history back. Files in ~/.zsh_sessions not modified for two weeks are deleted, at most once a day, when a Terminal shell exits. SHELL_SESSIONS_DISABLE=1 (usually in ~/.zshenv) turns the mechanism off. Other terminal apps (iTerm2, IDE terminals) do not set TERM_PROGRAM=Apple_Terminal and get only the plain HISTFILE. bash has the same design with ~/.bash_history, ~/.bash_sessions and /etc/bashrc_Apple_Terminal.
Tampering with history is not persistence in itself: it is what hands-on-keyboard intruders do so that their own commands are not recorded. It is listed here because the usual form, a history file linked to /dev/null, is a file on disk that persists and silences every future shell of that account.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/private/etc/zshrc, /private/etc/zshrc_Apple_Terminal | Apple's zsh defaults and Terminal session code | root (/etc is not on the sealed volume) |
~/.zshenv, ~/.zprofile, ~/.zshrc, ~/.zlogin | The user's settings, where HISTFILE/SAVEHIST tricks go | The user |
~/.zsh_history | Shared zsh history | The user |
~/.zsh_sessions/*.history, *.historynew, *.session | Terminal.app per-window history and state | The user |
~/.bash_history, ~/.bash_sessions/ | bash history | The user |
/private/var/root/.zsh_history | root's history (sudo -s, sudo -i) | root |
How attackers use it
Replace the history file with a link to /dev/null, once, for every future shell:
ln -sf /dev/null ~/.zsh_history
Switch history off in a startup file, or only for the current shell:
unset HISTFILE # nothing is written
export HISTFILE=/dev/null
SAVEHIST=0 # nothing kept in the file
fc -p # new empty in-memory list, no file
setopt HIST_IGNORE_SPACE # then prefix commands with a space
kill -9 $$ # exit without writing
Linux habits show up too: history -c (zsh answers "bad option: -c"), set +o history, export HISTFILESIZE=0, or simply deleting or truncating the file (: > ~/.zsh_history). ZDOTDIR set in ~/.zshenv moves the history somewhere a responder will not look.
Apple's Terminal session code changes the outcome. We tested each trick on macOS 27 with a Terminal-style session (scratch home folder, TERM_PROGRAM=Apple_Terminal):
| Change | What was recorded |
|---|---|
~/.zsh_history linked to /dev/null | Nothing in ~/.zsh_history; the commands were in ~/.zsh_sessions/<id>.history |
HISTFILE=/dev/null in ~/.zshrc | Overridden at the first prompt: commands in both ~/.zsh_history and the session file |
unset HISTFILE in ~/.zshrc | Nothing anywhere |
SAVEHIST=0 in ~/.zshrc | Both files left empty |
SHELL_SESSIONS_DISABLE=1 plus HISTFILE=/dev/null | Nothing anywhere |
Apple's script can change between releases, so check /etc/zshrc_Apple_Terminal on the Mac in front of you. Sessions over SSH or in other terminal apps have no session files at all.
How to check a host
On a live Mac, read-only:
ls -la ~/.zsh_history ~/.bash_history # "-> /dev/null" is the tell
sudo find /Users /private/var/root -maxdepth 2 -name '.*history' -type l -exec ls -l {} \;
ls -la ~/.zsh_sessions/
grep -nE 'HISTFILE|SAVEHIST|HISTSIZE|HISTFILESIZE|SHELL_SESSION|ZDOTDIR|fc -p|HIST_IGNORE_SPACE|history' \
~/.zshenv ~/.zprofile ~/.zshrc ~/.zlogin /etc/zshenv /etc/zprofile /etc/zshrc 2>/dev/null
stat -f 'born %SB modified %Sm %N' ~/.zsh_history # times of the link itself
ls -lO ~/.zsh_history # uchg on an empty file also blocks writes
A link's own birth time dates when it was made: put it next to logins, SSH sessions and other persistence created in the same minutes. A history that is empty or stops abruptly while ~/.zsh_sessions holds recent .history files is worth reading line by line. Also compare ~/.zsh_history with the account's activity: an admin who uses Terminal daily with no history at all is suspicious.
From a collection: keep symlinks (collect with tar, as the collection command does, rather than copying folders), take ~/.zsh_sessions and the shell startup files, including root's /private/var/root/.zsh_history, which the command also takes. Rebuild what the history would have shown from other sources: unified log entries for sudo, SSH logins, EDR process telemetry, FSEvents, quarantine events. macOS Persistence checks the links offline.
What macOS Persistence flags
The tool never parses or shows the commands in a history file. It keeps ~/.zsh_history, ~/.bash_history, ~/.sh_history and ~/.history of every home folder and of /var/root only when they are links, either a symlink in a tar archive or a small file holding just a target path (how a link arrives from a disk image export), and makes one entry per link with parseHistoryLink:
- History sent to /dev/null: the link points exactly at
/dev/null. - History redirected to target: the link points at any other file.
Settings in startup files are reported where they live, on the shell startup files entry, launch job or cron line: Turns shell history off fires on HISTFILE=/dev/null, unset HISTFILE, HISTSIZE=0, set +o history and history -c. History entries get no package or recency flags.
Limits: Turns shell history off also covers SAVEHIST=0 and fc -p; not detected are HIST_IGNORE_SPACE, SHELL_SESSIONS_DISABLE, a moved ZDOTDIR, and a history file that was deleted, truncated or made immutable. The tool does not read ~/.zsh_sessions, and a folder dropped into the browser may lose symlinks, so a missing finding is not proof the history is intact.
Cleaning up
- Preserve first:
tarthe home's history files (links stay links),~/.zsh_sessions,~/.bash_sessionsand the startup files, with times; note each link's target and birth time. - Remove the link (
rm ~/.zsh_historyremoves the link, not/dev/null) and the settings that disabled history; a new shell creates a fresh file. - Treat the account as used by an intruder: look for persistence created at the same time (LaunchAgents, SSH authorized keys, sudoers), and check other accounts and root for the same link.
- Rotate the account's password, SSH keys and any tokens or credentials the commands could have touched; what was typed is gone, so assume the worst about it.
References
See also
Related techniques
On Mac Forensics
The artifacts behind this technique, in the Mac Forensics reference: