# Bypassing SOP with Iframes - 2 {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * 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.
{% endhint %} ## Iframes in SOP-2 ์ด [**challenge**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc)์˜ [**solution**](https://github.com/project-sekai-ctf/sekaictf-2022/tree/main/web/obligatory-calc/solution)์—์„œ [**@Strellic\_**](https://twitter.com/Strellic\_)์€ ์ด์ „ ์„น์…˜๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด challenge์—์„œ ๊ณต๊ฒฉ์ž๋Š” **bypass**ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: ```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**). **์ฒซ ๋ฒˆ์งธ ๊ฒ€์‚ฌ**๋ฅผ ์šฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ **`window.calc.contentWindow`**๋ฅผ **`undefined`**๋กœ ๋งŒ๋“ค๊ณ  **`e.source`**๋ฅผ **`null`**๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: * **`window.calc.contentWindow`**๋Š” ์‹ค์ œ๋กœ **`document.getElementById("calc")`**์ž…๋‹ˆ๋‹ค. **`document.getElementById`**๋ฅผ **``**๋กœ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (Sanitizer API -[์—ฌ๊ธฐ](https://wicg.github.io/sanitizer-api/#dom-clobbering)-๋Š” ๊ธฐ๋ณธ ์ƒํƒœ์—์„œ DOM ํด๋กœ๋ฒ„๋ง ๊ณต๊ฒฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๋„๋ก ๊ตฌ์„ฑ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค). * ๋”ฐ๋ผ์„œ **`document.getElementById("calc")`**๋ฅผ **`
`**๋กœ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด **`window.calc`**๋Š” **`undefined`**๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. * ์ด์ œ **`e.source`**๊ฐ€ **`undefined`** ๋˜๋Š” **`null`**์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค (์™œ๋ƒํ•˜๋ฉด `==`๊ฐ€ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— **`null == undefined`**๋Š” **`True`**์ž…๋‹ˆ๋‹ค). ์ด๋ฅผ ์–ป๋Š” ๊ฒƒ์€ "์‰ฌ์šด" ์ผ์ž…๋‹ˆ๋‹ค. **iframe**์„ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ๋กœ๋ถ€ํ„ฐ **postMessage**๋ฅผ ๋ณด๋‚ด๊ณ  ์ฆ‰์‹œ **iframe**์„ **์ œ๊ฑฐ**ํ•˜๋ฉด **`e.origin`**์€ **`null`**์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. ```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 ``` **๋‘ ๋ฒˆ์งธ ์ฒดํฌ**๋ฅผ ์šฐํšŒํ•˜๊ธฐ ์œ„ํ•ด **`token`**์„ ๊ฐ’์ด `null`์ธ ์ƒํƒœ๋กœ ๋ณด๋‚ด๊ณ  **`window.token`** ๊ฐ’์„ **`undefined`**๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: * ๊ฐ’์ด `null`์ธ **postMessage**์—์„œ `token`์„ ๋ณด๋‚ด๋Š” ๊ฒƒ์€ ์‚ฌ์†Œํ•œ ์ผ์ž…๋‹ˆ๋‹ค. * **`document.cookie`**๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜ **`getCookie`**๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ **`window.token`**์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. **`null`** ์ถœ์ฒ˜ ํŽ˜์ด์ง€์—์„œ **`document.cookie`**์— ์ ‘๊ทผํ•˜๋ฉด **error**๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด **`window.token`**์€ **`undefined`** ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ตœ์ข… ์†”๋ฃจ์…˜์€ [**@terjanq**](https://twitter.com/terjanq)์— ์˜ํ•ด [**๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค**](https://gist.github.com/terjanq/0bc49a8ef52b0e896fca1ceb6ca6b00e#file-calc-html): ```html ``` {% hint style="success" %} AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
HackTricks ์ง€์›ํ•˜๊ธฐ * [**๊ตฌ๋… ๊ณ„ํš**](https://github.com/sponsors/carlospolop) ํ™•์ธํ•˜๊ธฐ! * **๐Ÿ’ฌ [**Discord ๊ทธ๋ฃน**](https://discord.gg/hRep4RUj7f) ๋˜๋Š” [**ํ…”๋ ˆ๊ทธ๋žจ ๊ทธ๋ฃน**](https://t.me/peass)์— ์ฐธ์—ฌํ•˜๊ฑฐ๋‚˜ **Twitter** ๐Ÿฆ [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**๋ฅผ ํŒ”๋กœ์šฐํ•˜์„ธ์š”.** * **[**HackTricks**](https://github.com/carlospolop/hacktricks) ๋ฐ [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) ๊นƒํ—ˆ๋ธŒ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— PR์„ ์ œ์ถœํ•˜์—ฌ ํ•ดํ‚น ํŠธ๋ฆญ์„ ๊ณต์œ ํ•˜์„ธ์š”.**
{% endhint %}