๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Web Hacking/Webhacking.kr

[Webhacking.kr] old-39 ์›Œ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์ด

by A Lim Han 2023. 8. 30.

๐Ÿท old-39 ์›Œ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์ด

1.  ๋ฌธ์ œ ํ™”๋ฉด์œผ๋กœ ๋“ค์–ด๊ฐ€ view-source ํด๋ฆญ

2.  ์ฝ”๋“œ ๋ถ„์„๊ณผ ํ•จ๊ป˜ ๋ฌธ์ œ ํ’€์ด ์กฐ๊ฑด ํƒ์ƒ‰

 

<?php
  include "../../config.php"; // ์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ ํฌํ•จ

  if($_GET['view_source']) view_source(); // ๋งŒ์•ฝ view_source ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜๋ฉด, ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ
?>

<html>
<head>
<title>Chellenge 39</title>
</head>
<body>
<?php
  $db = dbconnect(); // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์—ฐ๊ฒฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์—ฐ๊ฒฐ ๊ฐ์ฒด ์ƒ์„ฑ

  if($_POST['id']){ // ๋งŒ์•ฝ POST ์š”์ฒญ์œผ๋กœ 'id' ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜์—ˆ๋‹ค๋ฉด
    $_POST['id'] = str_replace("\\","",$_POST['id']); // ์—ญ์Šฌ๋ž˜์‹œ(\)๋ฅผ ๋นˆ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒด
    $_POST['id'] = str_replace("'","''",$_POST['id']); // ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋ฅผ ๋‘ ๊ฐœ์˜ ์ž‘์€๋”ฐ์˜ดํ‘œ('')๋กœ ๋Œ€์ฒด (SQL ์ธ์ ์…˜ ๋ฐฉ์ง€)
    $_POST['id'] = substr($_POST['id'],0,15); // 'id' ๊ฐ’์˜ ๊ธธ์ด๋ฅผ ์ตœ๋Œ€ 15๋กœ ์ œํ•œ

    // 'id' ๊ฐ’์˜ ๊ธธ์ด๊ฐ€ 14๋ณด๋‹ค ์ž‘๊ณ , ํ•ด๋‹น 'id' ๊ฐ’์„ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ 'member' ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
    $result = mysqli_fetch_array(mysqli_query($db,"select 1 from member where length(id)<14 and id='{$_POST['id']}'"));
    
    // ๋งŒ์•ฝ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ 1์ด๋ฉด
    if($result[0] == 1){
      solve(39); // ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ
    }
  }
?>

<!-- POST ์š”์ฒญ์œผ๋กœ 'id' ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ํผ ์ƒ์„ฑ -->
<form method=post action=index.php>
<input type=text name=id maxlength=15 size=30>
<input type=submit>
</form>

<!-- 'view-source' ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋งํฌ ์ƒ์„ฑ -->
<a href=?view_source=1>view-source</a>
</body>
</html>

 

++  ๊ณ ๋ คํ•ด์•ผ ํ•  ์‚ฌํ•ญ

โ“  SQL ๋ฌธ์˜ ๋งˆ์ง€๋ง‰์— ' ์ด ํ•˜๋‚˜ ๋น ์ ธ์žˆ์Œ

select 1 from member where length(id)<14 and id='{$_POST['id']}

โ“‘  ์ž‘์€ ๋”ฐ์˜ดํ‘œ( ' ) ์ž…๋ ฅ ์‹œ ํฐ ๋”ฐ์˜ดํ‘œ( " )๋กœ ์น˜ํ™˜

 $_POST['id'] = str_replace("'","''",$_POST['id']);

โ“’  id์˜ ๊ฐ’์€ 15๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ์ €์žฅ๋จ

$_POST['id'] = substr($_POST['id'],0,15);

3.  ์ƒ๋‹จ ์ฝ”๋“œ์™€ ํ•ด๊ฒฐ ์กฐ๊ฑด์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ž…๋ ฅ๊ฐ’ ์ž‘์„ฑ 

# ์ž‘์„ฑํ•œ ์ž…๋ ฅ๊ฐ’
1ใ…คใ…คใ…คใ…คใ…คใ…คใ…คใ…คใ…คใ…คใ…คใ…คใ…คใ…คใ…ค'

 

++  ์ž…๋ ฅ๊ฐ’ ์„ค๋ช…

๋ฌธ์ œ ํ•ด๊ฒฐ ์œ ์˜ ์‚ฌํ•ญ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•
โ“  SQL ๋ฌธ์˜ ๋งˆ์ง€๋ง‰์— ' ์ด ํ•˜๋‚˜ ๋น ์ ธ์žˆ์Œ SQL ๋ฌธ์˜ ๋งˆ์ง€๋ง‰์— ์ž‘์€ ๋”ฐ์˜ดํ‘œ( ' ) ์ถ”๊ฐ€
โ“‘  ์ž‘์€ ๋”ฐ์˜ดํ‘œ( ' ) ์ž…๋ ฅ ์‹œ ํฐ ๋”ฐ์˜ดํ‘œ( " )๋กœ ์น˜ํ™˜ ์ž‘์€ ๋”ฐ์˜ดํ‘œ( ' )๊ฐ€ 2๊ฐœ ๋ชจ์ด๋ฉด
ํฐ ๋”ฐ์˜ดํ‘œ( " )๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ์— ์ง‘์ค‘
โ“’  id์˜ ๊ฐ’์€ 15๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ์ €์žฅ๋จ ์ž‘์€ ๋”ฐ์˜ดํ‘œ( ' )๊ฐ€ ํฐ ๋”ฐ์˜ดํ‘œ( " )๋กœ ์น˜ํ™˜๋  ๋•Œ,
๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„( 16๋ฒˆ์งธ ์ž๋ฆฌ์˜ ' )์ด
id ๊ธธ์ด ์ œํ•œ์— ์˜ํ•ด ์ž˜๋ ค๋‚˜๊ฐ€๋„๋ก ํ•จ

4.  ๋‹ค์‹œ ๋ฌธ์ œ ํŽ˜์ด์ง€๋กœ ๋Œ์•„์™€ 3๋ฒˆ ๊ณผ์ •์—์„œ ์ž‘์„ฑํ•œ ๊ฐ’ ์ž…๋ ฅ

5.  ๊ฐ’ ์ž…๋ ฅ ํ›„ ์ œ์ถœ ๋ฒ„ํŠผ ํด๋ฆญ  -->  ๋ฌธ์ œ ํ•ด๊ฒฐ ์„ฑ๊ณต