r/dailyprogrammer • • Jul 21 '14
[7/25/2014] Challenge #172 [Intermediate] BREACH!
Description
This is the last time I hire monkeys to do my dirty work. Someone managed to break into our database and access all the data, I went in to inspect the problem and lo and behold, what do I see? Plaintext passwords!?
I hired some newer smarter guy who seemed to know what he was doing, I've spoken to my colleague who performed the code review on his program only to find out I've hired yet another monkey!
The password wasn't in plaintext, it was hashed, but an identical password brought back the same hash. How could I prevent this?
Maybe If I could get a unique hash for each user regardless of the password they enter that would solve the problem? Yes, that'll do...Damn monkeys...
Formal Inputs & Outputs
Input description
On standard console input you should enter a password of N length, it may contain any characters, numbers or punctuation.
Output description
The output will be a reasonably secure hash of the password. The hash should be different even if two passwords are the same. For example
peanuts
A2F9CDDA934FD16E07833BD8B06AA77D52E26D39
peanuts
0E18F44C1FEC03EC4083422CB58BA6A09AC4FB2ANotes/Hints
For this exercise, feel free to use any hashing algorithm you like, built-in or not.
You should probably research into GUID's and how they are used to prevent identical password hashing mistakes.
Here is a good read on this exact topic:
Bonus
Create the hashing algorithm yourself rather than using a built-in SHA-1 etc...
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
11
u/skeeto -9 8 Jul 21 '14 edited Jul 22 '14
C. I didn't want to use an external library, so I chose RC4 as the hash function since it's small. The difficulty is variable and can be changed at any time without effecting previous hashes. It's set by two parameters:
a, the number of key schedules to run (default: 262,143), andb, the number of bytes of output to skip (default: 16,777,215). These parameters along with the salt are all encoded as part of the hash, so they don't need to be tracked separately.RC4 isn't the best algorithm to use these days, but with the difficulty parameters I think my design should be reasonably secure. A nice property is that RC4 is automatically resistant to GPU attacks, because they're terrible at frequent, random-access lookups on arrays even as short as 256 bytes. The salt comes from /dev/urandom, which is a little overkill, but there are very few good sources of entropy in plain C.
Before I list the code, here's some sample output. These each take about a second to run on my computer due to the default difficulty settings.
Notice how all three hashes differ for the same password. The first 4 bytes is the salt, the next 4 is parameter a, the next 4 parameter b, then the RC4 output. The parameters are written in network order, so hashes will validate properly across architectures (I tested it between x86 and ARMv6). They all validate:
And just to make sure we're not fooling ourselves:
Edit: I have discovered a significant design flaw, related to RC4, that requires a breaking change in order to fix. Can you spot it?
Edit2: I turned it into a formal project, with the vulnerability fixed: https://github.com/skeeto/rc4hash
Here's the code:
2
u/sirtophat Jul 21 '14
aren't you just asking for a salt to be used? salt it with a random number or string or something and store that salt in the same row as the username and hash
5
u/jeaton Jul 24 '14
SHA-256 hash function in C++14 (based off of Wikipedia's pseudocode):
1
u/MrP_123 Jul 21 '14
Isn't it a bit early for that challenge?
3
u/lukz 2 0 Jul 22 '14
I guess the problem statement is still missing some information. I.e. I can get a password from input, then print out long random number and have quite high probability that the same passwords will give a different number each time (since I will not use the password at all). The statement does not say anything about later verifying that the number somehow belongs to that password.
1
u/Godspiral 3 3 Jul 21 '14
This tends to provide only moderate security improvement in that in the same compromised database that provides hashed passwords, will also contain the user names or other field that can be used to brute force common passwords. Even though this must be done for each user.
At any rate in J, 10 different passwords:
7b58f342121b2916175f9fc36cb62ac482f31aee 91e2084053b2daa6a3c4fe119ba129cc747eabb7 60b3af8bfe3735623c7d4a5ef749bb6ac1a4413a 2951be48a2647df1d36596d63a37fc62336ac3e1 987ea7c45096a7b5768f99bc0ad2610c21984eda fb709ec5dff41c211b87e92e41e2d975f4b2b8ce 5f4c6af0f3128c00cf3fe957ac2a7b5dc2eb1401 185b7281a32b37aa0635a436e6f8aa1641ebf478 f5049efe6e618f3af39967c68aab065fb88f3d5f ea113395e19c38825622684f0325baf0fc9f341e
1
u/MaximaxII Jul 22 '14
This will be the easiest one so far for me... because I already had the code for this laying around! It was for a project that I've pretty much abandonned. It's in 2 files - a maker and a checker.
#Challenge #172 Intermediate (2) - Python 2.7
password_maker.pypassword_checker.py#Sample input and output
Running the following command:
python password_maker.py test123will create the following output:salt.txt:encrypted.txt:And of course, running the
password_checker.pywith the correct password will return:1
u/[deleted] Jul 25 '14 edited Jul 25 '14
C#
[Github link](https://github.com/nathansoz/reddit-daily-programmer/blob/master/172-Breach%20(hashes)/c%23/Program.cs)
My solution is pretty basic. It prompts to enter the password and then generates a hash. It then regenerates a hash with a new GUID. Both get printed to the console. Right now I'm just using the .NET implementation of SHA1, but would like to re-write it, just for kicks.
I'm open to any comments or criticism!
Edit: fixed my link
1
u/dp_account Jul 25 '14
Simple solution in Python 3.4. It uses SHA1 and includes the salt at the beginning of the hash.
To Test it:
0
u/[deleted] Jul 21 '14
[deleted] — view deleted comment