rsp-∞
[DreamHack] session-basic 본문

문제 파일을 다운받고 코드를 살펴본다.
#!/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)
35번째 줄에서 53번째 줄까지 보면 로그인을 처리하는 구간이다. users에 저장되어 있는 guest와 user, admin의 정보가 이 구간에서 어떻게 동작하는지 알 수 있다. 쿠키에서 가져온 session id(25번째 줄)는 storage에서 username을 찾아낸다. 즉, storage가 이후 id를 받았을 때 쌍이 되는 username을 값으로 리턴한다고 이해할 수 있다.
56번째 줄에서 65번째 줄 사이에 있는 주석의 내용이 취약점이다. session id를 가져와서 storage 내부에 쌍이 되는 username을 찾는 과정이 생략되어 있는 것이다. /admin으로 접속만 하면 session storage의 내용이 전부 유출되어 버릴 수 있다. 그러면 생성된 서버 url 뒤에 /admin을 붙여 주고 해당 링크로 접속해 보도록 하자.

다음과 같이 admin의 session id가 저장되어 있는 것을 볼 수 있다. 이제 저 큰 따옴표 안에 있는 session id를 가져와 다시 개발자 도구를 열고, session id를 입력하는 부분을 admin의 id로 수정해 본다.

사이트를 새로고침해 보면 다음과 같이 플래그를 획득할 수 있다.
'Write-ups > web' 카테고리의 다른 글
[Dreamhack] csrf-1 (0) | 2025.04.04 |
---|---|
[Dreamhack] xss-2 (0) | 2025.04.04 |
[Dreamhack] xss-1 (0) | 2025.04.04 |
[DreamHack] cookie (0) | 2025.03.27 |
[DreamHack] devtools-sources (0) | 2025.03.20 |