Cookie poisoning is a general term for various attacks that aim to manipulate or forge HTTP cookies. Depending on the attack, cookie poisoning might lead to session hijacking, exposure of sensitive information, or taking over a victim’s account. Let’s see what attacks involve cookie poisoning and how you can prevent them.
Before we dive in, let’s quickly clear up the terminology. In a narrow sense, cookie poisoning refers to attacks that directly modify existing cookie values. Cookies are simply text values stored in the user’s browser, so without additional safeguards, you can easily change them manually or via the document.cookie
property in JavaScript. Fortunately, such trivial attacks are rarely possible in modern web development where session management and other operations that involve cookies are usually done at the framework level.
More broadly, the term “cookie poisoning” is often applied to all cookie-related attacks, even ones that, strictly speaking, don’t involve tampering with the cookies themselves. For example, a vulnerable web application might allow a malicious user to discover cookie values or set new cookies to perform a variety of attacks.
If you open the developer tools panel in your web browser, you can view and manually modify cookies that are currently set. A vulnerable website or web application might store sensitive state information directly in cookies, for example, a flag to allow admin access. In that case, manually changing cookie names and values in the browser and reloading the page may be enough to gain elevated privileges. In the age of web frameworks, it would be hard to find a production site or application where cookies are manually coded in such a vulnerable way, but theoretically it’s possible.
More usefully for attackers, JavaScript provides the document.cookie
property for manipulating cookies from scripts. If a sensitive cookie, such as the session cookie, does not have the additional httpOnly
flag, it can be read and modified by malicious scripts following a successful cross-site scripting (XSS) attack.
In its broader sense, cookie poisoning can mean any kind of cookie manipulation, usually targeting session cookies. HTTP is a stateless protocol, so applications use cookies to persist session information and other data on the user’s computer. The session identifier is the most valuable piece of data stored in application cookies, as it opens the way to session hijacking and related attacks.
All cookie-based attacks against user sessions have the same basic aim: to fool the web server into thinking that the attacker is the legitimate user. Here is a quick rundown:
Regardless of the method used, a successful session takeover can have disastrous consequences. Depending on the targeted site or application, attackers may be able to steal confidential user information, such as login credentials or personal information, or perform undesirable operations, such as transferring funds or adding a new user account for future access.
As with so many security vulnerabilities, the common denominator of most cookie-related attacks is insufficient input validation and trusting user-controlled data in HTTP requests. Proper cookie hygiene is a fundamental best practice in web application security and there are a number of security measures that can minimize the risk of cookie poisoning attacks:
httpOnly
flag, you can make a cookie inaccessible to scripts. This is recommended especially for session cookies, as is setting the secure
flag to ensure that the cookie is only sent over HTTPS. Carefully selecting values for the domain
attribute can also minimize cookie abuse – see our cookie security white paper for more information.Invicti can detect many cookie-related issues to help you secure your web applications. These include general warnings about user-controllable cookies but also more specific recommendations related to the httpOnly
and secure
flags. In addition, the Cookies node in the Invicti user interface provides a convenient, centralized view of all the cookies found in your application.