title: Session ID not random enough - ASP.NET
body: <p><strong>UPDATE</strong></p>
  
  <p>We eventually had a meeting with some programmers on the Acunetix team and they realized there may be a few bugs in their code that are causing this to be displayed in the scan as more of an issue than it actually may be. The general consensus was to ignore the scan results and use the out-of-the-box ASP.NET Session ID generation as it should be secure enough for our site.  </p>
  
  <p>@Vasile Bujac since your answer was the only one and mentioned using the ASP.NET standard solution I took that as the answer, but thanks everyone for your help. </p>
  
  <hr>
  
  <p>We use Acunetix's Retina scanner at work to do security scans on our applications.  It's telling us that our session ID's are not random enough and too predictable.  I'm not exactly sure how ASP.NET generates the session ID by default (I thought it was GUID anyways?), but I went ahead and implemented the method of extending the SessionIDManager class and overriding the CreateSessionID and Validate methods to use a Guid as explained in <a href="http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionidmanager.createsessionid.aspx" rel="noreferrer">this MSDN article</a>.  </p>
  
  <p>While this makes it slightly more random, it is still not producing the "desired" effect according to Acunetix.  I even added the <code>regenerateExpiredSessionId="true"</code> property to the web.config and that had no effect.  I have a feeling that I may need to deliberately call <code>Session.Abandon()</code> to truly clear the session and get a new ID.  Problem is then I have to call it right before a user logs in since it's the only fail-proof way to know the user is starting a new session.  So I couldn't set anything in session until the next page is loaded with the way the <code>Abandon</code> method works, and that would mean an in-between page which isn't very ideal but would do the trick.  </p>
  
  <p>Has anyone ever experienced this or successfully implemented a fix?</p>
  
  <p>Also, just an FYI, we don't use membership/forms authentication, we just create a new custom user class when someone logs in and save that in session for later use.  </p>
  
  <hr>
  
  <p><strong>Report from Acunetix:</strong></p>
  
  <p><a href="http://cwe.mitre.org/data/definitions/330.html" rel="noreferrer">CWE-330</a>
  <a href="http://capec.mitre.org/data/definitions/59.html" rel="noreferrer">CAPEC-59</a>
  <a href="https://www.owasp.org/index.php/Top_10_2007-A7" rel="noreferrer">OWASP2007-A7</a></p>
  
  <p><strong>Description:</strong>    Session tokens that exhibit low entropy ("randomness") are often susceptible to prediction attacks. Insecure tokens can be due to inadequate pseudo-random number generator, time-based values, static values, or values based on user attributes (username or user ID). This means that an attacker would be able to guess a valid session token after monitoring the application for a short period of time and gathering the session tokens it creates. If the attacker determines a valid session token for another user, then it may be possible to view, modify, or delete arbitrary users' data without having to guess the victim's username or password. Consequently, the ability to deduce valid session tokens enables the attacker to bypass login pages and obviate the need to brute force accounts. Additionally, static tokens can enable the attacker to target users even if the victim is not currently logged into the application. This increases the pool of victims which the attacker can target.</p>
  
  <p>Session tokens should be created with a strong random number generator and gathered from a large pool of numbers. For example, an operating system's rand() function can usually be sufficient if it can produce 32-bit values that are a statistically uniform distribution. Poor session tokens are incremental, rely on the user's account ID, only use time stamps, or have other highly deterministic information. Other methods of protecting a session token's security are to always transmit them over SSL, automatically expire the token after a certain period of time, and explicitly expiring the token whenever a user logs out of the application.</p>
  
  <p><strong>Recommendations</strong>:    If the session values exhibit strong randomness, but are chosen from a small pool of values, then the attacker has a better chance of simply guessing a valid token. A web application's session management can be improved by implementing several complementary techniques:</p>
  
  <ul>
  <li>Make sure that the Token values are at least 32 bits in size, especially for applications with large numbers of concurrent users and high amounts of daily page requests. </li>
  <li>The bit size of the source of the entropy (random values) is more important than the bit size of the actual session token. For example, an MD5 hash produces a 128 bit value. However, the MD5 hash of incremental values, a timestamp, or 8-bit random numbers are each insecure because the source of the random values can be easily predicted. Consequently, the 128 bit size does not represent an accurate measure of the session token. The minimum size of the entropy source is 32 bits, although larger pools (48 or 64 bits) may be necessary for sites with over 10,000 concurrent users per hour. </li>
  <li>In most cases, application-generated tokens (e.g. ASP.NET_SessionId, ASPSESSIONID, JSPSESSIONID, PHPSESSIONID) provide sufficiently large random values to prevent session prediction attacks. The application should use these session management alogorithms unless a custom session mechanism has been thoroughly reviewed and tested. </li>
  <li>Track user attributes associated with the session token with server-side objects to prevent user impersonation attacks. If the application does not strictly associate a user's session token with that user's profile information, then an attacker may be able to view arbitrary information by manipulating client-side values. For example, if the application sets a strong session token, but performs SQL queries based on a "UserId" cookie, then an attacker only needs to modify the "UserId" cookie to impersonate someone else. The application would be more secure if it associated the "UserId" value with the server-side session object because the attacker would not be able to modify the value. </li>
  <li>Expire session tokens when the user logs out of the application or after a predetermined period of inactivity. We recommend using a 20 minute timeout for a session token, although this largely depends on the type of application and the expected usage. </li>
  </ul>