TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (3) Thread Tools Search this Thread Display Modes
Old 09-18-2007, 12:19 PM   3 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Cryptography's Sodium Chloride

All hail the humourous titles. The cryptography definition of salt, as taken from Wikipedia.org, is that salt consists of random bits used as one of the inputs to a key derivation function. In more human terms, a salt is probably your best friend.

The weakest point to any one-way hash algorithm, such as the MD5 and SHA series, is that one string will always generate the exact same algorithm string - no matter how many times you hash it. This leads to a major downfall in that common words have common hashes. Take the following code which simply hashes the string buddha and hands us back a MD5 hash.

PHP Code:
echo md5('buddha'); 
The resulting hash is 4b3bd325788f47666ec36669c8aaf5d0. MD5 is a 32-bit algorithm which typically means we have 32 characters in the base 16 numbering system, hexadecimal, giving us 32 characters from 0 to F.

No matter how many times I execute that PHP script, the resulting hash for buddha will always be the previously stated 32 bit value. From this unavoidable downfall, many databases have been created that will accept a 32 bit MD5 hash, or a 40 bit SHA1 hash, or even a 256 bit SHA256 (although very rare that you'll find a database that stores these due to their rarity in common applications) hash, and give me back the word. No Albert Einsteins were resurrected for any mathematical equation into reversing the hash. As of the time of writing, MD5 and SHA1 are still both irreversible despite their source code being readily for the entire world and its gremlins to see. All it has done is performed a simple query on its database:

Code:
SELECT
	myWord
FROM
	myTable
WHERE
	myHash = '4b3bd325788f47666ec36669c8aaf5d0'
The result is the word buddha because buddha always generates the exact same hash - the one stored in the database. If that hash had been taken from a member table, I would now know the individual's password and be able to login immediately. This is where salt changes all of that.

You see, if the stored MD5 hash was scrambled in such a way that it would give me a nonsensical word such as !7ex?buddha then the resulting hash would return null when entered into a database of commonly stored hash strings. Notice that my password, buddha, is still in the string, but hashing the aforementioned string would give me a totally different hash algorithm: 936cc740307df25a1aca31b3afda2d3d.

You may now be asking yourself how, if my password is buddha, is a user going to remember his new password that we have constructed for him. The answer is, he doesn't. He still has to remember his entered password but we can append the rest when he attempts to login. The only aspect we are concerned about is having a unique hash to prevent individuals from somehow stealing our hash strings from the database and then cheekily accessing all our members' accounts.

In the example that I have crafted, !7ex?buddha, the bold part would become our salt. Whereas the word, buddha, would be our normal password that we enter. Upon logging in we append !7ex? to the beginning of buddha and MD5 them together. The hash would match the one in our database if we hashed !7ex? with buddha in the first place when the user registered. However, it would not match any common MD5 strings and that's definitely a big plus.

PHP Code:
$szPassword 'buddha';
echo 
md5('!7ex?' $szPassword); 
This will give us the exact same hash as we previously hashed: 936cc740307df25a1aca31b3afda2d3d. The crafty thing is, even if an attacker knows what the salt is, it's pretty useless because neither MD5 nor SHA1 can be reversed. Removing the salt is relatively impossible and would take an exceptionally fast machine - we do have those capabilities in the super-computers around the world, but this is outside the scope of the article.

If you can find a way to transform 936cc740307df25a1aca31b3afda2d3d into 4b3bd325788f47666ec36669c8aaf5d0 then you're wasting your life away on the Internet! You may be able to do it once in this instance, but coming up with an algorithm that transforms them all into their common hash, by removing the salt, is certainly a little more tricky.

Speaking only in essence, there are two ways of determining a hash. See our article on randomising values for generating a random hash. The 2 ways are as follow:
  • Static hash: the hash is always the same for every password generated.
  • Dynamic hash: each user has a unique salt which is stored in the database alongside their hashed password.

Using the static option, guessing one salt, although pretty harmless, would mean you know every single salt. However, with the dynamic salt, you would need to acquire the salts individually, because everyone has a different salt. The dynamic option would also require a little extra load on your query, but nothing at all major to cause any concerns. We could achieve the login SQL statement like so:

Code:
SELECT
	username
FROM
	members
WHERE
	password = MD5(CONCAT(salt, 'buddha'))
This would accept out password, buddha, and conflate it to the salt and MD5 it. If it is found then it would return the username, Wildhoney.

As you can see, our hash string is now undecipherable. Placing the hash into a website that checks against thousands, if not millions, of commonly used hashes is not going to make any different what-so-ever. In fact, the only security issue you have to worry about is how the cheeky little devils got the hash algorithm in the first place!

Incidentally, this article actually stemmed from an article I clicked through to from Pixel2Life. In the guy's tutorial he mentioned the following. Note that I've quoted exactly how he wrote it - no humourous modifications.

PHP Code:
$password 'dog'// lets pretend the password is dog - it is very bad to keep it in the database like this.

$password md5('dog'); // slightly better, but can be decrypted easily.

$password sha1(md5(md5(sha1(md5(sha1(sha1('dog'))))))); // much better, hackers would be quite good to decrypt that. 
Now what's so bad about this? Well, apart from the fact that hash algorithms inside hash algorithms inside hash algorithms has the likely possibility of causing all sorts of unwanted issues, such as a significant increase in the chance hash collisions. But the most worrying part of the script is that it would considerably slow down the script itself. I can't put it any simpler, but the code is both unnecessary and idiotic. The surprising thing is that I found that code in an article that was aptly named, Top 10 Beginner Mistakes in PHP - Learn about common coding errors in PHP. Now if that was me, that would be number 1.

Note: I would link to the article itself but it's not even worth a back-link.

As you can clearly see from the article, a cryptographic salt prevents a MD5 or SHA1 algorithm from outputting common hash strings if the string they receive to hash is a common word. Often a dictionary word. Although in itself salt cannot prevent brute force attacks, they do prevent people who have access to the algorithms from deducing the password.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-18-2007, 01:10 PM   #2 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 349
Thanks: 24
Haris is on a distinguished road
Default

Fantastic Adam.

Now tell me, how do you do dynamic salts?
Haris is offline  
Reply With Quote
Old 09-18-2007, 03:39 PM   #3 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 593
Thanks: 15
Village Idiot is on a distinguished road
Default

For passwords systems, I much prefer sha1, easy and uncrackable. sha1 uses a destructive, but consistent algorithm, it has been reverse engineered, but it still leaves an infinite amount of possibilities. More then one sha1 is not needed.
Village Idiot is offline  
Reply With Quote
Old 09-25-2007, 05:10 PM   #4 (permalink)
The Visitor
 
Join Date: Sep 2007
Location: Tulsa, Oklahoma
Posts: 2
Thanks: 0
steveco is on a distinguished road
Default

Very good tut. I don't usually register to sites, but after finding this one on pixel2life I felt like I wanted to respond, so now you have a new member. I too read the other tutorial found there about encrypting your strings with a ton of md5's and sha1's and did a legit spit take all over my monitor. Don't worry, it's clean now. The way I do my salts is I concatenate the timestamp from when they registered to their password. Before reading this I would do 2 queries, one to get the timestamp, then concat, md5, then another query to check against the hashed password in the database. Now thanks to this tut, I know that you can md5 something right in your query. Strange I've never come across that before in any of my books or other tutorials, so thank you very much, you saved me 1 query and a couple of micro seconds, which is always a plus. Keep up the good work.
steveco is offline  
Reply With Quote
Old 09-25-2007, 06:05 PM   #5 (permalink)
The Contributor
 
mortisimus's Avatar
 
Join Date: Sep 2007
Location: United Kingdom
Posts: 41
Thanks: 4
mortisimus is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
I much prefer sha1, easy and uncrackable.
Cracked: http://www.undosha1.com/
Send a message via Skype™ to mortisimus
mortisimus is offline  
Reply With Quote
Old 09-25-2007, 07:14 PM   #6 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Wales - UK
Posts: 8
Thanks: 0
Dorza is on a distinguished road
Default

That system hasn't cracked SHA1, it seems to be a comparison system...

If you go here and create an alphanumeric string with lower and upper case characters say about 6 characters long and make an SHA1 hash of it then try to decrypt it with the one on undoMD5, it will more than likely not be able to to it. But if you first enter those same characters into undoMD5; create a hash of it and then try to decrypt it, it will find the characters.

Thats all it does is search for the entered characters and bring back the hash from another field. The hash was created and stored in the database along with the string you wanted to encrypt when you first created a hash, so when you go to decrypt the hash thats all it does is search for the hash and bring back the plain text characters for that hash. It's hardly "decrypting" anything, it's kind of false advertising;). The more plain text characters entered on that site the better it becomes and finding the hash.
Dorza is offline  
Reply With Quote
Old 09-26-2007, 04:20 AM   #7 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 593
Thanks: 15
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by mortisimus View Post
Not at all, its simply a database comparison, not cracked at all. I could write a script that complex in 20min, the ajax would take longer then the php.

