r/PowerShell • u/RealAgent0 • Jun 06 '26
Question How "Secure" is Get-Credential?
Trying to make a justification for storing a particular set of credentials using this methodology.
How "Easy" is it to crack? Don't need an actual method, just need someone to tell me how tricky or not tricky it is.
76
Upvotes
72
u/sid351 Jun 06 '26
You don't store credentials with
Get-Credentialdirectly. If you're saving the output of that into a variable, it's trivial to get the actual password as plain text in that user session.For storing, you can use
ConvertTo-SecureStringand export that to a text file. By default that's encrypted by your user account on that specific computer, so it can only be read back in (ConvertFrom-SecureString) by that user account on that computer. You can use other cryptographic approaches if you want.That's not really "secure" (it's not cryptographically secure), but it might be "secure enough" for what you need right now.
If you need proper security you're looking at needing something like hardware security modules (HSM) at some point along the line really. (Even if that's "just" to unlock a password manager)
11
u/mrbiggbrain Jun 06 '26 edited Jun 07 '26
Get-Credential outputs a PSCredential object which is a class that represents credentials. That object then has a Password property which is a SecureString, a type that represents an encrypted password.
The intention of SecureString is to keep secrets encrypted in memory. Secrets are converted into plain text just in time only when needed, and only for long enough to be used. Best practice is to pass around the secure string and to not decrypt it until the very moment it is needed. So if you had a function that needed a password you should accept a secure string and then only decrypt it the moment before you spoke to something that could not take a secure string.
SecureString can be converted to a normal string using ConvertFrom-SecureString. The resulting object will be a normal string but encrypted with the users DPAPI key which is stored in the registry. (Note how we are converting from a secure string, people often confuse this). We can later take that string and use ConvertTo-SecureString to get back our in-memory object we can pass around. We store that normal encrypted string in a file.
DPAPI relies on the security of the user, it's used to encrypt many secrets on the system and is as secure as the credentials vault which also uses it.
6
u/LALLANAAAAAA Jun 06 '26
Storing where? How?
3
u/AlexHimself Jun 06 '26
It's easy if you have access to the logged in session or the memory. So, any background process can easily access it. It's probably "good enough", but Microsoft says you shouldn't treat SecureString as any more secure than a string.
Basically, if anything malicious is on your device it can probably get access to it.
It's recommended to use the Secret Management Module, which can handle 100% local, offline storage too.
4
u/justaguyonthebus Jun 06 '26
It is as secure as the interactive user session. It's a prompt to the user in user space. The credential sits in memory but that requires debug (admin) privileges to access. I think it's in a secure string but will hit plain text at some point when you go to use it.
It's the optimal way to get a cred directly from the user if you can't use native Windows single sign on creds already loaded.
But that only gets it from the user. You mentioned storing it, is that short term for current execution? Or actually stored for future sessions?
Microsoft has a credential management model for other scenarios.
2
u/LogMonkey0 Jun 06 '26
Storage would involve Import/Export-CliXml on the resulting PSCredential object or using SecretManagemenr module
Others have already commented on the DPAPI part that handles the SecureString password field in the object
2
u/TofuBug40 Jun 06 '26
You really need a vault we used the local secrets management one for an automated process that used the system account and just wholesale copy the vault down to read from. Since SYSTEM is a well known SID it's encryption algorithm is identical. From there all that's stored in the process is a string used to generate the password for the vault itself. The actual process of generating the password sits in a layer beneath what the logs or an operator can see. From there we have 2 more public end points. First Action_VaultSet which takes in an object and a string token. It basically stores the object securely in the vault. The second and more widely used is Func_VaultGet which only takes a string token but returns a fully formed object.
So what that means is we preload the automation bundle with a minimal version of the vault that only has a pair of credentials to connect it to the main system. From there we pull down a fresh copy of the vault (it's just files to copy when not in use) and now we just hand it what token we want.
So all the logs see is something like
19:00 1/1/2026 Mapping drive to \server\share 19:01 1/1/2026 using token RM1 19:02 1/1/2026 Mapped Drive
Obviously I'm being hyperbolic with the "times" in these mock logs.
The point is anytime a module in the process needs credentials it only requires the person designing that module to ask Func_VaultGet for a secret matching a token. Since what is returned is a fully formed PSCredential object and it only exists for the duration of the current modules existence in a closed system there's no way for it to be read extracted sniffed etc without debug level access which only a strict set of engineers have.
An additional benefit is since the entire mechanism is kind of brain dead in terms of what's in the vault it's reading from you can hot swap to any version of the vault with wholly different credential objects which means it can be dropped into any process that needs secure credentials.
Finally because the underlying control is just Microsoft.PowerShell.SecretManagement it's trivial to just swap it for something like KeePass, Azure KeyVault, CyberArk, etc without the processes, modules and the authors and operators seeing any differences in how they use them.
1
u/ihaxr Jun 06 '26
It's secured to the user and machine running the script, if you change the user or login as the user to another machine, the stored creds are useless.
That being said, you may wish to just investigate a password safe / credential manager that has API access from PowerShell if security is the goal.
It's not inherently MORE secure, but it's probably auditable which is what a lot of places care about... If the credentials are leaked either way it's bad, but not knowing what credentials were leaked and when they were leaked is worse.
1
u/holy_handgrenade Jun 06 '26 edited Jun 06 '26
There's not really a great justification for storage. You'd want something far more crytographycially secure which can be done but Get-Credential is purely for specific user sessions and wasnt ever meant to be secure. The only real security it does is it blocks out user imput from being read by anyone (Enter Password: ******)
You can convert to or from secure string if you need to pass that around or the user session will be long, but that's within that session and in memory. If it's leaving memory and going to longer term storage such as a password manager, this is not the secure way to do this.
It's been quite a few years since I've had to do this, but I wound up passing the secure string through an AES-256 encryption algorithm that would return a cryptographically secure string, which would then be hashed, and stored in a database. The hash would be checked and verified, decrypted, then converted from the secure string to be utilized again. That was a rather complex setup and there's far easier ways to do things today than there were back when I had to do this.
1
u/jdtrouble Jun 08 '26 edited Jun 08 '26
You can crack Get-Credential yourself.
From https://powershellfaqs.com/powershell-convert-secure-string-to-plain-text/
# Create a SecureString from a plain text password$securePassword = ConvertTo-SecureString "WashingtonDC2025!" -AsPlainText -Force# Convert SecureString to plain text$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)$plainText = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)# Output the plain text passwordWrite-Output $plainText# Free the allocated memory[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ptr)When feasible, don't save credentials in variables. Have them evaluated immediately, for example
NewPSSession srv01 -Credential (Get-Credential user)(Final edit) Or dispose the variable from memory ASAP
1
u/node77 Jun 06 '26
Good question and I use it often. The question is I think what is the secure string comprised of. I suppose some level of encryption. I don’t think Microsoft ever truly explained that. But I could be wrong. For what I use it for I am comfortable with the Secure String.
1
u/Difficult_Horse_8912 Jun 06 '26
It's secure, no other user can decrypt the secure string and use it. They would have to be in a session as your user on the computer/server it was created on to use it
-1
u/Ok_Mathematician6075 Jun 07 '26
You should have asked this question 10 years ago.
-8
u/[deleted] Jun 06 '26
[removed] — view removed comment