php or apache? exec, popen, system and proc_open commands do not execute any command not even ls

吃可爱长大的小学妹 提交于 2019-11-29 12:58:49

Here's another functions to add to the list :)

/**
 * 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 );
}

Example of use for your case:

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

The function above is offers more features than using the PHP ones directly. It could help you at debugging, though it's more intended for production code.

In general, this is what you should be aware off:

  • You're not under safe mode
  • Your executable does exist
  • Your executable is runnable - know that FTP clients often use ASCII/Text mode, changing CRLFs in uploaded files thus corrupting them
  • Be sure to chmod your executable at least 755

Is safe mode running? Are you trying to access files outside of safe_mode_exec_dir?

"When safe_mode is on, only executables located in the safe_mode_exec_dir will be allowed to be executed via the exec family of functions."

Are you on your own server, or a shared host? Some hosts will disable functioning of EXEC functions for security purposes.

The problem is the permissions on the directory. Every directory above yours in path must have at least read permission. Thus if you wanted to

# ls /root/test_dir

you would have to both

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

NOTE: this is a bad idea from a security POV. instead maybe:

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

note that here the 744 will apply to test_dir

anurag

//As Nemoden said use (back tick) :

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

If you do not want to store value use passthru command. exec and system command will only show one single line (data after line break is chopped off)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!