일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- shellcode
- XSS
- spoofing
- dns
- rao
- 시스템해킹
- pycrpytodome
- CSRF
- overthewire
- dreamhack
- arp
- RSA
- bandit
- 드림핵
- OverTheWire Bandit Level 1 → Level 2
- 암호학
- Montgomery Reduction
- weak key
- 웹해킹
- RSA Common Modulas Attack
- cryptography
- Cube Root Attack
- Bandit Level 1 → Level 2
- picoCTF
- return address overflow
- Franklin-Reiter Related Message Attack
- Crypto
- AES
- Hastad
- redirect
- Today
- Total
암호(수학) 등.. 공부한 거 잊을거 같아서 만든 블로그
[Dreamhack] session-basic 본문
문제
문제 파일 : 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',
'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)
풀이
접속을 하면 다음과 같은 웹페이지에 접속이 된다.
상단 왼쪽에 Login 을 누르면 다음과 같이 로그인 창이 나온다.
문제 웹페이지에 접속후 소스코드를 보면 guest/guest 계정이 있음을 알 수 있다. 또한 app.py에서도 다음 딕셔너리가 계정임을 알 수 있다.
admin 계정의 패스워드가 flag인 것도 확인할 수 있다.
guest 계정으로 로그인을 해보면 다음과 같은 창이 나온다.
admin 계정으로 로그인 하면 admin이라는 의미의 문구가 출력 된다는 것을 유추해볼 수 있다.
그러면 이제 문제 파일을 봐보자.
주석을 통하여 session_storage 딕셔너리가 세션을 저장하는 것을 알 수 있다.
.위 코드를 보면 /admin 경로로 들어가면 계정의 세션 정보를 웹페이지에 출력해 주는 것을 볼 수 있다.
실제로 /admin 경로로 들어가 보면 다음과 같다.
위 코드를 보면 세션 값에 맞는 계정의 id 값을 username 변수에 저장한다. 그리고 username이 'admin' 인지를 검증하여 flag를 출력할지 결정한다.
"flag is " + FLAG if username == "admin" else "you are not admin" if 문이 true 일 경우 "flag is " + FLAG, false 일 경우 "you are not admin" 를 출력한다.
따라서 '/' 경로에서 세션값을 admin의 것으로 바꾸면 flag가 출력 될 것이다.
개발자 도구를 이용하여 guest의 세션값에서 admin의 세션값으로 변경 후, 새로고침을 해보면 flag 가 출력되는 것을 볼 수있다.
'Dreamhack > Web' 카테고리의 다른 글
[Dreamhack] xss-2 (0) | 2023.04.08 |
---|---|
[Dreamhack] xss-1 (0) | 2023.04.07 |
[Dreamhack] cookie (0) | 2023.04.04 |
[Dreamhack] funjs (0) | 2023.04.03 |
[Dreamhack] Carve Party (0) | 2023.04.03 |