2022-10-01 20:48:38 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
##
|
|
|
|
## Author......: See docs/credits.txt
|
|
|
|
## License.....: MIT
|
|
|
|
##
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Crypt::Mode::ECB;
|
|
|
|
use Crypt::PBKDF2;
|
|
|
|
|
2022-10-06 14:02:02 +00:00
|
|
|
my $ENC_MAX_KEY_NUM = 8;
|
|
|
|
my $ENC_NONCE_SIZE = 8;
|
|
|
|
my $ENC_KEY_SIZE = 16;
|
|
|
|
my $ENC_BLOCK_SIZE = 16;
|
|
|
|
my $ENC_KEYCHAIN_SIZE = 128;
|
|
|
|
my $ENC_DEFAULT_MD5_ITERATIONS = 1000;
|
2022-10-02 11:10:56 +00:00
|
|
|
|
2022-10-01 20:48:38 +00:00
|
|
|
sub module_constraints { [[0, 256], [64, 64], [-1, -1], [-1, -1], [-1, -1]] }
|
|
|
|
|
|
|
|
sub module_generate_hash
|
|
|
|
{
|
2022-10-03 15:45:18 +00:00
|
|
|
my $word = shift;
|
|
|
|
my $salt = shift;
|
|
|
|
my $algo = shift // random_number (1, 4);
|
|
|
|
my $iv = shift // random_hex_string (16);
|
|
|
|
my $ct = shift;
|
|
|
|
my $iter = shift // 100000;
|
2022-10-01 20:48:38 +00:00
|
|
|
|
|
|
|
# pbkdf2 part
|
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
my $nb_keys = 1 << ($algo - 1);
|
|
|
|
|
|
|
|
my $key_len = $nb_keys * $ENC_KEY_SIZE;
|
|
|
|
|
2022-10-01 20:48:38 +00:00
|
|
|
my $pbkdf2 = Crypt::PBKDF2->new
|
|
|
|
(
|
|
|
|
hasher => Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA2', 256),
|
|
|
|
iterations => $iter,
|
2022-10-02 11:10:56 +00:00
|
|
|
output_len => $key_len
|
2022-10-01 20:48:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
my $salt_bin = pack ("H*", $salt);
|
|
|
|
|
|
|
|
my $key = $pbkdf2->PBKDF2 ($salt_bin, $word);
|
|
|
|
|
|
|
|
my $aes = Crypt::Mode::ECB->new ('AES', 0);
|
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
my $aes_key = substr ($key, 0, $ENC_KEY_SIZE);
|
|
|
|
|
2022-10-04 16:51:30 +00:00
|
|
|
## decrypt encrypted data using PBKDF2 key
|
|
|
|
|
2022-10-01 20:48:38 +00:00
|
|
|
my $iv_bin = pack ("H*", $iv);
|
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
my @ivs;
|
|
|
|
|
|
|
|
$ivs[0] = $iv_bin;
|
|
|
|
|
|
|
|
for (my $i = 1; $i < $nb_keys; $i++)
|
|
|
|
{
|
|
|
|
my $next8 = substr ($key, $i * $ENC_KEY_SIZE, $ENC_NONCE_SIZE); ## its strange to skip 8 byte of key material every 16 byte
|
|
|
|
|
|
|
|
$ivs[$i] = xor_len ($iv_bin, $next8, 8);
|
|
|
|
}
|
|
|
|
|
2022-10-01 20:48:38 +00:00
|
|
|
my $ctr_len = 16;
|
|
|
|
|
|
|
|
my $ctr;
|
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
for (my $i = 0, my $counter = 1; $i < ($ctr_len / $ENC_BLOCK_SIZE); $i++, $counter++)
|
2022-10-01 20:48:38 +00:00
|
|
|
{
|
2022-10-02 11:10:56 +00:00
|
|
|
my $counter_be = pack ("Q>", $counter);
|
|
|
|
|
|
|
|
my $tmp_iv = $ivs[0] . $counter_be;
|
|
|
|
|
|
|
|
my $enc = $aes->encrypt ($tmp_iv, $aes_key);
|
|
|
|
|
|
|
|
my $out = $enc;
|
2022-10-01 20:48:38 +00:00
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
for (my $i = 1; $i < $nb_keys; $i++)
|
|
|
|
{
|
|
|
|
my $tmp_iv = $ivs[$i] . $counter_be;
|
|
|
|
|
|
|
|
my $enc = $aes->encrypt ($tmp_iv, $aes_key);
|
|
|
|
|
|
|
|
$out = xor_len ($enc, $out, $ENC_BLOCK_SIZE);
|
|
|
|
}
|
2022-10-01 20:48:38 +00:00
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
$ctr .= $out;
|
2022-10-01 20:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $pt_bin;
|
|
|
|
|
|
|
|
if (defined $ct)
|
|
|
|
{
|
|
|
|
my $ct_bin = pack ("H*", $ct);
|
|
|
|
|
2022-10-19 17:48:11 +00:00
|
|
|
$pt_bin = xor_len (substr ($ctr, 4, 8), $ct_bin, 8);
|
2022-10-01 20:48:38 +00:00
|
|
|
|
2022-10-19 17:48:11 +00:00
|
|
|
# we compare only 56 bit, see https://github.com/hashcat/hashcat/issues/3467
|
|
|
|
|
|
|
|
if (substr ($pt_bin, 0, 7) eq "\xd2\xc3\xb4\xa1\x00\x00\x00")
|
2022-10-01 20:48:38 +00:00
|
|
|
{
|
|
|
|
# ok
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-19 17:48:11 +00:00
|
|
|
$pt_bin = "\xff\xff\xff\xff\xff\xff\xff\xff";
|
2022-10-01 20:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-26 06:06:08 +00:00
|
|
|
$pt_bin = "\xd2\xc3\xb4\xa1\x00\x00\x00\x30";
|
2022-10-01 20:48:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 17:48:11 +00:00
|
|
|
my $ct_bin = xor_len (substr ($ctr, 4, 8), $pt_bin, 8);
|
2022-10-01 20:48:38 +00:00
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
my $hash = sprintf ('$encdv-pbkdf2$1$%d$%s$%s$32$%s$%d', $algo, unpack ("H*", $iv_bin), unpack ("H*", $ct_bin), unpack ("H*", $salt_bin), $iter);
|
2022-10-01 20:48:38 +00:00
|
|
|
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub module_verify_hash
|
|
|
|
{
|
|
|
|
my $line = shift;
|
|
|
|
|
|
|
|
my $idx = index ($line, ':');
|
|
|
|
|
|
|
|
return unless $idx >= 0;
|
|
|
|
|
|
|
|
my $hash = substr ($line, 0, $idx);
|
|
|
|
my $word = substr ($line, $idx + 1);
|
|
|
|
|
|
|
|
return unless substr ($hash, 0, 14) eq '$encdv-pbkdf2$';
|
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
my (undef, $signature, $version, $algo, $iv, $ct, $salt_len, $salt, $iter) = split '\$', $hash;
|
2022-10-01 20:48:38 +00:00
|
|
|
|
|
|
|
return unless defined $signature;
|
|
|
|
return unless defined $version;
|
2022-10-02 11:10:56 +00:00
|
|
|
return unless defined $algo;
|
2022-10-01 20:48:38 +00:00
|
|
|
return unless defined $iv;
|
|
|
|
return unless defined $ct;
|
|
|
|
return unless defined $salt_len;
|
|
|
|
return unless defined $salt;
|
|
|
|
return unless defined $iter;
|
|
|
|
|
|
|
|
return unless ($version == 1);
|
2022-10-02 11:10:56 +00:00
|
|
|
return unless ($algo >= 1);
|
|
|
|
return unless ($algo <= 4);
|
2022-10-01 20:48:38 +00:00
|
|
|
return unless ($salt_len == 32);
|
|
|
|
|
|
|
|
my $word_packed = pack_if_HEX_notation ($word);
|
|
|
|
|
2022-10-02 11:10:56 +00:00
|
|
|
my $new_hash = module_generate_hash ($word_packed, $salt, $algo, $iv, $ct, $iter);
|
2022-10-01 20:48:38 +00:00
|
|
|
|
|
|
|
return ($new_hash, $word);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub xor_len
|
|
|
|
{
|
|
|
|
my $in1 = shift;
|
|
|
|
my $in2 = shift;
|
|
|
|
my $len = shift;
|
|
|
|
|
|
|
|
my $out;
|
|
|
|
|
|
|
|
for (my $i = 0; $i < $len; $i++)
|
|
|
|
{
|
|
|
|
$out .= chr (ord (substr ($in1, $i, 1)) ^ ord (substr ($in2, $i, 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|