hacktricks/pentesting-web/hacking-with-cookies/cookie-tossing.md

95 lines
6.2 KiB
Markdown
Raw Normal View History

2022-04-28 16:01:33 +00:00
<details>
2024-01-10 10:22:19 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-10 10:22:19 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-10 10:22:19 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 07:14:36 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2024-01-10 10:22:19 +00:00
* **Share your 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>
2022-05-01 12:41:36 +00:00
## Description
2021-10-20 23:25:53 +00:00
2022-10-12 01:20:52 +00:00
If an attacker can **control a subdomain or the domain of a company or finds an XSS in a subdomain** he will be able to perform this attack.
2021-10-19 00:01:07 +00:00
2022-10-12 00:53:16 +00:00
As it was indicated in the Cookies Hacking section, when a **cookie is set to a domain (specifying it) it will be used in the domain and subdomains.**
2021-10-19 00:01:07 +00:00
{% hint style="danger" %}
2022-10-12 00:53:16 +00:00
Therefore, **an attacker is going to be able to set to the domain and subdomains a specific cookie doing something like** `document.cookie="session=1234; Path=/app/login; domain=.example.com"`
2021-10-19 00:01:07 +00:00
{% endhint %}
2022-10-12 00:53:16 +00:00
This can be dangerous as the attacker may be able to:
2021-10-19 00:01:07 +00:00
2022-10-12 00:53:16 +00:00
* **Fixate the cookie of the victim to the attacker's account** so if the user doesn't notice, **he will perform the actions in the attacker's account** and the attacker may obtain some interesting information (check the history of the searches of the user in the platform, the victim may set his credit card in the account...)
2021-10-19 00:01:07 +00:00
* If the **cookie doesn't change after login**, the attacker may just **fixate a cookie**, wait until the victim logs in and then **use that cookie to log in as the victim**.
2021-11-30 16:46:07 +00:00
* If the **cookie is setting some initial value** (like in flask where the **cookie** may **set** the **CSRF token** of the session and this value will be maintained after the victim logs in), the **attacker may set this known value and then abuse it** (in that scenario, the attacker may then make the user perform a CSRF request as he knows the CSRF token).
2021-10-19 00:01:07 +00:00
2022-05-01 12:41:36 +00:00
## Cookie Order
2021-10-19 00:01:07 +00:00
When a browser receives two cookies with the same name **partially affecting the same scope** (domain, subdomains and path), the **browser will send both values of the cookie** when both are valid for the request.
2021-11-30 16:46:07 +00:00
Depending on who has **the most specific path** or which one is the **oldest one**, the browser will **set the value of the cookie first** and then the value of the other one like in: `Cookie: iduser=MoreSpecificAndOldestCookie; iduser=LessSpecific;`
2021-10-19 00:01:07 +00:00
2022-10-12 01:19:24 +00:00
Most **websites will only use the first value**. Then, if an attacker wants to set a cookie it's better to set it before another one is set or set it with a more specific path.
2021-10-19 00:01:07 +00:00
{% hint style="warning" %}
Moreover, the capability to **set a cookie in a more specific path** is very interesting as you will be able to make the **victim work with his cookie except in the specific path where the malicious cookie set will be sent before**.
{% endhint %}
2022-05-01 12:41:36 +00:00
## Protection Bypass
2021-10-19 00:01:07 +00:00
2022-10-12 00:53:16 +00:00
Possible protection against this attack would be that the **web server won't accept requests with two cookies with the same name but two different values**.
2021-10-19 00:01:07 +00:00
2021-11-30 16:46:07 +00:00
To bypass the scenario where the attacker is setting a cookie after the victim was already given the cookie, the attacker could cause a **cookie overflow** and then, once the **legit cookie is deleted, set the malicious one**.
2021-10-19 00:01:07 +00:00
{% content-ref url="cookie-jar-overflow.md" %}
[cookie-jar-overflow.md](cookie-jar-overflow.md)
{% endcontent-ref %}
2021-11-30 16:46:07 +00:00
Another useful **bypass** could be to **URL encode the name of the cookie** as some protections check for 2 cookies with the same name in a request and then the server will decode the names of the cookies.
2021-10-19 00:01:07 +00:00
2022-05-01 12:41:36 +00:00
## Cookie Bomb
2021-10-20 23:25:53 +00:00
2022-10-12 01:33:31 +00:00
A Cookie Tossing attack may also be used to perform a **Cookie Bomb** attack:
2021-10-20 23:25:53 +00:00
{% content-ref url="cookie-bomb.md" %}
[cookie-bomb.md](cookie-bomb.md)
{% endcontent-ref %}
2022-05-01 12:41:36 +00:00
## Defense**s**
2021-10-19 00:01:07 +00:00
2022-05-01 12:41:36 +00:00
### **Use the prefix `__Host` in the cookie name**
2021-10-19 00:01:07 +00:00
* If a cookie name has this prefix, it **will only be accepted** in a Set-Cookie directive if it is marked Secure, was sent from a secure origin, does not include a Domain attribute, and has the Path attribute set to /
* **This prevents subdomains from forcing a cookie to the apex domain since these cookies can be seen as "domain-locked"**
2022-05-01 12:41:36 +00:00
## References
2021-10-19 00:01:07 +00:00
2022-04-05 22:24:52 +00:00
* [**@blueminimal**](https://twitter.com/blueminimal)
* [**https://speakerdeck.com/filedescriptor/the-cookie-monster-in-your-browsers**](https://speakerdeck.com/filedescriptor/the-cookie-monster-in-your-browsers)
* [**https://github.blog/2013-04-09-yummy-cookies-across-domains/**](https://github.blog/2013-04-09-yummy-cookies-across-domains/)
2022-04-28 16:01:33 +00:00
<details>
2024-01-10 10:22:19 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-10 10:22:19 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-10 10:22:19 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 07:14:36 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2024-01-10 10:22:19 +00:00
* **Share your 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>