Sessions are key to a good user experience when using the web. However, managing sessions incorrectly can lead to security holes that attackers can exploit.
You navigate to a website and log in. As normal, you fill up your cart with products you'd like to buy. Then, whoops - your hand slips and closes the browser tab. After a minor panic, you enter the URL of the site back into the browser and hit the "Enter" key. You are brought back into the site, logged in, and all of your items are still in the cart. Whew.
How did the site know who you were without re-authenticating? It identified you because it was using sessions. Sessions are key to a good user experience when using the web. However, managing sessions incorrectly can lead to security holes that attackers can exploit.
Let's now review what session management means, how weak session management can hurt you, and what you can do to manage sessions properly.
A session refers to a value stored on the server, specific to a single user of the application. This is necessary for two reasons: First, HTTP is a stateless protocol. Each request is separate and has no knowledge of requests that have come before or after it. A session helps the server track who sent the request. Otherwise, you would need to login every time you clicked on a button or a link.
The second reason for sessions is for authorization of the user. The session identifier can be used to recognize a specific user with specific rights within the system. The application will know who the person is and what they are allowed to do.
There are two components to a session. A data store on the server-side stores a session identifier and maps it to information about the user such as their user id or cart information. The same session identifier is sent to the browser in a cookie. The cookies are stored by the browser on the user's system. The client passes the cookie with each request, letting the server knows that this request is coming from the same user. Most applications use sessions to track users both pre- and post-authentication.
Proper session management is essential to the security of an application. A valid session ID has the same level of trust as a username/password, or even a second-factor authentication token.
Poor session management can lead to complete account takeover. This means customer data can be stolen, or products could be purchased fraudulently. There are several ways for attackers to obtain a valid session ID.
A session fixation attack occurs when sessions are not changed at key times, such as when a user logs into the system, and if session identifiers can be set using the URL. Setting session identifiers in this way may be used to keep users logged in across different applications that use the same authentication source. In this case, an attacker can browse to a website and gain a session ID. The attacker then sends a URL to an unsuspecting victim via email with the session ID in the URL. The victim clicks the URL in the email and logs into the website. If the session ID is not rotated upon login, the attacker now has a valid, authenticated session ID. This allows complete account takeover.
Another attack upon poor session management is a brute-force guessing attack. When developers try to create their own session management systems, often they use session IDs that are pretty simple to guess. These could be a sequence (1, 2, 3) or a predictable pattern of some kind. The attacker simply keeps guessing session IDs until a valid one is discovered. This also leads to an account takeover.
Sessions that aren't invalidated automatically after a certain amount of time has passed can be exploited to attack users. A successful cross-site request forgery attack depends on sessions that are still valid after the user leaves the site. Let's say an attacker places an iframe or image on a site visited by the user. The "src" (source) attribute is set to the URL of the vulnerable site and performs an action on behalf of the user. For example, a vulnerable banking application could be made to transfer money to an attacker's account without the user's permission.
Session management can be tricky, and weaknesses can be devastating. However, it's a well-known problem and it can be solved.
Session management is a core piece of any web application. As a result, many web development frameworks have built-in session management functionality. These systems have been scrutinized by experts to find and weed out problems. Use them.
Some common properties of good session management include:
Random session IDs generated that attackers can't guess
Sessions are invalidated when a user logs out
Sessions are invalidated automatically after a certain amount of time has passed
Session IDs are changed after the user logs in
Session IDs that are at least 128 bits long to prevent brute force attacks
Bottom line: Don't create your own session management system from scratch.
Check out our free learning resources to find out more about secure session management. Learning how to secure your sessions will help prevent user account takeover, reputation damage, and lost revenue due to security breaches. Secure your sessions and keep your users safe.