Prevent session from being replicated when JSESSIONID cookie copied

ares

Background: I have a javaee webapp deployed on tomcat which uses form based authentication. When the web server receives a login request, it sends the request to a dedicated authentication service which validates user login (User id and password). After successful authentication user's session is maintained in the web server.

Problem: I have written a simple webpp source code here, to simulate the scenario. On successful login the current HttpSession instance is invalidated and new instance is created. For each request for a post login page, the session is validated. A new JSESSIONID cookie is set which is used to identify the user during the session until session is expired or user logs out. This cookie can easily viewed in browser's dev tools. If I copy the cookie and set this in a different browser via JavaScript (document.cookie="JSESSIONID=xyzz") and then try to access a post login page, the server identifies it as a valid request and session is validated successfully. The post login page is served without user being challenged for user Id and password.

POC: User opens chrome and enter the URL http://localhost:8080/mywebapp/ and logs in with admin and pass1234. On successful log in the home page http://localhost:8080/mywebapp/home is shown. Now the JSESSIONID cookie is copied and set in FireFox. User enters http://localhost:8080/mywebapp/home in Firefox and is shown the home page without being challenged for userId and password.

Question: How can this be prevented wherein same session is getting replicated over multiple browsers?

enter image description here

BalusC

You can't prevent this specific case of simply copying the cookie from your own browser (or by copying the cookie value from a HTTP payload copypaste/screenshot posted by an ignorant somewhere on the Internet). You can at most prevent the cookie getting hijacked by XSS or man-in-middle attacks.

This all is elaborated in Wikipedia page on the subject Session Hijacking of which I snipped away irrelevant parts (either already enforced by Servlet API, or are simply not applicable here).

Prevention

Methods to prevent session hijacking include:

  • Encryption of the data traffic passed between the parties by using SSL/TLS; in particular the session key (though ideally all traffic for the entire session[11]). This technique is widely relied-upon by web-based banks and other e-commerce services, because it completely prevents sniffing-style attacks. However, it could still be possible to perform some other kind of session hijack. In response, scientists from the Radboud University Nijmegen proposed in 2013 a way to prevent session hijacking by correlating the application session with the SSL/TLS credentials[12]
  • (snip, not relevant)
  • (snip, not relevant)
  • Some services make secondary checks against the identity of the user. For example, a web server could check with each request made that the IP address of the user matched the one last used during that session. This does not prevent attacks by somebody who shares the same IP address, however, and could be frustrating for users whose IP address is liable to change during a browsing session.
  • Alternatively, some services will change the value of the cookie with each and every request. This dramatically reduces the window in which an attacker can operate and makes it easy to identify whether an attack has taken place, but can cause other technical problems (for example, two legitimate, closely timed requests from the same client can lead to a token check error on the server).
  • (snip, not relevant)

In other words:

  • Use HTTPS instead of HTTP to prevent man-in-middle attacks.
  • Add a checkbox "Lock my IP" to login form and reject requests from different IP associated with same session in a servlet filter. This only works on users who know themselves they have a fixed IP.
  • Change session cookie on every request. Interesting at first sight, but breaks when user has same website open in multiple browser tabs/windows in same "session".
  • Not mentioned, but make sure you don't have a XSS hole anywhere, else it's very easy stealing cookies.

Last but not least, I'd like to make clear that this problem is absolutely not specifically related to Servlet API and the JSESSIONID cookie. All other stateful server side languages/frameworks such as PHP (PHPSESSID) and ASP (ASPSESSIONID) also expose exactly the same security problem. The JSESSIONID was previously (decade ago orso) only a bit more in news because by default it was possible to pass the session identifier along in the URL (which was done to support HTTP session in clients who have cookies disabled). Trouble started when ignorant endusers copypasted the full URL with JSESSIONID inside to share links with others. Since Servlet 3.0 you can turn off JSESSIONID in URLs by enforcing a cookie-only policy.

<session-config>
    <tracking-mode>COOKIE</tracking-mode>
</session-config>

See also:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Prevent line numbers from being copied to clipboard

From Dev

