Monday, August 13, 2012

Security questions are salt

It's common for web sites to have a password recovery feature and some ask the user to set up answers to security questions which they can answer later. The main feature of these is that they are intended to be something the user knows and can answer later without remembering. So questions like "What was the make of your first car?" are common.

Unfortunately, these questions are weak because a determined attacker can often find out the information required to answer the questions. In one notable case a US Vice Presidential candidate's email was hacked by searching for and finding the target's high school name and date of birth. This leads to these questions being very insecure.

In addition, some questions, such as "What was the make of your first car?" have a very small likely answer space. If the target is British, for example, the number of car makers is small and a guess is quite likely to work, especially considering that it's unlikely that a first car will come from a luxury manufacturer.

My personal approach is to not forget my passwords (because I use unique passwords that I can access as needed), and I fill in these questions with nonsense.

But, if you do want the option of using the recovery feature then there's a simple solution: consider the question as password salt and answer using a hash.

Here's how that works:

1. Think of a password that you will remember, that's long and complex (perhaps even a passphrase as you will not need it often). You'll use this password to create answers to security questions everywhere.

2. When confronted by a security question answer with the result of hash_function(passphrase, security_question).

For example, suppose that you've chosen the passphrase "honi soit qui mal y pense" and you are being asked to choose an answer to the question "What is the make of your first car?" you would calculate (here I am using bcrypt (program below) because it's secure and slow) a hash:

    perl bcrypt.pl "honi soit qui mal y pense" "What is the make of your first car?"
  ouVXFntvrfbJCHwoEvfbF8Hn3gYik.W

and enter the response as "ouVXFntvrfbJCHwoEvfbF8Hn3gYik.W"

Any time you actually have to answer the question it's simply a matter of recalculating the hash from the question and the password you've chosen.

The security question is acting as salt, the security of this relies on: the long, complex passphrase chosen and the secure hash algorithm.
use strict;
use warnings;

use Digest::SHA1 qw(sha1);
use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);

my ($passphrase, $question) = @ARGV;
my $salt = en_base64(substr(sha1($passphrase),0,16));

my $hash = bcrypt($question, '$2a$16$' . $salt);
$hash =~ /$salt(.*)$/;
print "$1\n";
PS As Damian points out below, reading one of these would be a nightmare over the phone. So an improvement is to find a sequence of words that encodes the hash. Here's one approach based on finding words that contain subsequences of the hash value in the dictionary.
use strict;
use warnings;

use Digest::SHA1 qw(sha1);
use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);

my ($passphrase, $question) = @ARGV;
my $salt = en_base64(substr(sha1($passphrase),0,16));

my $hash = bcrypt($question, '$2a$16$' . $salt);
$hash =~ /$salt(.*)$/;
my $output = $1;
print "$output\n";

my @words;

open F, "</usr/share/dict/words";
while (<F>) {
 chomp;
 my $w = $_;
 if ( $w =~ /^[a-z]{5,}$/ ) { 
  push @words, lc($w);
 }
}
close F;

@words = sort { length($a) <=> length($b) } @words;

$output = lc($output);
$output =~ tr/0123456789/oizeasblxq/;
$output =~ s/[^a-z]//g;

print word($output), "\n";

sub word {
 my ( $w ) = @_;

 my $found = find_word($w);

 if ( $found ne "" ) {
  return $found;
 } else {
  foreach my $i (reverse 1..length($w)-1) {
   my ( $left, $right ) = (substr( $w, 0, $i), substr($w, $i));
   $found = find_word($left);
   if ( $found ne "" ) {
    return "$found " . word($right);
   }
  }

  die "Couldn't find a word";
 }
}

sub find_word {
 my ( $w ) = @_;

 $w = join('.', split(//,$w));

 foreach my $x (@words) {
  if ( $x =~ /$w/ ) {
   return $x;
  }
 }

 return "";
}

Then instead of trying to say "ouVXFntvrfbJCHwoEvfbF8Hn3gYik.W" you say "bogus vexed definitive prefab jackhammer whole vivify abaft exchange angry bilks aglow"

And you could likely get away with not the entire response but something shorter, say just take the first two words. So, the response to "What was the make of your first car?" could be simply "bogus vexed"

If you enjoyed this blog post, you might enjoy my travel book for people interested in science and technology: The Geek Atlas. Signed copies of The Geek Atlas are available.

8 Comments:

OpenID damiancugley said...

Nice!

My only quibble is that they might rephrase the security question slightly which would ruin everything, and you better use an algorithm you can if need be write the script for extemporaneously on someone else’s computer, just in case of emergencies!

It will also be really annoying reading these ‘answers’ over the phone to a call centre person … :-)

10:46 AM  
Blogger zyphlar said...

I like the idea of at least lying about the answer (your first car was a banana?) or salting it in English (your first car was a holli polli cracker BMW?)

11:10 AM  
Blogger Edd Dumbill said...

Smart work. It's a nice touch that your passphrase is my old grammar school motto. Theoretically discoverable in my case ;)

2:02 PM  
Blogger btilly said...

A compromise of one site that uses a given security question may result in someone being able to get into a different site that uses the same security question.

To fix that the site MUST be part of your hashing algorithm.

2:21 PM  
OpenID Henrik Kaare Poulsen said...

I would suggest the following, which should work on most linux/unix/*bsd systems:

echo "sitename username answer" | openssl dgst -sha1 -binary | openssl enc -nosalt -bf -base64

The first digest ensures that passwords will always be 33 characters long,
and that the passwords for "sitename username", "sitename1 username", "sitename username1" will not be similar.

The -binary on dgst ensures consistent results on different linux/unix/*bsd systems.

We use unsalted blowfish encryption. There seems to be nothing gained by using salt in this usecase.
If you DO use a salt (option -S), remember to filter the base64 password throuhg "cut -b22-".

Finally, a note on the use of "words":
Using /usr/share/dict/words is dangerous!
On a different system, or after a system upgrade, the list may be different,
and your password/answer difficult to obtain!

1:34 PM  
OpenID b8a105dc-e776-11e1-a148-000bcdca4d7a said...

Questions such as "the make of first car" do not have to be answered with a real make. My first car was a Ford, but I would never give "Ford" as an answer to the above question.

7:48 AM  
Blogger Matt said...

Brilliant post! Thank you.

I've been just creating random responses to security questions and then storing the question/answer pairs encrypted. Your technique is way better.

The only problem (for both of our techniques) is that many sites are starting to limit what you can type as answers. They have maximum lengths, won't accept non-alpha, etc.

The worst example is a bank I am forced to work with where one of the security questions is the year you graduated from high-school, and they won't let you enter anything other than a 4 digit number!!!

6:42 PM  
Blogger Matt said...

Brilliant post! Thank you.

I've been just creating random responses to security questions and then storing the question/answer pairs encrypted. Your technique is way better.

The only problem (for both of our techniques) is that many sites are starting to limit what you can type as answers. They have maximum lengths, won't accept non-alpha, etc.

The worst example is a bank I am forced to work with where one of the security questions is the year you graduated from high-school, and they won't let you enter anything other than a 4 digit number!!!

6:42 PM  

Post a Comment

Links to this post:

Create a Link

<< Home