1. 程式人生 > 其它 >【[HCTF 2018]WarmUp 1】BUUCTF

【[HCTF 2018]WarmUp 1】BUUCTF

這個題主要是考察程式碼審計,開啟容器

 

 

 就一張滑稽表情包,我們先檢視網頁原始碼

發現有一點被註釋掉了,嘗試訪問這個檔案

<?php
    highlight_file(__FILE__);
    class emmm
    {
        public static function checkFile(&$page)
        {
            $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
            if (! isset($page) || !is_string($page)) {  //is_string()

 函式用於檢測變數是否是字串。
                echo "you can't see it";
                return false;
            }

            if (in_array($page, $whitelist)) {  //in_array() 函式搜尋陣列中是否存在指定的值。
                return true;
            }

            $_page = mb_substr(   //mb_substr() 函式返回字串的一部分,之前我們學過 substr() 函式,它只針對英文字元,如果要分割的中文文字則需要使用 mb_substr()。

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

            $_page = urldecode($page);    //
urldecode():解碼已編碼的 URL 字串

            $_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'])  //empty()

 函式用於檢查一個變數是否為空。
        && is_string($_REQUEST['file'])  //is_string() 函式用於檢測變數是否是字串。
        && emmm::checkFile($_REQUEST['file'])
    ) {
        include $_REQUEST['file'];
        exit;
    } else {
        echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
    }  
?>

看到如上圖的PHP程式碼

 

 可以看到這個地方有兩個PHP,我們已經用了一個,那麼就先嚐試一下另一個

 

 這樣我們就知道了flag所在的地方

我們開始直接嘗試查詢flag

.代表當前目錄..代表上級目錄

 

 

 

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