views:

65

answers:

1

i need help understanding password security. concept of salts, nonce etc.

i read http://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords but its quite advanced for me. i don't really understand what problems salts etc solve, and how it is implemented, maybe from the basics

// basics i learnt from school, using md5 for user input
SELECT UserID, Username FROM Users 
WHERE Username = "user1" 
AND Password = MD5("pass1")

i guess the problem is hackers can have a dictionary of common passwords hashed with MD5() to find out user passwords? and just curious, they do this so they can find out which user uses which password? provided they got the user database? if i am wrong, what is the issue of using just MD5?

i think i need help understanding what is the problem with using MD5 only, then progressively, how does salts help, different hashing methods etc.

as i do not develop really secure apps, i also want to consider performance. i dont want a very secure but slow app :) so no need overkills, which will likely confuse me anyway

UPDATE

i dont a very basic example seeing of my logic of authenticating users with salts is right. basically, i need to get the salt of the user from the database using the username then do the actual authentication query right?

mysql_connect("localhost", "root");
mysql_select_db("test");

// simulate user entry 
$username = "user1";
$password = "password1";

// get salt for username 
$result = mysql_query("SELECT salt FROM users WHERE username = '$username'");
if ($row = mysql_fetch_assoc($result)) {
  $salt = $row["salt"];

  // actual authentication
  $sql = "SELECT id FROM users WHERE username = '$username' AND password = '" . hash_hmac("sha256", $password, $salt) . "'";
  $result = mysql_query($sql);
  if ($row = mysql_fetch_assoc($result)) {
    echo "login success. userid = " . $row["id"];
  } else {
    // wrong password
    echo "login failed";
  }
} else {
  // wrong username
  echo "login failed!";
}
A: 

Salting stops someone from a rainbow table attack against any stolen hashes they may have.

For example, if your password in the database is stored as the md5 hash "098f6bcd4621d373cade4e832627b4f6", you can look that up in a rainbow table (a pre-calculated table FULL of hashes), and see that this hash is the word "test".

Now, lets say we salt the very-easy to crack password above with some data such as "jiewmengasked14minsago" and put "test" onto the end of it. We get "819217402cd5e9755d28fefb26adad52", and you probably won't find this in the rainbow table. Obviously, use a more random string than that.

I would advise to use a salt for each user, not the entire database. This way, if someone learns the salt, they might just generate their own rainbow table (granted, this takes a lot of time), but if everyone has their own hash, you've made it extra hard for them to take everything.

The problem with md5 is that the algo used to generate your hash is not as secure against modern cracking techniques, try using something like SHA-256.

Hashing and salting will NOT hurt preformance to any massive degree. You are putting all your users at great risk, because people share passwords. Thanks to products like MSN messneger ("windows live"), which clearly don't care either, are the reason we get so many problems: they didn't bother to secure to password data, and can be cracked in seconds with programs like Cain. This will expose your user to a world of hurt because they're using that same password for 5 emails, google, stack overflow, banking, and who knows what else. Even though YOU might not care about password security, I do, so don't screw with me, I'm your user.

if i have 1 salt per user, and i cant store the salt in the database, how shld it be stored/implemented?
jiewmeng
Why can't you store it in a database? The salt is not the password. http://stackoverflow.com/questions/674904/salting-your-password-best-practices
hmm i read it somewhere, i thought it makes sense. to use a dictionary attack, i think he will have hacked got the database. so he can see your salt and hash. then he can use your salt crack your password?
jiewmeng
No. That's not how hashes work. You need to stop what you're doing, sit down and take the time to properly research this so you know what you're doing, passwords and security are not a "whatever" issue.
oh i get what you link means. even if the hacker knows your salt for the user, he stills need to create a dictionary for that one password to hack it. to hack all the data it will takes `the number of user` times more. isit?
jiewmeng
@jiewmeng If the attacker has the salt and hash, he still has to do a brute force attack on the password. The random salt makes using precomputed tables infeasible for the attacker.http://stackoverflow.com/questions/1645161/salt-generation-and-open-source-software/1645190#1645190
David
ok now to the implementation. i am using PHP so i read abt [hash_hmac](http://www.php.net/manual/en/function.hash-hmac.php) in `hash_hmac('sha256', 'password', 'salt')` can i say that the function creates a hash of `password` using `sha256` with the salt of `salt`? the docs says `hash_hmac` generates a **keyed** hash using the **hmac** method. can i say keyed is salted and whats hmac
jiewmeng
also on implementation, how shld i generate the salts. will `hash('sha256', rand())` do?
jiewmeng
No, do not use rand. And do not use MT_Rand, neither of these are secure enough. See the comment here: http://www.php.net/manual/en/function.mt-rand.php#83655
wow ... i think i just want to understand whats happening in that code, correct me if i am wrong. i am using the random number generator of the OS instead of PHP. the code gets 16 bytes of random data generated from `/dev/urandom` in linux and `CAPI_UTIL` in windows. then i assume `$pr_bits` will be used as the salt, but why in windows `md5()` is run on `$pr_bits`.
jiewmeng
and i also want to know `hash_hmac('sha256', 'password', 'salt')` can i say that the code hashes the string `password` with `sha256` with salt `salt`?
jiewmeng
can u also check my update and comment if the logic is right? i know i did not use the way u recommend i generate salts, as i dont really get it yet. and i also am not using prepared statements or PDO etc. i am only wanting to understand the concept of salting
jiewmeng