1. ๋ฌธ์
https://dreamhack.io/wargame/challenges/409
session-basic
Description ์ฟ ํค์ ์ธ์ ์ผ๋ก ์ธ์ฆ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ ๊ฐ๋จํ ๋ก๊ทธ์ธ ์๋น์ค์ ๋๋ค. admin ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ์ ์ฑ๊ณตํ๋ฉด ํ๋๊ทธ๋ฅผ ํ๋ํ ์ ์์ต๋๋ค. ํ๋๊ทธ ํ์์ DH{...} ์ ๋๋ค. Reference Background: Cook
dreamhack.io
- admin ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ์ ์ฑ๊ณตํ๋ฉด ๋๋ค.
์ด๋ฒ ๋ฌธ์ ๋ ์ฟ ํค์ ์ธ์ ์ ๋ํ ๋ฌธ์ ๋ผ ์ฟ ํค์ ์ธ์ ์ ๋ํด ์ ๋ฆฌํด๋ณด์๋ค.
HTTP ํต์ ์ ์ฐ์ด๋ ์ฟ ํค์ ์ธ์ ์ ๋ํ ์ดํด
2. ํด๊ฒฐ ๊ณผ์
(1) ๋ฌธ์ ํ์ผ ๋ค์ด๋ก๋
#!/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)
์ ์ ์ ๋ํ ์ ๋ณด๊ฐ ์๋ค.
(2) ํ์ด์ง ์ ์
๋ก๊ทธ์ธ ํ๋ฉด
์ฐ์ guest๋ก ๋ก๊ทธ์ธํด์ฃผ์๋ค.
admin ํ์ด์ง๊ฐ ์ฝ๋์ ์กด์ฌํ๋๋ฐ ๋ฑํ ๋์ด๊ฐ ์๊ฐ ์์ด์ ์ง์ ์ ๋ ฅํด์ ๋ค์ด๊ฐ๋ณด์๋ค. ์๋ง ์ฌ๊ธฐ์ session id๊ฐ ์ ์ฅ๋๋ ๊ฒ ๊ฐ๋ค.
@app.route('/admin')
def admin():
# what is it? Does this page tell you session?
# It is weird... TODO: the developer should add a routine for checking privilege
return session_storage
์์ธํ ๋ณด๋ฉด admin์ ์ธ์ ๊ฐ๋ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ธ์ ๊ฐ์ admin์ ์ธ์ ๊ฐ์ผ๋ก ๋ฐ๊ฟ์ค ๋ค์์ ์๋ก๊ณ ์นจ์ ํด๋ณด์๋ค.
ํ๋๊ทธ๊ฐ์ด ์ถ๋ ฅ๋์๋ค.