Skip to content

Instantly share code, notes, and snippets.

@taco-shellcode
Last active November 6, 2019 19:44
Show Gist options
  • Save taco-shellcode/7bc07dc1cfbcc6be80d78979381a3e12 to your computer and use it in GitHub Desktop.
Save taco-shellcode/7bc07dc1cfbcc6be80d78979381a3e12 to your computer and use it in GitHub Desktop.
Function Get-PwnedAccount {
Param (
[Parameter(Mandatory=$true)]
[ValidatePattern('(\w+@[]a-zA-Z_]+?\.[a-zA-Z]{2,6})')]
[string]$EmailAddress,
[Parameter(Mandatory=$false)]
[Boolean]$IncludeUnverified
)
#Sets the security protocol used for HTTP Connections to TLS 1.2
#https://haveibeenpwned.com/API/v2#HTTPS
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#The public API is rate limited to one per every 1500 milliseconds, sleeping for 1600 milliseconds to prevent any latency issues
#https://haveibeenpwned.com/API/v2#RateLimiting
Start-Sleep -milliseconds 1600
Try {
#Performs the API request for a breached account
#https://haveibeenpwned.com/API/v2#BreachesForAccount
If ($IncludeUnverified -eq $true) {
$requestData = Invoke-RestMethod -Uri "https://haveibeenpwned.com/api/v2/breachedaccount/$($emailAddress)?includeUnverified=true"
} Else {
$requestData = Invoke-RestMethod -Uri "https://haveibeenpwned.com/api/v2/breachedaccount/$emailAddress"
}
} Catch [System.Net.WebException] {
$errorData = @{
errorCode = ''
errorMessage = ''
}
Switch ($_.Exception.Message) {
'The remote server returned an error: (400) Bad Request.' {
$errorData.errorCode = 400
$errorData.errorMessage = 'Bad Request - the account does not comply with an acceptable format.'
return $errorData
}
'The remote server returned an error: (403) Forbidden.' {
$errorData.errorCode = 403
$errorData.errorMessage = 'Forbidden - no user agent has been specified in the request.'
return $errorData
}
'The remote server returned an error: (404) Not Found.' {
$errorData.errorCode = 404
$errorData.errorMessage = 'Email address not found.'
return $errorData
}
'The remote server returned an error: (429) Too Many Requests.' {
$errorData.errorCode = 429
$errorData.errorMessage = 'Too many requests - the rate limit has been exceeded.'
return $errorData
}
}
}
return $requestData
}
Function Get-PwnedBreach {
#Sets the security protocol used for HTTP Connections to TLS 1.2
#https://haveibeenpwned.com/API/v2#HTTPS
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#The public API is rate limited to one per every 1500 milliseconds, sleeping for 1600 milliseconds to prevent any latency issues
#https://haveibeenpwned.com/API/v2#RateLimiting
Start-Sleep -milliseconds 1600
Try {
#Performs the API request for all breaches
#https://haveibeenpwned.com/API/v2#AllBreaches
$requestData = Invoke-RestMethod -Uri 'https://haveibeenpwned.com/api/v2/breaches'
} Catch [System.Net.WebException] {
$errorData = @{
errorCode = ''
errorMessage = ''
}
Switch ($_.Exception.Message) {
'The remote server returned an error: (400) Bad Request.' {
$errorData.errorCode = 400
$errorData.errorMessage = 'Bad Request.'
return $errorData
}
'The remote server returned an error: (403) Forbidden.' {
$errorData.errorCode = 403
$errorData.errorMessage = 'Forbidden - no user agent has been specified in the request.'
return $errorData
}
'The remote server returned an error: (404) Not Found.' {
$errorData.errorCode = 404
$errorData.errorMessage = 'Not found - No breach results found.'
return $errorData
}
'The remote server returned an error: (429) Too Many Requests.' {
$errorData.errorCode = 429
$errorData.errorMessage = 'Too many requests - the rate limit has been exceeded.'
return $errorData
}
}
}
return $requestData
}
Function Get-PwnedDataClass {
#A "data class" is an attribute of a record compromised in a breach. e.g. Email addresses, Passwords, Credit Cards, etc.
#Sets the security protocol used for HTTP Connections to TLS 1.2
#https://haveibeenpwned.com/API/v2#HTTPS
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#The public API is rate limited to one per every 1500 milliseconds, sleeping for 1600 milliseconds to prevent any latency issues
#https://haveibeenpwned.com/API/v2#RateLimiting
Start-Sleep -milliseconds 1600
Try {
#Performs the API request for all data classes
#https://haveibeenpwned.com/API/v2#AllDataClasses
$requestData = Invoke-RestMethod -Uri 'https://haveibeenpwned.com/api/v2/dataclasses'
} Catch [System.Net.WebException] {
$errorData = @{
errorCode = ''
errorMessage = ''
}
Switch ($_.Exception.Message) {
'The remote server returned an error: (400) Bad Request.' {
$errorData.errorCode = 400
$errorData.errorMessage = 'Bad Request.'
return $errorData
}
'The remote server returned an error: (403) Forbidden.' {
$errorData.errorCode = 403
$errorData.errorMessage = 'Forbidden - no user agent has been specified in the request.'
return $errorData
}
'The remote server returned an error: (404) Not Found.' {
$errorData.errorCode = 404
$errorData.errorMessage = 'Not found - No data class results found.'
return $errorData
}
'The remote server returned an error: (429) Too Many Requests.' {
$errorData.errorCode = 429
$errorData.errorMessage = 'Too many requests - the rate limit has been exceeded.'
return $errorData
}
}
}
return $requestData
}
Function Get-PwnedPasteAccount {
Param (
[Parameter(Mandatory=$true)]
[ValidatePattern('(\w+@[]a-zA-Z_]+?\.[a-zA-Z]{2,6})')]
[string]$EmailAddress
)
#Sets the security protocol used for HTTP Connections to TLS 1.2
#https://haveibeenpwned.com/API/v2#HTTPS
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#The public API is rate limited to one per every 1500 milliseconds, sleeping for 1600 milliseconds to prevent any latency issues
#https://haveibeenpwned.com/API/v2#RateLimiting
Start-Sleep -milliseconds 1600
Try {
#Performs the API request for a paste account (e.g. pastebin)
#https://haveibeenpwned.com/API/v2#PastesForAccount
$requestData = Invoke-RestMethod -Uri "https://haveibeenpwned.com/api/v2/pasteaccount/$emailAddress"
} Catch [System.Net.WebException] {
$errorData = @{
errorCode = ''
errorMessage = ''
}
Switch ($_.Exception.Message) {
'The remote server returned an error: (400) Bad Request.' {
$errorData.errorCode = 400
$errorData.errorMessage = 'Bad Request - the account does not comply with an acceptable format.'
return $errorData
}
'The remote server returned an error: (403) Forbidden.' {
$errorData.errorCode = 403
$errorData.errorMessage = 'Forbidden - no user agent has been specified in the request.'
return $errorData
}
'The remote server returned an error: (404) Not Found.' {
$errorData.errorCode = 404
$errorData.errorMessage = 'Email address not found.'
return $errorData
}
'The remote server returned an error: (429) Too Many Requests.' {
$errorData.errorCode = 429
$errorData.errorMessage = 'Too many requests - the rate limit has been exceeded.'
return $errorData
}
}
}
return $requestData
}
Function Get-PwnedPassword {
Param (
[Parameter(Mandatory=$false, ParameterSetName = 'Password')]
[string]$Password,
[Parameter(Mandatory=$false, ParameterSetName = 'SHA1')]
[string]$SHA1
)
#Sets the security protocol used for HTTP Connections to TLS 1.2
#https://haveibeenpwned.com/API/v2#HTTPS
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Try {
#Performs the API request for a password, securestring, or sha1 hash
#https://haveibeenpwned.com/API/v2#PwnedPasswords
Switch ($PSCmdlet.ParameterSetName) {
'Password' {
$requestData = Invoke-RestMethod -Uri "https://api.pwnedpasswords.com/pwnedpassword/$Password"
}
'SHA1' {
$requestData = Invoke-RestMethod -Uri "https://api.pwnedpasswords.com/pwnedpassword/$SHA1"
}
}
} Catch [System.Net.WebException] {
$errorData = @{
errorCode = ''
errorMessage = ''
}
Switch ($_.Exception.Message) {
'The remote server returned an error: (400) Bad Request.' {
$errorData.errorCode = 400
$errorData.errorMessage = 'Bad Request - the account does not comply with an acceptable format.'
return $errorData
}
'The remote server returned an error: (403) Forbidden.' {
$errorData.errorCode = 403
$errorData.errorMessage = 'Forbidden - no user agent has been specified in the request.'
return $errorData
}
'The remote server returned an error: (404) Not Found.' {
$errorData.errorCode = 404
$errorData.errorMessage = 'Password not found.'
return $errorData
}
'The remote server returned an error: (429) Too Many Requests.' {
$errorData.errorCode = 429
$errorData.errorMessage = 'Too many requests - the rate limit has been exceeded.'
return $errorData
}
}
}
$data = @{
Pwned = $true
Count = $requestData
}
return $data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment