2011-09-19 10 views
19

Desidero decodificare la password da System.Security.SecureString a una password leggibile.PowerShell - Decode System.Security.SecureString a password leggibile

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

Questa parte di codice funziona correttamente, posso utilizzare l'oggetto $ credentials. Ma più avanti nel mio codice ho bisogno della password in un formato leggibile. Perché un metodo richiede la password in stringa leggibile. Quindi devo decodificare la password.

Come è possibile decodificare la password dall'oggetto $ credentials?

Aggiornamento

Non funziona:

$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 
+0

Una volta ottenuto l'oggetto 'PSCredentials', è sufficiente eseguire: ' '' $ credentials.GetNetworkCredential(). Password''' – CubanX

risposta

20

Qui si va:

$password = ConvertTo-SecureString '[email protected]' -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

+0

Grazie per la risposta! Questo mi aiuta molto! Ma perché non è possibile usare lo stesso codice con $ credentials.password? – LaPhi

+0

@manojlds grazie per la modifica. –

+0

Ma perché quel codice non funziona per $ credentials.password? Esistono diverse SecureStrings? O la chiave per la crittografia è un'altra? – LaPhi

2

I dettagli sono spiegati 0.123.754,474056 millions

e ho ancora un altro modo leggermente diverso di farlo.

$pass=convertto-securestring "[email protected]" -asplaintext -force | ConvertFrom-SecureString 
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR((ConvertTo-SecureString $pass))) 

P @ ssw0rd

3
($credentials.GetNetworkCredential()).Password 
10

Per un oggetto "System.Net.NetworkCredential", tutto quello che dovete fare è leggere la password String.

$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;} 

Se si finisce con un oggetto PSCredential, da un comando interattivo come Get-Credential uso

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

Vedi http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx per i dettagli.

Nota: ho utilizzato PS 4 per questo esempio.

+0

Funziona anche in PS 3.0 – helb