๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[Dreamhack]WebHacking/Wargame&CTF

[Dreamhack] Level1: Type c-j

by Yun2๐Ÿ‘ 2024. 2. 2.
๋ฐ˜์‘ํ˜•

๐Ÿ›Ž๏ธ Access

php๋กœ ์ž‘์„ฑ๋œ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.
์•Œ๋งž์€ Id๊ณผ Password๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•˜์„ธ์š”.
ํ”Œ๋ž˜๊ทธ์˜ ํ˜•์‹์€ DH{…} ์ž…๋‹ˆ๋‹ค.

 

 

๐Ÿ‘พ Exploit Algorithm & Payload

> index.php

๋”๋ณด๊ธฐ
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>Type c-j</title>
</head>
<body>
    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="/">Type c-j</a>
        </div>
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="/">index page</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav><br/><br/><br/>
    <div class="container">
      <div class="box">
      <h4>Enter the correct ID & Password.</h4>
        <p>
          <form method="post" action="/check.php">
              <input type="text" placeholder="Id" name="input1">
              <input type="text" placeholder="Password" name="input2">
              <input type="submit" value="์ œ์ถœ">
          </form>
        </p>
      </div>

    <?php
        require_once('flag.php');
        error_reporting(0);
    ?> 
    </div> 
</body>
</html>

> check.php

๋”๋ณด๊ธฐ
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>Type c-j</title>
</head>
<body>
    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="/">Type c-j</a>
        </div>
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="/">Index page</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav><br/><br/><br/>
    <div class="container">
    <?php
    function getRandStr($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
    
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[mt_rand(0, $charactersLength - 1)];
        }
        return $randomString;

    }
    require_once('flag.php');
    error_reporting(0);
    $id = getRandStr();
    $pw = sha1("1");
    // POST request
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      $input_id = $_POST["input1"] ? $_POST["input1"] : "";
      $input_pw = $_POST["input2"] ? $_POST["input2"] : "";
      sleep(1);

      if((int)$input_id == $id && strlen($input_id) === 10){
        echo '<h4>ID pass.</h4><br>';
        if((int)$input_pw == $pw && strlen($input_pw) === 8){
            echo "<pre>FLAG\n";
            echo $flag;
            echo "</pre>";
          }
        } else{
          echo '<h4>Try again.</h4><br>';
        }
      }else {
      echo '<h3>Fail...</h3>';
     }
    ?> 
    </div> 
</body>
</html>

 

 

 

#1


: '/' ํŽ˜์ด์ง€์—์„œ id์™€ password๋ฅผ ์ž…๋ ฅ ํ›„ ์ œ์ถœํ•˜๋ฉด ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค.

: ๊ทธ๋Ÿฌ๋‚˜ ์–ด๋–ค ๊ฐ’์„ ์ž…๋ ฅํ•˜์—ฌ๋„ "Try again."์ด๋ผ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋˜์–ด์„œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ–ˆ๋‹ค.

 

 

#2


...
$id = getRandStr();
$pw = sha1("1");
...

: id๋Š” getRandStr() ํ•จ์ˆ˜๋ฅผ ๋ถ„์„ํ•˜๋ฉด ๋  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

: pw๋Š” sha1 ํ•จ์ˆ˜๋Š” SHA-1 16์ง„์ˆ˜ 40์ž๋ฆฌ๋กœ ๋ Œ๋”๋ง๋˜๋Š” ์•”ํ˜ธํ™” ํ•ด์‰ฌ ํ•จ์ˆ˜๋กœ ๋ณด์ธ๋‹ค.

 

function getRandStr($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';

         for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[mt_rand(0, $charactersLength - 1)];
        }
        return $randomString;
}
require_once('flag.php');
error_reporting(0);   

$id = getRandStr();   
$pw = sha1("1");   

