공부/웹해킹 (Web hacking)

[드림핵 | 웹해킹] LEVEL 1: [wargame.kr] login filtering

eunjuu 2023. 10. 13. 15:51
728x90

👾 문제 설명

I have accounts. but, it's blocked.
can you login bypass filtering?

📎 https://dreamhack.io/wargame/challenges/336

 

[wargame.kr] login filtering

Description I have accounts. but, it's blocked. can you login bypass filtering?

dreamhack.io

 

당신은 필터링을 우회할 수 있나요?


접속 정보에 "서버 생성하기"를 눌러서 링크에 접속한다. 

위와 같은 페이지가 등장한다. get source를 클릭해보자.

 

이런 코드가 나온다.

코드를 분석해보자!

 

<?php

if (isset($_GET['view-source'])) {
    show_source(__FILE__);
    exit();
}

 

view-source를 통해 코드를 보여줍니다.

 

/*
create table user(
 idx int auto_increment primary key,
 id char(32),
 ps char(32)
);
*/

주석 부분! 아이디와 패스워드와 같은 사용자 데이터를 저장하기 위해 “user” 데이터베이스 테이블을 생성하는 명령어

 

 if(isset($_POST['id']) && isset($_POST['ps'])){
  include("./lib.php"); # include for $FLAG, $DB_username, $DB_password.

  $conn = mysqli_connect("localhost", $DB_username, $DB_password, "login_filtering");
  mysqli_query($conn, "set names utf8");

 

쿼리를 연결시키는 코드다.

 

  $id = mysqli_real_escape_string($conn, trim($_POST['id']));
  $ps = mysqli_real_escape_string($conn, trim($_POST['ps']));

  $row=mysqli_fetch_array(mysqli_query($conn, "select * from user where id='$id' and ps=md5('$ps')"));

'user' 테이블에서 'id'와 패스워드가 일치하는 데이터를 검색한다.

 

  if(isset($row['id'])){
   if($id=='guest' || $id=='blueh4g'){
    echo "your account is blocked";
   }else{
    echo "login ok"."<br />";
    echo "FLAG : ".$FLAG;
   }
  }else{
   echo "wrong..";
  }
 }
?>
  • $row['id']를 확인하여, 데이터베이스에서 일치하는 사용자를 찾았는지 확인합니다.
  • 사용자의 'id'가 'guest' 또는 'blueh4g'와 같다면 "your account is blocked"를 출력합니다.
  • 그렇지 않으면 "login ok"과 $FLAG 값을 출력합니다.
  • 'id'가 일치하지 않으면 "wrong.."을 출력합니다.
  • 전체적으로 이 코드는 사용자 인증 상태를 확인하고, 특정 아이디에 대한 차단 여부를 검사하며, 성공적인 로그인 시 "login ok" 및 $FLAG 값을 표시합니다. 그렇지 않으면 "wrong.." 또는 "your account is blocked" 메시지를 표시합니다.

 

<!DOCTYPE html>
<style>
 * {margin:0; padding:0;}
 body {background-color:#ddd;}
 #mdiv {width:200px; text-align:center; margin:50px auto;}
 input[type=text],input[type=[password] {width:100px;}
 td {text-align:center;}
</style>
<body>
<form method="post" action="./">
<div id="mdiv">
<table>
<tr><td>ID</td><td><input type="text" name="id" /></td></tr>
<tr><td>PW</td><td><input type="password" name="ps" /></td></tr>
<tr><td colspan="2"><input type="submit" value="login" /></td></tr>
</table>
 <div><a href='?view-source'>get source</a></div>
</form>
</div>
</body>
<!--

you have blocked accounts.

guest / guest
blueh4g / blueh4g1234ps

-->

 

guest/guest로 로그인했더니 "your account is blocked" 메시지가 뜬다.


쿼리는 대소문자를 구분하지 않는 점을 이용한다. where절에서 대소문자를 구분하지 않는다고 한다.

 

SQL 쿼리 언어는 대소문자에 민감하지 않으며, 쿼리의 예약어 (예: SELECT, FROM, WHERE)를 모두 대문자로 또는 모두 소문자로 작성하거나 혼합하여 작성해도 결과에는 영향을 주지 않습니다.

예를 들어, 다음 두 쿼리는 동일한 결과를 가져올 것입니다:
SELECT * FROM Users WHERE username = 'john';
select * from users where username = 'john';

 

id=Guest를 넣어주면 php코드에서 필터링이 되지 않으며, 쿼리에서는 guest와 같은 값 인줄 알고 flag 값을 내어준다.

 

728x90