Web Hacking/webhacking.kr 풀이

old-18 write-up

박연준 2024. 1. 10. 01:02

문제 정보

https://webhacking.kr/challenge/web-32/

 

Challenge 18

 

webhacking.kr

 

 

풀이

가장 처음 접속하면 SQL Injection 문제인 것을 알 수 있고, view-source 버튼이 있다.

 

 

view-source의 소스 코드는 다음과 같다. 분석해보면 index.php에 get 메소드를 통해서 이름이 no인 입력문자열을 받아서 제출하는 폼이 존재한다. 또한 아래 php 코드를 통해서 no라는 파라미터를 get 메소드로 요청받게 되면 preg_match 함수인 정규표현식을 통해서 no 파라미터 값에 키워드가 포함된다면 no hack이라는 문자열을 출력한다. $result 부분에는 요청되는 쿼리를 보여주고 있다. 그 아래는 id가 guest라면 hi guest를 출력하고 id가 admin이면 문제가 해결된다.

<?php
  include "../../config.php";
  if($_GET['view_source']) view_source();
?><html>
<head>
<title>Challenge 18</title>
<style type="text/css">
body { background:black; color:white; font-size:10pt; }
input { background:silver; }
a { color:lightgreen; }
</style>
</head>
<body>
<br><br>
<center><h1>SQL INJECTION</h1>
<form method=get action=index.php>
<table border=0 align=center cellpadding=10 cellspacing=0>
<tr><td><input type=text name=no></td><td><input type=submit></td></tr>
</table>
</form>
<a style=background:gray;color:black;width:100;font-size:9pt;><b>RESULT</b><br>
<?php
if($_GET['no']){
  $db = dbconnect();
  if(preg_match("/ |\/|\(|\)|\||&|select|from|0x/i",$_GET['no'])) exit("no hack");
  $result = mysqli_fetch_array(mysqli_query($db,"select id from chall18 where id='guest' and no=$_GET[no]")); // admin's no = 2

  if($result['id']=="guest") echo "hi guest";
  if($result['id']=="admin"){
    solve(18);
    echo "hi admin!";
  }
}
?>
</a>
<br><br><a href=?view_source=1>view-source</a>
</center>
</body>
</html>

 

먼저 요청되는 쿼리가 no= 다음에 입력한 파라미터가 오기 때문에 no=no로 입력하면 참이 된다. 따라서 다음과 같이 no를 작성해서 제출을 눌러도 되고 url 주소창에 작성해도 된다. 그러면 result 아래 부분에 hi guest가 출력되는 것을 볼 수 있다. 

 

 

다음으로 분석해야 할 것은 아래 코드이다. 제일 뒤에있는 i는 정규표현식의 시작과 끝을 나타내며 i는 대소문자를 구분하지 않는다. 또한 |는 or 연산자이며 공백 / ( ) | & 의 특수문자와 select와 from, 0x 까지의 문자열을 검사하고 있다. 또한 요청되는 쿼리문을 보면 주석으로 뒤에 admin의 no는 2라고 되어있다.

if(preg_match("/ |\/|\(|\)|\||&|select|from|0x/i", $_GET['no'])) exit("no hack");

 

 

따라서 요청되는 쿼리에서 처음 no는 and 연산자 이므로 틀린 값으로 전송하고 or을 뒤에 입력해서 no=2를 입력해주면 된다. 다음과 같이 +를 이용해서 공백을 우회했는데 잘 먹히지 않은 것 같다.

 

%09로 공백을 우회해서 제출을 눌러봤는데 잘 되지 않아 url에 다시 입력해서 요청했더니 해결되었다.

'Web Hacking > webhacking.kr 풀이' 카테고리의 다른 글

old-16 write-up  (0) 2024.01.10
old-17 write-up  (0) 2024.01.10
old-26 write-up  (0) 2024.01.08
old-24 write-up  (0) 2024.01.08
old-54 write-up  (0) 2024.01.08