Wargame/websec.fr

websec.fr easy level 22

JeonYoungSin 2019. 8. 22. 20:43

source.php


<?php

include "flag.php";

class 
Flag {
    public function 
__destruct() {
       global 
$flag;
       echo 
$flag
    }
}

function 
sanitize($data) {
    
/* i0n1c's bypass won't save you this time! (https://www.exploit-db.com/exploits/22547/) */
    
if ( ! preg_match ('/[A-Z]:/'$data)) {
        return 
unserialize ($data);
    }

    if ( ! 
preg_match ('/(^|;|{|})O:[0-9+]+:"/'$data )) {
        return 
unserialize ($data);
    }

    return 
false;
}

$data = Array();
if (isset (
$_COOKIE['data'])) {
    
$data sanitize (base64_decode ($_COOKIE['data']));
}

if (isset (
$_POST['value']) and ! empty ($_POST['value'])) {
    
/* Add a value twice to remove it from the list. */
    
if (($key array_search ($_POST['value'], $data)) !== false) {
        unset (
$data[$key]);
    } else { 
/* Else, simply add it. */
        
array_push ($data$_POST['value']);
    }
    
setcookie ('data'base64_encode (serialize ($data)));
}

?>

<!DOCTYPE html>
<html>
<head>
        <title>#WebSec Level Twenty</title>
        <link rel="stylesheet" href="../static/bootstrap.min.css" />
    <!-- Thanks to XeR for helping debugging this level. -->
</head>
<body>
    <div id="main">
        <div class="container">
            <div class="row">
                <h1>LevelTwenty <small> - Call me maybe</small></h1>
            </div>
            <div class="row">
                <p class="lead">
                    Since there is nothing that a good blacklist can't fix,
                    we're using <a href="https://secure.php.net/manual/en/function.unserialize.php">unserialize</a>
                    with a <b>bullet-proof</b> one for our amazing todo-list.<br>
                    You can get the sources <a href="source.php">here</a>.
                </p>
            </div>
        </div>
    <br>
        <div class="container">
            <div class="row">
                <form class="form-inline col-md-3" method='post'>
                    <input name='value' id='value' class='form-control' type='text' placeholder='Item'>
                    <input class="form-control btn btn-default" name="submit" value='Add' type='submit'>
                </form>
            </div>
            <div class="row col-md-3">
        <br>
                <ul class="list-group">
                <?php 
                    
foreach ($data as $value)
                        print 
'<li class="list-group-item">' htmlentities($value) . '</li>';
                
?>
                </ul>
            </div>
        </div>
    </div>
</body>
</html>


PHP Sandbox 인데 길이제한이 좀 빡세다. 대충 아래와 같은형태로하면 함수하나에 인자는 6글자까지 사용할수있게 만들었다.


http://websec.fr/level22/index.php?code=${~'%A0%B8%BA%AB'}{0}()&0=phpinfo


그냥 system류로 경로따고 cat f* 이런식으로하면 될 것 같았는데 system류 함수가 다 막혀있었다. 


그래서 그냥 scandir, glob같은거로 경로따서 플래그파일 보면 되겠지 했는데 플래그 파일명이 file_containing_the_flag_parts.php 요놈인데 엄청길다. 


뭐지하고 좀 보다보니 이상한게 있었는데 A라는 객체를 생성해서 필드에 값을 채워넣는 문제 로직이랑 전혀 상관없는 코드가 있었다. 해당 객체 필드를 못보게 -를 필터하고 unset까지해서 객체 인자로 들어가는 변수까지 초기화한걸 보니 저 객체만 어떻게 뿌려주면 될 것 같아서 그냥 var_dump로 객체를 뿌려주니 필드쪽에 플래그가 나눠서 세팅되어 있었다.


payload


http://websec.fr/level22/index.php?code=${~'%A0%B8%BA%AB'}{0}($a)&0=var_dump