Prevent war file from being copied to .m2/repository when running mvn clean install

From Dev

How to prevent part of HTML text from being copied when copying adjacent?

From Dev

How to prevent part of HTML text from being copied when copying adjacent?

From Dev

Can I prevent object from being copied by std::memcpy?

From Dev

Is there a very effective method to prevent a .jpg file for being copied from a page?

From Dev

When is the JSESSIONID cookie added to the response

From Dev

Session Handling in servlet using JSESSIONID cookie not getting session

From Dev

Prevent duplicate ids from being added to session (array)

From Dev

Prevent C# WPF BitmapSource bytes from being copied before render

From Dev

Prevent QR code from being copied and QR code should be scanable by my mobile app only

From Dev

using a wildcard to prevent subtotal line from being filtered and copied excel vba

From Dev

protect images from being copied

From Dev

Executable getting somehow corrupted when being copied

From Dev

How to prevent binary file folder also being copied to output dir?

From Dev

Avoid the li tag symbol from being copied

From Dev

Protecting raw JSON data from being copied

From Dev

SQL Server DB stop from being copied

From Dev

Prevent GtkTreeRow from being selected when toggleButton is clicked

From Dev

How to prevent database from being DDOSed by application when cache expires?

From Dev

How to prevent UIPopoverPresentationController from being dismissed when clicking outside popover?

From Dev

How to prevent a thread from being terminated when conditions are not satisfied momentarily?

From Dev

How to prevent a <div> element from being resized when zooming

From Dev

How to prevent score from being overwritten when the same activity is called

From Dev

How to prevent a Fragment from being added when an Activity is closing

From Dev

How to prevent a thread from being terminated when conditions are not satisfied momentarily?

From Dev

How to prevent XSS vulnerability from being introduced when using .val()?

From Dev

How to prevent $() from being executed prematurely when creating an alias

From Dev

Prevent "^C" from being printed when aborting editing current prompt

Related Related

  1. 1

    Prevent line numbers from being copied to clipboard

  2. 2

    Prevent war file from being copied to .m2/repository when running mvn clean install

  3. 3

    How to prevent part of HTML text from being copied when copying adjacent?

  4. 4

    How to prevent part of HTML text from being copied when copying adjacent?

  5. 5

    Can I prevent object from being copied by std::memcpy?

  6. 6

    Is there a very effective method to prevent a .jpg file for being copied from a page?

  7. 7

    When is the JSESSIONID cookie added to the response

  8. 8

    Session Handling in servlet using JSESSIONID cookie not getting session

  9. 9

    Prevent duplicate ids from being added to session (array)

  10. 10

    Prevent C# WPF BitmapSource bytes from being copied before render

  11. 11

    Prevent QR code from being copied and QR code should be scanable by my mobile app only

  12. 12

    using a wildcard to prevent subtotal line from being filtered and copied excel vba

  13. 13

    protect images from being copied

  14. 14

    Executable getting somehow corrupted when being copied

  15. 15

    How to prevent binary file folder also being copied to output dir?

  16. 16

    Avoid the li tag symbol from being copied

  17. 17

    Protecting raw JSON data from being copied

  18. 18

    SQL Server DB stop from being copied

  19. 19

    Prevent GtkTreeRow from being selected when toggleButton is clicked

  20. 20

    How to prevent database from being DDOSed by application when cache expires?

  21. 21

    How to prevent UIPopoverPresentationController from being dismissed when clicking outside popover?

  22. 22

    How to prevent a thread from being terminated when conditions are not satisfied momentarily?

  23. 23

    How to prevent a <div> element from being resized when zooming

  24. 24

    How to prevent score from being overwritten when the same activity is called

  25. 25

    How to prevent a Fragment from being added when an Activity is closing

  26. 26

    How to prevent a thread from being terminated when conditions are not satisfied momentarily?

  27. 27

    How to prevent XSS vulnerability from being introduced when using .val()?

  28. 28

    How to prevent $() from being executed prematurely when creating an alias

  29. 29

    Prevent "^C" from being printed when aborting editing current prompt

HotTag

Archive