如何使用autoexpect将密码传递给命令:
这些步骤以Ubuntu 12.10桌面版为例进行说明。您的发行版的确切命令可能略有不同。
这很危险,因为您可能会将使用的任何密码暴露给可以读取autoexpect脚本文件的任何人。
请勿通过此方式通过expect来传递您的root密码或超级用户密码。Rootkits将在瞬间找到它,并控制您的设备。
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
您需要验证上述expect命令是否适当。如果autoexpect脚本过于敏感或不够敏感,则会挂起。在这种情况下,这是可以接受的,因为expect正在等待始终到达的文本。
以用户el身份运行expect脚本:
expect my_test_expect.exp
spawn sudo chown root:root myfile.txt
[sudo] password for el:
我的测试脚本 my_test_expect.exp 中的密码被用户 el 通过管道传递到 chown 到 root。要查看密码是否被接受,请查看 myfile.txt:
ls -l
-rw-r--r-- 1 root root 0 Dec 2 14:48 myfile.txt
它能够成功是因为它是root,而el从未输入过密码。 如果您使用此脚本公开了您的root、sudo或超级用户密码,则获取您的系统root权限将变得容易。 这就是一个安全系统允许任何人不加问询地进入的惩罚。
autoexpect
了吗?它再也没有比那更简单的了。你只需传递命令,它就会记录你所做的一切并为你创建expect文件。 - SiegeX