Web Hacking/Dreamhack 풀이

Session-basic

박연준 2023. 6. 25. 15:20

문제 정보

  • 쿠키와 세션으로 인증 상태를 관리하는 간단한 로그인 서비스입니다.
  • admin 계정으로 로그인에 성공하면 플래그를 획득할 수 있습니다.
  • 플래그 형식은 DH{…} 입니다.

 

풀이

먼저 사이트에 접속하면 이런 화면이 나온다.

다음은 Login 페이지이고 username과 password 입력란이 있다.

 

여기서 문제를 살펴보겠다.

from flask import Flask, request, render_template, make_response, redirect, url_for

app = Flask(__name__)

try:
    FLAG = open('./flag.txt', 'r').read()
except:
    FLAG = '[**FLAG**]'

users = {
    'guest': 'guest',
    'user': 'user1234',
    'admin': FLAG
}

# this is our session storage
session_storage = {
}

@app.route('/')
def index():
    session_id = request.cookies.get('sessionid', None)
    try:
        # get username from session_storage
        username = session_storage[session_id]
    except KeyError:
        return render_template('index.html')

    return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not admin"}')

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    elif request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        try:
            # you cannot know admin's pw
            pw = users[username]
        except:
            return '<script>alert("not found user");history.go(-1);</script>'
        if pw == password:
            resp = make_response(redirect(url_for('index')) )
            session_id = os.urandom(32).hex()
            session_storage[session_id] = username
            resp.set_cookie('sessionid', session_id)
            return resp
        return '<script>alert("wrong password");history.go(-1);</script>'

@app.route('/admin')
def admin():
    # developer's note: review below commented code and uncomment it (TODO)

    #session_id = request.cookies.get('sessionid', None)
    #username = session_storage[session_id]
    #if username != 'admin':
    #    return render_template('index.html')

    return session_storage

if __name__ == '__main__':
    import os
    # create admin sessionid and save it to our storage
    # and also you cannot reveal admin's sesseionid by brute forcing!!! haha
    session_storage[os.urandom(32).hex()] = 'admin'
    print(session_storage)
    app.run(host='0.0.0.0', port=8000)

user엔 'guest': 'guest', 'user': 'user1234', 'admin': FLAG 값이 들어있다.

users = {
    'guest': 'guest',
    'user': 'user1234',
    'admin': FLAG
}

먼저 Login페이지에서 username과 password에 guest guest를 입력해보았다.

여기서 render_template가 반환되는걸 보면 username이 admin이 아니기 때문에 Hello guest, you are not admin이 나온다.

@app.route('/')
def index():
    session_id = request.cookies.get('sessionid', None)
    try:
        # get username from session_storage
        username = session_storage[session_id]
    except KeyError:
        return render_template('index.html')

    return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not admin"}')

다음 코드를 보면 /admin 페이지가 하나 더 있는 것을 볼 수 있다. 여기서 session_storage가 admin의 세션값이 os.urandom(32).hex()로 인해서 admin페이지에 저장되어 있다.

@app.route('/admin')
def admin():
    # developer's note: review below commented code and uncomment it (TODO)

    #session_id = request.cookies.get('sessionid', None)
    #username = session_storage[session_id]
    #if username != 'admin':
    #    return render_template('index.html')

    return session_storage

if __name__ == '__main__':
    import os
    # create admin sessionid and save it to our storage
    # and also you cannot reveal admin's sesseionid by brute forcing!!! haha
    session_storage[os.urandom(32).hex()] = 'admin'
    print(session_storage)

여기서 admin의 session값을 확인할 수 있다.

다음 저 값을 복사한 후 f12를 눌러 Application의 Cookies 탭에서 sessionid 값을 바꿔준 후에 새로고침하면 flag 값이 나온다.

'Web Hacking > Dreamhack 풀이' 카테고리의 다른 글

simple_sqli_chatgpt  (0) 2023.06.25
CSRF-2  (0) 2023.06.25
CSRF-1  (0) 2023.06.25
XSS-2  (0) 2023.06.25
XSS-1  (0) 2023.06.25