SWLUG/CTF ๋ฌธ์ œ ํ’€์ด (2)

[Dreamhack/๋“œ๋ฆผํ•ต] PHPreg

waterproof 2024. 5. 7. 03:38

 

 

 

https://dreamhack.io/wargame/challenges/873

 

phpreg

Description php๋กœ ์ž‘์„ฑ๋œ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. ์•Œ๋งž์€ Nickname๊ณผ Password๋ฅผ ์ž…๋ ฅํ•˜๋ฉด Step 2๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Step 2์—์„œ system() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•˜์„ธ์š”. ํ”Œ๋ž˜๊ทธ๋Š” ../dream/flag.txt์— ์œ„์น˜ํ•ฉ๋‹ˆ

dreamhack.io


[1] ๋ฌธ์ œ ํ’€์ด

 

 

๋ฌธ์ œ ์„ค๋ช…์„ ๋‹ค์‹œ ๋ณด์ž๋ฉด,

 

์ด๋Ÿฌํ•˜๋‹ค!

 

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ php๋กœ ์ž‘์„ฑํ•œ ํŽ˜์ด์ง€์— ์ ‘์†์„ ํ•˜๋ฉด, Ncickname๊ณผ Password๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ํ™”๋ฉด์ด ๋‚˜์˜จ๋‹ค.

๋จผ์ € ์•Œ๋งž์€ Nickname๊ณผ Password๋ฅผ ์ž…๋ ฅํ•˜์—ฌ Step 2๋กœ ๋„˜์–ด๊ฐ€๋ณด์ž!

 

 

Step 1 ํŽ˜์ด์ง€์˜ ์†Œ์Šค์ฝ”๋“œ ํŽ˜์ด์ง€๋ฅผ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ ๋ณ„ ๊ฒŒ ์—†์–ด์„œ ๋ฌธ์ œ ํŒŒ์ผ์„ ์‚ดํŽด๋ณด์•˜๋‹ค,

 

 

index.php๋Š” ์œ„์˜ ๋ฌธ์ œ ํŽ˜์ด์ง€ ์†Œ์Šค์ฝ”๋“œ ํŒŒ์ผ๊ณผ ๋‚ด์šฉ์ด ๋˜‘๊ฐ™์•˜๋‹ค.

 

๋‹ค์Œ์€ step2.php ํŒŒ์ผ์˜ ๋‚ด์šฉ์ด๋‹ค.

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>PHPreg</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="/">PHPreg</a>
      </div>
      <div id="navbar">
        <ul class="nav navbar-nav">
          <li><a href="/">Step 1</a></li>
          <li><a href="/step2.php">Step 2</a></li>
        </ul>
      </div><!--/.nav-collapse -->
    </div>
  </nav><br/><br/><br/>
  <div class="container">
    <div class="box">
      <!-- PHP code -->
      <?php
          // POST request
          if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $input_name = $_POST["input1"] ? $_POST["input1"] : "";
            $input_pw = $_POST["input2"] ? $_POST["input2"] : "";

            // pw filtering
            if (preg_match("/[a-zA-Z]/", $input_pw)) {
              echo "alphabet in the pw :(";
            }
            else{
              $name = preg_replace("/nyang/i", "", $input_name);
              $pw = preg_replace("/\d*\@\d{2,3}(31)+[^0-8\"]\!/", "d4y0r50ng", $input_pw);
              
              if ($name === "dnyang0310" && $pw === "d4y0r50ng+1+13") {
                echo '<h4>Step 2 : Almost done...</h4><div class="door_box"><div class="door_black"></div><div class="door"><div class="door_cir"></div></div></div>';

                $cmd = $_POST["cmd"] ? $_POST["cmd"] : "";

                if ($cmd === "") {
                  echo '
                        <p><form method="post" action="/step2.php">
                            <input type="hidden" name="input1" value="'.$input_name.'">
                            <input type="hidden" name="input2" value="'.$input_pw.'">
                            <input type="text" placeholder="Command" name="cmd">
                            <input type="submit" value="์ œ์ถœ"><br/><br/>
                        </form></p>
                  ';
                }
                // cmd filtering
                else if (preg_match("/flag/i", $cmd)) {
                  echo "<pre>Error!</pre>";
                }
                else{
                  echo "<pre>--Output--\n";
                  system($cmd);
                  echo "</pre>";
                }
              }
              else{
                echo "Wrong nickname or pw";
              }
            }
          }
          // GET request
          else{
            echo "Not GET request";
          }
      ?>
    </div>
  </div>

  <style type="text/css">
    h4 {
      color: rgb(84, 84, 84);
    }
    .box{
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }
    pre {
      width: 80%;
    }
    .door_box {
      position: relative;
      width: 240px;
      height: 180px;
      margin: 20px 0px;
    }
    .door_black {
      position: absolute;
      width: 140px;
      height: 180px;
      background-color: black;
      border-radius: 10px;
      right:0px;
    }
    .door {
      z-index: 2;
      position: absolute;
      width: 140px;
      height: 180px;
      background-color: #b9abf7;
      border-radius: 10px;
      right: 100px;
    }
    .door_cir{
      z-index: 3;
      position: absolute;
      border-radius: 50%;
      width: 20px;
      height: 20px;
      border: 2px solid rgba(255, 222, 113, 0.873);
      background-color: #ffea98;
      top: calc( 180px / 2 - 10px );
      right: 10px;
    }
  </style>
