본문 바로가기

워게임

Lord of SQL Injection - 12번 문제풀이

[12번 문제 - darknight]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php 
  include "./config.php"
  login_chk(); 
  $db = dbconnect(); 
  if(preg_match('/prob|_|\.|\(\)/i'$_GET[no])) exit("No Hack ~_~"); 
  if(preg_match('/\'/i'$_GET[pw])) exit("HeHe"); 
  if(preg_match('/\'|substr|ascii|=/i'$_GET[no])) exit("HeHe"); 
  $query = "select id from prob_darkknight where id='guest' and pw='{$_GET[pw]}' and no={$_GET[no]}"
  echo "<hr>query : <strong>{$query}</strong><hr><br>"
  $result = @mysqli_fetch_array(mysqli_query($db,$query)); 
  if($result['id']) echo "<h2>Hello {$result[id]}</h2>"
   
  $_GET[pw] = addslashes($_GET[pw]); 
  $query = "select pw from prob_darkknight where id='admin' and pw='{$_GET[pw]}'"
  $result = @mysqli_fetch_array(mysqli_query($db,$query)); 
  if(($result['pw']) && ($result['pw'== $_GET['pw'])) solve("darkknight"); 
  highlight_file(__FILE__); 
?>
cs

해당 문제는 8번 쿼리문이 길어보여서 복잡해 보일 수 있으나 조건문 부분만 잘 생각하면 쉽게 풀 수 있는 Blind SQLi 문제이다.

 

pw와 no 파라미터 부분에서 싱글쿼터(') 를 사용할 수 없으니 pw 파라미터는 아무 값이나 넣고 no 파라미터 뒷부분에 조건문을 추가로 붙여서 결과값을 도출해 내는게 핵심이다.

 

하지만 no 파라미터에서 substr, ascii, "=" 문자열도 필터링 하고있으니 같은 역할을 하는 함수를 사용해야 한다.

substr을 대체할 함수는 mid

ascii를 대체할 함수는 ord

"="을 대체할 in ("문자열")을 사용해서 id가 admin인 조건문만 만들어주면 된다.

 

where 절 뒤의 id='guest' and pw=1&no=1 or id in ("admin") and ord(mid(pw,1,1))>30#

이런식으로 공격 구문을 삽입해서 pw를 찾아낼 수 있다.

 

[문제 풀이 구문]

https://los.rubiya.kr/chall/darkknight_5cfbc71e68e09f1b039a8204d1a81456.php?pw=0b70ea1f