Skip to content

Commit fd1f472

Browse files
committed
Update Get-PwnedPassword to use anonymous endpoint
1 parent 1b757e8 commit fd1f472

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

Diff for: Get-PwnedPassword.ps1

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<#
22
.SYNOPSIS
3-
Connects to the API at https://haveibeenpwned.com/ to see if a Password or Password hash has been found in a breach
3+
Connects to the API at https://api.pwnedpasswords.com/ to see if a Password or Password hash has been found in a breach
44
55
.DESCRIPTION
6-
Connects to the API at https://haveibeenpwned.com/ to see if a Password or Password hash has been found in a breach
6+
Connects to the API at https://api.pwnedpasswords.com/ to see if a Password or Password hash has been found in a breach
77
88
Troy Hunt @troyhunt has created an API which allows you to query if a Password has been found in a breach.
99
This is a simple function enabling you to query it
1010
11-
IT IS NOT RECOMMENDED TO USE ACTIVE PASSWORDS WITH THIS SERVICE
11+
ONLY THE FIRST FIVE (5) CHARACTERS OF THE PASSWORD HASH ARE EVER SENT
1212
1313
.PARAMETER Password
1414
The password to check as a secure string. If not supplied will be prompted
@@ -20,30 +20,28 @@ A SHA1 hash of the password to be checked
2020
$Password = Read-Host -AsSecureString
2121
Get-PwnedPassword -Password Password
2222
23-
Connects to the API at https://haveibeenpwned.com/ and checks if a password has been found
23+
Connects to the API at https://api.pwnedpasswords.com/ and checks if a password has been found
2424
in a breach.
2525
2626
2727
.EXAMPLE
2828
Get-PwnedPassword -Hash 8be3c943b1609fffbfc51aad666d0a04adf83c9d
2929
30-
Connects to the API at https://haveibeenpwned.com/ and checks if the SHA1 hash of 'Password' has been found
30+
Connects to the API at https://api.pwnedpasswords.com/ and checks if the SHA1 hash of 'Password' has been found
3131
in a breach.
3232
3333
Don't run this. It has!!
3434
3535
.EXAMPLE
3636
Get-PwnedPassword
3737
38-
Prompts for a Password and connects to the API at https://haveibeenpwned.com/ and checks if it has been found
38+
Prompts for a Password and connects to the API at https://api.pwnedpasswords.com/ and checks if it has been found
3939
in a breach.
4040
4141
.NOTES
4242
AUTHOR : Rob Sewell @sqldbawithbeard https://sqldbawithabeard.com
4343
DATE : 4th August 2017
4444
45-
IT IS NOT RECOMMENDED TO USE ACTIVE PASSWORDS WITH THIS SERVICE
46-
4745
With many many thanks to Troy Hunt for creating this service
4846
You can find Troy on Twitter @TroyHunt
4947
You can read his blog at https://troyhunt.com
@@ -78,11 +76,21 @@ function Get-PwnedPassword {
7876
switch ($PSCmdlet.ParameterSetName) {
7977
'Password' {
8078
$Pass = (New-Object PSCredential "user", $Password).GetNetworkCredential().Password
81-
$URL = 'https://haveibeenpwned.com/api/v2/pwnedpassword/' + $Pass
79+
$StringBuilder = New-Object System.Text.StringBuilder
80+
[System.Security.Cryptography.HashAlgorithm]::Create("SHA1").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($Pass))|% {
81+
[Void]$StringBuilder.Append($_.ToString("x2"))
82+
}
83+
$sha1 = $StringBuilder.ToString()
84+
$sha1Prefix = $sha1.Substring(0, 5)
85+
$sha1Suffix = $sha1.Substring(5)
86+
87+
$URL = 'https://api.pwnedpasswords.com/range/' + $sha1Prefix
8288
break
8389
}
8490
'Hash' {
85-
$URL = 'https://haveibeenpwned.com/api/v2/pwnedpassword/' + $Hash
91+
$sha1Prefix = $Hash.Substring(0, 5)
92+
$sha1Suffix = $Hash.Substring(5)
93+
$URL = 'https://api.pwnedpasswords.com/range/' + $sha1Prefix
8694
break
8795
}
8896
default {
@@ -109,7 +117,13 @@ function Get-PwnedPassword {
109117
break
110118
}
111119
if ($Response.StatusCode -eq '200') {
112-
Write-Warning -Message "Oh No! - Password has been pwned - Change it NOW! `nYou should sign up for free at https://haveibeenpwned.com/ to be notified when your account is in a breach"
120+
if ($Response.Content.Contains($sha1Suffix.ToUpper())) {
121+
# Password has been pwned
122+
Write-Warning -Message "Oh No! - Password has been pwned - Change it NOW! `nYou should sign up for free at https://haveibeenpwned.com/ to be notified when your account is in a breach"
123+
}
124+
else {
125+
Write-Output "Hurrah! - No Password found - Congratulations this password has not been pwned. `nYou should still sign up for free at https://haveibeenpwned.com/ to be notified when your account is in a breach"
126+
}
113127
}
114128
}
115129
end {
@@ -123,7 +137,7 @@ function Get-PwnedPassword {
123137
124138
.AUTHOR Rob Sewell @sqldbawithbeard https://sqldbawithabeard.com
125139
126-
.DESCRIPTION Connects to the API at https://haveibeenpwned.com/ to see if a Password or Password hash has been found in a breach. Troy Hunt @troyhunt has created an API which allows you to query if a Password has been found in a breach. This is a simple function enabling you to query it
140+
.DESCRIPTION Connects to the API at https://api.pwnedpasswords.com/ to see if a Password or Password hash has been found in a breach. Troy Hunt @troyhunt has created an API which allows you to query if a Password has been found in a breach. This is a simple function enabling you to query it
127141
128142
.COMPANYNAME Sewells Consulting
129143

0 commit comments

Comments
 (0)