// POST request   
if ($_SERVER["REQUEST_METHOD"] == "POST") {
      $input_id = $_POST["input1"] ? $_POST["input1"] : "";
      $input_pw = $_POST["input2"] ? $_POST["input2"] : "";
      sleep(1);

      if((int)$input_id == $id && strlen($input_id) === 10){
        echo '<h4>ID pass.</h4><br>';
        if((int)$input_pw == $pw && strlen($input_pw) === 8){
            echo "<pre>FLAG\n";
            echo $flag;
            echo "</pre>";
          }
        } else{
          echo '<h4>Try again.</h4><br>';
        }
      }else {
      echo '<h3>Fail...</h3>';     
}

: ๋ถ„์„ ๊ฒฐ๊ณผ if((int)$input_id == $id && strlen($input_id) === 10), $input_id๋Š” (int)๋ฅผ ํ†ตํ•ด ์ •์ˆ˜๋กœ ํ˜•๋ณ€ํ™˜ ๋œ ํ›„์— $id์™€ ๋น„๊ต๋˜๊ณ , ๊ธธ์ด๋Š” 10์ด๋‹ค.

: ์—ฌ๊ธฐ์„œ PHP ์—์„œ์˜ ์—ฐ์‚ฐ์ž ์ทจ์•ฝ์  '==' ์ด ์ทจ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.(Loose Comparison)

: '==' ์—ฐ์‚ฐ์ž๋Š” PHP๋ฌธ์—์„œ $a์™€ $b๊ฐ€ ๊ฐ™์œผ๋ฉด true, '===' ์—ฐ์‚ฐ์ž๋Š” $a์™€ $b๊ฐ€ ๊ฐ™๊ณ , ๋™์ผํ•œ ์ž๋ฃŒํ˜•์ผ ์‹œ true

: ์ฆ‰, '==' ์—ฐ์‚ฐ์ž๋Š” ๋ฌธ์ž์—ด๊ณผ ์ •์ˆ˜๋ฅผ ๋น„๊ตํ–ˆ์„ ๋•Œ, ๋ฌธ์ž์—ด์ด ์ •์ˆ˜(int) ํ˜น์€ ์‹ค์ˆ˜(float)์œผ๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.

 

 

๐Ÿ”‘Analysis and results for obtaining the Flag DH{…}


// ๊ธธ์ด 10
// 0e ๋’ค๋กœ ์ˆซ์ž ๋žœ๋คํ•˜๊ฒŒ ๊ฐ€๋Šฅ
// ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ํ˜• ๋ณ€ํ™˜ํ•  ๋•Œ๋Š” ๋ฌธ์ž์—ด์˜ ์•ž ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ์ˆซ์ž๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋ณ€ํ™˜
(int) $input_id = 0e12345678 → 0 * ... → 0
(int) $input_id = 0e1234567a → 0 * ... → 0
...

: ์ด๋ฅผ ํ™œ์šฉํ•ด ๋งค์งํ•ด์‰ฌ(0e: ์ด๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด ๋’ค๊ฐ€ ๋ชจ๋‘ ์ˆซ์ž๋ฉด floatํ˜•ํƒœ๋กœ ์ธ์‹ํ•œ๋‹ค)๋ฅผ '==' ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ ์šฉํ•  ๊ฒฝ์šฐ ์ทจ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

// ๊ธธ์ด 8
$input_pw = 356A192B

: $pw์˜ ๊ฒฝ์šฐ๋Š” https://www.convertstring.com/ko/Hash/SHA1 ํ•ด๋‹น ์‚ฌ์ดํŠธ์—์„œ SHA-1 ํ•ด์‹œํ•˜๊ณ ์ž ํ•˜๋Š” ํ…์ŠคํŠธ์ธ "1"์„ ์ž…๋ ฅํ•˜์˜€๋”๋‹ˆ "356A192B7913B04C54574D18C28D46E6395428AB" ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค.

 

๋ฐ˜์‘ํ˜•

'[Dreamhack]WebHacking > Wargame&CTF' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Dreamhack] Level1:Beginner blue-whale  (1) 2024.02.07
[Dreamhack] Level2: Dream Gallery  (0) 2024.02.03
[Dreamhack] Level2: filestorage  (0) 2024.02.02
[Dreamhack] Level1: baby-union  (2) 2024.02.02
[Dreamhack] CTF Season 5 Round #2 - php7cmp4re  (0) 2024.01.28