Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags
more
Archives
Today
Total
관리 메뉴

CIDY

[Web_Hacking] stage3_문제풀이(cookie) 본문

Hack/DreamHack(로드맵)

[Web_Hacking] stage3_문제풀이(cookie)

CIDY 2022. 7. 27. 17:57
#!/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계정으로 로그인해야 플래그를 내어주는 구조이다.

 

/페이지는 이전과 다를 바 없고, 로그인 페이지도 이전과 똑같다.

 

코드를 보면 메소드가 GET이냐 POST냐에 따라 행동이 약간 달라진다. GET의 경우 로그인 템플릿을 제공하고, POST의 경우 username과 password변수를 users변수와 비교해서 로그인 할 수 있다.

 

users변수는 위에서 볼 수 있다.

 

근데 얘는 진짜 뭐지 싶은게 쿠키이름이 'username'이고 쿠키값이 username으로 만들어진다.

 

 

ㅋㅋㅋ

 

 

flag