Adversaries may establish persistence through executing malicious commands triggered by a user’s shell. User [Unix Shell](https://attack.mitre.org/techniques/T1059/004)s execute several configuration scripts at different points throughout the session based on events. For example, when a user opens a command-line interface or remotely logs in (such as via SSH) a login shell is initiated. The login shell executes scripts from the system (/etc
) and the user’s home directory (~/
) to configure the environment. All login shells on a system use /etc/profile when initiated. These configuration scripts run at the permission level of their directory and are often used to set environment variables, create aliases, and customize the user’s environment. When the shell exits or terminates, additional shell scripts are executed to ensure the shell exits appropriately.Adversaries may attempt to establish persistence by inserting commands into scripts automatically executed by shells. Using bash as an example, the default shell for most GNU/Linux systems, adversaries may add commands that launch malicious binaries into the
/etc/profile
and/etc/profile.d
files.(Citation: intezer-kaiji-malware)(Citation: bencane blog bashrc) These files typically require root permissions to modify and are executed each time any shell on a system launches. For user level permissions, adversaries can insert malicious commands into,
/.bash_profile/.bash_login
, orwhich are sourced when a user opens a command-line interface or connects remotely.(Citation: anomali-rocke-tactics)(Citation: Linux manual bash invocation) Since the system only executes the first existing file in the listed order, adversaries have used
/.profile/.bash_profile
to ensure execution. Adversaries have also leveraged thefile which is additionally executed if the connection is established remotely or an additional interactive shell is opened, such as a new tab in the command-line interface.(Citation: Tsunami)(Citation: anomali-rocke-tactics)(Citation: anomali-linux-rabbit)(Citation: Magento) Some malware targets the termination of a program to trigger execution, adversaries can use the
/.bashrc/.bash_logout
file to execute malicious commands at the end of a session.For macOS, the functionality of this technique is similar but may leverage zsh, the default shell for macOS 10.15+. When the Terminal.app is opened, the application launches a zsh login shell and a zsh interactive shell. The login shell configures the system environment using
/etc/profile
,/etc/zshenv
,/etc/zprofile
, and/etc/zlogin
.(Citation: ScriptingOSX zsh)(Citation: PersistentJXA_leopitt)(Citation: code_persistence_zsh) The login shell then configures the user environment withand
/.zprofile/.zlogin
. The interactive shell uses theto configure the user environment. Upon exiting,
/.zshrc/etc/zlogout
and/.zlogout
are executed. For legacy programs, macOS executes/etc/bashrc
on startup.
Adds a command to the .bash_profile file of the current user
Supported Platforms: macOS, Linux
Name | Description | Type | Default Value |
---|---|---|---|
command_to_add | Command to add to the .bash_profile file | string | /path/to/script.py |
echo "#{command_to_add}" >> ~/.bash_profile
Adds a command to the .bashrc file of the current user
Supported Platforms: macOS, Linux
Name | Description | Type | Default Value |
---|---|---|---|
command_to_add | Command to add to the .bashrc file | string | /path/to/script.py |
echo "#{command_to_add}" >> ~/.bashrc