Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

Making Sure Your Users' Passwords Are Secure

  • Print
  • + Share This
One way to improve the security of your sites is to beef up your methodologies for storing and handling user passwords. In this article, web developer Larry Ullman explains how to more securely store passwords and properly allow users to access your site when they’ve forgotten their password.
From the author of

If your website has users that can log in, you are either storing the user’s credentials in some manner or using OpenID (or the like) to put that burden elsewhere. If you are storing the user’s credentials, you must secure the password to an appropriate level for your site. Towards that end, this article specifically discusses two aspects of password management: how to properly hash passwords for storage, and how to securely handle the forgotten password situation. But first, I want to rehash (pun!) what security is.

Understanding Security

The first thing everyone needs to understand about security is that it’s not a binary thing that can be turned on and off like a switch. Security is a sliding scale. You cannot make a website “secure”—you can only make it “more secure” or “less secure.” Thinking that your site is 100% secure is the kind of attitude that gets you into trouble.

As an analogy, think about where you live: a house, an apartment, a dorm room, whatever. Presumably, you lock the door(s) when you leave. Maybe you even have two locks. But does that mean the house or apartment or whatever is secure? No. Someone could break a window or pick the lock. So does that mean your home is not secure? Again, no. The locked door(s) only mean that it’s not easy for someone to get in.

This apparent contradiction leads me to my second point: Understanding that security is a scale—not an on/off state—you have to implement the appropriate level of security for the application. Depending upon many factors, your abode may be secure enough with just a locked door. For someone else, a monitored security system provides the correct level of security. The same is true for a website: Find the right security tools for the site in question. A content management system (CMS) does not require the same level of security as an e-commerce site, which still does not need to be as secure as an online banking or investing site.

Why wouldn’t you always implement the highest level of security, you may ask. Remember that security comes at a price. First, more security requires more of your time and effort. It also requires more of the end user. Second, more security normally has an adverse affect on performance. For example, requiring users to log in is an imposition on them, but is often a reasonable imposition to make.

The reason I’m explaining all this is that you need to decide when and how to implement the following specific ideas based upon the particulars of your site and its sensitivities.

Selecting a Hashing Algorithm

The first thing you should make sure your site is doing is storing passwords in a secure manner. On most sites these days, the password itself is not stored but rather a “hashed” version of the password is. A hash is just a mathematically computed representation. Storing a hash is more secure than storing a password, but how securely hashing is done depends upon two things:

  • The hashing algorithm used
  • The use and choice of salts

The hashing algorithm is the bit of programming that generates a hash of a value. For years, MD5 was commonly used, but it has been cracked for some time (i.e., it’s been proven to be less secure). Next, people turned to SHA1. While more secure than MD5, SHA1 has also been cracked. Again, this doesn’t mean you should never use SHA1, but it means that’s a less secure solution than some others.

If you’re using MySQL as your database application, you can use SHA2. Support for it was added in MySQL 5.5.5, and it is more secure than MD5 or SHA1. Here’s how you would use SHA2 to store a hashed version of a password:

INSERT INTO some_table (pass) VALUES (SHA2('actual password'))

That’s the short version of the applicable INSERT query; obviously, the real-world use would depend upon the particulars of the application. To verify that the correct credentials were supplied upon login, you would again use SHA2():

SELECT some_columns FROM some_table WHERE email='provided email address' AND pass=SHA2('provided password')

If that query returned one record, then the proper credentials were entered.

For an even more secure hashing algorithm, you can turn to the HASH Message Digest Framework in PHP. This toolset, built into PHP as of version 5.1.2, allows you to choose from a range of hashing algorithms to hash passwords on a security level of your specific choosing. It does require a bit more knowledge of security and algorithms in general, though.

To use this route, invoke the hash_hmac() function in PHP:

$pass = hash_hmac('sha512', 'actual password', 'secret key');

The first argument to this function is the hashing algorithm. Depending upon your operating system and version of PHP, there may be 40 or more to choose from. You’ll need to do some research to select an appropriate one for your situation. As with everything in security, the most secure algorithms will take longer to hash the data.

The ‘secret key’ value needs to be kept in a safe place, such as a secure file in which it’s defined as a PHP constant. Of course, the same secret key and algorithm must be used to hash the user’s password upon registration and to compare the password during login attempts.

Salting Passwords

Storing hashed versions of passwords is far, far more secure than storing passwords in plain text, but that alone is not secure enough under many situations. Better security can be attained by adding a “salt” to a password prior to hashing it. A salt is just a random collection of characters. Adding a salt has two benefits:

  • It makes the password longer, and longer passwords are always more secure (e.g., the salt can help atone for users who provide short passwords).
  • It results in different hashed versions of the same password by multiple users.

