CVE-2021-3156 — Baron Samedit
sudo のヒープベースのバッファオーバーフロー → ローカル権限昇格
ITSOLERA サイバーセキュリティ部門 | レッドチームインターンシップ 2026
██████╗ █████╗ ██████╗ ██████╗ ███╗ ██╗ ███████╗ █████╗ ███╗ ███╗███████╗██████╗ ██╗████████╗
██╔══██╗██╔══██╗██╔══██╗██╔═══██╗████╗ ██║ ██╔════╝██╔══██╗████╗ ████║██╔════╝██╔══██╗██║╚══██╔══╝
██████╔╝███████║██████╔╝██║ ██║██╔██╗ ██║ ███████╗███████║██╔████╔██║█████╗ ██║ ██║██║ ██║
██╔══██╗██╔══██║██╔══██╗██║ ██║██║╚██╗██║ ╚════██║██╔══██║██║╚██╔╝██║██╔══╝ ██║ ██║██║ ██║
██████╔╝██║ ██║██║ ██║╚██████╔╝██║ ╚████║ ███████║██║ ██║██║ ╚═╝ ██║███████╗██████╔╝██║ ██║
╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═════╝ ╚═╝ ╚═╝
⚠️ 教育目的 / 隔離ラボ専用
すべてのテストは Docker ラボコンテナ内でのみ実施してください。
実システム、本番環境、共有システムに対しては実行しないでください。
目次
- CVE 概要
- この CVE の特異性
- プロジェクト構成
- チーム成果物
- クイックスタート
- エクスプロイトの仕組み
- 脆弱性カナリアテスト
- exploit.py の使い方
- ラボのリセットとスナップショット
- パッチ適用済み vs 脆弱な状態の比較
- 参考資料
CVE 概要
この CVE の特異性
重要な洞察: オーバーフローは引数を解析する関数である set_cmnd() 内で発生し、これは sudo が /etc/sudoers を参照する前に実行されます。10秒前に作成された権限ゼロのアカウントでもこれを悪用できます。
プロジェクト構成
CVE-2021-3156-Project/
│
├── README.md ← you are here
│
├── lab/ ← Member 1: Lab Environment
│ ├── Dockerfile ← Ubuntu 20.04 + sudo 1.8.31 (pinned)
│ ├── docker-compose.yml ← vulnerable target + patched reference
│ ├── evidence_helper.sh ← pre/post-exploit state capture
│ ├── SETUP.md ← step-by-step VM/Docker guide
│ └── config_notes.md ← CVE conditions & container details
│
├── exploit/ ← Member 2: Exploit Development
│ ├── exploit.py ← Python LPE framework + canary test
│ └── payloads.txt ← heap overflow research notes
│
├── docs/ ← Member 3: Research & Documentation
│ ├── root_cause_analysis.md ← set_cmnd() deep dive + off-by-one
│ ├── references.md ← all sources, PoCs, CWE mapping
│ └── mitigation.md ← sudo upgrade + hardening checklist
│
├── proof/ ← Member 4: Evidence Collection
│ ├── screenshots/ ← exploitation screenshots
│ └── terminal_logs.txt ← command output template + logs
│
└── report/
└── CVE-2021-3156_Report.docx ← Member 4: Final professional report
チーム成果物
メンバー1 — ラボ環境と CVE 検証
メンバー2 — エクスプロイト開発(PoC スクリプト)
注記: 倫理ガイドラインに基づき、動作するカーネル/ヒープエクスプロイトコードは含まれていません。
exploit.py の TODO スタブは、チームで学習・統合するための権威ある公開 PoC として
https://github.com/blasty/CVE-2021-3156 と https://github.com/worawit/CVE-2021-3156 を参照しています。
メンバー3 — 根本原因分析と調査
| ファイル | 説明 |
|---|
docs/root_cause_analysis.md | 詳細: set_cmnd() のサイズ計算とコピー量の不一致、コード例付きのオフバイワン、パッチ差分、エクスプロイトチェーン、CVSS 7.8 のメトリクス別分析、10年間のタイムライン、ディストリビューションへの影響テーブル |
メンバー4 — エビデンス収集、レポート作成、統合
| ファイル | 説明 |
|---|
proof/terminal_logs.txt | エビデンス収集テンプレート(実際のラボ出力を記入) |
proof/screenshots/ | 必須の7枚のスクリーンショット用ディレクトリ |
report/CVE-2021-3156_Report.docx | 12セクション構成のプロフェッショナルレポート: 表紙、エグゼクティブサマリー、CVE の説明、根本原因分析、CVSS の内訳、悪用手順、コード解説、エビデンスチェックリスト、緩和策、タイムライン、参考資料、免責事項 |
クイックスタート
1. ラボのビルドと起動
cd CVE-2021-3156-Project/lab/
# Build the vulnerable image (downloads Ubuntu 20.04, pins sudo 1.8.31)
docker compose build
# Start both containers
docker compose up -d
# Confirm containers are running
docker compose ps
期待される出力:
NAME STATUS
baron_samedit_target running
baron_samedit_patched running
2. 脆弱なコンテナに入る
docker exec -it baron_samedit_target bash
3. 脆弱性の条件を確認する
# Inside the container as labuser
whoami # Expected: labuser
id # Expected: uid=1000(labuser)...
sudo --version # Expected: Sudo version 1.8.31
sudo -l # Expected: Sorry, user labuser may not run sudo...
sudoedit -s '\' 2>&1 # Expected: NOT "usage:" → VULNERABLE
4. エクスプロイトをコピーして実行する
# From host machine
docker cp exploit/exploit.py baron_samedit_target:/home/labuser/
# Inside container
python3 exploit.py --check-only # verify all prerequisites pass
python3 exploit.py --safe-mode # canary only, no exploitation
python3 exploit.py --cmd "id" # LPE → show root identity
python3 exploit.py --cmd "whoami /all" --output /tmp/proof.txt
5. エビデンスを収集する
# Inside container — before exploit
~/evidence_helper.sh > /tmp/before.txt
# After exploit
~/evidence_helper.sh > /tmp/after.txt
# Copy to host
docker cp baron_samedit_target:/tmp/before.txt proof/terminal_logs_before.txt
docker cp baron_samedit_target:/tmp/after.txt proof/terminal_logs_after.txt
エクスプロイトの仕組み
Step 1: Run 'sudoedit -s <crafted_argument>'
↓
Step 2: sudo calls set_cmnd() to parse arguments in shell mode
↓
Step 3: set_cmnd() COUNTS bytes for the cmnd_args buffer (correct size)
↓
Step 4: set_cmnd() COPIES bytes — but reads 1 byte past the null
terminator of any argument ending with '\'
↓
Step 5: Off-by-one overflow writes 1 unexpected byte beyond cmnd_args
↓
Step 6: Heap grooming (environment variable layout manipulation) ensures
a valuable sudo struct sits adjacent to the overflowed buffer
↓
Step 7: The overflow byte corrupts a pointer in the adjacent struct
↓
Step 8: sudo follows the corrupted pointer → executes attacker-controlled
command (via SUDO_EDITOR) with root privileges
↓
Result: uid=0(root) — from a user with ZERO sudo permissions
根本原因を一言でいうと
size_calculation("A\") = 2 bytes ≠ copy("A\") = writes 3 bytes → overflow
脆弱性カナリアテスト
CVE-2021-3156 の存在を確認する最も安全な方法です — 悪用は一切不要:
sudoedit -s '\' 2>&1; echo "Exit: $?"
exploit.py の使い方
Usage: python3 exploit.py [OPTIONS]
Modes (mutually exclusive):
--check-only Run all pre-checks only — no exploitation
--safe-mode Run canary test only — confirm vulnerability
--cmd COMMAND Execute COMMAND as root after successful LPE
Options:
--verbose, -v Show detailed heap/system debug output
--output, -o FILE Save all output to FILE
--no-colour Disable ANSI colour codes (for log files)
--help Show this help message
Examples:
python3 exploit.py --check-only
python3 exploit.py --safe-mode
python3 exploit.py --cmd "id"
python3 exploit.py --cmd "cat /etc/shadow" --output proof/root_output.txt
python3 exploit.py --cmd "whoami" --verbose
実行される事前チェック
ラボのリセットとスナップショット
クイックリセット(コンテナは維持し、一時ファイルを削除)
docker exec baron_samedit_target bash -c "rm -f /tmp/*.txt /tmp/exploit* /home/labuser/exploit.py"
スナップショットを保存
docker commit baron_samedit_target baron-samedit:clean-state
echo "[+] Snapshot saved as baron-samedit:clean-state"
スナップショットを復元
docker compose down
docker run -it --name baron_samedit_target baron-samedit:clean-state bash
完全リセット(ゼロから再ビルド)
docker compose down -v
docker compose up -d --build
パッチ適用済み vs 脆弱な状態の比較
パッチ適用済みコンテナでテストする場合:
docker exec -it baron_samedit_patched bash
# Then repeat the canary test — should output "invalid argument"
参考資料
倫理上の注意
┌─────────────────────────────────────────────────────────────────┐
│ This project was created for EDUCATIONAL PURPOSES ONLY as │
│ part of the ITSOLERA Cybersecurity Red Team Internship 2026. │
│ │
│ ✅ DO: Use against the isolated Docker lab only │
│ ✅ DO: Study the vulnerability to understand heap exploits │
│ ✅ DO: Contribute findings to the team report │
│ │
│ ❌ DON'T: Run against any real system, VM, or cloud instance │
│ ❌ DON'T: Share outside the internship programme │
│ ❌ DON'T: Use for any unauthorised access │
└─────────────────────────────────────────────────────────────────┘
ITSOLERA サイバーセキュリティ部門 — レッドチームインターンシップ、2026年夏