【代码审计---PHP】[HCTF 2018]WarmUp---day01

题目

一、页面

image
进来之后,滑稽脸

二、查看源码

image
有个提示source.php进去看一下

 <?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\" />";
    }  
?> 

三、源码分析

首先看下面这块
image
可以看出是要通过file变量来传数据,方式是$_REQUEST。
image
可以接受get、post、cookie的请求。
然后先判断,满足三个条件:

  • file不为空且为字符串
  • file要通过emmm类的 checkFile()函数

补充:emmm :: checkFile()

参考:
https://www.php.cn/php-weizijiaocheng-378631.html

image
image

四、checkFile()分析

1、mb_substr()

image

2、mb_strpos

这个函数首先是做了一个连接,把$page和"?"连接,然后再查找"?"的位置,就是看一下字符串长度呗。
另外补充:

PHP中.=和+=是什么意思详解
//.=通俗的说,就是累积。
//比如:
$a = 'a'; //赋值
$b = 'b'; //赋值
$c = 'c'; //赋值
$c .= $a;
$c .= $b;

echo $c; 就会显示 cab

image
image

五、解题思路

经过上过程可以看出,截取了两次?,然后最后一次再次判断是否在白名单里,满足条件的话就返回真,然后包含文件file得到flag。
在代码那里有个hint.php
image
暗示flag在../../../../../ffffllllaaaagggg中。
构造payload:

index.php?file=hint.php?../../../../../ffffllllaaaagggg
posted @ 2021-05-17 15:04  DarkerG  阅读(85)  评论(0编辑  收藏  举报