소소한 IT이야기/WARGAME

[Lord of SQL Injection] 21번 iron golem (feat. error based sqli)

Klaus 2023. 2. 11. 11:10

0. 들어가기

블로그 이전으로 인하여, 기존 문제는 아래 링크에 있습니다.

https://toriyong.tistory.com/category/%EB%AA%A8%EC%9D%98%ED%95%B4%ED%82%B9/Wargame

 

'모의해킹/Wargame' 카테고리의 글 목록

 

toriyong.tistory.com

 

문제 풀이전에 알아야할 지식

- UNION SQL

-  페이로드 작성시 SQL 구문의 정확도(?)

 > 구문이 틀린상태에서 문제를 푼다는 것은 해답을 얻을 수 없다는 의미!

 > 정확하게 인지

1. 전체 소스 코드

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

 

2. 주요 코드 분석

 

if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~");
if(preg_match('/sleep|benchmark/i', $_GET[pw])) exit("HeHe");

[+] preg_match 함수 필터링

- 기존과 같게 필터링 하게 됩니다. (i 대소문자 구별)

- sleep/benchmark의 경우 time based 접근은 할 수 없다.

 

[+] Solve 조건 및 쿼리문

  $query = "select pw from prob_iron_golem where id='admin' and pw='{$_GET[pw]}'";
  $result = @mysqli_fetch_array(mysqli_query($db,$query));
  if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("iron_golem");

- 이번 query를 보면 pw값을 받아 DB에서 비교해서 맞을 경우 Solve되는 조건을 확인할 수 있다.

- pw를 찾아야 문제풀이가 가능하다.

==>>>>>   즉, blind sql  injection 접근

 

 

3. Solve 

 

[+] error 구문이 출려되는가? 그리고 참과 거짓을 확인할 수 있는가?

 

- 먼저 에러가 일단 출력되는가? 일단, 화면에 출력됨.

pw=1'#

 

- 거짓(SQL 구문이 정확하게 맞는 상태)

더보기

pw=1' || if(length(pw)>5, (select 1 union select 2),1)%23

 

[+] IRON GOLEM Solve