shell_exec 中的 PHP sudo

发布于 2024-11-01 02:30:53 字数 102 浏览 4 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

嘴硬脾气大 2024-11-08 02:30:54

您可以使用phpseclib(一个纯 PHP SSH 实现)的最新 SVN 版本来执行此操作。例如。

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
$ssh->login('username', 'password');

$ssh->read('[prompt]');
$ssh->write("sudo command\n");
$ssh->read('Password:');
$ssh->write("Password\n");
echo $ssh->read('[prompt]');
?>

You could use the latest SVN version of phpseclib, a pure PHP SSH implementation, to do this. eg.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
$ssh->login('username', 'password');

$ssh->read('[prompt]');
$ssh->write("sudo command\n");
$ssh->read('Password:');
$ssh->write("Password\n");
echo $ssh->read('[prompt]');
?>
冬天的雪花 2024-11-08 02:30:54

问题不在于您的页面是否安全,问题在于赋予 php 页面运行某些 sudo 命令的能力会将其赋予所有页面包括服务器上任何站点的任何不安全页面上的任何注入代码。

也就是说,最好制作一个包装器脚本,只执行需要执行的一项工作,然后为 http 用户提供对该 1 个命令的访问权限(如 sudo)

http  ALL=(ALL) NOPASSWD:/user/local/bin/your_wrapper_script.sh

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

http  ALL=(ALL) NOPASSWD:/user/local/bin/your_wrapper_script.sh
简单气质女生网名 2024-11-08 02:30:54

绝对不建议。但是,您需要考虑编辑 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 a NOPASSWD for the command you need to run. This will only allow him to sudo 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.

春花秋月 2024-11-08 02:30:54

我只是在 Google 上搜索 php sudo shell_exec ,结果发现这是排名第一的匹配项:

http://www.php.net/manual/en/function.shell-exec.php#101440

ilya 于 linemedia dot ru 2010 年 12 月 16 日 04:36
无需将 pass 存储在文件中即可执行 sudo

system('echo "PASS" | sudo -u root -S COMMAND');

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

ilya at linemedia dot ru 16-Dec-2010 04:36
sudo can be executed without storing pass in a file

system('echo "PASS" | sudo -u root -S COMMAND');
李不 2024-11-08 02:30:54
$aux=echo "admin-pass" | your command;
echo $aux;

/********************************
************例子*************
******************************/

运行名为 my_perl_script.pl 的 Perl 脚本:

$aux=echo "admin-pass" | sudo -u root -S perl /path-to-the-script/my-perl-script.pl;
echo $aux;
$aux=echo "admin-pass" | your command;
echo $aux;

/*******************************
************Example*************
*******************************/

Run a Perl script named my_perl_script.pl:

$aux=echo "admin-pass" | sudo -u root -S perl /path-to-the-script/my-perl-script.pl;
echo $aux;
并安 2024-11-08 02:30:54

最佳方法:

$descriptorSpec = array(
    0 => STDIN,
    1 => STDOUT,
    2 => STDERR,
);

if (posix_getuid() === 0) {
    echo "Root\n";
} else {
    echo "No root\n";
    $command = 'sudo ' . PHP_BINARY . ' ' . implode(' ', $_SERVER['argv']);

    $pipes = [];
    $process = proc_open($command, $descriptorSpec, $pipes);
    if (is_resource($process)) {
        proc_close($process);
    }
}

再次运行相同的命令,并带有 sudo 前缀。

Best way to do it:

$descriptorSpec = array(
    0 => STDIN,
    1 => STDOUT,
    2 => STDERR,
);

if (posix_getuid() === 0) {
    echo "Root\n";
} else {
    echo "No root\n";
    $command = 'sudo ' . PHP_BINARY . ' ' . implode(' ', $_SERVER['argv']);

    $pipes = [];
    $process = proc_open($command, $descriptorSpec, $pipes);
    if (is_resource($process)) {
        proc_close($process);
    }
}

It runs the same command again, with sudo prefixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文