WriteUp/Webhacking.kr

Challenge 18 (100pt)

vared 2020. 5. 10. 12:05

sql injection 문제다. 제출링크가 있고 get 방식으로 no라는 변수에 넘겨준다는 것을 확인할 수 있다.

밑에줄에는 result 도 출력해준다. 소스를 한번 보자.

<?php
  include "../../config.php";
  if($_GET['view_source']) view_source();
?><html>
<head>
<title>Challenge 18</title>
<style type="text/css">
body { background:black; color:white; font-size:10pt; }
input { background:silver; }
a { color:lightgreen; }
</style>
</head>
<body>
<br><br>
<center><h1>SQL INJECTION</h1>
<form method=get action=index.php>
<table border=0 align=center cellpadding=10 cellspacing=0>
<tr><td><input type=text name=no></td><td><input type=submit></td></tr>
</table>
</form>
<a style=background:gray;color:black;width:100;font-size:9pt;><b>RESULT</b><br>
<?php
if($_GET['no']){
  $db = dbconnect();
  if(preg_match("/ |\/|\(|\)|\||&|select|from|0x/i",$_GET['no'])) exit("no hack");
  $result = mysqli_fetch_array(mysqli_query($db,"select id from chall18 where id='guest' and no=$_GET[no]")); // admin's no = 2

  if($result['id']=="guest") echo "hi guest";
  if($result['id']=="admin"){
    solve(18);
    echo "hi admin!";
  }
}
?>
</a>
<br><br><a href=?view_source=1>view-source</a>
</center>
</body>
</html>

우리가 잘 봐야할 부분은 php 태그 내부이므로, 한번 보자.

Get['no']는 / ( ) | & select from 0x 를 필터링 한다.

주석으로 친절하게 admin의 no가 2라는 것도 알려줬다. 

2를 넣어봤는데 물론 작동하지 않는다. -> 왜냐하면

select id from chall18 where id='guest' and no=$_GET[no]

이기 때문에, id 가 guest로 설정되어있다. 즉 or 문을 이용해서 no=2일때를 넣어주면 되겠다.

?no=0 or no=2

이렇게 넣어주니까 no hack 즉 필터링 당했다. 공백이 필터링되는 것 같다. 공백을 %0a로 넣어주자.

?no=0%0aor%0ano=2

 

Clear!