gpt4 book ai didi

powershell - 文件 "randomly"中的 SecureString 停止工作

转载 作者:行者123 更新时间:2023-12-02 22:12:12 24 4
gpt4 key购买 nike

我开发了一个 PowerShell 脚本,它处理某些文件并通过 SFTP 传输它们。
为此,只要需要凭据向 SFTP 服务器进行身份验证,它就会使用 SecureString 类型从加密文本文件中读取和解密 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 用户下运行的计划任务执行。

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

最佳答案

解密就停止工作真的有点奇怪,这听起来很环保。然而,你为什么要这样做,这样……

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"

... vs 选择这些路线。
$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 的了解,对于大多数人来说,它仍然是一件事,尽管根据我上面展示的内容,我不确定为什么。

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

关于powershell - 文件 "randomly"中的 SecureString 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60511076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com