2021-07-24 11:56:49 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
##
|
|
|
|
## Author......: See docs/credits.txt
|
|
|
|
## License.....: MIT
|
|
|
|
##
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2022-01-30 09:35:51 +00:00
|
|
|
use Digest::MD5 qw (md5 md5_hex);
|
|
|
|
use Digest::HMAC qw (hmac_hex);
|
2021-07-24 11:56:49 +00:00
|
|
|
|
2021-07-26 17:04:30 +00:00
|
|
|
sub module_constraints { [[8, 256], [24, 3000], [-1, -1], [-1, -1], [-1, -1]] }
|
2021-07-24 11:56:49 +00:00
|
|
|
|
|
|
|
sub module_generate_hash
|
|
|
|
{
|
|
|
|
my $word = shift;
|
|
|
|
my $salt = shift;
|
2022-01-30 09:35:51 +00:00
|
|
|
my $pkt_num = shift // int (rand (100000000));
|
|
|
|
my $engineID = shift // random_hex_string (26, 34);
|
2021-07-24 11:56:49 +00:00
|
|
|
|
|
|
|
# make even if needed
|
|
|
|
|
2022-01-30 09:35:51 +00:00
|
|
|
if (length ($salt) % 2 == 1)
|
2021-07-24 11:56:49 +00:00
|
|
|
{
|
|
|
|
$salt = $salt . "8";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $string1 = $word x 1048576;
|
|
|
|
|
|
|
|
$string1 = substr ($string1, 0, 1048576);
|
|
|
|
|
|
|
|
my $md5_digest1 = md5_hex ($string1);
|
|
|
|
|
|
|
|
my $buf = join '', $md5_digest1, $engineID, $md5_digest1;
|
|
|
|
|
2022-01-30 09:35:51 +00:00
|
|
|
my $md5_digest2 = md5 (pack ("H*", $buf));
|
2021-07-24 11:56:49 +00:00
|
|
|
|
2022-01-30 09:35:51 +00:00
|
|
|
my $digest = hmac_hex (pack ("H*", $salt), $md5_digest2, \&md5);
|
2021-07-24 11:56:49 +00:00
|
|
|
|
|
|
|
$digest = substr ($digest, 0, 24);
|
|
|
|
|
|
|
|
my $hash = sprintf ("\$SNMPv3\$1\$%s\$%s\$%s\$%s", $pkt_num, $salt, $engineID, $digest);
|
|
|
|
|
|
|
|
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 length ($word) gt 0;
|
|
|
|
return unless substr ($hash, 0, 10) eq '$SNMPv3$1$';
|
|
|
|
|
|
|
|
my (undef, $signature, $version, $pkt_num, $salt, $engineID, $digest) = split '\$', $hash;
|
|
|
|
|
|
|
|
return unless defined $signature;
|
|
|
|
return unless defined $version;
|
|
|
|
return unless defined $pkt_num;
|
|
|
|
return unless defined $salt;
|
|
|
|
return unless defined $engineID;
|
|
|
|
return unless defined $digest;
|
|
|
|
|
|
|
|
my $word_packed = pack_if_HEX_notation ($word);
|
|
|
|
|
2021-07-26 17:04:30 +00:00
|
|
|
my $new_hash = module_generate_hash ($word_packed, $salt, $pkt_num, $engineID);
|
2021-07-24 11:56:49 +00:00
|
|
|
|
|
|
|
return ($new_hash, $word);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|