보안 스터디/웹 해킹

[드림핵/워게임][wargame.kr] login filtering (웹해킹)

성밍쟁 2024. 9. 20. 14:23
728x90
반응형

프롤로그

하루 1 워게임 도전중

 

 

문제

소스코드를 다운받아도, 아무것도 없길래 일단 넘어가겠다.

 

그래서 사이트를 접속하게 되면

get source 부분이 있는데 저것을 클릭하면

 

소스코드

<?php

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

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

 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')"));
  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..";
  }
 }
?>
<!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

-->

 

 

코드 분석

맨 밑에 주석처리되어 있는 

you have blocked accounts.

guest / guest
blueh4g / blueh4g1234ps

이게 아마 아이디, 비밀번호일것이다.

 

isset($_POST['id']) && isset($_POST['ps'])

여긴 POST 요청으로 아이디, 비밀번호가 요청된 게 있는 확인하는 곳

mysqli_connect("localhost", $DB_username, $DB_password, "login_filtering");

여긴 my_sql연결

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

여긴 SQL Injection 방지

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

여긴 SQL문 조회이다.

 

풀이전략

핵심 코드는 바로

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

여기와

   if($id=='guest' || $id=='blueh4g'){

여기.

 

 

PHP는 대소문자 구별을 한다.

그러나 MySQL문 쿼리 같은 경우 윈도우/맥 환경일 경우에는 대소문자 구분을 하지 않는다.

 

그래서 php에서 guest가 입력이 되는 경우에는 바로 입구컷이 되어버리나, Guest로 입력이 되면 1차 통화

이 Guest로 입력되 것이 MySQL문 쿼리로 들어갈때는 다시 대소문자 구분이 안 되기 때문에 로그인이 가능하다.

그러므로 guest가 아닌 대문자 하나라도 섞어주면 해결이다.

 

실행

했더니 성공하였다.

 

 

에필로그

웹 해킹 처음으로 혼자 풀어봄

728x90
반응형