2017-08-09 141 views
0

我需要訪問需要用戶名和密碼的服務,我不希望將其存儲在代碼中。該代碼在具有kerberos的Linux服務器上運行。在linux中存儲憑證

任何人都可以指出的例子,允許我在Linux上用Python或bash shell從linux存儲密碼?

目前我在一個只有當前用戶權限的文件中使用明文密碼。

我已經指出了一個指南使用PowerShell:

#The following command create a txt file containing a encrypted version of 
#the password string (in this example "[email protected]"). This is something you do once while 
#logged on as the account that will eventually decrypt the password 
####################################################################################### 
"[email protected]" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Temp\Password.txt" 


####################################################################################### 
#In your script that are using the encrypted password, you can do the following: 
####################################################################################### 
# 1) Read the encrypted password from the txt file and convert it to a SecureString object 
$pass = Get-Content "C:\Temp\Password.txt" | ConvertTo-SecureString 

# 2) Convert the SecureString object to a plain text variable that can be used in the script 
#The decrypted password is now available in the $UnsecurePassword variable. 
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass) 
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) 
cls 
Write-Host $UnsecurePassword 
+0

將加密密碼放入文件沒有多大意義。腳本必須包含解密密鑰,所以任何可以讀取腳本的人都可以解密文件中的密碼。 – Barmar

+0

@Barmar >> Powershell技巧中涉及到一些Windows魔法,也就是說,如果您將加密文件複製到另一臺機器或從另一個用戶的會話訪問它,您將無法使用「automagic」鍵。好的,在Windows中有多個特權升級問題,但這是另一回事...... –

+0

[我需要在Python中安全地存儲用戶名和密碼,我的選項是什麼?](https://stackoverflow.com/questions/7014953/I-需要對安全店內-A-的用戶名和密碼,在-python的 - 什麼 - 是 - 我的選項) –

回答

0

對於存儲配置值,我建議使用configparser,讓您存儲任何類型的配置和參數的配置文件。

存儲用戶名和密碼當然非常敏感。一種方法是用主密鑰加密數據,只有您自己知道並且不在任何地方進行硬編碼。相反,每次運行或啓動腳本時,都可以提示該主密鑰,然後解密存儲在配置文件中的加密密碼。這種方法解釋爲here