[Dreamhack] cookie 풀이
https://dreamhack.io/wargame/challenges/6
cookie
쿠키로 인증 상태를 관리하는 간단한 로그인 서비스입니다. admin 계정으로 로그인에 성공하면 플래그를 획득할 수 있습니다. 플래그 형식은 DH{...} 입니다. Reference Introduction of Webhacking
dreamhack.io
문제 설명
쿠키로 인증 상태를 관리하는 간단한 로그인 서비스입니다.
admin 계정으로 로그인에 성공하면 플래그를 획득할 수 있습니다.
플래그 형식은 DH{...} 입니다.
라고 적혀있다.

주어진 주소로 들어가면 다음과 같은 웹 화면이 나온다.
로그인 화면은 다음과 같다.
admin 계정으로 로그인하면 flag가 주어진다고 한다.
다음과 같은 코드가 주어진다
#!/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)
이 코드를 봤을 때 admin 계정의 username은 admin이 아닐까 싶다
다른 것을 username으로 두고 login하면 not found user라고 뜨지만
admin을 넣고 login 하면 wrong password라고 뜬다.
그렇다면 username은 admin이 맞는 걸까…
앗! 봤던 코드 다시보자!
username: guest
password: guest 와 같이 로그인 하니 다음과 같은 결과가 뜬다
파이썬 코드에 따르면
return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not admin"}')
username이 admin이면 flag가 나온다.
그렇다면 username이 guest일 때도 flag가 나오게 하면 되지 않나?
guest로 로그인 될 때, guest라는 cookies가 생성된다.
value를 admin으로 변경했더니 flag가 나온다!