Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - Decode System.Security.SecureString to readable password

I want to decode the password from a System.Security.SecureString to a readable password.

$password = convertto-securestring "TestPassword" -asplaintext -force $credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain") 

This code part works fine, I can use the $credentials object. But later in my code I need the password in a readable format. Because a methode needs the password in readable string. So I must decode the password back.

How it is possible to decode the password from the $credentials object?

Update

Not working:

$password = convertto-securestring "TestPassword" -asplaintext -force $credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")  $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($credentials.password) $result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) $result  
like image 820
LaPhi Avatar asked Sep 19 '11 08:09

LaPhi


People also ask

Is Convertfrom SecureString secure?

Converts a secure string to an encrypted standard string.

How do I pass credentials in PowerShell without prompt?

$cred = Get-Credential without asking for prompts in powershell - Microsoft Tech Community.

What is a SecureString PowerShell?

In PowerShell, there are a number of cmdlets that work with something called a secure string. When you create a saved credential object, the password is stored as a secure string.

Is SecureString encrypted?

As others have already answered, the contents of SecureString are encrypted using DPAPI, so the keys aren't stored in your application, they're part of the OS.


2 Answers

Here you go:

$password = ConvertTo-SecureString 'P@ssw0rd' -AsPlainText -Force  $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($password) $result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) $result  

P@ssw0rd

like image 92
Shay Levy Avatar answered Sep 20 '22 11:09

Shay Levy


For a "System.Net.NetworkCredential" object, all you need to do is read the String password.

$password = convertto-securestring "TestPassword" -asplaintext -force $credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain") $credentials.Password TestPassword  $credentials | gm  TypeName: System.Net.NetworkCredential  Name           MemberType Definition ----           ---------- ---------- Equals         Method     bool Equals(System.Object obj) GetCredential  Method     System.Net.NetworkCredential GetCredential(uri uri, str GetHashCode    Method     int GetHashCode() GetType        Method     type GetType() ToString       Method     string ToString() Domain         Property   string Domain {get;set;} Password       Property   string Password {get;set;} SecurePassword Property   securestring SecurePassword {get;set;} UserName       Property   string UserName {get;set;} 

If you end up with a PSCredential object, from an interactive command like Get-Credential use

$credentials=Get-Credential $credentials.GetNetworkCredential().UserName TestUsername $credentials.GetNetworkCredential().Domain TestDomain $credentials.GetNetworkCredential().Password TestPassword 

See http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx for details.

Note: I used PS 4 for this example.

like image 25
Greg Little Avatar answered Sep 16 '22 11:09

Greg Little