通过我的网络应用程序使用 sudo 运行 shell 脚本
我的 Web 应用程序中有一些与服务器操作系统交互的功能。我已经编写了一个 bash 脚本,并且能够从我的应用程序中运行它。
但是,该脚本的某些功能需要超级用户权限。
安全地运行此脚本的最明智的方法是什么?它从网络表单传递参数,但应该只能由经过身份验证的用户调用,我相信不会对其进行破坏。
I have some functionality that interfaces with the server's OS in my web application. I've written a bash script and am able to run it from within my app.
However, some functionality of the script requires superuser privileges.
What is the most sane way to run this script securely? It is being passed arguments from a web form, but should only be able to be called by authenticated users that I trust not to haxxor it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您采取哪种方式,都可能非常危险。您是否可以编写一个具有所需权限的本地守护程序,并使用一些消息总线来生成/消耗由这个需要超级用户的组件处理的事件?
这样,您可以仔细验证消息的内容,并减少被利用的可能性。
Whichever way you do this is likely to be very dangerous. Can you perhaps write a local daemon with the required privileges, and use some some of message-bus that produces/consumes events to be processed by this super-user requiring component?
That way, you can carefully validate the contents of the message, and reduce the likelihood of exploitation..
如果您确实关心安全性,请要求 Web 客户端提供密码并使用 ssh 密钥。然后在
ssh-agent
下运行脚本,对于敏感部分执行以下操作:ssh root@localhost 命令...
。您可能会想为此目的创建 ssh 密钥对,因为我不会在 Web 表单中输入普通的 SSH 密码(谁会信任您的 Web 表单呢?)。如果您不想要这么多的安全性,并且如果您真的非常相信您的 Web 表单可以正确地验证其用户,没有任何错误,您可以决定信任 Web 服务器来运行您需要的命令。 (我不会。)在这种情况下,我将使用
/etc/sudoers
文件来允许 Web 服务器运行感兴趣的命令,而无需提供密码。然后您的脚本应该使用 sudo 来运行这些命令。If you really care about security, require the web client to provide a passphrase and use an ssh key. Then run the script under
ssh-agent
, and for the sensitive parts dossh root@localhost command...
. You probably will want to create ssh keypairs just for this purpose, as typing one's normal SSH passphrase into a web form is not something I would do (who trusts your web form, anyway?).If you don't want quite this much security, and if you really, really believe that your web form can correctly authenticate its users, without any bugs, you could instead decide to trust the web server to run the commands you need. (I wouldn't.) In this case I would use the
/etc/sudoers
file to allow the web server to run the commands of interest without providing a password. Then your script should usesudo
to run those commands.