-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck-Passwords.PS1
More file actions
157 lines (149 loc) · 4.82 KB
/
Copy pathCheck-Passwords.PS1
File metadata and controls
157 lines (149 loc) · 4.82 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<#
.Synopsis
Calls the haveibeenpwned API to check if a password is contained on a breach list
.DESCRIPTION
Takes a password and hashes to a SHA-1 hash, then sends the first 5 characters to the haveibeenpwned API. The response contains all the hashes that match and is compared on the local machine.
.EXAMPLE
Check-Password
.EXAMPLE
Check-Password -password P@ssw0rd!
.INPUTS
None
.OUTPUTS
Text declaring number of times breached
.NOTES
This program does not send complete password hashes over the internet.
.COMPONENT
Works on the haveibeenpwned API
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
Checks passwords against the haveibeenpwned API.
#>
function Check-Password
{
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
SupportsShouldProcess=$true,
PositionalBinding=$false,
HelpUri = 'https://haveibeenpwned.com/API/v2',
ConfirmImpact='Medium')]
[Alias()]
[OutputType([String])]
Param
(
# For demonstration purposes
[switch]$demo,
# Doesn't obscure passwords while typing
[switch]$Insecure,
# Password you want to check
$password,
#Have I been PWNed URL
$URL = "https://api.pwnedpasswords.com/range/"
)
Begin
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (!($password))
{
if ($Insecure)
{
$password = Read-Host "Enter the password to check against the haveibeenpwned database"
}
else
{
if ($demo)
{
Write-Host '$SecurePassword = Read-Host "Enter the password to check against the haveibeenpwned database" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)'
pause
}
$SecurePassword = Read-Host "Enter the password to check against the haveibeenpwned database" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
}
}
if ($demo)
{
Write-Host '#http://jongurgul.com/blog/get-stringhash-get-filehash/
Function Get-StringHash([String] $String,$HashName = "SHA1")
{
$StringBuilder = New-Object System.Text.StringBuilder
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{
[Void]$StringBuilder.Append($_.ToString("x2"))
}
$StringBuilder.ToString()'
Pause
}
#http://jongurgul.com/blog/get-stringhash-get-filehash/
Function Get-StringHash([String] $String,$HashName = "SHA1")
{
$StringBuilder = New-Object System.Text.StringBuilder
[System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{
[Void]$StringBuilder.Append($_.ToString("x2"))
}
$StringBuilder.ToString()
}
if ($demo)
{
Write-Host '$passHash = Get-StringHash $password'
Write-Host $passHash
Pause
}
$passHash = Get-StringHash $password
}
Process
{
if ($demo)
{
Write-Host '$results = Invoke-RestMethod -Uri ($url + $passHash.Substring(0,5)) -Method Get'
Pause
$results
Pause
}
$results = (Invoke-RestMethod -Uri ($url + $passHash.Substring(0,5)) -Method Get).split("`n")
if ($demo)
{
Write-Host 'ForEach ($result in $results)
{
[PSCustomObject]$hash = @{hash=$result.split(":")[0];breachcount=$result.split(":")[1]}
[array]$hashes += $hash
}'
Pause
}
ForEach ($result in $results)
{
[PSCustomObject]$hash = @{hash=$result.split(":")[0];breachcount=$result.split(":")[1]}
[array]$hashes += $hash
}
if ($demo)
{
Write-Host '$breachcount = ($hashes | Where {$passhash.substring(5).toUpper() -in $_.hash}).breachCount'
Pause
}
$breachcount = ($hashes | Where {$passhash.substring(5).toUpper() -in $_.hash}).breachCount
}
End
{
if ($demo)
{
Write-Host 'If ($breachCount -gt 0)
{
Write-Host "Oh no! You've been Pwned $breachCount Times" -ForeGroundColor Red
}
Else
{
Write-Host "Looks good, your password isn't on haveibeenpwned.com" -foreGroundColor Green
}'
Pause
}
If ($breachCount -gt 0)
{
Write-Host "Oh no! You've been Pwned $breachCount Times" -ForegroundColor Red
}
Else
{
Write-Host "Looks good, your password isn't on haveibeenpwned.com" -ForegroundColor Green
}
}
}