r/PowerShell • u/powershellnut • Apr 26 '20
A fun little function to check if your password is in the haveibeenpwned database
I recently watched a computerphile about how people crack passwords using already leaked passwords from past breaches. He wrote a program in python to check passwords in the haveibeenpwned database, so I figured I would do the same for powershell. It is super basic as I didn't want to spend much time on it. Much more could be added like checking if the password is a certain length, has x, y or z. So really a skeleton of a more robust password security function, but I figured I would share it anyways. I don't really see the practical use of this function since I use password generators for complex passwords, but it is fun none the less.
Anyways here is the function
function Test-PasswordSecurity{
param(
[Parameter(Mandatory)]
[securestring]$Password
)
$StringBuilder = New-Object System.Text.StringBuilder
$Cred = [pscredential]::new("User",$Password)
[System.Security.Cryptography.HashAlgorithm]::Create("SHA1").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($Cred.GetNetworkCredential().Password)) |
foreach {[Void]$StringBuilder.Append($_.ToString("x2"))}
$Hash = $StringBuilder.ToString()
$First5 = $Hash.Substring(0,5)
$Results = (Invoke-RestMethod -Uri "https://api.pwnedpasswords.com/range/$First5").split("`n") | foreach {
[PSCustomObject]@{
Hash = $First5 + $_.Split(':')[0]
PasswordsFound = [int]($_.Split(':')[1])
}
}
if ($Results.Hash -contains $Hash){
$Count = ($Results | where Hash -eq $Hash).PasswordsFound
Write-Warning "Password is inscure! Do not use this password! $Count Passwords found on pwnedpasswords!"
}
else{
Write-Host "Password has never been seen before. Safe to use." -ForegroundColor Green
}
}11
u/R-EDDIT Apr 26 '20
There is a fairly full featured module here:
7
u/powershellnut Apr 26 '20
Oh dang, that is much more comprehensive. I figured there was more thought out functions/modules for using this API. Thanks for sharing.
4
u/Potato-9 Apr 26 '20
Haven't needed it for a while but I don't know why mine's so much longer lol
I used it to look up lists, For example to check the spreadsheet an MSP just emailed you :/
3
u/powershellnut Apr 26 '20
Your function seems more robust, mine is more of skeleton of a function.
I haven't seen [Net.ServicePointManager] class before, it might something I should read up on.2
u/Potato-9 Apr 27 '20
.net had a TLS seeing to only talk 1.0 and his nothing to do with any Windows setting (classic MS) but I think they improved it in ps 6 or 7.
I just save what it was, set it to 1.2 for the request then put it back to what it was in case someone has a particular config. Saves me learning everything about it to avoid interfering.
2
u/da_chicken Apr 27 '20
.net had a TLS seeing to only talk 1.0 and his nothing to do with any Windows setting (classic MS)
That's really not a fair argument. Before PS v4, MS used .Net 3.0 for PowerShell. .Net 3.0 was released in 2006. You know when the TLS 1.1 specification was released? 2006. The TLS 1.2 specification is from 2008. It's not remotely surprising why TLS 1.2 isn't well supported by .Net 3.0. TLS was compromised in 2011, too. They did add support to the newer protocols to older versions of .Net -- you just had to install newer versions of the .Net framework -- but implementing security protocols in a way that doesn't break everything that came before is not trivial. That's why you had to specify the version of protocol to use.
Like it takes time for stuff to filter through. If TLS had a 1-2 year development, and .Net had a 1-2 year development, and PowerShell had a 1-2 year development, well, that's 3 to 6 years. Windows PowerShell development stopped in 2016, 8 years after TLS 1.2 was released and after MS had released .Net Core 1.0 and their new .Net Standard strategy.
Honestly, the real problem here is that we're still stuck with this legacy Windows PowerShell, which is still the default install. They need to cut the cord and finally move everything to PS Core. Granted, PowerShell 7 is the first version that feels like I can really just stop using Windows PowerShell forever, but the central issue is that TLS 1.1 only had a 5 year lifespan and then MS started juggling .Net Framework and .Net Core and PowerShell got stuck in the middle of it. That's the only reason we're still talking about this issue. Otherwise, it would've been fixed in 2016 and we wouldn't even be thinking about it anymore. It would be like not having a
Countproperty on singleton objects from PowerShell 1.0.Yes, MS should probably have added a couple commands to get/set this setting more intuitive. Yes, MS could have had a better error message than just "connection terminated unexpectedly by remote host" when you had a TLS handshake failure of some sort. Yes, the default in .Net Core 2.0+ that uses
[System.Net.SecurityProtocolType]::SystemDefaultis way better and should have been implemented way before 2017. But I think it's difficult to really criticize MS for Windows PowerShell's behavior.Also, if you've never needed to use this setting before, then the web services you were communicating with still supported TLS 1.0/1.1. Is that MS's fault, too? I mean, you can run TLS 1.1 fairly securely, you just need to have proper mitigations in place on the server. Neither TLS 1.0 nor 1.1 were deprecated by the major browser vendors until just a few weeks ago in March 2020. Also, making the client -- which PowerShell always is unless you're doing something really strange -- somehow bear the burden of securely communicating with your web service is pretty sketchy. The web service needs to be secure if it's going to have that protocol enabled, right? It shouldn't allow insecure ciphers or protocols it doesn't consider insecure. Yes, if it's a third party you don't know, but you can easily enable it. It's not difficult; it's just obscure.
I just save what it was, set it to 1.2 for the request then put it back to what it was in case someone has a particular config. Saves me learning everything about it to avoid interfering.
Setting
[Net.ServicePointManager]::SecurityProtocolonly survives for the current session. It resets to the framework default the next time you run PowerShell. I suppose if you're using it in a function or cmdlet it makes sense, but how often are you really calling that kind of function when you still need to revert to the older protocol? If you are, why net set it to'Tls12, Tls11,Tls'so it can fall back?2
u/Potato-9 Apr 27 '20
Thanks for the full reply. I have more limited sympathy than yourself for the hardships Microsoft themselves inflicted upon them who then make us deal with it.
But as for you last point, I basically have powershell open if my computer is on. So imo it's not wise to just assume there's a clean session all the time or ps is closed once your code runs.
2
u/da_chicken Apr 27 '20
I can appreciate having limited sympathy.
But as for you last point, I basically have powershell open if my computer is on. So imo it's not wise to just assume there's a clean session all the time or ps is closed once your code runs.
I often do the same thing, but I have switched to the Windows Terminal preview from the MS store, which lets you easily open a new session in the tabbed interface. I highly, highly recommend it. I have mine set to open PowerShell Core by default with options for Windows PowerShell, cmd, and Python 3.
My workflow now is to have one "dirty" session of Powershell 7 open all the time for real ad hoc stuff, and then I open a session whenever I'm doing something remotely complicated. I recycle the dirty session every so often into a new session.
The app is still in preview, but supposedly it should be released to v1.0 very soon. It does require you to be on 1903 or later, however, but at this point I don't know why you wouldn't be.
2
2
u/mercuic Apr 27 '20
Aren't you potentially feeding a website new passwords not already know?
3
u/rmbolger Apr 27 '20
No. Have a read about Validating Leaked Passwords with k-Anonymity
Basically, only the first 5 characters of a hash are ever sent anywhere. You're not ever sending the plain text password or the full hash.G
-1
1
u/sideh7 Apr 27 '20
Thanks for sharing this mate. It's created a great discussion with some great solutions.
1
55
u/rmbolger Apr 26 '20
If you're looking for something a bit more fleshed out that also has the ability to audit Active Directory passwords, I wrote a module called PwnedPassCheck and an associated blog post.
Auditing Active Directory Passwords with PwnedPassCheck