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

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

waterproof 2024. 3. 24. 09:59

 

 

 


[1] ๋ฌธ์ œ ํ’€์ด

 

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

 

cookie ๋ฌธ์ œ์™€ ํŽ˜์ด์ง€ ๊ตฌ์„ฑ์ด ๋™์ผํ•˜๋‹ค.

Home ํŽ˜์ด์ง€์™€ AboutํŽ˜์ด์ง€๋Š” ์ฐจ์ด๊ฐ€ ์—†๋‹ค.

(About์„ ๋ˆ„๋ฅด๋ฉด URL๋งํฌ ๋งˆ์ง€๋ง‰์— '#'์ด ๋ถ™๋Š”๋‹ค๋Š” ์ •๋„...?)

 

 

Login ํ™”๋ฉด๊นŒ์ง€ ๋˜‘๊ฐ™์•˜๋‹ค.

์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์—†์–ด์„œ ๋ฐ”๋กœ ๋ฌธ์ œ ํŒŒ์ผ์„ ๋ถ„์„ํ•ด๋ณด์•˜๋‹ค.

 

 

(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',
    'user': 'user1234',
    'admin': FLAG
}

session_storage = {
}

@app.route('/')
def index():
    session_id = request.cookies.get('sessionid', None)
    try:
        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:
            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(4).hex()
            session_storage[session_id] = username
            resp.set_cookie('sessionid', session_id)
            return resp 
        return '<script>alert("wrong password");history.go(-1);</script>'

if __name__ == '__main__':
    import os
    session_storage[os.urandom(1).hex()] = 'admin'
    print(session_storage)
    app.run(host='0.0.0.0', port=8000)

 

 

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

 

๋ถ€๋ถ„์„ ๋ณด๊ณ , username๊ณผ password์— ๊ฐ๊ฐ์˜ ๊ฐ’์„ ์ž…๋ ฅํ•˜์—ฌ ๋กœ๊ทธ์ธ์„ ์‹œ๋„ํ•ด๋ณด์•˜๋‹ค.

 

 

 

guest - guest ๋กœ ๋กœ๊ทธ์ธ์„ ์‹œ๋„ํ•œ ๊ฒฐ๊ณผ / ํŽ˜์ด์ง€์— ์œ„์˜ ํ™”๋ฉด๊ณผ ๊ฐ™์€ ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜ํƒ€๋‚ฌ๋‹ค.

 

username์€ guest์˜€๊ณ ,

sessionid ํ•ญ๋ชฉ์˜ ์ฟ ํ‚ค๊ฐ’์ด ์–ด๋–ค ๊ฐ’์„ ์ธ์ฝ”๋”ฉํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋˜์–ด์žˆ์—ˆ๋‹ค.

 

 

 

 

 

        if pw == password:
            resp = make_response(redirect(url_for('index')) )
            session_id = os.urandom(4).hex()
            session_storage[session_id] = username
            resp.set_cookie('sessionid', session_id)
            return resp 
        return '<script>alert("wrong password");history.go(-1);</script>'



if __name__ == '__main__':
    import os
    session_storage[os.urandom(1).hex()] = 'admin'
    print(session_storage)
    app.run(host='0.0.0.0', port=8000)

 

 

app.py์—์„œ ์ด ๋ถ€๋ถ„์„ ๋ณด๋ฉด, ์ž˜์€ ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜... session_id๊ฐ€ ์–ด๋– ํ•œ ๊ทœ์น™์— ์˜ํ•ด ๊ฐ’์ด ์กฐ์ž‘๋˜์–ด ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์˜€๋‹ค.

 

 

์ด ์ด์ƒ์œผ๋กœ๋Š” ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ ์—†์–ด์„œ ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

 


์ฐพ์•„๋ณด๋‹ˆ,

        if pw == password:
            resp = make_response(redirect(url_for('index')) )
            session_id = os.urandom(4).hex()
            session_storage[session_id] = username
            resp.set_cookie('sessionid', session_id)
            return resp 
        return '<script>alert("wrong password");history.go(-1);</script>'



if __name__ == '__main__':
    import os
    session_storage[os.urandom(1).hex()] = 'admin'
    print(session_storage)
    app.run(host='0.0.0.0', port=8000)

 

์ด ์ฝ”๋“œ ๋ถ€๋ถ„์—์„œ os.urandom(4).hex() ํ•จ์ˆ˜๋Š” 4๋ฐ”์ดํŠธ์˜ ๋ฌด์ž‘์œ„ ๋ฐ”์ดํŠธ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๊ณ , ์ด๋ฅผ 16์ง„์ˆ˜ ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰, 8๊ฐœ์˜ 16์ง„์ˆ˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ƒ์„ฑ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด์€ ์„ธ์…˜ ID๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

 

๊ทธ๋Ÿฐ ๋‹ค์Œ, ์„ธ์…˜ ID๋ฅผ ํ‚ค๋กœ ํ•˜์—ฌ session_storage ๋”•์…”๋„ˆ๋ฆฌ์— ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ์ €์žฅํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ด๋‹น ์„ธ์…˜ ID๊ฐ€ ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•œ ์‚ฌ์šฉ์ž์˜ ์„ธ์…˜์„ ๋‚˜ํƒ€๋‚ด๊ฒŒ ๋œ๋‹ค. (์„ธ์…˜ ID๋Š” ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฟ ํ‚ค๋กœ ์ „์†ก๋˜์–ด ๋‚˜์ค‘์— ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ์„ธ์…˜์„ ์‹๋ณ„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.)

 

