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.

<$BlogCommentBody$>

<$BlogCommentDateTime$> <$BlogCommentDeleteIcon$>

Post a Comment

Links to this post:

<$BlogBacklinkControl$> <$BlogBacklinkTitle$> <$BlogBacklinkDeleteIcon$>
<$BlogBacklinkSnippet$>
Create a Link

<< Home