Trusted Design

T1156 - Malicious Shell Modification

概要

Adversaries may establish persistence through executing malicious commands triggered by a user’s shell. User shells 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 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). These files require root permissions 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, or ~/.profile (Rocke) which are sourced when a user opens a command line interface or connects remotely. Adversaries often use ~/.bash_profile since the system only executes the first file that exists in the listed order. Adversaries have also leveraged the ~/.bashrc file (Tsunami, Rocke, Linux Rabbit, Magento) 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. Some malware targets the termination of a program to trigger execution (Cannon), adversaries can use the ~/.bash_logout file to execute malicious commands at the end of a session(Pearl_shellbot).

For macOS, the functionality of this technique is similar but leverages 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. The login shell then configures the user environment with ~/.zprofile and ~/.zlogin. The interactive shell uses the ~/.zshrc to configure the user environment. Upon exiting, /etc/zlogout and ~/.zlogout are executed. For legacy programs, macOS executes /etc/bashrc on startup.

管理者によるコメント

T1156はかつて「.bash_profile and .bashrc(シェル設定ファイルの改ざん)」として定義されていた、Linux/Unix環境における定番の自動実行・潜伏手法です。

ユーザーがLinuxサーバー等にログインしたり、新しいターミナル(コマンドライン)を開いたりした際に自動読み込みされるシェル(主にBash)の環境設定ファイルを改ざんし、ユーザーの日常操作に便乗してマルウェアやコマンドを自動実行させる手法です。主に「永続化(Persistence)」と「特権昇格(Privilege Escalation)」のために利用されます。

1. 概要

この手法で攻撃者は、「システム起動時ではなく、管理者やユーザーが作業を始めようとコマンド画面を開いた瞬間を狙った、見つかりにくい自動実行環境」を実現します。

何を実現できるのか

  • 確実な永続化:
    サーバー管理者は障害対応やメンテナンスのために頻繁にSSH等でサーバーにログインします。その「ログイン」という動作そのものがマルウェアの起動スイッチになります。

  • 特権の便乗(昇格):
    もし root ユーザー(システム管理者)の設定ファイルが改ざんされていた場合、root がログインした瞬間にマルウェアも root権限(最高権限) で実行されるため、容易に特権を奪取できます。

2. 対象となる主な設定ファイル

Linux(主にデフォルトシェルであるBash)には、用途や権限に応じていくつかの自動実行スクリプトが存在し、攻撃者はこれらを狙います。

① ユーザー権限で改ざん可能なもの(個人のホームディレクトリ配下)

一般ユーザーとして侵入した段階で、そのユーザーの権限のまま書き換えることができます。

  • ~/.bash_profile / ~/.bash_login / ~/.profile
    (ユーザーがSSHなどで「ログイン」した時に、最初に1回だけ実行される)

  • ~/.bashrc
    (ログイン後、新しいターミナルウィンドウやタブを開くなど「対話型シェル」が起動するたびに毎回実行される)

  • ~/.bash_logout
    (ユーザーが作業を終えて「ログアウト(接続終了)」した瞬間に実行される)

② 管理者(root)権限が必要なもの(システム全体への影響)

root権限を奪取した後に、システム全体の全ユーザーを監視・一網打尽にするために改ざんされます。

  • /etc/profile
  • /etc/profile.d/ 配下のスクリプト群
    (システム上の誰がログインしても、必ず強制的に実行される共通設定領域)

3. 攻撃の流れ

多くの場合、攻撃者はWebアプリケーションの脆弱性などを突いてサーバー内に足がかり(一般ユーザー権限)を得た後に、この設定を行います。

  1. 侵入:
    攻撃者が特定のユーザー権限でサーバーの操作権を確保します。

  2. 設定ファイルの改ざん:
    ユーザーのホームディレクトリにある .bashrc.bash_profile の末尾に、隠しバックドアを起動するためのコマンドを追記します。

    • コマンドの例(ファイル末尾への追記):
      echo "(/tmp/.backdoor_agent &) > /dev/null 2>&1" >> ~/.bashrc

    (※バックグラウンドで静かにマルウェアを動かし、画面にエラー等を表示させないようにカモフラージュする記述)

  3. 潜伏:
    攻撃者は一旦接続を切り、痕跡を消して潜伏します。

  4. 発動:
    翌日、正規のシステム管理者がサーバーの様子を確認するためにSSHでログイン、またはコマンドラインを開きます。

  5. コード実行:
    OSが環境設定として .bashrc を読み込んだ瞬間、末尾に仕込まれたバックドアコマンドが自動発動し、攻撃者のC2サーバーへ逆接続(リバースシェル)が確立されます。

4. 防御・対策

「ファイルの変更監視」と「ログイン時のプロセスチェック」が基本です。

  • 整合性監視(FIM)による検知:
    /etc/profile や、各ユーザーのホームディレクトリ直下にあるドットファイル(.bashrc などの隠しファイル)に対する予期せぬ変更・書き込みを監視します。通常の運用で、これらのファイルが頻繁に書き換わることはありません。

  • 不審なプロセスツリーの監視:
    ユーザーがログインした直後に、シェルプロセス(bashsh)を親プロセスとして、一時フォルダ(/tmp/dev/shm)から見覚えのないバイナリや、外部IPへのネットワーク接続(nccurl など)が発生していないかをEDRや監査ログ(auditd)で監視します。

  • デフォルトプロファイルの保護:
    /etc/profile/etc/profile.d/ のアクセス権限を厳格に管理し、root以外のユーザーによる書き込みを一切禁止します。

5. 関連する主なCWE

  • CWE-15: External Control of System or Configuration Setting:
    システムやシェルの環境設定ファイル(.bashrc等)を外部から不正に書き換えられてしまう不備。

  • CWE-732: Incorrect Permission Assignment for Critical Resource:
    重要な設定ファイルに対するファイルパーミッション(アクセス権限)の割り当てミス。

6. 関連する代表的な事例

Linux環境を標的にするサイバースパイや、サーバーを乗っ取って仮想通貨をマイニングさせるマルウェアで定番の手法です。

  • Linux向け各種Rootkit / ボットネット:
    侵入したLinuxサーバーで永続性を確保するため、クローンジョブ(cron)の設定と並んで、この .bashrc の書き換え(T1156 / T1546.004)が二重三重のバックドア用保険としてよく利用されます。

  • 高度なAPT攻撃(Linux標的型):
    攻撃者がシステム管理者のアカウントを乗っ取った際、管理者がログインするたびに秘密裏に環境情報(環境変数や入力されたコマンド履歴)を盗み出すカスタムスクリプトを .bash_profile にインジェクションするケースが確認されています。

実務上のアドバイス

Linuxサーバーのセキュリティ調査(フォレンジック)を行う際、不審な常駐プロセスが見当たらないにもかかわらず「特定のユーザーがログインした時だけ怪しい外部通信が発生する」という場合は、そのユーザーのホームディレクトリにある .bashrc.profilecat -v などのコマンドで直接開いて中身をチェックしてください。エディタで見えにくいように先頭に大量の改行やスペースを入れて隠蔽されているケースもあります。

分析

この攻撃手法を利用する脅威アクター

関連する CVE

攻撃手法 – 脅威アクター Graph


← Technique一覧に戻る ← Tactics一覧に戻る