Skip to content

Instantly share code, notes, and snippets.

@vexx32
Last active March 12, 2019 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vexx32/55753d35e58e0006728e8641bf89c718 to your computer and use it in GitHub Desktop.
Save vexx32/55753d35e58e0006728e8641bf89c718 to your computer and use it in GitHub Desktop.
function Test-PasswordLeaked {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[PSCredential]
$Credential
)
begin {
$SHA1 = [System.Security.Cryptography.SHA1Managed]::new()
}
process {
$Hash = $SHA1.ComputeHash(
[System.Text.Encoding]::UTF8.GetBytes($Credential.GetNetworkCredential().Password)
)
[GC]::Collect()
$HashString = -join $Hash.ForEach{ $_.ToString("x2") }
$QueryPortion = $HashString.Substring(0, 5)
$Results = (Invoke-RestMethod -Uri "https://api.pwnedpasswords.com/range/$QueryPortion") -split '\r?\n'
$Match = @($Results) -match $HashString.Substring(5)
if ($Match) {
foreach ($Item in $Match) {
[PSCustomObject]@{
Hash = $Item -replace '\:.+$'
PwnCount = [int]($Item -replace '^.+\:')
}
}
}
else {
[PSCustomObject]@{
Hash = $HashString
PwnCount = 0
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment