Security Through Obesity
Jeremy Spilman recently proposed changes to how user's hashes are stored in website's and companies databases. This post was originally going to look at some of the issues involved in the scheme he envisioned, however, he rather quickly posted a followup article with a well thought out solution that countered all of the issues that other people and myself were able to come up with. I'd strongly recommend reading both if you haven't done so. Instead of announcing flaws, I'm turning this into a post with a simple functional implementation of the described scheme in Ruby using DataMapper.
At first I'd like to point out that this is one of those few examples where a form of security through obscurity is actually increasing not only the perceived security but the cost to attack a system as well.
Please note this code is a minimal, functional, example and should not be used in production. It is missing a lot of things that I personally would add before attempting to use this but that is an exercise for the reader. I'll walk through the code briefly afterwards going over some bits.
|
|
I tried to keep this as a simple minimum implementation without playing golf. Strictly speaking the validations on the data_mapper models aren't necessary and could have been removed, in this case, however, the length fields do actually indicate a bit more of what you might expect to see in the database, while the requires are just good habits living on.
Both of the two models are required to have both a salt and a hash, the name crypt_hash
was chosen do too a conflict with one of data_mapper's reserved words hash
, the same goes for the model name, however, that class comes from elsewhere. Raw scrypt'd hashes are 256 bits long or 64 hex characters long, while the salts are 64 bits (16 hex characters) plus some meta-data totaling 25 hex characters in this example.
Salts are hashes are computed by the "scrypt" gem. In this example I've bumped up the max time option to create a hash from the default of 0.2 seconds up to 1 second. This is one of those things that I could have left out as the default is fine for an example, but it also couldn't hurt slightly increasing it in case someone did copy-paste this into production.
The one thing that I'd like to point out is a couple of "puts" statements I dropped in the check_password method on the User model. The first one simply announces an invalid password. A lot of these could indicate a brute force attack. The second one is more serious, it indicates that there is either a bug in the code, a hash collision has occurred, or an attacker has been able to drop in hash of their choosing into the site_hashes table, but haven't updated the verification hash on the user model yet. I'd strongly recommend reading through both of Jeremy's posts if you want to understand how this threat works and specifically the second post to see how the verification hash protects what it does.
So how would you use this code? Well you'd want to create a user with a password and then check if their password is valid or not later on like so:
One of the key ways this separation increases the security of real users hashes is by having a large number of fake hashes in the hash table that the attackers will have to crack at the same time. As a bonus I've written a module to handle just that for the code I've already provided. Once again this is licensed under the MIT license and should not be considered production ready.
|
|
development programming ruby security
5cc61bdd @ 2024-07-15