首页 > 解决方案 > 文件中的 SecureString“随机”停止工作

问题描述

我开发了一个 PowerShell 脚本,它处理某些文件并通过 SFTP 传输它们。为此,它使用 SecureString 类型从加密的文本文件中读取和解密 SFTP 用户凭据,只要凭据需要对 SFTP 服务器进行身份验证。这是必需的,因为我不允许将凭据作为纯文本存储在文件(或脚本本身)中,否则它将无法通过下一次安全审核。

除了脚本之外,还有一个单独的配置脚本必须运行一次以指定 sftp 用户凭据、加密它们并将它们保存到文本文件中。这是该配置脚本的(简化)代码:

Write-Output "Please enter the username of the SFTP user."
$username = Read-Host
Write-Output "Please enter the password of the SFTP user"
$password = Read-Host -AsSecureString
$username | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "$scriptroot\Credentials.dat"
$password | ConvertFrom-SecureString | Out-File -Append "$scriptroot\Credentials.dat"

基本上这两个脚本工作得很好,可以做他们应该做的事情。但是,无论出于何种原因,一旦我注销然后再次登录 Windows,解密将停止工作。

我不明白为什么这会一遍又一遍地停止工作,我不知道该怎么办。有没有人知道为什么解密由于我退出系统而停止工作的方法?该脚本甚至不在我自己的用户下运行,它作为计划任务执行,在完全不同的 AD 用户下运行。

我还读到微软不再建议在新项目中使用 SecureString,但是可以使用什么替代方案呢?我还应该如何将用户凭据安全地存储在文件中,而无需将某种纯文本解密密钥存储在脚本或其他文件中?

标签: powershellcredentialssecurestring

解决方案


解密停止工作真的很奇怪,这听起来很环保。可是,你为什么要这样,这样……

Write-Output "Please enter the username of the SFTP user."
$username = Read-Host
Write-Output "Please enter the password of the SFTP user"
$password = Read-Host -AsSecureString
$username | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "$scriptroot\Credentials.dat"
$password | ConvertFrom-SecureString | Out-File -Append "$scriptroot\Credentials.dat"

......与这些路线中的一条相比。

$UserCreds = Get-Credential -Message 'Please enter the username of the SFTP credentials'
$UserCreds.GetNetworkCredential().UserName
$UserCreds.GetNetworkCredential().Password
<#
# Results

postanote
password
#>

或者这样,

Get-Credential -Message 'Please enter the username of the SFTP credentials.' | 
Export-Clixml -Path 'D:\temp\SFTPUserCreds.xml'
Get-Content -Path 'D:\temp\SFTPUserCreds.xml'
<#
# Results

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCredential</T>
      <T>System.Object</T>
    </TN>
    <ToString>System.Management.Automation.PSCredential</ToString>
    <Props>
      <S N="UserName">postanote</S>
      <SS N="Password">01000000d08c9ddf0115d1118c7a00c04fc297eb01000000bd0a9dd27ffa41419fb2b289838ebe6400000000020000000000106600000001000020000000ad2e4c96413a3e93e461f600e98f72aa5
e3493bde41ac40491bfd4bedc462152000000000e8000000002000020000000e0016657a9f6e545b55876fa8095143c08046f1361baec0f693a1e823cc6ee0d200000006949857c09b23099dab66fdc3a7e4f7637970dbd3aa13
be11fda7c8de63df70e40000000f9a266cbb34440f64d9a2bdeaaec3a6cda483138e0be29323ca0a523ac475e169793557e74dd208e3d4292aa4fbe5b90fc7023d41c4dd6d01f819366e0587885</SS>
    </Props>
  </Obj>
</Objs>
#>

无需解密,只需传递变量名

($UserCreds = Import-Clixml -Path 'D:\Temp\SFTPUserCreds.xml')
<#
# Results

UserName                      Password
--------                      --------
postanote System.Security.SecureString
#>

或者这样

Get-Credential -Message 'Please enter the username of the SFTP credentials.' | 
Export-PSCredential -RegistryPath 'HKCU:\software\SFTP' -Name SFTPUserCreds
$UserCred = Import-PSCredential -RegistryPath 'HKCU:\Software\SFTP' -Name SFTPUserCreds

# Or just use Windows Credential Manager
Find-Module -Name '*CredentialManager*' | Format-Table -AutoSize

Version Name                          Repository Description                                                                        
------- ----                          ---------- -----------                                                                        
2.0     CredentialManager             PSGallery  Provides access to credentials in the Windows Credential Manager                   
...

因此,即使基于您所阅读的有关 SecureString 的内容,对于大多数人来说它仍然是一件事情,尽管根据我上面展示的内容,我不确定为什么。

无论如何,给上面的一个或多个镜头,看看这是否能让你更加一致。


推荐阅读