Security/Web hacking

[Dreamhack] wargame cookie 문제풀이

잉퓨_ 2021. 11. 8. 13:33
728x90

문제정보, 접속정보 및 접속파일

이번 문제는 쿠키를 통해 인증상태를 관리하는 로그인 서비스에서, 쿠키를 이용해 관리자 계정으로 로그인에 성공하는 방법을 알아보고자 한다.

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

문제의 코드 파일은 다음과 같다. 위와 같은 코드파일을 하나하나 뜯어보도록 하자..

try:
    FLAG = open('./flag.txt', 'r').read()
except:
    FLAG = '[**FLAG**]'

users = {
    'guest': 'guest',
    'admin': FLAG //관리자
}

위와 같이 봤을때, user는 guest와 admin으로 나뉘는 것을 볼 수 있다. 우선, guest의 경우 ID, PW가 모두 guest일때 풀 수 있는것을 보아 우선 guest로 로그인을 해보고자 한다.

 

guest로 로그인 했을 때의 화면

guest로 로그인을 하게 되면 다음과 같은 화면이 뜬다. 실제로 코드에도 다음과 같이 명시되어있다

@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')

cookie

cookie를 보면 아까 user/user로 로그인 했을 때의 쿠키가 남아있다. 이때 name의 값이 username이고 value가 guest인것을 볼 수 있는데, 코드에 username의 value값이 admin이라면 flag값이 나온다고 명시되어있으니, 본 값을 admin으로 수정해보고자 한다.

 

admin으로 수정한 모습

admin으로 수정하게 되면 다음과 같은 플래그가 뜨고 문제풀이가 완료된다!

728x90