[Dreamhack] session-basic

2025. 3. 15. 15:01·Webhacking/Dreamhack
 

session-basic

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

dreamhack.io


app.py

#!/usr/bin/python3
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)

문제 풀이

접속했을 때의 메인 화면이다.

guest : guest 입력했을 때의 화면이다.

익스플로잇

이용자별 세션 정보를 조회한다.

/admin 페이지 접속 후 세션 정보를 조회하여 Session ID와 username을 조회한다.

-> admin 값을 복사한다.

admin 값을 value에 붙여넣는다.

결과

새로고침 시 플래그 값을 획득할 수 있다.

저작자표시 비영리 동일조건 (새창열림)

'Webhacking > Dreamhack' 카테고리의 다른 글

[Dreamhack] csrf-1  (0) 2025.03.15
[Dreamhack] xss-2  (0) 2025.03.15
[Dreamhack] xss-1  (0) 2025.03.15
[Dreamhack] cookie  (0) 2025.03.15
[Dreamhack] devtools-sources  (0) 2025.03.15
'Webhacking/Dreamhack' 카테고리의 다른 글
  • [Dreamhack] xss-2
  • [Dreamhack] xss-1
  • [Dreamhack] cookie
  • [Dreamhack] devtools-sources
배움이 머무는 곳
배움이 머무는 곳
  • 배움이 머무는 곳
    wlgus
    배움이 머무는 곳
  • 전체
    오늘
    어제
    • 분류 전체보기 (69) N
      • 이것저것.zip (7)
      • CVE (6)
      • CTF (2)
      • Wargame (24) N
      • Webhacking (19)
        • WebGoat (2)
        • Dreamhack (15)
      • Web (5)
      • Pwnable (5)
        • Dreamhack (5)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
  • hELLO· Designed By정상우.v4.10.5
배움이 머무는 곳
[Dreamhack] session-basic
상단으로

티스토리툴바