Self-Improvement

DELETE Error SQLi(MySQL, MSSQL, Oracle) 본문

SQLi

DELETE Error SQLi(MySQL, MSSQL, Oracle)

JoGeun 2020. 4. 20. 18:09

INSERT, UPDATE, DELETE 구문에서는 UNION SQLi 기법은 사용이 불가능하지만 Error SQLi는 가능하다.

하지만 위 3가지를 SQLi할때는 중요한 사항들이 존재한다.


DELETE FROM () WHERE ();

Delete 구문을 사용하는 페이지는 회원 탈퇴, 게시글 삭제 및 취소 등의 기능이 대표적이다. Delete 구문에서 가장 주의해야 할 사항으로 참인 명제 조건이 된다면 해당 테이블의 모든 데이터가 삭제된다는 것이다.

Update 구문처럼 WHERE() 절의 조건문이 가장 주의해야한다.

 

MySQL 소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
 
if (!$link = mysql_connect('localhost''root''root')) {
    echo 'Could not connect to mysql';
    exit;
}
 
if (!mysql_select_db('test'$link)) {
    echo 'Could not select database';
    exit;
}
 
$queryCheck="select 1 from users";
$result = mysql_query($queryCheck$link);
 
if($result == FALSE){
    $createQuery="CREATE TABLE USERS (" .
         "idx int(11) AUTO_INCREMENT PRIMARY KEY," .
         "ID varchar(12) NOT NULL," .
         "NAME varchar(25) NOT NULL," .
         "LEVEL int(2) NOT NULL);";
    $result = mysql_query($createQuery$link);
}
 
echo "<script language='javascript'>\n";
echo "    function edit(idx,id,name){\n";
echo "        document.webform.idx.value=idx;\n";
echo "        document.webform.submit();\n";
echo "    }\n";
echo "</script>\n";
 
echo "MySQL : SQL Injection in DELETE Query<br><br>\n";
//echo "<hr>";
echo "<form action='" . $_SERVER['PHP_SELF'] . "' method='post' name='webform'>\n";
echo "    <input type='hidden' name='idx'>\n";
echo "</form>\n";
echo "<hr>\n<br>\n<br>\n";
 
$_idx='';
if (isset($_POST['idx'])) $_idx = $_POST['idx'];
 
$sql = "delete from USERS where idx=$_idx";
$result = mysql_query($sql$link);
 
if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
 
echo 'Record Deleted : ' . mysql_affected_rows();
 
$sql = "select * from USERS";
$result = mysql_query($sql$link);
 
print "<table border=1 cellpadding=5 cellspacing=0>\n";
print "\t<tr>\n\t\t<td width='50'>num</td><td width='120'>id</td><td width='240'>name</td><td width='50'>Edit</td>\n\t</tr>\n";
 
while ($row = mysql_fetch_assoc($result)) {
    print "\t<tr>\n\t\t<td>" . $row['idx'] . "</td><td>" . $row['ID'] . "</td><td>" . $row['NAME'] . "</td><td><input type='button' onClick=\"edit('" . $row['idx'] . "')\" value='Del'></td>\n\t</tr>\n";
}
print "</table>\n";
 
mysql_free_result($result);
 
?>
cs

 

MySQL, MSSQL, Oracle 데이터베이스 전부 다 앞서 배운 INSERT, UPDATE 구문과 동일한 Payload로 Error SQLi가 가능하다.