Trusted Design

T1166 - Setuid and Setgid

概要

When the setuid or setgid bits are set on Linux or macOS for an application, this means that the application will run with the privileges of the owning user or group respectively (Citation: setuid man page). Normally an application is run in the current user’s context, regardless of which user or group owns the application. There are instances where programs need to be executed in an elevated context to function properly, but the user running them doesn’t need the elevated privileges. Instead of creating an entry in the sudoers file, which must be done by root, any user can specify the setuid or setgid flag to be set for their own applications. These bits are indicated with an "s" instead of an "x" when viewing a file's attributes via ls -l. The chmod program can set these bits with via bitmasking, chmod 4777 [file] or via shorthand naming, chmod u+s [file].

An adversary can take advantage of this to either do a shell escape or exploit a vulnerability in an application with the setsuid or setgid bits to get code running in a different user’s context. Additionally, adversaries can use this mechanism on their own malware to make sure they're able to execute in elevated contexts in the future (Citation: OSX Keydnap malware).

管理者によるコメント

T1166は、LinuxやmacOSにおける権限昇格・永続化の古典的かつ強力なテクニックです。

Linux/Unix系OS特有のファイル権限システムを逆手に取り、「一般ユーザーがそのファイルを起動しても、自動的に『所有者(通常はroot)』の最高権限でプログラムが実行される」という特殊な設定を悪用する手口です。

1. 概要(攻撃者は何を実現できるのか?)

この手法で攻撃者は、「いつでも、一般ユーザー権限から一瞬で、パスワードを入力することなくroot(最高権限)のシェルを起動できる『秘密のバックドア(特権昇格の足がかり)』をシステム内に常駐させること」を実現します。

2. 攻撃の流れ

攻撃者が一度システムを掌握(root化)した後に、将来の再侵入や権限維持のためにSetuidを仕込む(ポストエクスプロイト)ケースを例に解説します。

  1. 初期侵入と一時的な特権強奪:
    脆弱性(例: 前述の sudo の脆弱性など)を突き、Linuxサーバー内で一度 root 権限を確保します。

  2. バックドアバイナリの配置:
    攻撃者は一般ユーザーのホームディレクトリや、目立たないシステムフォルダ(/tmp//usr/bin/ など)に、シンプルなシェル(/bin/sh)を起動させるための短いプログラム(例: backdoor)を設置します。

  3. Setuidビットの付与(トリガー):
    chmod コマンドを悪用し、そのファイルの所有者を root にした上で、「4000」のパーミッション(Setuid)を付与します。

    • コマンド例:
      chown root:root /tmp/backdoor
      chmod 4755 /tmp/backdoor
      (※パーミッションの先頭の 4 がSetuidを意味します。これにより、ファイルの権限表示が -rwxr-xr-x から -rwsr-xr-x に変わり、実行権限(x)の場所が s に変化します)
  4. 一般ユーザーへの復帰:
    攻撃者はログを消し、一旦一般ユーザーに戻るか、あるいは外部から一般ユーザーとして再度ログインします。

  5. 一瞬での特権昇格(目的達成):
    一般ユーザー権限のまま、手順3で仕込んだ /tmp/backdoor を実行します。OSは「Setuidが有効、かつ所有者がroot」であることを確認し、このプログラムを root権限で実行 します。プログラムが起動したシェル(cmd)はrootの特権を持っているため、攻撃者はパスワードなしでシステムを完全に再掌握します。

3. 防御・対策

システム内に「野良Setuidファイル」が存在しないかを定期的に監査・検知するのが最大の防御となります。

4. 重要ポイント

5. 関連する主なCWE

6. 関連する代表的なCVE(および脆弱性タイプ)

Setuidの悪用そのものはOSの機能(パーミッション設定)であるため、これ単体にCVEがあるわけではありません。しかし、「標準でSetuidが立っている正規のLinuxプログラム」にバグ(脆弱性)があった場合、一般ユーザーが一発でrootを奪取できる極めて凶悪なローカル特権昇格(LPE)脆弱性へと発展します。

分析

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

関連する CVE

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


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