How does one securely handle passwords in a custom written PowerShell cmdlet?

后端 未结 5 757
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 16:43

Assume I have a custom PowerShell Cmdlet that exports data and encrypts it using a password.

[Cmdlet(VerbsData.Export, \"SampleData\")]
public class ExportSample         


        
5条回答
  •  日久生厌
    2021-01-22 17:16

    SecureString-Handling gives you a feeling to be a bit more secure, even if this is not the case. You can easily encrypt any SecureString like this...

    $mrsh = [System.Runtime.InteropServices.Marshal]
    $ptr  = $mrsh::SecureStringToBSTR($secureString)
    $pass = $mrsh::PtrToStringAuto($ptr)
    

    or even without marshalling just with a webclient-object like this...

    $cred = Get-Credential
    $web = [Net.WebClient]::new()
    $web.Credentials = [System.Net.NetworkCredential]::new($cred.UserName,$cred.Password)
    $pass = $web.Credentials.Password 
    

    So, in sum neither the password nor the SecureString-Password should be stores permanently as a file or anything else. Make the lifetime of this information as short as possible - e.g. from entering the password till a cleanup of the password-variable and all its inherited variables like so:

    Remove-Variable pass, cred, web -ea 0 
    

提交回复
热议问题