请就这两个脚本的安全性发表您的专家意见:
注意事项:这两个脚本的输出将是流水线的。他们不会被分配给任何变量。“
First :
Function GetFrom-SecureString([SecureString]$SecureString) {
[IntPtr]$valuePtr = [IntPtr]::Zero
try {
$valuePtr = [Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($SecureString)
return [Runtime.InteropServices.Marshal]::PtrToStringUni($valuePtr);
}
finally {
[Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($valuePtr);
}
}第二版:
Function Decode-SecureString([SecureString]$SecureString){
try{
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
$length = [Runtime.InteropServices.Marshal]::ReadInt32($bstr, -4)
for ( $i = 0; $i -lt $length; ++$i ) {
[CHAR][Runtime.InteropServices.Marshal]::ReadByte($bstr, $i)
}
}
finally{
if ( $bstr -ne [IntPtr]::Zero ) {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
}
}
}我知道使用$cred.GetNetworkCredential().Password并不安全,因为它首先将安全字符串转换为内存,许多人已经向我指出要使用编组。因此,我已经构建了上述两个脚本作为试用,我想知道是否有任何鲁莽的策略在这两个脚本。
编辑:
我已经更新了我的第二个字符串,以便通过实现以下操作来清除BSTR:
finally{
if ( $bstr -ne [IntPtr]::Zero ) {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) #Frees the BSTR.
}
}但是,在管道中捕获的返回值似乎根本不安全,因为它也在内存中,直到清理完毕。
注释:--我还没有编写接受流水线值的脚本。
使用安全字符串的目的似乎是通过将其解密回明文来破坏它。好像我们永远不应该解密安全字符串。如果我错了,请纠正我。
发布于 2018-02-19 14:51:09
所选择的解密技术并不重要,因为您仍在转换为明文.
即使在内存中的秘密只存在很短时间,有什么原因阻止我使用调试技术从内存中读取它呢?
Wait-Debugger)。-Verbose时记录它时。在本文中,使用管道或直接变量并不重要。这两种方法本质上都不安全,都需要/导致在内存中使用纯文本的秘密。
我将寻找一个选项来指定加密到目标的秘密值,从而完全绕过解密的需要。
https://stackoverflow.com/questions/48865592
复制相似问题