在Powershell脚本中安全的使用密码信息


在使用基于Powershell自动化的脚本中,大多数场景下需要未脚本传递凭据以供验证环节能够正常工作,很多管理员习惯于在他们的脚本中使用明文密码,但如果是测试脚本,倒是无关紧要,若是用于生产环境的脚本,这种行为不但很糟糕,还很可怕,不怀好意的同志们会很乐意并轻松的拿到它然后搞一些或轻或重的破坏。但是,如果正确使用powershell所提供的密码加密功能,能很轻松的解决这个问题。

获取凭据的方式

PowerShell中,提供了两种生成凭据的方式分别是Get-CredentialRead-Host
Get-Credential
Get-Credential命令被用来创建PSCredential对象并且将之存储到变量中,并保证不在屏幕上显示如下

image.png

image.png

上面的命令将凭据信息保存到变量$crd中 ,这个变量可被很容易的用于访问需指定凭据的Cmdlets
Read-Host
使用Read-Host同样可以起到保护明文密码显示的功能 如下,-AsSecureString可以将明文密码存储成加密密码串对象,SecureString对象可被用于可接受该类型参数的命令,可以通过定义变量和管道输出将该对象传递给命令。

image.png

image.png

加密字符串的方式

ConvertTo-SecureString
ConvertTo-SecureString用于加密密码和其它字符串,它可以接受字符串然后输出为SecureString对象,SecureString对象可被用于可接受该类型参数的命令,可以通过定义变量和管道输出将该对象传递给命令。

语法如下:

Syntax:
ConvertTo-SecureString [-String] SomeString
ConvertTo-SecureString [-String] SomeString [-SecureKey SecureString]
ConvertTo-SecureString [-String] SomeString [-Key Byte[]]
ConvertTo-SecureString [-String] SomeString [-AsPlainText] [-Force]

–String String
The string to convert to a SecureString

–SecureKey SecureString
Encryption key as a SecureString.

–Key Byte[]
Encryption key as a byte array.

–AsPlainText
Tells command to treat string as plain text. The string is not encrypted when
using this command. Because of the lack of security, the -Force parameter 
is also required.

–Force
Confirms you understand the lack of security when using -AsPlainText

当不适用参-Key或者-SecureKey时,PowerShell将会调用Windows Data Protection API (DPAPI)加密\解密字符串。但这将导致生成的SecureString对象在其它计算机上使用。
如下将把明文"Password"转化为SecureString

"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force
image.png

这种方式虽然能够将明文字符转换为SecureString对象,但是却无法将其存储为一个文件以后用。必须把它转化为标准加密字符串才可以,要达到这个目的,要用到ConvertFrom-SecureString命令。
ConvertFrom-SecureString
使用ConvertFrom-SecureString命令可以将SecureString对象存储成为标准加密字符串,可以将SecureString对象或者命令产生的SecureString对象通过管道传给它来实现

语法:

Syntax:
ConvertFrom-SecureString [-SecureString] SecureString
ConvertFrom-SecureString [-SecureString] SecureString [-SecureKey SecureString]
ConvertFrom-SecureString [-SecureString] SecureString [-Key Byte[]]

–String String
The string to convert to a SecureString

–SecureKey SecureString
Encryption key as a SecureString.

–Key Byte[]
Encryption key as a byte array.

上面的例子产生的SecureString对象可以用于此处转换成标准加密字符串

image.png

存储标准加密字符串为txt文件

结合上面几个命令,我们可以有以下几种方法将SecureString转换成的标准加密字符串通过Out-File命令存储成txt文件

导出纯文本模式生成的SecureString对象

PS N:\> "Password" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File $home\Documents\password.txt

导出Get-Credential产生的SecureString对象

(Get-Credential).Password |  ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File $home\Documents\password.txt

导出通过Read-Host产生的SecureString对象

Read-Host "Enter Password" -AsSecureString |  ConvertFrom-SecureString | Out-File $home\Documents\password.txt

使用标准字符串文件

将上述存储的标准加密字符串文件转换回SecureString并用来创建PSCredential

$pass = Get-Content "$home\Documents\password.txt" | ConvertTo-SecureString
$User = "Test User"
$Crd = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$pass

跨计算机标准加密字符串文件的使用

上面提到,在使用ConvertTo-SecureStringConvertFrom-SecureString加密凭据时,如不使用-Key-SecureKey,则PowerShell会调用DPAPI进行加解密,因为无法再创建其之外的机器上使用,如下
同一台主机

image.png

不同主机上使用上面同一个txt文件

image.png

使用Key或者SecureKey实现跨机器使用加密文件
使用-Key参数创建保存标准加密字符串的文件,下面将会用到Windows Class “System.Security.Cryptography.RNGCryptoServiceProvider"来创建随机数据来填充作为-Key的参数值的数组

使用随机数据来创建AES key并导出为文件

PS N:\> $Key = New-Object Byte[] 16 
PS N:\> [Security.Cryptography.RNGCryptoSeviceProvider]::Creat().GetBytes($Key) 
PS N:\> [System.Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
PS N:\> $key | Out-File $keyfile

创建SecureString对象

PS N:\> $passwordfile="$home\documents\passwordfile.txt"
PS N:\> $password = "password" | ConvertTo-SecureString -AsPlainText -Force
PS N:\> $password | ConvertFrom-SecureString -Key $Key | Out-File $passwordfile

创建凭据

PS N:\> $User = "testuser"
PS N:\> $Crd = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,(Get-Content $passwordfile | ConvertTo-SecureString -Key $key)

在另外的机器上

PS C:\Windows\system32> $keyfile = "$home\desktop\AES.key"
PS C:\Windows\system32> $passwordfile = "$home\desktop\passwordfile.txt"
PS C:\Windows\system32> $User = 'testuser'
PS C:\Windows\system32> $key = Get-Content $keyfile
PS C:\Windows\system32> $crd = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,(Get-Content $passwordfile | ConvertTo-SecureString -Key $key)

上面的内容已经完全可以帮助我们在powershell脚本中来加密密码了,如果有其它问题欢迎留言。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容