Improper handlings of session variables in an ASP.NET website is considered to be a serious threat and opens various doors to malicious hackers. For instance, a session variable could be manipulated in such a way as to subvert a login authentication mechanism. However, this article illustrates a session fixation bug in a .NET website by providing various live scenarios that usually lead to a website being vulnerable in terms of session hijacking. Moreover, the article circulates detailed information about exploiting vulnerable websites as well as recommended practices for protecting them against session fixation attacks.
Session Fixation Internal
A session fixation attack allows spoofing another valid user and working on behalf of its credentials. It typically fixates on another person's session identifier to breach in the current communication. An ASP.NET based website usually maintains session variables to track a user by creating a cookie called ASP.NET_SessionId in the browser. A Session variable is typically used to record the currently logged-in user and such cookie value is validated on each round-trip to ensure that the data being served is specific to that user. Here the following image describes the process of cookie-based authentication, where the user does the login operation to a vulnerable website and in return, the server issues this specific user a cookie token value for session management.

Websites usually, engage session management to construct a user-friendly environment. But this mechanism is vulnerable in some extent because session IDs present an attractive target for attackers that is stored on the server and associated with respective users by a unique session identifier value. There are a couple of approaches applied by the attacker to perform a session fixation attack, depending on the session ID transport mechanism (cookies, hidden fields and URL arguments) and the loopholes identified on the target system.
The mechanics of session management is that the server generates a unique session identifier value during user authentication and sends this session ID back to the client browser and ensure that this same ID will be returned by the browser with each forthcoming request. Hence, such a unique session ID value thereby becomes identification tokens for users and servers can use them to maintain session data.

An ASPNET_SessionID cookie is only configured by the server on behalf of any page request of the website. So when the login page is first accessed, the ASP.NET_SessionID cookie value is set by the client browser and the server uses such a cookie value for all subsequent requests even after authentication is successful and logged out, the ASP.NET_SessionID value does not change. This results in the possibility of session fixation attack, where a hacker can potentially sniff the traffic across the wire or physically access the victim machine in order to get the stored cookies values in the browser and fix a victim's session by accessing the login page, even if not having the actual user name or password. The following image shows the real-time session fixation attack scenario where a potential hacker sits somewhere in the network and intercepts the traffic that happens between the server and client. Here, the hacker employs a packet sniffer to capture a valid token session and then utilizes the valid token session to gain unauthorized access to the Web Server. Finally, the hacker successfully accesses the ASPNET_SessionID value and logs in successfully to the website sensitive zone.

Vulnerable Code Scenario
With session fixation, usually bugs occur on the website that manipulates sensitive data in a transaction or incorporated with a login page to authenticate valid users on behalf of the correct user name and password. This paper illustrates this crucial bug in detail by presenting this vulnerable login authentication code as in the following:
- if (txtUsr.Text.Equals("frank") && txtPwd.Text.Equals("password"))
- {
- Session["LIn"] = txtU.Text.Trim();
- Server.Transfer("Home.aspx");
- }
- else
- {
- lblMessage.Text = "Wrong username or password";
- }

The user typically assumes that his transaction is safe and has less possibility of happening other website related attacks because entering to sensitive zones of web pages could happen after being validated first from the login page that is checking the correct credentials, But still, a couple of serious attacks such as spoofing, replay and session hijacking attacks could be possible even if managing user name and password correctly that we shall see in forthcoming segments of this article series.
Stealing Cookies
Valid session IDs are not only recognized to be identification tokens, but also employed as an authenticators. Users are authenticated based on their credentials (for example: user names and passwords) while login and issued session IDs that will effectively serve as temporary static passwords for accessing their sessions that makes session IDs a very appealing target for attackers. The moment the user enters his credentials on login to authenticate, the data stored in the session and cookies is generated in the client browser. A user is typically over-confident that when he ha logged out all the data is scrubbed automatically and the session is terminated, but unfortunately cookie values are not deleted from the client browser even if the session has ended and such cookies values can be exploited by a hacker to breach into the website sensitive zone without being aware of the user name and password. As the following figure shows, when a user is logged in, the browser shows cookie values that are generated during authentication.






