繁体   English   中英

对 Plink/PuTTY 使用加密密码

[英]Use encrypted password for Plink/PuTTY

我想在 PowerShell 中加密密码并将其与plinkputty一起使用。

是的,我知道它只需要明文密码(使用 SecureString for plink.exe 命令的密码加密)。

不,我不会使用生成的密钥,因为我们不支持它。

我的问题:

  1. 任何建议如何在puttyplink中为-pw标志使用加密密码
  2. 我可以生成特定的字符串作为键吗? 我的意思是获取当前的明文密码并将其转换为密钥,然后将其用作-i而不是-pw

我的securePass.ps1代码:

$password = read-host -prompt "Enter your Password" 
write-host "$password is password" 
$secure = ConvertTo-SecureString $password -force -asPlainText 
$bytes = ConvertFrom-SecureString $secure 
$bytes | out-file C:\encrypted_password1.txt

主要:

$securePass = Get-Content C:\encrypted_password1.txt
$pass = $securePass | ConvertTo-SecureString
plink -batch -ssh $defUser@$srv -pw $pass
putty -ssh $defUser@$srv -pw $pass

如您所知,您不能对 PuTTY/Plink 使用加密密码 ( SecureString )。

您所能做的就是解密安全字符串并将解密后的纯文本密码传递给 PuTTY/Plink。

对于解密,请参阅PowerShell - 将 System.Security.SecureString 解码为可读密码

$securePass = Get-Content C:\encrypted_password1.txt
$pass = $securePass | ConvertTo-SecureString

$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($pass)
$decrypted = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
plink -batch -ssh $defUser@$srv -pw $decrypted 

PuTTY 0.77 Plink 新支持-pwfile开关,允许通过本地文本文件(虽然仍然是纯文本)以更安全的方式传递密码。


你的问题2)没有任何意义。 您写道,您不能使用密钥。 所以你不能使用-i开关。 更不用说使用一些“生成的密码”了。

$Credential = $(Get-Credential)
$user = $Credential.GetNetworkCredential().Username
$pass = $Credential.GetNetworkCredential().Password

是我在使用 -pw 的脚本中使用的; $ $putty -ssh $server -l $user -pw $pass -m $command

我知道你是说你做了 -I 而不是 -pw 但是我发现这很好用,因为在任何地方都没有存储密码的文件。

这是我的解决方案,它在菜单循环中运行。 效果很好。 我只需要“缓存”我输入的输入或(将先前输入的凭据自动传递到对话框中)否则每次我必须重新输入凭据。

$Key = New-Object Byte[] 32
     [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | Out-File AES.key
(get-credential).Password | ConvertFrom-   SecureString -key (get-content AES.key) | set-content "AESPassword.txt"
$password = Get-Content AESPassword.txt |   ConvertTo-SecureString -Key (Get-Content AES.key)
$credential = New-Object System.Management.Automation.PsCredential($env:userName,$password)
$ServerName = Read-Host -Prompt "What is the server name?"
$Command = ".\plink.exe"
$arg1  =  '-t'
$arg2 = $credential.GetNetworkCredential().username+'@'+$ServerName
$arg3 = '-pw'
$arg4 = $credential.GetNetworkCredential().Password
$arg5 = $scriptcmd
#Write-Output $Command $arg1 $arg2 $arg3 $arg4 $arg5
& $Command $arg1 $arg2 $arg3 $arg4 $arg5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM