Skip to content

CODExperiments

Doing it MY way!

  • Home
  • 2010
  • July
  • 26
  • You want salt with that?

You want salt with that?

Posted on July 26, 2010January 2, 2015 By Zoon 3 Comments on You want salt with that?
PHP, Programming

Yes you would, and you should!

Explanation follows shortly. Over the last few weeks, I’ve been helping out a friend with his new site. He payed some programmers from India a few hundred euros to complete the site, and they did.

However, they made more than a few errors!


The first most n00bish error they made, was not to properly escape input to MySQL, meaning the whole site was vulnerable to SQL injection attack. I tried it, and it worked like a charm = bad!

This error was quick to fix, and zoon after the site was a tad bit more secure. But then I discovered the next big(huge!) mistake.

All passwords are stored in PLAINTEXT in the database!!!!1!!

[ad name=”Google Adsense-1″]

Why is this bad? Well, if someone got a hold of the database, they could use it to access that persons account. They could even be so lucky, that they could access a different site, using the same username and password = not good!

People are generally not good at using different passwords for different sites.

[ad name=”Google Adsense-1″]

So what are the options?

  • Store the cryptographic hash (aka message digest) of the password instead of the password.
  • Store the cryptographic hash of the message + a salt

If you just want security against you plain kiddy hacker, you could go for option number one.
[sourcecode language=”php”]
<?php
mysql_query( sprintf("insert into usertable(name,password) values (‘%s’,’%s’)", mysql_real_escape_string($username), mysql_real_escape_string( md5($password)));
?>
[/sourcecode]

However, MD5 has been proven to be “weak”. You could/should replace it with SHA256.

If a good(proven) hacker gets hold of your passwords in the database, he would most likely use a Rainbow table to reverse engineer all the passwords.

Using a salt
Lets say that you for each user in your usertable, adds a new field which contains a salt. This salt is different for each user. The length of the salt should be fairly high, say about >128 characters.

Before the password is hashed, the password is appended/prefixed the random generated hash value.
[sourcecode language=”php”]
<?php
// generated random salt – length 128 chars
$salt = substr(md5(uniqid(rand(), true)), 0, 128);
// generate hash with salt
$hash = md5($salt.$password);
?>
[/sourcecode]

If the attacker gets hold of your usertable, he (or she for that matter) will have to calculate a new Rainbow table for each user. The larger the salt you use, the bigger the Rainbow table, the longer it takes to calculate.

A hidden salt value would be ideal, but thats not always possible.

Tags: HTML MySQL PHP

Post navigation

❮ Previous Post: Duplicate Image Finder … sort of!
Next Post: Windows 7 and PHP/MySQL ❯

You may also like

Programming
Crash course in HLSL
March 25, 2010
Programming
NZB Completion Checker v1.2.1.0 released
August 29, 2012
Programming
Sharepoint 2010 MCTS
November 1, 2010
Programming
NZB Completion Checker 0.9a update #3
January 19, 2012

3 thoughts on “You want salt with that?”

  1. chris says:
    July 26, 2010 at 15:10

    Nice post bud.

  2. zoonman says:
    August 16, 2010 at 19:16

    Sorry, but hacker gets your users table with salt and all your tricks is will be stultified.

  3. Zoon says:
    August 31, 2010 at 12:56

    I’m pretty sure he/she can get the user table. The question is how long it will take for the hacker to crack the passwords and for you to change them!

Leave a Reply

You must be logged in to post a comment.

Downloads

Get it while it's HOT:
NZBcc 1.2.2.0

Downloads
might be ad supported. If so, click in top right corner to continue when the counter reaches 0.

Support NZBcc



Sign up to Dropbox using this link

Archives

  • March 2016
  • September 2014
  • July 2014
  • January 2014
  • August 2013
  • January 2013
  • December 2012
  • November 2012
  • September 2012
  • August 2012
  • June 2012
  • May 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • August 2011
  • July 2011
  • April 2011
  • January 2011
  • December 2010
  • November 2010
  • August 2010
  • July 2010
  • April 2010
  • March 2010
  • January 2009

Tag Cloud

AJAX Android Articles C# Download FMV Giganews HLSL HTML HTML5 Javascript jQuery MPEG Decoding MySQL Netflix NZB NZBcc PHP SQL Usenet Windows 7 XNA

Copyright © 2025 CODExperiments.

Theme: Oceanly News Dark by ScriptsTown