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

zombie_assasin

Kortsec1 2023. 8. 3. 16:16

1. Code

<?php 
  include "./config.php"; 
  login_chk(); 
  $db = dbconnect();
  $_GET['id'] = strrev(addslashes($_GET['id']));
  $_GET['pw'] = strrev(addslashes($_GET['pw']));
  if(preg_match('/prob|_|\.|\(\)/i', $_GET[id])) exit("No Hack ~_~"); 
  if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); 
  $query = "select id from prob_zombie_assassin where id='{$_GET[id]}' and pw='{$_GET[pw]}'"; 
  echo "<hr>query : <strong>{$query}</strong><hr><br>"; 
  $result = @mysqli_fetch_array(mysqli_query($db,$query)); 
  if($result['id']) solve("zombie_assassin"); 
  highlight_file(__FILE__); 
?>

 


2. Condition

  • GET ํŒŒ๋ผ๋ฏธํ„ฐ id, pw๊ฐ’์ด addslashesํ•จ์ˆ˜์™€ strrevํ•จ์ˆ˜๋ฅผ ๊ฑฐ์นœ๋‹ค.

 


3. Solution

๋ฌธ์ œ ์ง„ํ–‰์„ ์œ„ํ•ด php ํ•จ์ˆ˜์ธ addslashes์™€ strrev์— ๋Œ€ํ•ด ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

 

 

addslashes

๋ฏธ๋ฆฌ ์ •์˜๋œ ๋ฌธ์ž(' " \ NULL) ์•ž์— backslash๋ฅผ ๋ถ™์—ฌ, escaping ์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ด๋‹ค.

์˜ˆ์‹œ๋Š” ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™๋‹ค.

input output
Hello world Hello world
'apple' \'apple\'
hihi%00 hihi%5c%00
"utopia' \"utopia\'

 

 

strrev

์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์„ ๋’ค์ง‘์–ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์˜ˆ์‹œ๋Š” ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™๋‹ค.

input output
Hello olleH

 

 

๋‹ค์‹œ ์œ„ ๋ฌธ์ œ๋กœ ๋Œ์•„์™€์„œ, ์šฐ๋ฆฌ๊ฐ€ ์ž…๋ ฅํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ addslashes → strrev ์ˆœ์œผ๋กœ ๊ฑฐ์น˜๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด, ๋๋ถ€๋ถ„์— backslash๊ฐ€ ์˜ค๊ฒŒ ํ•จ ์œผ๋กœ์จ, ๊ธฐ์กด ์ฟผ๋ฆฌ ์† single quote๋ฅผ escaping ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

select id from prob_zombie_assassin where id='{$_GET[id]}' and pw='{$_GET[pw]}'

select id from prob_zombie_assassin where id='\' and pw='{$_GET[pw]}'

 


4. Injection

๋”๋ณด๊ธฐ

 

์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์„ ๋งŒ๋“œ๋ ค๋ฉด, id๊ฐ’์— NULLํ˜น์€ "๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ๋œ๋‹ค.

addslashes ํ•จ์ˆ˜์— ๊ฑธ๋ฆฌ๋Š” ํŠน์ˆ˜ํ•œ ๋ฌธ์ž์•ž์— ๋ถ™์€ backslash๊ฐ€ strrevํ•จ์ˆ˜๋ฅผ ๊ฑฐ์ณ, ๊ฐ€์žฅ ๋’ค๋กœ ๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค.

 

$_GET[id] addslashes strrev
" \" "\
%00 %5c%00 %00%5c

 

 

 

๋‹ค์Œ์œผ๋กœ pw๊ฐ’์— or 1=1๊ณผ ๊ฐ™์€ ์ฟผ๋ฆฌ๊ฐ’์„ ๋’ค์ง‘์–ด ๋„ฃ์–ด์ฃผ๋ฉด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค.

 

๋ณ€์ˆ˜๋ช… ๊ฐ’
id "
pw %231=1 ro 

 

 

img_1 zombie_assassin clear

 

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

xavis  (0) 2023.08.03
nightmare  (0) 2023.08.03
succubus  (0) 2023.08.01
assasin  (0) 2023.08.01