It is literally impossible to crack sha1, it is a destructive algorithm. Have you ever noticed all hashes, no matter how long are the same length? The closest you can come to hacking it is reverse engineering the algorithm, leaving an unlimited number of possibilities, a large number of practical possibilities. If you know the format of what it is (such as a credit card number), you might be able to crack it. Therefore it is not safe to keep credit card numbers with it, you use 256 bit encryption for that.

A group in china is rumored to have found a process to reverse engineer, but I dont know if its true.
Village Idiot is offline  
Reply With Quote
Old 09-26-2007, 06:19 PM   #8 (permalink)
The Contributor
 
mortisimus's Avatar
 
Join Date: Sep 2007
Location: United Kingdom
Posts: 41
Thanks: 4
mortisimus is on a distinguished road
Default

well... that told me... :P
Send a message via Skype™ to mortisimus
mortisimus is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/tips-tricks/1162-cryptographys-sodium-chloride.html
Posted By For Type Date
PHP Cryptographys Sodium Chloride Tutorial This thread Refback 12-28-2007 09:24 PM
Light Blue Touchpaper » Blog Archive » Google as a password cracker This thread Refback 12-23-2007 07:06 PM
PHP Security Cryptographys Sodium Chloride Tutorial This thread Refback 12-22-2007 04:44 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:34 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0