Just like everything else in ColdFusion there are multiple ways to handle session tokens. Technically there is no "right way", however, some ways are better than others. At least, in my humble opinion.
So, as I discussed in my previous post session tokens are used to identify a client to the web server so that the server can relate the correct session information for that client.
Well, if our users' session tokens can be guessed or otherwise compromised, we could have malicious users impersonating our legitimate users. Here is how that can happen:
- Our legitimate user logs into the web application and is assigned a token. They spend some time on the site, look around, update their credit card information, and whatever else.
- Meanwhile, our legitimate user is using the site, a hacker has somehow obtained that users session token. (We will get into how that can happen another day)
- The hacker goes to our web application and receives a new session token.
- Using a Firefox extension like Add N Edit Cookies or Edit Cookies our hackers opens the cookie that our application created and pastes our legitimate users session token into it.
- The hacker hits refresh, the browser passes the new token to the server, and the server now thinks the hacker is the legitimate user.
- Now the hacker can use the web application as if he were the legit user, even if the legit user has logged off. As long as the session was taken over before it had a chance to expire. And since a session cannot be forced to expire, the session will persist up to the length of time you have set in your sessiontimeout settings on the server or in your Application.cfc. This can give your hacker a large window for intrusion.
This is called session hi-jacking.
So how can we prevent session hijacking?One method is the use of more secure session tokens. So that they cannot be guessed and to narrow the window of opportunity for a hijack attempt.
One of the first things, and easiest things, you can do to secure your session tokens is to lower the timeout value for your users' sessions in your Application.cfc. In the Application.cfc psuedo contructor you can create (or modify if it already exists) this line.
<cfset this.sessiontimeout = CreateTimeSpan(0,0,10,0) />
This line tells the server to expire sessions after 10 minutes of inactivity. You should set the timeout value to a length of time appropriate to the security level required for your application.
For highly secure apps, like online banking, you would want it low, like 5 minutes. For less secure sites, like simple social networking sites that do not have personal data, maybe stick with the 20 minute standard.
You should try to choose a length that will not inconvenience your users, yet still make their sessions more secure. A 5 minute timeout for a Content Management System is highly unreasonable, since many times a user might spend 20 minutes or more entering data into a single form. If it timed out while they were doing that, they would get angry.
The second thing we can do, is make our session tokens
less more difficult to guess. By "guess" I mean less more difficult to find a valid token using a program like
The default session tokens used by ColdFusion actually suck pretty bad. But first, let's talk about the different types of session management available in ColdFusion.
There are two types of session management available in ColdFusion. There are, ColdFusion Session Management and J2EE Session Management. They each have their pros and cons.
The default configuration of ColdFusion Session Management is the one that sucks. It uses a CFID and an CFTOKEN combined with the application name to create the session id.
So a standard session id in this set up would look like: MYAPP_103_13624879
Now you might say to yourself. "Huh, that seems pretty secure. It has letters and numbers, it's pretty long. It looks random enough:..."
Well, as it turns out, none of that is true. Yes it does have letters and numbers, but the letters are not really part of the security. In fact, everyone that visits this application will get those same letters because that is the name of the application that was assigned in the Application.cfc:
<cfset this.name = "myApp" />
So, we know that the first part is not secure. What about those three digits in the middle (Which is the CFID btw), are they randomly generated?
They increment by one with each new session and max out at 4 digits, so the next person that visits and gets a new session will get CFID 104. So we already know the first half of the next persons sessionid. Huh, this does not bode well for the next person.
So what about that last group of number (the CFToken)? Surely that must be secure, look how long it is.
Well, the last set of numbers are (psuedo) randomly generated, that part is true. But secure... not really. The first problem is... they are only numbers! No letters, no dashes or other special characters, just numbers.
Now, since they are just numbers, it makes it real easy to do the math to determine how many possible CFToken values there could be. The CFToken is 8 charatcers long. So the math would be (10^8) or roughly one hundred million (100,000,000) possible values.
Wow, 100,000,000!!! That's a lot. Where are we ever going to find a computer that could try 100,000,000 session tokens in the 20 minutes it takes for a session to timeout?
Well, aside of the fact that just about any consumer PC could handle that kind of task, a few misconceptions can be easily reached when thinking about this.
- The hacker does not need to try all one hundred million to hit the right one. In fact after guessing half he's already had a 50% shot at getting one.
- The hacker is not limited to only the 20 minute period between the last thing that the end user does on the site to when the session times out. The hacker can be working on the hijack the entire time the user in on the site. That could be hours, or even days depending on the session timeout and any AJAX style keep-alive features like gmail has.
- The hacker is able to try again with the next user, if the last one fails.
So how can we make our tokens less easily "guessed"Well, ColdFusion Administrator has a great feature under SETTINGS called "Use UUID for cftoken"
What this feature does is it creates a UUID string of charatcers for the CFToken instead of those 8 stupid little digits.
A UUID is a 35 character alphanumeric string with an unbelievable amount of combinations. Look at our scenario earlier of our hacker who wanted to guess our 100,000,000 possible tokens, he thought that was pretty easy. But now our hacker may need to change his shorts.
The number of theoretically possible UUIDs is therefore 216*8 = 2128 = 25616 or about 3.4 × 1038. This means that 1 trillion UUIDs would have to be created every nanosecond for 10 billion years to exhaust the number of UUIDs. - Wikipedia
So our session token goes from looking like this: MYAPP_103_13624879
To looking like this: MYAPP_106_cd72ce3e98123588-75FEC96D-9137-2EF1-3880EA6C56CD1F4E
Let's see those side-by-side:
Well, this is just awesome. We've got our secure session token. We're done now. Right?
But we'll talk about J2EE session tokens tomorrow.