ck_pass.pl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Digest::SHA qw(sha1_hex);
  5. # Script to check a password via pwnedpasswords.com with k-anonimity
  6. # https://www.troyhunt.com/ive-just-launched-pwned-passwords-version-2/#cloudflareprivacyandkanonymity
  7. # https://haveibeenpwned.com/API/v2#PwnedPasswords
  8. my $api = "https://api.pwnedpasswords.com/range/";
  9. my $curlBin = `which curl`;
  10. chomp $curlBin;
  11. if ( ! -f $curlBin ) {
  12. print "You need curl to use this script\n";
  13. exit 1;
  14. }
  15. if ( ! defined $ARGV[0] ) {
  16. print "Please pass a password\n";
  17. exit 1;
  18. }
  19. # Clear term to remove visible pw from screen
  20. my $clear_bin = `which clear`;
  21. chomp $clear_bin;
  22. if ( -f $clear_bin ) {
  23. #system("$clear_bin");
  24. }
  25. my $pw = shift(@ARGV);
  26. chomp $pw;
  27. my $pw_sha1 = uc(sha1_hex("$pw"));
  28. $pw_sha1 =~ m/(^[0-9A-Z]{40})/;
  29. $pw_sha1 = $1;
  30. $pw_sha1 =~ m/(^[0-9A-Z]{5})([0-9A-Z]{35})/;
  31. my $first_five = $1;
  32. my $rest = $2;
  33. chomp $first_five; chomp $rest;
  34. my @results = split("\n", `curl -s $api/$first_five`);
  35. my $count = 0;
  36. foreach my $result ( @results ) {
  37. chomp $result;
  38. $result =~ m/(^[0-9A-Z]{35})/;
  39. my $segment = $1;
  40. if ( $segment eq $rest ) {
  41. $result =~ m/^([0-9A-Z]{35})\:([0-9].*)$/;
  42. my $count = $2;
  43. $count =~ s/\r//g;
  44. print "$count appearances\n";
  45. exit 0;
  46. } else {
  47. next;
  48. }
  49. }
  50. print "No appearances\n";