【问题标题】:php or apache? exec, popen, system and proc_open commands do not execute any command not even lsphp还是apache? exec、popen、system 和 proc_open 命令不执行任何命令,甚至 ls
【发布时间】:2011-08-06 03:14:42
【问题描述】:

我的问题是从我的 php 主页执行任何命令

解决了一半,我的 pythonscript 正在运行,但是 pythonscript 没有执行其模块的权限

但我开始认为问题出在 apache 而不是 php?我试图运行一个实际上是我最终要运行的pythonscript,然后它似乎运行但当脚本试图访问一个看起来像这样的txt文件时停止

[stdout] =>
[stderr] => Traceback (most recent call last): 
 File "pythontest.py", line 4, in ? f = open("(path)/temp.txt", "w") 
 IOError: [Errno 13] Permission denied: '(path)/temp.txt'

我已经尝试了所有不同类型的执行方法,我可以找到 exec、system、popen、proc_open 几乎没有运行,我可以从 system("pwd") 获取字符串但 system("ls") 返回一个数字 127或 126。我还尝试运行已编译的 c 程序(代码如下) exec 将返回一个对象“Array”,但系统只会像 ls 一样返回 127 或 126。

我还试图从“数组”对象中获取值?从 exec 但它什么也不返回,如果我 php print_R Array 页面将不会加载。

php 输出是(下面的代码)-> a: , out1: 126, t[[0]: , strlen out: 5, out: Array, t: 不管我实际提出了多少论据!

ls 的完整路径为 126,而“ls”则为数字 127

我已测试将所有文​​件 chmod 为完全权限,从 page.php 到测试程序,但它仍然给出相同的输出

php 安全模式已关闭

echo '<pre>';
print_r(execute('ls'))
echo '</pre>';

返回

array
(
    [stdout] => 
    [stderr] => sh: /var/www/html/grndb/upscgenesearch/test1: Permission denied

    [return] => 126
)

为什么当 test1 程序具有完全权限时它给我 Permission denied ?

<?php
$a = system("/bin/ls",$out1);
echo "a: "; echo $a;
echo ", out1: "; echo $out1;
$t = exec('pwd/test1 aa hh 11',$out);

foreach ($t as &$value) {
    echo ($value);
}

echo ", t[[0]: "; echo $t[0]; 
echo ", strlen out: "; echo strlen($out);
echo ", out: "; echo $out; 
echo ", t: "; echo $t;
?>

C 源代码

int main(int argc, char *argv[])
{
int i;

for(i = 0; i < argc; i++)
printf("arg %d: %s\n", i, argv[i]);
return 0;
}

【问题讨论】:

  • 什么 echo 'ls';生产? (用反引号替换')
  • 如果/bin/ls 的“command not found”结果仍然存在,那么您很可能在 mod_chroot/jail 中运行您的网络服务器。 (实际上是一次有效的安全预防措施。)
  • 网络服务器是默认安装的,不知道会不会有问题?
  • 如果有帮助的话,网络服务器是 apache/2.2.3

标签: php apache command


【解决方案1】:

这是要添加到列表中的另一个函数:)

/**
 * Executes a program and waits for it to finish, taking pipes into account.
 * @param string $cmd Command line to execute, including any arguments.
 * @param string $input Data for standard input.
 * @return array Array of "stdout", "stderr" and "return".
 * @copyright 2011 K2F Framework / Covac Software
 */
function execute($cmd,$stdin=null){
    $proc=proc_open($cmd,array(0=>array('pipe','r'),1=>array('pipe','w'),2=>array('pipe','w')),$pipes);
    fwrite($pipes[0],$stdin);                      fclose($pipes[0]);
    $stdout=stream_get_contents($pipes[1]);        fclose($pipes[1]);
    $stderr=stream_get_contents($pipes[2]);        fclose($pipes[2]);
    $return=proc_close($proc);
    return array( 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$return );
}

您的案例的使用示例:

echo '<pre>';
print_r(execute('ls'));
echo '</pre>';

上面的函数提供了比直接使用 PHP 更多的功能。它可以帮助您进行调试,尽管它更适用于生产代码。

一般来说,这是你应该注意的:

  • 您未处于安全模式
  • 您的可执行文件确实存在
  • 您的可执行文件可运行的 - 知道 FTP 客户端经常使用 ASCII/文本模式,更改上传文件中的 CRLF 从而损坏它们
  • 确保对您的可执行文件进行 chmod 至少 755

【讨论】:

  • Array ([stdout] => [stderr] => sh: ls: command not found [return] => 127)
  • 这是你的函数的输出,如果为 ls 给出完整路径它返回 126
  • 退出代码 127 已经是什么了。 126 stdout/stderr 返回了什么?
  • @DavidS:如果显示“找不到命令”,请检查print_r($_SERVER) 中的PATH 变量。或者像你建议的那样尝试/bin/ls
  • 当我使用 bin/ls 时,答案是 Array ( [stdout] => [stderr] => sh: /bin/ls: Permission denied [return] => 126 )
【解决方案2】:

是否正在运行安全模式?您是否尝试访问 safe_mode_exec_dir 之外的文件?

“当 safe_mode 开启时,只有位于 safe_mode_exec_dir 中的可执行文件才能通过 exec 系列函数执行。”

您是在自己的服务器上,还是在共享主机上?出于安全考虑,某些主机会禁用 EXEC 功能。

【讨论】:

  • 没有关闭安全模式,但我没有从 safe_mode_exec_dir 运行任何程序
【解决方案3】:

问题在于目录的权限。 路径中你上面的每个目录都必须有 至少读取权限。因此,如果你想

# ls /root/test_dir

你必须两个

# chmod 777 /root/test_dir
# chmod 777 /root

注意:从安全 POV 来看,这是一个坏主意。 而是可能:

# mkdir /public
# chmod 744 /public
# mv /root/testdir /public

请注意,这里的 744 将应用于 test_dir

【讨论】:

    【解决方案4】:

    //正如尼莫登所说的使用(反勾号):

    $cmd='ls /var/www/';
    $variable_name=`$cmd`;
    

    如果您不想存储值,请使用 passthru 命令。 exec 和 system 命令将只显示一行(断行后的数据被切断)

    【讨论】:

    • 你到底想做什么?这将导致错误,因为没有名为 $cmd 的命令。
    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 2017-05-06
    • 2019-11-17
    • 2012-08-18
    • 2013-07-28
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多