♧ Godown 安全知识树

sudo 授权 hashcat 的 --stdout 参数读取任意 root 文件

pentest/privilege-escalation · verified

一句话结论

当 sudoers 允许普通用户以 root 无密码执行 hashcat 且未限制参数时,可用 --stdout 把任意 root 可读文件当作词表输出到标准输出,读取 /root/root.txt、SSH 私钥等敏感文件。

原理

  • sudo 授权的是程序而非参数;若规则为 user ALL=(root) NOPASSWD: /usr/bin/hashcat,用户可以传入任意 hashcat 参数,程序以 root EUID 运行。
  • hashcat --stdout <file> 的语义是把输入文件作为候选词表,经过规则变换后逐行输出到 stdout;输入文件不需要是合法 hash 格式,普通文本/私钥都会被当作词表读取并回显。
  • 因此 root 可读、普通用户不可读的文件(/root/root.txt、/root/.ssh/id_rsa)会被 hashcat 以 root 权限打开并输出。
  • 不需要破解任何哈希,也不依赖 hashcat 的漏洞,纯粹是程序自身“读取输入文件并输出”的功能被授权放大。

触发条件

  • sudoers 中存在允许当前用户以 root 执行 /usr/bin/hashcat 的规则,且未限制参数(常见 NOPASSWD)。
  • 目标已安装 hashcat(/usr/bin/hashcat)。
  • 目标文件对 root 可读,且 hashcat 进程未被 SELinux/AppArmor 限制访问该路径。

利用步骤

  1. sudo -l 查看可执行命令,确认包含 /usr/bin/hashcat。
  2. 用 --stdout 读取目标文件:sudo /usr/bin/hashcat --stdout /root/root.txt。
  3. 读取 SSH 私钥:sudo /usr/bin/hashcat --stdout /root/.ssh/id_rsa。
  4. 保存私钥后直接 SSH 登录 root;或读取 flag。

PoC

sudo -l
# (ALL) NOPASSWD: /usr/bin/hashcat

sudo /usr/bin/hashcat --stdout /root/root.txt
sudo /usr/bin/hashcat --stdout /root/.ssh/id_rsa
# 注意:-r 参数用于指定规则文件,不是第二个输入文件;
# 读取任意文件用单个 --stdout <目标文件> 即可,不要依赖 -r 读文件。

变体与关联

  • 变体:同类“被 sudo 授权的程序可读任意文件”还有 cat、less、head、tail、vim、base64、xxd、tar 等,具体以 GTFOBins 的 file read 分类为准。
  • 关联:[[pentest-privilege-escalation-sudo-allowed-program-gtfobins-escape]](sudo 授权程序逃逸的总类卡片)。
  • 边界:sudoers 规则若把参数固定(例如只允许 hashcat -m 0 -a 0 /fixed/hash)则无法换文件;若 hashcat 被 AppArmor 限制读取 /root 也不成立。

检测与防御

  • 检测:审计 sudoers 中授权给普通用户的 hashcat 等可读写任意文件的程序;监控 sudo hashcat 的参数中出现 /root、/etc/shadow、.ssh 等路径。
  • 修复:不在 sudoers 中授权通用文件处理/破解工具;必须授权时用参数白名单固定输入文件。
  • 加固:启用 NOEXEC/INTERCEPT 仅能限制子命令,不能阻止程序自身读文件,应直接移除该类授权。