PHPreg

2023. 8. 30. 13:32· 보안/Dreamhack
반응형

Untitled.png

index.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">
        <h4>Step 1 : Open the door & Go to Step 2 !!</h4>
        <div class="door"><div class="door_cir"></div></div>
        <p>
          <form method="post" action="/step2.php">
              <input type="text" placeholder="Nickname" name="input1">
              <input type="text" placeholder="Password" name="input2">
              <input type="submit" value="제출">
          </form>
        </p>
      </div>
    </div> 

    <style type="text/css">
      h4 {
        color: rgb(84, 84, 84);
      }
      .box{
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
      }
      .door { 
        position: relative;
        margin: 20px 0px;
        width: 140px;
        height: 180px;
        background-color: #b9abf7;
        border-radius: 10px;
      }
      .door_cir{
        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>

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>

일단 Step 1의 Nickname과 Password의 필터링 과정이 있다.

여기에서 nickname과 pw의 답이 정해져 있다.

nickname = dnyang0310

password = d4y0r50ng+1+13

이여야 Step 2로 이동이 됨

// 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>';

name의필터링을우회하기위해name의 필터링을 우회하기 위해 name의필터링을우회하기위해name 의 값에 nyang이 들어가면 공백으로 치환됨

dnynyangang0310 을 이용해 nyang을 필터링하여 공백을 만들면 dnynyangang0310 부분이 사라져 $name값은 dnyang0310 으로 입력됨

 

$input_pw는 소문자 a부터 z, 대문자 A부터 Z까지 필터링을 거침

→ 영어는 사용을 하지 못함

$pw 의 값에 규칙에는

\d* : 숫자가 0회 이상 있을때

@ : ‘@’ 문자가 있을때

\d{2,3} : 숫자가 2회 또는 3회 있을 때

(31)+ : ‘31’ 이라는 문자가 1회 이상 있을 때

[^0-8] : 0부터 8까지의 숫자를 제외한 어떤 문자든지 하나 있을때 (대괄호 안에 캐럿(^) 기호는 “제외한다” 라는 뜻)

! : ‘!’ 문자가 있을때

 

숫자가 0회 이상 있을 때, 숫자가 2회 있을때

→ 12

 

‘@’ 문자가 있을때, 숫자가 3회 있을때

→ 12@345

 

‘31’ 이라는 문자열이 있을때, 0부터 8까지의 숫자를 제외한 문자가 하나있을때, ‘!’ 문자가 있을 때

→ 12@34531%!

 

d4y0r50ng 다음으로 +1+13 가 와야될 때

→ 12@34531%!+1+13

Untitled.png

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

Step 2 에서는 필터링이 하나밖에 없다

대소문자 구분없이 flag 라는 단어가 들어가면 Error를 나타내도록 필터링되어져있음

우회하는 방법으로는 .txt 의 확장자 파일명을 cat으로 모두 출력하도록 명령어를 작성하면 풀린다

cat ../dream/*.txt

Untitled.png

반응형
'보안/Dreamhack' 카테고리의 다른 글
  • command-injection-1
  • amocafe_2
  • baby-linux
Future0_
Future0_
rm -rf /
Future0_
Luna Developer Blog
Future0_
전체
오늘
어제
  • 분류 전체보기 (112)
    • 프로그래밍 (4)
      • 알고리즘 (4)
    • 보안 (14)
      • Dreamhack (4)
      • Hackthebox (1)
      • Webhacking (9)
    • 프로젝트 (4)
    • 공부 (80)
      • Database (2)
      • Python (11)
      • System (4)
      • Java (13)
      • JSP (13)
      • Spring (11)
      • Kotlin (16)
      • 자료구조 (10)
      • 기계학습 (0)
    • Docker (4)
    • Github (2)
    • Tip (1)
    • 잡담 (2)

블로그 메뉴

  • 홈
  • 태그

공지사항

인기 글

태그

  • android studio 삭제
  • 자료구조
  • docker
  • 키 해시
  • Java
  • dreamhack
  • jsp
  • shared preference
  • 알고리즘
  • Computer science
  • Database
  • 상속
  • 보안
  • webhacking
  • cs
  • spring
  • Kotlin
  • api 통신
  • React
  • Android Studio
  • 자바빈즈
  • 컴퓨터
  • native app
  • 코틀린기본문법
  • 디버깅키해시
  • SpringBoot
  • Python
  • 프로그래밍
  • ViewModel
  • 1.9.22

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.0
Future0_
PHPreg
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.