【问题标题】:PHP cannot execute local programs when accessed through a browserPHP通过浏览器访问时无法执行本地程序
【发布时间】:2021-02-07 21:14:43
【问题描述】:

总的来说,我对 PHP 和 Web 开发有点陌生,我正在尝试从 php.ini 执行一个程序。我设置了我的虚拟机并正确托管它,但是当网站尝试加载页面时,它说程序无法执行:“权限被拒绝”。我尝试了所有方法,包括:

  1. 为 /var/www/html 中的每个文件添加执行权限
  2. setfacl -m u:apache:rwx /var/www/
  3. chown -R apache:apache /var/www/

进一步的背景:

  1. 我在 fedora linux 上

这里是 index.php:

<html>
<body>                                                                                                                                                                                                                                          <p>
<?php
        exec("whoami && ./program 2>&1", $out);

        foreach ($out as $element) {
                echo $element;
                echo "<br>";
        }
?>
</p>

</body>
</html>

当我从浏览器运行它时,输出如下:

[fedora@fedora html]$ curl localhost
<html>
<body>

<p>
apache<br>sh: ./program: Permission denied<br></p>

</body>
</html>

互联网上的大多数提示都告诉您确保用户“apache”有权执行该文件。我已经做到了,并通过执行以下命令进行了验证。

[fedora@fedora html]$ sudo -u apache bash
bash-5.0$ ls -la
total 48
drwxrwxrwx. 2 apache apache  4096 Oct 25 11:22 .
drwxrwxrwx+ 4 apache apache  4096 Oct 25 09:51 ..
-rw-rw-rw-. 1 apache apache   158 Oct 25 11:04 index.php
-rwxrwxrwx+ 1 apache apache 20648 Oct 25 10:02 program
-rwxr-xr-x. 1 apache apache    23 Oct 25 10:57 p.sh
bash-5.0$ ./program
test
test
test
test
test
test
test
test
test
bash-5.0$ php index.php
<html>
<body>

<p>
apache<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br></p>

</body>
</html>
bash-5.0$

我 sudo 进入了 apache 用户,并且能够读取目录中的每个文件并通过 shell 执行它们。之后,我运行了 php index.php,它完美地执行了脚本,并得到了 exepcted 的输出。

我完全不知道为什么它不能在浏览器中执行这个脚本,任何帮助将不胜感激!

编辑: 我检查了我的 php.ini 文件,没有禁用的功能

...
disable_functions =
...

【问题讨论】:

标签: php web exec


【解决方案1】:

我检查了我的 SELinux 配置,但脚本没有设置正确的权限。当它们需要 httpd_sys_script_exec_t 时,它们被设置为 httpd_sys_content_t。

简单的解决方法是:

chcon -R -t httpd_sys_script_exec_t program

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 2015-06-27
    • 2015-03-09
    • 1970-01-01
    • 2019-01-09
    • 2022-11-17
    • 2014-08-20
    • 2021-05-23
    相关资源
    最近更新 更多