This second fact is the most important, particularly as your site has more users. Knowing why this is beneficial requires some knowledge of how systems are hacked, but the short description is this: If multiple users register with the same password, which can easily happen (just look online for lists of common passwords), then the hash of that password for all of those users will be exactly the same (when not using salts). This makes it easier for a hacker to break into the system using rainbow or lookup tables.

The solution is to add a randomly generated salt to each password. By doing so, even if multiple users have the same password, the stored hashed versions differ.

A common mistake is to think that the salts have to be secret—they don’t. They just need to be random, relatively unique, and the longer the salt is, the better. A single-character salt is better than none at all, but ideally the salt should be the same length as the output of the hash itself. For example, SHA1 results in a string 40 characters long, so you should use a 40-character salt.

So how do you make a good salt? In PHP, the most secure solution is to use the openssl_random_pseudo_bytes() function, added in version 5.3. Provide it with a length argument, which is the number of bytes that should be returned:

$salt = openssl_random_pseudo_bytes(20);

Keep in mind that this function returns bytes of data (i.e., binary data). To convert the binary salt to a character string, apply bin2hex():

$salt = bin2hex(openssl_random_pseudo_bytes(20));

Now the salt can be appended to the password, and the combination salted:

$pass = hash_hmac('sha512', 'actual password' . $salt, 'secret key') ;

Be sure to store the salt in the database record, too, so that it can be used again during verification of the credentials.

Handling Forgotten Passwords

If your site stores user credentials, then you inevitably have users who forget their passwords. As a hash of the password is stored, not the password itself, you cannot resend the registered password to the user. One solution is to reset the password in the system to something random, and then send that in an email to the registered email address. Using the new password, the user can log in (and hopefully change their password upon doing so). For some sites, this approach may be okay, but email is not a secure protocol (normally), so sending passwords in emails is not ideal. Second, such systems put no time limit on when the new password can be used.

Offering a non-password solution to the forgotten password scenario can mitigate both of these problems. This alternative is a token-based solution, that works like so:

  1. When the user submits the lost password form, a token is generated by the system.
  2. The token is sent to the user’s email address as part of a link.
  3. When the user clicks that link, the user is taken to a specific page on the site that validates the token.
  4. If the token is valid, the user is immediately asked to change his or her password.

This may sound similar to the new password approach, but note that no password is being sent in an email. Second, and more importantly, the token should have a limited life to it (i.e., set to expire within X number of minutes). The token should also expire once used.

The token needs to be associated with the user’s account, but should not be reflective of the user’s information. In other words, you can’t just use the user’s ID or email address as the token. One idea would be to create a hash of a combination of the user’s ID, email address, and perhaps registration date. Salt the value, too, and you have a nice, unique token.

In terms of code, the page that handles the forgotten password request would validate the submitted username or email address and then use that to retrieve the pertinent information from the database:

$q = "SELECT CONCAT(id, email, date_registered), id FROM users WHERE email='actual email'";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) === 1) {
    list($data, $id) = mysqli_fetch_array($r);
} else {
    // Report problem.
}

That information can be used to create the token:

$salt = bin2hex(openssl_random_pseudo_bytes(20));
$token = hash_hmac('sha512', $data . $salt, 'secret key');

Store this token in a separate database table, along with an expiration date and time. Here’s that query:

INSERT INTO password_tokens (token, id, expiration)
 VALUES ('$token', $id, DATE_ADD(NOW(), INTERVAL 15 MINUTE))

And, again, this token is sent to the user’s email address, as part of a link:

http://www.example.com/reset.php?t=$token

The reset.php page would first confirm that a token was received in the URL, and that it’s of the correct length.

The page would then check the token against the stored tokens. That query is:

SELECT id FROM password_tokens WHERE token='$token' AND expiration<NOW()

As you can see, the query uses an extra conditional that confirms that the expiration date and time is less than the current moment. This prevents password reset attempts that take too long (which can be suspicious).

If that query returns one record, the user should be presented with the option to change his or her password to a new one. The token record should also be deleted from the database:

DELETE FROM password_tokens WHERE token='$token'

If the SELECT query did not return a record, then either the token is invalid or it has expired. In either case, simply explain to the user that the password reset process must be started again, and that the user must act upon the email within the time limitation.

And that’s how you would implement this token-based system. As an added benefit, this approach allows users to ignore unwanted password resets. For example, if I were to go to X website and enter someone else’s email address in the lost password form, this token-based system would not actually change the user’s password. Yes, the user would get an unrequested email (as far as he or she could tell), but the user can ignore that email and continue logging in with his or her current password.

Conclusion

There is always more to be learned about security, and this article should have added another trick or two to your arsenal. Consider implementing these ideas on your next new project, and perhaps even go back to an older site and see if it could stand to be improved. Most importantly, make sure that you’re adhering to an appropriate level of security for each project, based upon that project’s needs and sensitivities.

  • + Share This
  • 🔖 Save To Your Account