forked from mikepound/pwned-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwned.pl
executable file
·140 lines (122 loc) · 3.99 KB
/
pwned.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/perl
# This is a perl version of the original pwned-search created by Dr. Mike Pound
# This program is a free software. You are free to use it under the terms of
# GNU GPL license either version 3 or, at your choice, any later version.
# Copyright 2019 Lucas V. Araujo <[email protected]>
# Required module: WWW::Curl::Easy (libwww-curl-perl)
use strict;
use warnings;
use Getopt::Long;
use WWW::Curl::Easy;
use Digest::SHA qw(sha1_hex);
use vars qw ( $VERSION );
$VERSION = "2019.1208.0012";
sub request
{
# Performs an http request to a given url using the WWW::Curl::Easy module
# and returns, if successful, the received page.
#gets the url passed as argument
my $url = shift;
my $prxy = shift;
#initialize a new instance of Curl::Easy
my $curl = WWW::Curl::Easy->new();
#define the HEADER option as true
$curl->setopt(CURLOPT_HEADER, 1);
#define the target url
$curl->setopt(CURLOPT_URL, $url);
#declare a variable to hold the returned page
my $data = undef;
#define the variable as a filehandle to store the data into
$curl->setopt(CURLOPT_WRITEDATA, \$data);
#sets the proxy to be used, if any
$curl->setopt(CURLOPT_PROXY, $prxy) if $prxy;
#performs the request
my $err = $curl->perform();
#checks if successfull
unless ($err)
{
return $data;
}
else
{
print("error: $err ".$curl->strerror($err)."\n".$curl->errbuf."\n");
return "";
}
}
sub lookup_password
{
#Given a plain password, gets the hash and search for it on the database
#using the especified proxy
my $plain = shift;
my $proxy = shift;
#get the SHA-1 hashed password
my $hashed = sha1_hex($plain);
#separate the hash into a head containing the first 5 bytes, and a tail
#containing the rest
$hashed =~ /([\d\w]{5})([\d\w]*)/;
my $head = $1;
my $tail = $2;
#format the head into a url to be requested
my $url = "https:\/\/api.pwnedpasswords.com\/range\/$head";
#realize the request and get the response
my $resp = request($url, $proxy);
#if nothing is found, it is considered that the number of times it leaked
#(for all we know) was 0
my $count = 0;
#match the response against a regular expression to extract the number of
#times that the password was been leaked
if ($resp =~ /$tail\:([\d]*)/i )
{
$count = $1;
}
#returns an array with the hash and the count
return ($hashed, $count);
}
sub main
{
my $version = 0;
my $proxy = undef;
my $help = 0;
my $tor = 0;
GetOptions(
"version!" => \$version,
"proxy=s" => \$proxy,
"help!" => \$help,
"tor!" => \$tor,
);
if ($version)
{
print "$VERSION\n";
exit(0);
}
if ($help || !(scalar @ARGV))
{
print "pwned-search - Pwned Password API lookup tool\n\n" .
"usage: pwned.pl [options] <password0> ... <passwordN>\n\n" .
"Options:\n" .
"-v, --version show program's version number and exit\n" .
"-h, --help show this help message and exit\n" .
"-p, --proxy define a proxy to be used in requests\n" .
" (the proxy must be in format TYPE://ADDRESS[:PORT])\n" .
"-t, --tor use tor proxy\n" .
" (same as --proxy=socks5://127.0.0.1:9050)\n\n" .
"This is a perl version of the pwned-search by Dr. Mike Pound\n" .
"Copyright (C) 2019 Lucas V. Araujo <lucas.vieira.ar\@disroot.org>\n" .
"GitHub: https://github.com/LvMalware/pwned-search \n";
exit(0);
}
$proxy = "socks5://127.0.0.1:9050" if $tor;
for my $password (@ARGV)
{
my ($hash, $count) = lookup_password($password, $proxy);
if ($count > 0)
{
print "$password was found with $count occurrences (hash: $hash)\n";
}
else
{
print "$password was not found\n";
}
}
}
main();