CTF代码审计之[HCTF 2018]WarmUp

[HCTF 2018]WarmUp

参考链接 https://www.jianshu.com/p/36eaa95068ca

<?php
    highlight_file(__FILE__);
    class emmm
    {
        public static function checkFile(&$page)
        {
            $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
            if (! isset($page) || !is_string($page)) {
                echo "you can't see it";
                return false;
            }

            if (in_array($page, $whitelist)) {
                return true;
            }

            $_page = mb_substr(
                $page,
                0,
                mb_strpos($page . '?', '?')
            );
            if (in_array($_page, $whitelist)) {
                return true;
            }

            $_page = urldecode($page);
            $_page = mb_substr(
                $_page,
                0,
                mb_strpos($_page . '?', '?')
            );
            if (in_array($_page, $whitelist)) {
                return true;
            }
            echo "you can't see it";
            return false;
        }
    }

    if (! empty($_REQUEST['file'])
        && is_string($_REQUEST['file'])
        && emmm::checkFile($_REQUEST['file'])
    ) {
        include $_REQUEST['file'];
        exit;
    } else {
        echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
    }  
?>

一、在 hint.php  中我们可以知道 flag 在 ffffllllaaaagggg

 

二、审计下面一部分代码

 if (! empty($_REQUEST['file'])  //$_REQUEST['file']值非空
        && is_string($_REQUEST['file'])  //$_REQUEST['file']值为字符串
        && emmm::checkFile($_REQUEST['file'])  //能够通过checkFile函数校验
    ) {
        include $_REQUEST['file'];  //包含$_REQUEST['file']文件
        exit;
    } else {
        echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
        //打印滑稽表情
    }  

可以知道要求传入的 file变量的值需要满足:非空,字符串,能通过 checkFile()函数

上面的checkFile函数有四个检测 

    highlight_file(__FILE__); //打印代码
    class emmm  //定义emmm类
    {
        public static function checkFile(&$page)//将传入的参数赋给$page
        {
            $whitelist = ["source"=>"source.php","hint"=>"hint.php"];//声明$whitelist(白名单)数组
            if (! isset($page) || !is_string($page)) {//若$page变量不存在或非字符串
                echo "you can't see it";//打印"you can't see it"
                return false;//返回false
            }

            if (in_array($page, $whitelist)) {//若$page变量存在于$whitelist数组中
                return true;//返回true
            }

            $_page = mb_substr(//该代码表示截取$page中'?'前部分,若无则截取整个$page
                $page,
                0,
                mb_strpos($page . '?', '?')
            );
            if (in_array($_page, $whitelist)) {
                return true;
            }

            $_page = urldecode($page);//url解码$page
            $_page = mb_substr(
                $_page,
                0,
                mb_strpos($_page . '?', '?')
            );
            if (in_array($_page, $whitelist)) {
                return true;
            }
            echo "you can't see it";
            return false;
        }
    }

分别表示 判断是否为字符串 ,判断$page是否在$whitelist数组中 ,截取$page中'?'前一部分判断是否$whitelist数组中 ,判断url解码并截取后的$page是否存在于$whitelist中

在第四个if语句中,我们可以通过先对?进行2次url编码,此时服务端解码一次,checkFile函数解码一次,结果仍是'?',可以绕过

构造url:http://7e76bca4-7e05-434e-943d-daedb85da0a6.node3.buuoj.cn/source.php?file=source.php%253f../../../../../ffffllllaaaagggg

得到flag

 
posted @ 2020-09-08 12:04  airtail  阅读(341)  评论(0编辑  收藏  举报