From 7cb5e77f1628989513f7b698d80ac3ad766a21f2 Mon Sep 17 00:00:00 2001 From: jsteube Date: Tue, 19 Feb 2019 10:53:49 +0100 Subject: [PATCH] Add -m 10200 unit test --- tools/test_modules/m10200.pm | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tools/test_modules/m10200.pm diff --git a/tools/test_modules/m10200.pm b/tools/test_modules/m10200.pm new file mode 100644 index 000000000..a343c34e7 --- /dev/null +++ b/tools/test_modules/m10200.pm @@ -0,0 +1,76 @@ +#!/usr/bin/env perl + +## +## Author......: See docs/credits.txt +## License.....: MIT +## + +use strict; +use warnings; + +use Digest::MD5 qw (md5); +use Digest::HMAC qw (hmac_hex); +use MIME::Base64 qw (encode_base64 decode_base64); + +sub module_constraints { [[0, 255], [0, 255], [0, 55], [0, 55], [-1, -1]] } + +sub module_generate_hash +{ + my $word = shift; + my $salt = shift; + my $username = shift // "user"; + + my $challengeb64 = encode_base64 ($salt, ""); + + my $hash_buf = hmac_hex ($salt, $word, \&md5); + + my $responseb64 = encode_base64 ($username . " " . $hash_buf, ""); + + my $hash = sprintf ('$cram_md5$%s$%s', $challengeb64, $responseb64); + + return $hash; +} + +sub module_verify_hash +{ + my $line = shift; + + # Cram MD5 + next unless (substr ($line, 0, 10) eq '$cram_md5$'); + + # get hash + my $index1 = index ($line, "\$", 10); + + return if $index1 < 1; + + # challenge + + my $challengeb64 = substr ($line, 10, $index1 - 10); + + my $salt = decode_base64 ($challengeb64); + + # response + + my $index2 = index ($line, ":", $index1 + 1); + + return if $index2 < 1; + + my $responseb64 = substr ($line, $index1 + 1, $index2 - $index1 - 1); + + my $response = decode_base64 ($responseb64); + + my $param = substr ($response, 0, length ($response) - 32 - 1); # -1 is for space + + my $word = substr ($line, $index2 + 1); + + return unless defined $salt; + return unless defined $word; + + $word = pack_if_HEX_notation ($word); + + my $new_hash = module_generate_hash ($word, $salt, $param); + + return ($new_hash, $word); +} + +1;