워게임 스터디

[241112][dreamhack] Cookie

jisu0924 2024. 11. 12. 21:01

웹해킹 문제 사이트로 들어가니 (http://host3.dreamhack.games:24341/)

 

이런 창이 떴다.

 

Login 창이 있어서 클릭 후

아무거나 입력 했더니

이런식으로 떴다.

 

뭔지 모르겠어서 사이트에서 같이 준 코드를 확인해 봤다.

 

#!/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)

읽어보니 

guest와 admin 계정이 있는 건가 근데 admin 계정은 FLAG를 모르니? 아는 건 guest 뿐 

  username = request.cookies.get('username', None)

username은 cookie와 관련돼 있나보다. 현재는 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')

만약 username일 경우? Hello {username}을 띄워주고 만약 그 username이 admin이면 FLAG를 띄워주고 아니면 you are not admin이라는 문구를 보여주는 것이다. 밑으로 내려가서

if pw == password:
            resp = make_response(redirect(url_for('index')) )
            resp.set_cookie('username', username)

이 부분을 보면 pw가 password면 cookie가 username으로 지정된다는 뜻인가? 

 

우선 개발자창 열어서 쿠키창 가봤다.

비어있음. 그래서 아까 코드에서 확인한 guest 계정으로 로그인해보자.

아까 설명한 you are not admin이 떴다. username이 admin이 아니기 때문이다. 다시 개발자창 들어가서 cookie 확인해 주니까

username이 guest로 설정돼 있다. 이것을 

바꿔주고 새로고침 하면 flag is + FLAG 가 뜰 것

이걸 이제 Dreamhack 사이트 가서 입력하면 풀린다.

'워게임 스터디' 카테고리의 다른 글

[241119][dreamhack] command-injection-1  (2) 2024.11.20
[241119][dreamhack] devtools-sources  (0) 2024.11.20
[241112][SuNiNaTaS] level 3  (0) 2024.11.12
[241104][SuNiNaTaS] level 2  (0) 2024.11.05
[241104][SuNiNaTaS] level 1  (0) 2024.11.04