Web/Wargame

[Dreamhack] Cookie

와븨 2022. 9. 17. 04:14

 

 

[문제 파일]

#!/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)
  • users에는 guest, admin 총 2개의 계정이 저장되어 있다.
  • 사용자가 입력한 아이디가 클라이언트의 요청에 포함된 쿠키에 존재하는지 확인 후, index.html에서 text를 보여준다.
  • 사용자가 입력한 패스워드와 users에 저장된 해당 아이디의 패스워드가 같으면 username을 쿠키 변수로 생성한다.

 

이 문제에서는 쿠키를 조작하여 플래그를 찾아 볼 것이다.

 

 

 

 

첫 페이지에서 로그인창으로 이동한 후, 현재 우리가 비밀번호를 알고 있는 guest 계정으로 로그인을 한다.

 

 

쿠키 변수를 username으로 생성했기 때문에 username을 admin으로 변경하게 되면 admin 계정을 탈취할 수 있다.

개발자 도구에서 Application > Cookie > http://... 에서 username을 admin으로 바꿔준다.

 

 

 

바꿔준 후, 첫 페이지로 돌아오면 플래그가 보이게 된다.

 

 

 

답 : DH{7952074b69ee388ab45432737f9b0c56}