• PowerShell 脚本中的密码


    引言

    笔者在《PowerShell 远程执行任务》一文中提到了在脚本中使用用户名和密码的基本方式:

    $Username = 'xxxx'
    $Password = 'yyyy'
    $Pass = ConvertTo-SecureString $Password -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Pass

    上面的代码仅仅是能工作而已,因为在稍有安全性要求的环境中都不会允许明文密码的出现。本文将介绍在 PowerShell 脚本中如何以比较安全的方式使用密码。

    把密码转为 SecureString

    先来了解下面两个概念:

    SecureString
    Encrypted Standard String

    SecureString 是 .net 中的一个类型,它是为了解决安全性而设计出来的一种特殊的字符串类型。比如你使用一个密码字符串创建 SecureString 对象,你无法通过这个对象还原出原来的密码字符串,但是却可以把 SecureString 对象当做密码使用。
    Encrypted Standard String 是指经过加密后的一个字符串。

    ConvertTo-SecureString 命令可以通过明文的字符串创建 SecureString 对象:

    $SecurePwd = ConvertTo-SecureString "123456" -AsPlainText -Force

    然后再使用 $SecurePwd 创建 Credential 等身份信息。这种方式就是笔者在引言部分使用的方法,这是不安全的,因为任何能够查看脚本的人都能从中找出密码。

    把 SecureString 转为加密字符串

    通过 ConvertFrom-SecureString 命令,我们可以把一个 SecureString 对象转换成一个 Encrypted Standard String(加密后的一个字符串),然后保存到文件中。在创建 Credential 时直接使用前面保存的文件,从而避免明文密码在系统中出现。

    $SecurePwd = ConvertTo-SecureString "123456" -AsPlainText -Force
    ConvertFrom-SecureString $SecurePwd

    上图显示 ConvertFrom-SecureString 命令生成的加密字符串,我们把它保存到文本文件中:

    ConvertFrom-SecureString $SecurePwd | Out-File "D:pwd.txt"

    看看内容:

    好了,接下来就可以直接使用 pwd.txt 文件了。

    一种看起来比较正常,也很安全的推荐用法:

    Read-Host "Enter Password" -AsSecureString |  ConvertFrom-SecureString | Out-File "D:pwd.txt"

    执行这行命令,会要求你输入密码:

    此处使用键盘输入代替了明文的密码字符串。

    介绍了 ConvertFrom-SecureString 命令的用法后就可以介绍 ConvertTo-SecureString 命令的另外一个用法,把加密字符串转换成 SecureString 对象:

    $f = "D:pwd.txt"
    $SecurePwd = Get-Content $f | ConvertTo-SecureString
    $SecurePwd.Length

    这次我们把 pwd.txt 文件中的内容通过 ConvertTo-SecureString 命令重新生成 SecureString 对象。最后通过 Length 属性检查了密码的长度。

    下图概括了本文中主要术语和命令的关系:

    应用密码的正确姿势

    下面是在脚本中使用密码的建议做法:

    # 生成并保存密码文件
    Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "D:pwd.txt"
    # 使用密码文件创建 Credential 信息
    $f = "D:pwd.txt"
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential `
                       -ArgumentList UserName, (Get-Content $f | ConvertTo-SecureString)

    这种用法也有不足之处,就是只能在生成 pwd.txt 文件的机器上使用这个文件。如果把它复制到其它的机器上,执行 Get-Content $f | ConvertTo-SecureString 时就会报错:

    这是一种安全性限制,如果我们想在其它机器上使用 pwd.txt,就得了解些高级的用法!

    高级玩法

    ConvertTo-SecureString 和 ConvertFrom-SecureString 命令都支持选项 -Key。在处理密码时通过使用 Key 选项可以提供额外的安全性,并且允许在不同的环境中使用密码文件。

    先生成 32 位的 Key 并保存在文件 AES.key 中:

    $keyFile = "D:aes.key"
    $key = New-Object Byte[] 32
    [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($key)
    $key | out-file $keyFile

    使用 Key 生成并保存密码文件:

    Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString -key $key | Out-File "D:pwd.txt"

    使用密码文件创建和 Key 文件创建 Credential 信息:

    $userName = "YourUserName"
    $passwdFile = "D:pwd.txt"
    $keyFile = "D:aes.key"
    $key = Get-Content $keyFile
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential `
                       -ArgumentList $userName, (Get-Content $passwdFile | ConvertTo-SecureString -Key $key)

    通过这种方法,把 pwd.txt 和 aes.key 文件拷贝到其它的机器上也是可以工作的。但是我们需要额外维护一个 key 文件的安全,这一般通过设置文件的访问权限就可以了。

    总结

    PowerShell 提供的安全选项使我们可以避免在脚本中直接使用明文的密码,把密码加密后保存在文件中的好处是可以通过文件的权限管理进行安全性控制。而使用 Key 选项则可以对上述用例进行扩展,从而支持更多的使用场景。但是带来的不便是我们需要另外维护一个机密的 Key 文件。从这点也可以看出,所谓的安全性其实是由安全机制和用户一起来保证的!无论多么安全的机制,在用户泄露了认证信息的情况下都是没有意义的。

  • 相关阅读:
    x8086汇编在显存中显示字符串
    x8086汇编实现dos清屏(clear screen)
    原创:根据题目要求,通过素数的方式判断一个小的字符串是否属于另一个大的字符串的子集
    python signal信号
    转:python signal信号
    python signal(信号)
    python问题记录
    Python语言and-or的用法
    perl6中的q/qq/qx/qqx
    upupw注入by pass
  • 原文地址:https://www.cnblogs.com/sparkdev/p/7258507.html
Copyright © 2020-2023  润新知