茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1673094 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00378.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2016/1/28 2:57:39

Secure Password with PowerShell: Encrypting Credentials – Part 1

Posted on Posted in PowerShell

Like many systems administrators out there, I’ve often found myself with a task that needs to be automated. Automating is great with PowerShell until you need to pass credentials into a script.

kris_resized

At this point, I have seen many administrators put passwords into the body of their script. For testing purposes, this may be forgiven, but in production scripts, putting your passwords in plain view isn’t just a bad thing…it’s a terrifying thing. It should be a cardinal sin. But you can secure a password with Powershell (or at least reduce password visibility).

First, we should touch base on how to supply a credential without having to save it directly in your script.


Get-Credential and Read-Host

You can create a PSCredential object by using the cmdlet Get-Credential and storing the output into a variable. You can pass that variable directly into cmdlets that support PSCredential objects.


$MyCredential = Get-Credential

secure password powershell

Notice that when you access the variable $MyCredential, you are able to see the username but you are unable to see the password. It only displays, “System.Security.SecureString” on the screen. This is because the password is now stored as a SecureString.

You can then use this new PSCredential object directly with cmdlets that support PSCredential objects. You can also individually reference the username or the password for cmdlets that don’t accept a PSCredential object but will support username and password parameters.

In those cases, you can use $MyCredential.Username and $MyCredential.Username


Alternatively, you can use Read-Host to prompt for input and store the result in a variable. This includes prompting for a SecureString (for a password).


$user = Read-Host "Enter Username"
$pass = Read-Host "Enter Password" -AsSecureString

secure password powershell

Notice that the output is very similar to the output of the Get-Credential variable we used, $MyCredential. It shows the username as, “MyUserName” and the password as, “System.Security.SecureString.”

This is great for manual runs of scripts as it helps to remove the password from the script, but it doesn’t really help with our automation. We’re looking for a solution that will be able to run automatically without having to constantly supply credentials via Get-Credential/Read-Host or by leaving our passwords in plain view for anybody to read.


ConvertTo-SecureString – Encrypting passwords and other strings

ConvertTo-SecureString is used to convert plain text or encrypted standard strings into a SecureString object. The SecureString object can be used with cmdlets that support parameters of type SecureString, as is the case with a PSCredential object. You can use the command directly or pipe results into the command.

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

 

When you are not using the –Key or –SecureKey parameters, PowerShell uses the Windows Data Protection API (
DPAPI) to encrypt/decrypt your strings. This effectively means that only the same user account on the same computer will be able to use this encrypted string. That is something to keep in mind as you attempt to automate any scripts. If you’re using a service account, you’ll need to use the –Key or -SecureKey parameters.

Let’s say, for example, you want to take the text, “P@ssword1” and convert it to a SecureString. Since this is a plain text string, we’re going to use the –AsPlainText and –Force parameters.


"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force

secure powershell password

The result is a SecureString object. Unfortunately, you cannot directly save a SecureString object to a file for later use. You have to convert this SecureString object to an encrypted standard string. You can do this with ConvertFrom-SecureString.

ConvertFrom-SecureString – Saving encrypted standard strings

ConvertFrom-SecureString is used to convert secure strings into encrypted standard strings. You can use the command directly or pipe results into the command.

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.

 

Following the same example above, we’ll take the output of the previous example and pipe it into the ConvertFrom-SecureString command to get an encrypted standard string.


"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString

secure password powershell

The result is an encrypted standard string that you can then save for later retrieval.

Putting it all together

We now know how to convert a SecureString to an encrypted standard string. We can take any method we like to get a SecureString, convert it to a standard string and then save it to a file. Here is an example of each:

Exporting SecureString from Plain text


"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Temp 2\Password.txt"

Exporting SecureString from Get-Credential


(Get-Credential).Password | ConvertFrom-SecureString | Out-File "C:\Temp 2\Password.txt"

Exporting SecureString from Read-Host


Read-Host "Enter Password" -AsSecureString |  ConvertFrom-SecureString | Out-File "C:\Temp 2\Password.txt"

Any one of these examples should provide you with a Password.txt file that has an encrypted standard string the represents the password.

When you need to use this encrypted password, you simply reverse the process by importing the data from your file and use ConvertTo-SecureString. If all you need is a SecureString, you can stop there. You could even take it a step further and create a PSCredential object.

Creating SecureString object


$pass = Get-Content "C:\Temp 2\Password.txt" | ConvertTo-SecureString

Creating PSCredential object


$User = "MyUserName"
$File = "C:\Temp 2\Password.txt"
$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

Final Notes

This will not stop anybody who knows what they’re doing from decrypting your password or from reusing your encrypted password if they ever are able to compromise your login. The whole point of converting your password to a SecureString and storing it in a file is to keep it out of plain text in your scripts so that it’s not as easily discovered. It’s not foolproof, but it’s pretty good.

As mentioned above, when you are not specifying a key or securekey, this will only work for the same user on the same computer will be able to decrypt the encrypted string if you’re not using Keys/SecureKeys. Any process that runs under that same user account will be able to decrypt that encrypted string on that same machine.

If you want to be able to share a credential with multiple machines/logins/etc, then you’ll need to use Keys/SecureKeys. I’ll save that for another post.



原文出處:Secure Password with PowerShell: Encrypting Credentials - Part 1
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|