【问题标题】:How to handle sensitive data, and pass it to a function?如何处理敏感数据,并将其传递给函数?
【发布时间】:2017-05-11 01:39:29
【问题描述】:

我有一些参数存储在本地硬盘上的加密文本文件中。这些参数必须保密。

如何从文件中读取/解密参数并将它们传递给powershell函数,以使变量永远不会以纯文本形式公开/?

我想使用安全字符串,但如何将它们传递给 powershell 函数?这个我试过了:

function Create-Securestring
{
    $my_secure_password = convertto-securestring "password" -asplaintext -force
    return $my_secure_password

}

function Accept-Securestring
{
    param($secure)  
    $cast = [string]$secure ## maybe cast works?
    $test = "$cast test string"
    if ($secure -eq "password"){ write "works fine" }
    return $test
}


$secure = Create-Securestring
$test = Accept-Securestring $secure
$test # -> System.Security.SecureString test string

我在这里给出了一个外部文件的例子,但是任何如上所述的存储和使用秘密数据的方法都是可以接受的。

【问题讨论】:

  • 您希望如何在不解密的情况下处理数据?如果您不使用它:为什么还要首先加载它?
  • 是的,解密并转换为安全字符串。
  • 在文件中存储(和恢复)SecureString 值的大量示例在线(以及在此站点上)存在。这里的实际问题是什么?你有什么问题?
  • 数据是如何加密的?此外,您确实意识到安全字符串 aren't as secure as one would expect,不是吗?
  • 当然您可以将SecureString 对象传递给PowerShell 函数。如果它不适合您:请显示您的代码和您遇到的任何错误。

标签: powershell encryption


【解决方案1】:

您的问题是将安全字符串转换回常规字符串。你可以把它变成PScredential 并使用GetNetworkCredential().Password 像 Ansgar 链接,或者你可以使用[System.Runtime.InteropServices.Marshal] 中的一些方法。

function Create-Securestring {
    convertto-securestring "password" -asplaintext -force
}

function Accept-Securestring {
    $secure = Create-Securestring
    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)            
    $cast = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    "$cast test string"
    if ($cast -eq "password"){ write "works fine" }
}

但请注意,将它们转换回来会使它们变成“纯文本”。这让你回到了 Ansgar 在第一条评论中的观点,如果它现在应该是安全的,你却让它变得不安全。

【讨论】:

  • 就是因为这个问题才问了这个问题。
  • @joops 让我重新表述我对您问题的第一条评论:您必须先解密您的数据,然后才能实际使用它们。这意味着您的数据将在某些时候未加密。没有办法解决这个问题。
  • securestring传递给PSCredential object怎么样?比如:` $MyCredential = New-Object -TypeName System.Management.Automation.PSCredential ` -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key) ` ?
  • @joops 这在功能上等同于这个。
  • 所以解密并转换为明文,而不是在使用后立即处理明文变量是我能做的最好的事情吗?
猜你喜欢
  • 2020-11-10
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
相关资源
最近更新 更多