3 回答

TA貢獻1848條經驗 獲得超2個贊
如何使用autoexpect將密碼傳遞到命令中:
這些步驟在Ubuntu 12.10桌面上進行了說明。您分發的確切命令可能略有不同。
這很危險,因為您可能會將使用的任何密碼暴露給可以讀取autoexpect腳本文件的任何人。
請勿通過這樣的方式通過管道傳遞您的root密碼或超級用戶密碼。根工具包將立即發現此問題,您的盒子將歸擁有。
EXPECT產生一個進程,讀取輸入的文本,然后發送腳本文件中預定義的文本。
請確保您有expect與autoexpect安裝:
sudo apt-get install expect
sudo apt-get install expect-dev
閱讀它:
man expect
man autoexpect
轉到您的主目錄:
cd /home/el
用戶el無法將文件授予root用戶,必須輸入密碼:
touch testfile.txt
sudo chown root:root testfile.txt
[enter password to authorize the changing of the owner]
這是我們要自動執行的密碼輸入。重新啟動終端,以確保sudo再次要求我們提供密碼。再次轉到/ home / el并執行以下操作:
touch myfile.txt
autoexpect -f my_test_expect.exp sudo chown root:root myfile.txt
[enter password which authorizes the chown to root]
autoexpect done, file is my_test_expect.exp
您已創建my_test_expect.exp文件。您的超級秘密密碼以純文本格式存儲在此文件中。這會讓您非常不舒服。通過盡可能地限制權限和所有權來減輕不適感:
sudo chown el my_test_expect.exp //make el the owner.
sudo chmod 700 my_test_expect.exp //make file only readable by el.
您會在以下內容的底部看到這些命令my_test_expect.exp:
set timeout -1
spawn sudo chown root:root myfile.txt
match_max 100000
expect -exact "\[sudo\] password for el: "
send -- "YourPasswordStoredInPlaintext\r"
expect eof
您將需要驗證上述期望命令是否合適。如果autoexpect腳本過于敏感或不夠敏感,則它將掛起。在這種情況下,這是可以接受的,因為期望值正在等待始終到達的文本。
以el用戶身份運行expect腳本:
expect my_test_expect.exp
spawn sudo chown root:root myfile.txt
[sudo] password for el:
my_test_expect.exp中包含的密碼由用戶el通過管道傳遞給root用戶。要查看密碼是否被接受,請查看myfile.txt:
ls -l
-rw-r--r-- 1 root root 0 Dec 2 14:48 myfile.txt
之所以有效,是因為它是root用戶,并且el從未輸入密碼。如果使用此腳本公開您的root,sudo或超級用戶密碼,那么在您的機器上獲取root將會很容易。這是對安全系統的懲罰,這種安全系統可以使每個人都沒有問題。

TA貢獻1846條經驗 獲得超7個贊
您可以使用該-S標志從標準輸入中讀取。在下面找到一個示例:
function shutd()
{
echo "mySuperSecurePassword" | sudo -S shutdown -h now
}
- 3 回答
- 0 關注
- 1291 瀏覽
添加回答
舉報