【问题标题】:Running shell script as root using apache/php使用 apache/php 以 root 身份运行 shell 脚本
【发布时间】:2013-07-26 06:42:30
【问题描述】:

我想使用由 执行的shell 脚本更改目录的权限,因此它在apache 用户下运行。 不幸的是,有些目录的权限设置为root

有没有办法以root 运行此脚本,或者我可以考虑其他替代方案吗?

【问题讨论】:

    标签: php php shell permissions root


    【解决方案1】:

    您正在寻找名为 sudo: https://en.wikipedia.org/wiki/Sudo 的工具,但最有可能的是,您应该重新考虑您的应用架构,而不是使用 sudo,因为一旦您的 httpd 受到攻击,攻击者就可以轻松获得 root 权限。

    【讨论】:

    • 嗯,我决定使用 cgi 脚本 suid。感谢您的回复。
    【解决方案2】:

    我最近发布了一个项目,它允许 PHP 获取真实的 Bash shell 并与之交互。在这里获取:https://github.com/merlinthemagic/MTS

    下载后您只需使用以下代码:

        $shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
        $return1  = $shell->exeCmd("/path/to/script");
        //the return will be a string containing the return of the script
        echo $return1;
    

    或者您可以简单地绕过脚本,直接在 PHP 中通过 shell 发出原始命令。这种方法将允许您处理异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-21
      • 2011-03-10
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多