SWLUG/CTF ๋ฌธ์ œ ํ’€์ด (2)

[Dreamhack/๋“œ๋ฆผํ•ต] cookie

waterproof 2024. 3. 23. 16:18

 

 


[1] ๋ฌธ์ œ ๋ถ„์„

 

(1) ์›น ํŽ˜์ด์ง€ ๋ถ„์„

 

 

 

๋ฌธ์ œ ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐ€๋ฉด ๋‚˜์˜ค๋Š” ํŽ˜์ด์ง€์ด๋‹ค.

 

์œ„์˜ ํŽ˜์ด์ง€ ๋ชฉ๋ก ์ค‘์— Home, About ์„ ํด๋ฆญํ•ด๋„ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜์˜จ๋‹ค.

 

 

 

 

 

Login ์„ ๋ˆ„๋ฅด๋ฉด 

์œ„์™€ ๊ฐ™์€ ํŽ˜์ด์ง€๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

 

 

 

username์— 1234

password์— 1234๋ฅผ ์ž…๋ ฅํ•˜์˜€๋”๋‹ˆ ์œ„์™€ ๊ฐ™์€ ๋ฉ”์„ธ์ง€ ํŒ์—…์ฐฝ์ด ๋‚˜์™”๋‹ค.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋ฌธ์ œ์—์„œ "admin ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด ..." ์ด๋ผ๊ณ  ํ–ˆ๋˜ ๋ฌธ๊ตฌ๋ฅผ ๋– ์˜ฌ๋ ค,

username์— admin์„,

password์— admin์„ ์ž…๋ ฅํ•˜์˜€๋‹ค.

 

๊ทธ๋žฌ๋”๋‹ˆ "wrong password"๋ผ๋Š” ๋ฉ”์„ธ์ง€ ํŒ์—…์ฐฝ์ด ๋‚˜ํƒ€๋‚ฌ๋‹ค.

 

์•„๊นŒ๋Š” "not found user"๋ผ๊ณ  ํ–ˆ๋Š”๋ฐ, ์ด๋ฒˆ์—” ํ‹€๋ฆฐ ํŒจ์Šค์›Œ๋“œ๋ผ๋Š” ์•Œ๋ฆผ์ด ๋œจ๋Š” ๊ฑธ๋กœ ๋ด์„œ, username์ด admin์€ ๋งž๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

 

(2) ๋ฌธ์ œ ํŒŒ์ผ ๋ถ„์„

โš™๏ธ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',
    '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)

 

 

๋‹ค์šด๋ฐ›์€ app.py ํŒŒ์ผ์ด๋‹ค.

 

 

 

users = {
    'guest': 'guest',
    'admin': FLAG
}

 

 

์œ„์˜ ๋ถ€๋ถ„์„ ๋ณด๋ฉด username์„ guest, password๋ฅผ guest๋กœ ๋กœ๊ทธ์ธ์„ ์‹œ๋„ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

 

 

 

guest, guest๋กœ ๋กœ๊ทธ์ธ์„ ์‹œ๋„ํ–ˆ๋”๋‹ˆ ์„ฑ๊ณตํ–ˆ๋‹ค.

์ฟ ํ‚ค ๊ฐ’์— 'username' ๋ถ€๋ถ„์— 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')

 

 

app.py์˜ ์ผ๋ถ€๋ถ„์ธ๋ฐ,

/ ํŽ˜์ด์ง€์—์„œ, 'username' ๋ถ€๋ถ„์—์„œ ์ฟ ํ‚ค๋ฅผ ๊ฐ€์ ธ์™€ username์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๊ณ ,

๊ทธ username์˜ ๊ฐ’์ด "admin"์ผ ๊ฒฝ์šฐ Hello username flag is FLAG ๋ผ๋Š” ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

username์˜ ๊ฐ’์ด "admin"์ด ์•„๋‹ ๊ฒฝ์šฐ you are not admin์ด๋ผ๋Š” ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

์‹ค์ œ๋กœ username์„ guest๋กœ ํ•˜์—ฌ ๋กœ๊ทธ์ธ์„ ์‹œ๋„ํ–ˆ์„ ๋•Œ, you are not admin์ด๋ผ๋Š” ๋ฌธ๊ตฌ๋ฅผ / ํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

๊ทธ๋ ‡๋‹ค๋ฉด, username์— ์ €์žฅ๋œ ์ฟ ํ‚ค ๊ฐ’์„ admin์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ฒ ๋‹ค.

 

 

 

 

 

์ฟ ํ‚ค์„ ์ž„์˜๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด flag ๊ฐ’์„ ํš๋“ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.