Regis BittencourtPosted Nov 7, 2023, 4:46 PM
Https://stackoverflow.com/questions/1419340/how-to-change-session-id-after-login-in-asp-net/77439946#77439946 For those that are searching now and seeing all those reflection hacks and are struggling with the session fixation issue. The only way to get to asp.net to create a new session id is to fake a `SessionIDManager` that returns null for you login page. That way asp.net thinks the browser never sent a cookie.
Regis BittencourtPosted Nov 7, 2023, 4:45 PM
Https://stackoverflow.com/questions/1419340/how-to-change-session-id-after-login-in-asp-net/77439946#77439946For those that are searching now and seeing all those reflection hacks and are struggling with the session fixation issue. The only way to get to asp.net to create a new session id is to fake a `SessionIDManager` that returns null for you login page. That way asp.net thinks the browser never sent a cookie. Here are the steps: - Create a `CustomSessionIDManager`, pay attention to `YourLoginController/LoginMethod` : ``` public class CustomSessionIDManager : ISessionIDManager { private readonly SessionIDManager _sessionIDManager; public CustomSessionIDManager() { _sessionIDManager = new SessionIDManager(); } public string CreateSessionID(HttpContext context) { return _sessionIDManager.CreateSessionID(context); } public string GetSessionID(HttpContext context) { var path = context.Request.Path.ToString(); if (path.EndsWith("YourLoginController/LoginMethod")) return null; return _sessionIDManager.GetSessionID(context); } public void Initialize() { _sessionIDManager.Initialize(); } public bool InitializeRequest(HttpContext context, bool suppressAutoDetectRedirect, out bool supportSessionIDReissue) { return _sessionIDManager.InitializeRequest(context, suppressAutoDetectRedirect, out supportSessionIDReissue); } public void RemoveSessionID(HttpContext context) { _sessionIDManager.RemoveSessionID(context); } public void SaveSessionID(HttpContext context, string id, out bool redirected, out bool cookieAdded) { _sessionIDManager.SaveSessionID(context, id, out redirected, out cookieAdded); } public bool Validate(string id) { return _sessionIDManager.Validate(id); } }``` - Put it on the `web.config`: <system.web> <sessionState sessionIDManagerType="CustomSessionIDManager" /> </system.web> Now it should return a new session for each login.
Madan ShekarPosted Sep 9, 2019, 2:07 AM
This solution is working for me in locally when i moved to server it is not working .
Philip DarkwaPosted Nov 7, 2016, 7:30 AM
I think the second Cookie(AuthToken) helps if the user logs out . but will not help when the user just leaves the application by closing the browser without logging out. Any ideas on protecting users who dont signout properly will be great.
Michal KPosted Sep 14, 2016, 3:41 AM
Hi, I dont understand how does the 2nd cookie help. I mean, if the attacker is able to steal cookie value of ASP.NET_SessionId, isnt he also able to steal the second one (AuthToken) ? And then use both keys to pretend he owns the session of the logged-in user.
James Vanlalfakawma ChhangtePosted Dec 8, 2015, 12:32 AM
In the page load event of master page or webpage of that master page
James Vanlalfakawma ChhangtePosted Dec 8, 2015, 12:31 AM
protected void Page_Load(object sender, EventArgs e) { if (Session["LIn"] != null && Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null) { if (!Session["AuthToken"].ToString().Equals( Request.Cookies["AuthToken"].Value)) { lblMessage.Text = "You are not logged in."; } else { .. } } .. }
James Vanlalfakawma ChhangtePosted Dec 8, 2015, 12:31 AM
When using master page, where should i put this code :
yash singhPosted Oct 16, 2015, 2:06 AM
Hi Ajay, This artical is very infomative for understanding Session Fixation Bug but i will request you can if you can show us how one can login into someone account by using only session id but not the user name or password . You only mentioned that attacker can inject the stolen cookie into brower and can successfully access the sensitive area of website without being logged in but i want you to kindly explain step by step how attacker can achieve this.
Gaurav Kumar AroraPosted Nov 14, 2014, 3:17 AM
well defined and detailed article
Sanjay TiwariPosted Nov 9, 2014, 12:22 AM
Dear Sir, Very useful article. I just wanted to ask one question .. I have created few sessions with the name Session["Name"] , Session["ID"], and let us assume the session id is 01234567890 2. I have changed the Session ID programtically then the session id is 09876543210(Let us assume). 3. After changing the SessionID programatically the session value which I have created earlier is becoming null.. Could you please let me know "How would I fix this". I want to change session id after login.
Devesh OmarPosted Nov 7, 2014, 6:22 AM
Very nice
Vithal WadjePosted Nov 6, 2014, 12:42 PM
super..no words to express
Jaganathan BantheswaranPosted Nov 6, 2014, 4:56 AM
interesting to know about Session Fixation Vulnerability
Rajeev RanjanPosted Nov 6, 2014, 4:36 AM
awsm article @ajay yadav