Dreamhack/Web

[Dreamhack] cookie

h34hg0 2023. 4. 4. 12:04

문제


 

문제 파일 : 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',
    'admin': FLAG
}

@app.route('/')
def index():
    username = request.cookies.get('username', None)
    if username:
        return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not admin"}')
    return render_template('index.html')

@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:
            pw = users[username]
        except:
            return '<script>alert("not found user");history.go(-1);</script>'
        if pw == password:
            resp = make_response(redirect(url_for('index')) )
            resp.set_cookie('username', username)
            return resp 
        return '<script>alert("wrong password");history.go(-1);</script>'

app.run(host='0.0.0.0', port=8000)

풀이


 

문제 파일에서 위 코드부분을 보자.

 

username = request.cookies.get('username', None)

 

username 쿠키의 값을 username 변수에 저장을 한다.

 

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

 

username 이 'admin' 이면 flag 를 웹페이지에 출력해준다.

"flag is " + FLAG if username == "admin" else "you are not admin"  if 문이 true 일 경우 "flag is " + FLAG, false 일 경우  "you are not admin" 를 출력한다.

따라서 '/' 경로에서 username 쿠키값을 admin으로 수정해주면 flag가 웹페이지 화면에 출력 될 것이다.

 

flag

username : admin 쿠키를 추가하여 새로고침하면 플래그를 알 수 있다.