</body>
</html>

 

 

"<?php"๋กœ ์‹œ์ž‘ํ•˜๋Š” php ๋ฌธ ์‹œ์ž‘ ๋ถ€๋ถ„์ด ๋ˆˆ์— ๋ˆ๋‹ค.

 

 

 

๋‹ค์Œ์€ step2.php ํŒŒ์ผ ๋‚ด์šฉ์˜ ์ผ๋ถ€์ด๋‹ค.

<?php
          // POST request
          if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $input_name = $_POST["input1"] ? $_POST["input1"] : "";
            $input_pw = $_POST["input2"] ? $_POST["input2"] : "";

            // pw filtering
            if (preg_match("/[a-zA-Z]/", $input_pw)) {
              echo "alphabet in the pw :(";
            }
            else{
              $name = preg_replace("/nyang/i", "", $input_name);
              $pw = preg_replace("/\d*\@\d{2,3}(31)+[^0-8\"]\!/", "d4y0r50ng", $input_pw);
              
              if ($name === "dnyang0310" && $pw === "d4y0r50ng+1+13") {
                echo '<h4>Step 2 : Almost done...</h4><div class="door_box"><div class="door_black"></div><div class="door"><div class="door_cir"></div></div></div>';

 

 

php ๋ฌธ๋ฒ•์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด, preg match() ํ•จ์ˆ˜์™€ preg_replace() ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด์•˜๋‹ค.

 

 


โญ preg_match() ํ•จ์ˆ˜๋ž€?

์ฃผ์–ด์ง„ ์ •๊ทœ ํ‘œํ˜„์‹ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๋‚ด์—์„œ ์ผ์น˜(match)ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ฐพ์•„๋‚ด๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

ํŠน์ง•

- ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์„ ๋ฌธ์ž์—ด์—์„œ ์ฒ˜์Œ์œผ๋กœ ๋ฐœ๊ฒฌํ•˜๋ฉด ๊ฒ€์ƒ‰์„ ์ค‘๋‹จํ•˜๊ณ  1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

- ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์„ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

- ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

- ์ด๋ฉ”์ผ ์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ, URL๊ณผ ๊ฐ™์€ ํŠน์ • ํŒจํ„ด์˜ ๋ฌธ์ž์—ด์„ ๊ฒ€์‚ฌํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

- ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฐ์—๋„ ์‚ฌ์šฉํ•œ๋‹ค.

- ์ฃผ์˜ํ•  ์ : ๊ธฐ๋ณธ์ ์œผ๋กœ preg_match() ํ•จ์ˆ˜๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค. ๋งŒ์•ฝ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ์‹ถ๋‹ค๋ฉด ์ •๊ทœ ํ‘œํ˜„์‹์— /i ํ”Œ๋ž˜๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

php_match() ํ•จ์ˆ˜์˜ ์˜ˆ์‹œ

// ์ „ํ™”๋ฒˆํ˜ธ ์ถœ๋ ฅ

$text = '๋ฌธ์ž์—ด์—์„œ ์ „ํ™”๋ฒˆํ˜ธ๋Š” 010-1234-5678๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.';
$pattern = "/\b(?:\d{2,3}[-.])?\d{3,4}[-.]\d{4}\b/";

if (preg_match($pattern, $text, $matches)) {
    $phoneNumber = $matches[0];
    echo '์ถ”์ถœ๋œ ์ „ํ™”๋ฒˆํ˜ธ: ' . $phoneNumber;
} else {
    echo '์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.';
}

// ์ถœ๋ ฅ: '์ถ”์ถœ๋œ ์ „ํ™”๋ฒˆํ˜ธ: 010-1234-5678'

 


 

 

๋‹ค์‹œ ๋Œ์•„์™€์„œ, step2.php์—์„œ ์‚ฌ์šฉ๋œ preg_match ํŒŒ์ผ์„ ๋ถ„์„ํ•ด๋ณด์ž.

 

if (preg_match("/[a-zA-Z]/", $input_pw)) {
              echo "alphabet in the pw :(";
            }

 

 

์—ฌ๊ธฐ์—์„œ ์‚ฌ์šฉ๋œ ์ •๊ทœ ํ‘œํ˜„์‹์€ /[a-zA-Z]/ ์ด๋‹ค.

[a-zA-Z]๋Š” ์†Œ๋ฌธ์ž a๋ถ€ํ„ฐ z๊นŒ์ง€ ๋˜๋Š” ๋Œ€๋ฌธ์ž A๋ถ€ํ„ฐ Z๊นŒ์ง€์˜ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ด ์ •๊ทœ ํ‘œํ˜„์‹์€ ๋ฌธ์ž์—ด์— ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ, ๋งŒ์•ฝ $input_pw ๋ณ€์ˆ˜์— ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด, "alphabet in the pw :("๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ™•์ธ์„ ์œ„ํ•ด 'admin', 'admin'์„ ์ž…๋ ฅํ•ด๋ณด์•˜๋‹ค.

 

 

 

์œ„์—์„œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์˜ˆ์ƒํ•œ ๊ฒฐ๊ณผ๋Œ€๋กœ ๋‚˜์™”๋‹ค.

 

์ด์ œ, preg_replace() ํ•จ์ˆ˜๋ฅผ ๋ถ„์„ํ•ด๋ณด์ž.

 


โญpreg_replace() ํ•จ์ˆ˜๋ž€?

preg_replace() ํ•จ์ˆ˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์น˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณด๋‹ค ๋ณต์žกํ•œ ์น˜ํ™˜์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์‚ฌ์šฉ๋˜๋Š” ์ธ์ˆ˜

$pattern ์ฐพ์„ ์ •๊ทœ ํ‘œํ˜„์‹
$replacement ์น˜ํ™˜ํ•  ๋ฌธ์ž์—ด
$subject ๋Œ€์ƒ ๋ฌธ์ž์—ด
$flags ์ •๊ทœ ํ‘œํ˜„์‹ ํ”Œ๋ž˜๊ทธ

 

 

์‚ฌ์šฉ ์˜ˆ์‹œ

// ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž๋ฅผ ๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ์น˜ํ™˜
$str = "Hello, world!";

// "Hello"๋ฅผ "Goodbye"๋กœ ์น˜ํ™˜
$str = str_replace("Hello", "Goodbye", $str);

echo $str; // Goodbye, world!

// ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐ
$str = "Hello, world!";

// "Hello"๋ฅผ ์ œ๊ฑฐ
$str = str_replace("Hello", "", $str);

echo $str; // world!

// ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์น˜ํ™˜
$str = "Hello, world! 12345";

// "Hello"์™€ "world"๋ฅผ "Goodbye"๋กœ ์น˜ํ™˜
$str = preg_replace("/Hello|world/", "Goodbye", $str);

echo $str; // Goodbye, Goodbye! 12345

 

 


 

๋‹ค์‹œ ๋Œ์•„์™€์„œ ํ™•์ธํ•ด๋ณด๋ฉด,

 

 else{
              $name = preg_replace("/nyang/i", "", $input_name);
              $pw = preg_replace("/\d*\@\d{2,3}(31)+[^0-8\"]\!/", "d4y0r50ng", $input_pw);
              
              if ($name === "dnyang0310" && $pw === "d4y0r50ng+1+13") {
                echo '<h4>Step 2 : Almost done...</h4><div class="door_box"><div class="door_black"></div><div class="door"><div class="door_cir"></div></div></div>';

 

 

- $input_name ๋ณ€์ˆ˜์—์„œ "nyang"์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ์ฐพ์•„ ์ œ๊ฑฐํ•œ ํ›„, ๊ฒฐ๊ณผ๋ฅผ $name ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.
- preg_replace() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๊ทœ ํ‘œํ˜„์‹ /nyang/i๋ฅผ ์ ์šฉํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ /i๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ์˜ต์…˜์ด๋‹ค.
- $input_pw ๋ณ€์ˆ˜์—์„œ ํŠน์ •ํ•œ ํŒจํ„ด์„ ์ฐพ์•„ "d4y0r50ng"์œผ๋กœ ๋Œ€์ฒดํ•œ ํ›„, ๊ฒฐ๊ณผ๋ฅผ $pw ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.
- preg_replace() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ œ๊ณต๋œ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ ์šฉํ•œ๋‹ค.
- ๋งŒ์•ฝ $name์ด "dnyang0310"์ด๊ณ , $pw๊ฐ€ "d4y0r50ng+1+13"๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ: HTML ์ฝ”๋“œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
- ์ถœ๋ ฅ๋˜๋Š” ๋‚ด์šฉ์€ "Step 2 : Almost done..."์ด๋ฉฐ, ์ด์–ด์„œ ๋ฌธ์„ ์—ฐ์ƒ์‹œํ‚ค๋Š” ์š”์†Œ๋“ค์ด ํฌํ•จ๋œ๋‹ค.

 

 

 

๋จผ์ € name์˜ ๊ฐ’์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž.

 

name์˜ ๊ฐ’์ด "dnyang0310"์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ, ํ•„ํ„ฐ๋ง์„ ์šฐํšŒํ•˜๊ธฐ ์œ„ํ•ด
"dnynyangang0310"์ด๋ผ๋Š” Nickname์„ ์ž…๋ ฅํ•˜๋ฉด ๋  ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ–ˆ๋‹ค.

 

nyang์ด ๊ณต๋ฐฑ์œผ๋กœ ์น˜ํ™˜๋˜๋ฉด์„œ ํ•„ํ„ฐ๋ง ๋˜๊ณ  ๊ฒฐ๊ตญ "dnyang0310"๋งŒ ๋‚จ๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

๊ทธ ๋‹ค์Œ์œผ๋กœ๋Š” pw์˜ ํ•„ํ„ฐ๋ง์„ ์‚ดํŽด๋ณด์ž.

 

$pw = preg_replace("/\d*\@\d{2,3}(31)+[^0-8]!/", "d4y0r50ng", $input_pw);

 

์—์„œ, ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ•˜๋‚˜ํ•˜๋‚˜ ํ•ด์ฒดํ•˜์—ฌ ์‚ดํŽด๋ณด์ž.

 

\d*

- 0์—์„œ 9๊นŒ์ง€์˜ ๋ฒ”์œ„์˜ ํ•œ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

- *๋Š” ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๊ฐ€ 0๊ฐœ ์ด์ƒ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

=> ๋”ฐ๋ผ์„œ 0~9 ์‚ฌ์ด์˜ ์ˆซ์ž๊ฐ€ 0๊ฐœ ์ด์ƒ ๋‚˜ํƒ€๋‚˜์•ผ ํ•œ๋‹ค.

 

\@

- @๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

\d{2,3}

- ์ˆซ์ž๊ฐ€ 2๊ฐœ ๋˜๋Š” 3๊ฐœ ์—ฐ์†๋ฉ๋‹ˆ๋‹ค.

 

(31)+

- '31'์ด๋ผ๋Š” ์ˆซ์ž๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ๋ฐ˜๋ณต๋œ๋‹ค.

 

[^0-8\"]

- '0'์—์„œ '8' ์‚ฌ์ด์˜ ์ˆซ์ž ๋˜๋Š” '"' ์ด์™ธ์˜ ๋ฌธ์ž๋ฅผ ์ฐพ๋Š”๋‹ค.

 

\!

- '!' ๊ธฐํ˜ธ๋ฅผ ์ฐพ๋Š”๋‹ค.

 

 

 

==> ์œ„์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ 1@12319! ๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.

์ด ๋ฌธ์ž์—ด์€ preg_replace() ํ•จ์ˆ˜์— ์˜ํ•ด "d4y0r50ng"๋กœ ๋ณ€ํ™˜๋  ๊ฒƒ์ด๋‹ค.

๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” "d4y0r50ng+1+13"์ด ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋’ค์— "+1+13"๋ฅผ ๋ถ™์—ฌ

"123@12319!+1+13"์„ ๋น„๋ฐ€๋ฒˆํ˜ธ์— ์ž…๋ ฅํ•ด๋ณด์ž.

 

 

 

 

์ด๋ ‡๊ฒŒ Step2๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜์—ˆ๋‹ค.

 


                $cmd = $_POST["cmd"] ? $_POST["cmd"] : "";

                if ($cmd === "") {
                  echo '
                        <p><form method="post" action="/step2.php">
                            <input type="hidden" name="input1" value="'.$input_name.'">
                            <input type="hidden" name="input2" value="'.$input_pw.'">
                            <input type="text" placeholder="Command" name="cmd">
                            <input type="submit" value="์ œ์ถœ"><br/><br/>
                        </form></p>
                  ';
                }
                // cmd filtering
                else if (preg_match("/flag/i", $cmd)) {
                  echo "<pre>Error!</pre>";
                }
                else{
                  echo "<pre>--Output--\n";
                  system($cmd);
                  echo "</pre>";
                }
              }
              else{
                echo "Wrong nickname or pw";
              }
            }
          }
          // GET request
          else{
            echo "Not GET request";
          }
      ?>

 

์ด์ œ๋Š” ์ด ๋ถ€๋ถ„์„ ์‚ดํŽด๋ณด์ž.

 

 

์ฒ˜์Œ์— ๋ฌธ์ œ ์„ค๋ช…์—์„œ

 

Step 2์—์„œ system() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•˜์„ธ์š”.

ํ”Œ๋ž˜๊ทธ๋Š” ../dream/flag.txt์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.

 

๋ผ๊ณ  ํ–ˆ์—ˆ๋‹ค.

 

 

๋จผ์ € php ๋ฌธ๋ฒ•์—์„œ์˜ system ํ•จ์ˆ˜๋ฅผ ์•Œ์•„๋ณด์ž.

 

 


โญsystem ํ•จ์ˆ˜๋ž€?

์™ธ๋ถ€ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ณ  ์ถœ๋ ฅ์„ ํ‘œ์‹œํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

system ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ๊ตฌ๋ฌธ

system(string $command, int &$return_var): string|false

 

- $command: ์‹คํ–‰ํ•  ๋ช…๋ น์–ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์ด๋‹ค.
- &$return_var: ๋ช…๋ น์–ด ์‹คํ–‰ ํ›„์˜ ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ๋ฐ›๋Š” ๋ฐ ์‚ฌ์šฉ๋  ๋ณ€์ˆ˜์ด๋‹ค. ์ด ๋ณ€์ˆ˜๋Š” ์„ ํƒ์ ์œผ๋กœ ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ๋‹ค. ๋ช…๋ น์–ด๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด 0์ด ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ์‹คํŒจํ•˜๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
- ๋ฐ˜ํ™˜ ๊ฐ’: ๋ช…๋ น์–ด์˜ ์ถœ๋ ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์‹คํŒจํ•œ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, ls ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์˜ ํŒŒ์ผ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ค๋Š” PHP ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค:

$output = system('ls', $return_var);
echo $output;

 

 

๋˜๋Š” ๋ช…๋ น์–ด์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ์— ์ƒ๊ด€ ์—†์ด ๋‹จ์ˆœํžˆ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค:

system('ls');

 

 

 

 


๋‹ค์‹œ ๋Œ์•„์™€์„œ,

 

                else if (preg_match("/flag/i", $cmd)) {
                  echo "<pre>Error!</pre>";
                }
                else{
                  echo "<pre>--Output--\n";
                  system($cmd);
                  echo "</pre>";
                }
              }
              else{
                echo "Wrong nickname or pw";

 

์ด ๋ถ€๋ถ„์„ ์ž์„ธํžˆ ๋ณด๋ฉด, ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„์—†์ด ๋ฌธ์ž์—ด์— "flag"๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด "Error!"๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด system() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” "flag" ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ์•ˆ ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ํ”Œ๋ž˜๊ทธ๋Š” ../dream/flag.txt์— ์œ„์น˜ํ•˜๋Š”๋ฐ, ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ ํ”Œ๋ž˜๊ทธ ํŒŒ์ผ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์„๊นŒ?

 

๋‚˜๋Š” '*' ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ../dream ๋””๋ ‰ํ† ๋ฆฌ ์•ˆ์˜ ๋ชจ๋“  ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅ๋˜๊ฒŒ ํ•  ๊ฒƒ์ด๋‹ค.

 

 

 

ํ”Œ๋ž˜๊ทธ๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

 

 

 

 

ํ’€์ด ์™„๋ฃŒ!

 


[2] ์ฐธ๊ณ  ์ž๋ฃŒ

 

์ •๊ทœํ‘œํ˜„์‹:
http://www.ktword.co.kr/test/view/view.php?no=5720

์ด์Šค์ผ€์ดํ”„:
https://choseongho93.tistory.com/entry/PHP-%EB%AC%B8%EC%9E%90%EC%97%B4%EC%97%90-%EC%82%AC%EC%9A%A9%ED%95%A0-%EC%88%98-%EC%9E%88%EB%8A%94-%EC%9D%B4%EC%8A%A4%EC%BC%80%EC%9D%B4%ED%94%84-%EC%8B%9C%ED%80%80%EC%8A%A4

preg_match:
https://codingeverybody.kr/php-preg_match-%ED%95%A8%EC%88%98/