shell_exec 中的 PHP sudo
我想使用 shell_exec 以 root 身份执行命令。现在我知道这很危险,但相信我,您需要使用 MOD_AUTH 登录并拥有正确的权限才能访问此页面。这是安全的。我怎样才能完成这件事?
I want to execute a command as root with shell_exec. Now I know this is dangerous, but believe me, you need to login with MOD_AUTH and have the right privilleges to come to this page. It's secure. How can I get this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用phpseclib(一个纯 PHP SSH 实现)的最新 SVN 版本来执行此操作。例如。
You could use the latest SVN version of phpseclib, a pure PHP SSH implementation, to do this. eg.
问题不在于您的页面是否安全,问题在于赋予 php 页面运行某些 sudo 命令的能力会将其赋予所有页面包括服务器上任何站点的任何不安全页面上的任何注入代码。
也就是说,最好制作一个包装器脚本,只执行需要执行的一项工作,然后为 http 用户提供对该 1 个命令的访问权限(如 sudo)
The problem isn't that your page is or isn't secure, the problem is that giving a php page the ability to run some sudo command would give it to all pages including any injected code on any insecure page on any site on the server.
That said, it might be best to make a wrapper script that does just the one job that needs doing, then give the http user access to just that ONE command as sudo
绝对不建议。但是,您需要考虑编辑
sudoers
文件,并添加用户 php 作为您需要运行的命令的NOPASSWD
运行。这将只允许他在不输入密码的情况下执行该命令。如果您需要更多命令,请添加更多命令。 Sudoers 配置 我知道 forum/post 是基于 debian 的,但 sudo 并不是严格意义上的 debian,而且应该可以帮助您了解需要放置的 sudo 配置值。
Definitley not advised. However, you will want to look into editing the
sudoers
file and add the user php is running as aNOPASSWD
for the command you need to run. This will only allow him tosudo
that one command with out entering a password.If you need more commands add more to it. Sudoers Configuration I know that forum/post is debian based but sudo is not strictly debian and that should help you out with the sudo configuration values that you need to put it.
我只是在 Google 上搜索
php sudo shell_exec
,结果发现这是排名第一的匹配项:http://www.php.net/manual/en/function.shell-exec.php#101440
I just Google'd for
php sudo shell_exec
and this came up as the #1 match:http://www.php.net/manual/en/function.shell-exec.php#101440
/********************************
************例子*************
******************************/
运行名为
my_perl_script.pl
的 Perl 脚本:/*******************************
************Example*************
*******************************/
Run a Perl script named
my_perl_script.pl
:最佳方法:
再次运行相同的命令,并带有 sudo 前缀。
Best way to do it:
It runs the same command again, with sudo prefixed.