๋”ฐ๋ผ์„œ ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•œ ์‚ฌ์šฉ์ž์˜ ์„ธ์…˜ ์ •๋ณด๊ฐ€ session_storage์— ์ €์žฅ๋˜๊ณ , ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ๋Š” ํ•ด๋‹น ์„ธ์…˜ ID๊ฐ€ ์„ธ์…˜ ์ฟ ํ‚ค๋กœ ์„ค์ •๋˜์–ด ๋ฐ˜ํ™˜๋œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„๋Š” ์ดํ›„ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ํ•ด๋‹น ์„ธ์…˜์„ ์‹๋ณ„ํ•˜๊ณ  ์‚ฌ์šฉ์ž๋ฅผ ์ธ์ฆํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์ด ์ฝ”๋“œ์—์„œ๋Š” ๊ด€๋ฆฌ์ž(admin)์˜ ์„ธ์…˜ ID๋ฅผ 1๋ฐ”์ดํŠธ(์ฆ‰, 2์ž๋ฆฌ 16์ง„์ˆ˜)์˜ ๋ฌด์ž‘์œ„ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ 1๋ฐ”์ดํŠธ๋กœ๋Š” ๊ฐ€๋Šฅํ•œ ๊ฐ’์ด 256๊ฐ€์ง€๋ฟ์œผ๋กœ ๋งค์šฐ ์ ์€ ํŽธ์ด๋‹ค.

์ฆ‰, ๊ณต๊ฒฉ์ž๋Š” 256๋ฒˆ ์‹œ๋„ํ•˜๋ฉด์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์„ธ์…˜ ID๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์ฐพ์€ ์„ธ์…˜ ID๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ด€๋ฆฌ์ž ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

(โญ ๋”ฐ๋ผ์„œ ์ด ์ฝ”๋“œ๋Š” ๋ณด์•ˆ์ƒ ์ทจ์•ฝ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์„ธ์…˜ ID๋Š” ๋งค์šฐ ์˜ˆ์ธก์ด ์–ด๋ ต๊ณ  ์•ˆ์ „ํ•œ ๊ฐ’์ด์–ด์•ผ ํ•œ๋‹ค. 1๋ฐ”์ดํŠธ๋กœ๋Š” ์•ˆ์ „ํ•œ ์„ธ์…˜ ID๋ฅผ ๋งŒ๋“ค๊ธฐ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•˜๋ ค๋ฉด ๋” ๋งŽ์€ ๋น„ํŠธ(๋ฐ”์ดํŠธ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌด์ž‘์œ„ํ•œ ๊ฐ’์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.)

 


burp suite ์˜ intruder๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜์—ฌ ๋ฌด์ž‘์œ„ ๋Œ€์ž… ๊ณต๊ฒฉ์„ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

 


 

/index ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋Š” ํŒจํ‚ท์„ ์บก์ฒ˜ํ•˜์˜€๋‹ค.

 

์›๋ž˜ ํ’€์ด๋Œ€๋กœ๋ผ๋ฉด ์ฟ ํ‚ค๊ฐ’๋„ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ...

์™œ ๋‚˜๋Š” ๋‚˜์˜ค์ง€ ์•Š๋Š” ๊ฑด์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค.

 

์ž„์˜๋กœ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์•˜๋‹ค.

 

 

 

 

๊ณต๊ฒฉ์„ ์‹œ๋„ํ•ด๋ณด์•˜๋‹ค.

 

 

 

์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒŒ ๋งž๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ’์„ ์ฐพ์•„๋ƒˆ๋‹ค.

 

 

๊ฐ’์„ ์ž…๋ ฅํ•˜์—ฌ ํ”Œ๋ž˜๊ทธ ๊ฐ’์„ ์ฐพ์•„๋ƒˆ๋‹ค.

 

 

 

ํ’€์ด ์„ฑ๊ณต!

 


[2] ๋Š๋‚€ ์ 

 

์„ธ์…˜ ID๊ฐ€ ๋„ˆ๋ฌด ์งง๊ณ  ์˜ˆ์ธก ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ๊ณต๊ฒฉ์ž๊ฐ€ ํ•ด๋‹น ์„ธ์…˜ ID๋ฅผ ์ถ”์ธกํ•˜์—ฌ ๊ด€๋ฆฌ์ž ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์„ธ์…˜ ID๋Š” ๋งค์šฐ ์˜ˆ์ธก์ด ์–ด๋ ค์šฐ๋ฉฐ, ์•ˆ์ „ํ•œ ๋ฌด์ž‘์œ„ ๊ฐ’์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ ์ ˆํ•œ ๋ณด์•ˆ ์ˆ˜์ค€์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋” ๋งŽ์€ ๋น„ํŠธ(๋ฐ”์ดํŠธ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌด์ž‘์œ„ํ•œ ๊ฐ’์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค.