๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[Dreamhack]WebHacking/๋กœ๋“œ๋งต_Basic

[Dreamhack] Level1: Session-basic

by Yun2๐Ÿ‘ 2023. 8. 17.
๋ฐ˜์‘ํ˜•

 

๐Ÿ›Ž๏ธAccess

์ฟ ํ‚ค๋กœ ์ธ์ฆ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋กœ๊ทธ์ธ ์„œ๋น„์Šค์—์„œ

admin ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

 

๐Ÿ‘พExploit Algorithm & Payload

๋”๋ณด๊ธฐ
#!/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
}


# this is our session storage
session_storage = {
}


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


@app.route('/admin')
def admin():
    # developer's note: review below commented code and uncomment it (TODO)

    #session_id = request.cookies.get('sessionid', None)
    #username = session_storage[session_id]
    #if username != 'admin':
    #    return render_template('index.html')

    return session_storage


if __name__ == '__main__':
    import os
    # create admin sessionid and save it to our storage
    # and also you cannot reveal admin's sesseionid by brute forcing!!! haha
    session_storage[os.urandom(32).hex()] = 'admin'
    print(session_storage)
    app.run(host='0.0.0.0', port=8000)

 

#1


@app.route('/admin')
def admin():
    # developer's note: review below commented code and uncomment it (TODO)

    #session_id = request.cookies.get('sessionid', None)
    #username = session_storage[session_id]
    #if username != 'admin':
    #    return render_template('index.html')

    return session_storage

: '/admin' ํŽ˜์ด์ง€๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , admin ๊ณ„์ •์˜ ๊ณ ์œ ํ•œ sessionid๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

#2


: ์‚ฌ์šฉ์ž๋“ค ์ค‘ guest๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธํ•˜์—ฌ guest์˜ ๊ณ ์œ ํ•œ sessionid๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

#3


: #1๊ณผ #2์—์„œ ์–ป์€ ๊ฒƒ์„ ํ†ตํ•ด guest ๊ณ„์ •์˜ sessionid๋ฅผ admin sessionid๋กœ ๋ณ€์กฐ ์‹œ๋„๋ฅผ ํ–ˆ๋‹ค.

 

 

๐Ÿ”‘Analysis and results for obtaining the Flag DH{…}


 

 

๐Ÿ“ŒSummary


session_storage ์ฝ”๋“œ ๋ถ€๋ถ„์—์„œ session_id๋ฅผ ํ‚ค(key)๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์„ธ์…˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ณ  ์žˆ์Œ 

์˜ˆ๋ฅผ๋“ค์–ด์„œ admin ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธ์„ ํ•˜๊ฒŒ ๋˜๋ฉด session_storage์— admin์˜ ์„ธ์…˜ ์ •๋ณด๊ฐ€ ์กฐํšŒ๊ฐ€ ๋˜๋Š” ๊ฒƒ

(guest, user๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž๋„ ๋กœ๊ทธ์ธํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด session_storage์— ํ•จ๊ป˜ ์กฐํšŒ๊ฐ€ ๋จ)

 

๋”ฐ๋ผ์„œ, ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ธ์…˜ ID๋ฅผ ์ƒ์„ฑํ•  ๋•Œ์—๋Š” ์˜ˆ์ธก์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๋žœ๋ค ๊ฐ’์„ ์ƒ์„ฑํ•œ ํ›„ ์ด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€ ์˜ˆ

 

์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ์„ธ์…˜ ์ •๋ณด๊ฐ€ session_storage ๋ณ€์ˆ˜์— ์ €์žฅ๋˜์–ด ๋Œ€์‘๋˜๋Š” ์ด์Šˆ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ

๋ฐ˜์‘ํ˜•

'[Dreamhack]WebHacking > ๋กœ๋“œ๋งต_Basic' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Dreamhack] Level1: csrf-2  (0) 2023.08.21
[Dreamhack] Level1: csrf-1  (0) 2023.08.21
[Dreamhack] Level1: xss-2  (0) 2023.08.19
[Dreamhack] Level1: xss-1  (0) 2023.08.19
[Dreamhack] Level1: cookie  (0) 2023.08.17