๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Web Hacking/Dreamhack

[Dreamhack] Session ๋“œ๋ฆผํ•ต ์›Œ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์ด

by A Lim Han 2023. 8. 12.

๐ŸŽž๏ธ Session ๋“œ๋ฆผํ•ต ์›Œ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์ด

1. ๋ฌธ์ œ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ฝ์€ ํ›„ ๋ฌธ์ œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ

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**]'

 

์ฝ”๋“œ ์„ค๋ช…
from flask import Flask, request, render_template,
make_response, redirect, url_for
์›น ์„œ๋ฒ„ ์ƒ์„ฑ, ์š”์ฒญ ์ฒ˜๋ฆฌ, ํ…œํ”Œ๋ฆฟ ๋ Œ๋”๋ง ๋“ฑ์—
ํ•„์š”ํ•œ ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ
app = Flask(__name__) Flask ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ
FLAG = open('./flag.txt', 'r').read() ํŒŒ์ผ์„ ์—ด์–ด FLAG ๊ฐ’ ์ฝ์–ด์˜ค๊ธฐ
except ํŒŒ์ผ์„ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์ธ FLAG ์‚ฌ์šฉ

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

session_storage = {
}

 

์ฝ”๋“œ ์„ค๋ช…
users = {...} ์‚ฌ์šฉ์ž๋ช…๊ณผ ํŒจ์Šค์›Œ๋“œ ์ •๋ณด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅ
session_storage = {...} ์‚ฌ์šฉ์ž ์„ธ์…˜ ์ •๋ณด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅํ•˜๊ณ , ์„ธ์…˜ ID์™€
์‚ฌ์šฉ์ž๋ช…์„ ๋งคํ•‘ํ•˜์—ฌ ๋กœ๊ทธ์ธ๋œ ์‚ฌ์šฉ์ž ์ถ”์ 

@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('/') ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ Root ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ํ•ธ๋“ค๋Ÿฌ ์ •์˜
index() ์ธ๋ฑ์Šค ํŽ˜์ด์ง€ ๋ฐ˜ํ™˜ ํ•จ์ˆ˜
(๋กœ๊ทธ์ธ๋œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ด€๋ฆฌ์ž์ธ ๊ฒฝ์šฐ FLAG ํ‘œ์‹œ)

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

 

์ฝ”๋“œ ์„ค๋ช…
@app.route('/login', methods=['GET', 'POST']) ๋กœ๊ทธ์ธ ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ํ•ธ๋“ค๋Ÿฌ ์ •์˜

A) GET ๋ฐฉ์‹ ์š”์ฒญ
  -->  Login ํŽ˜์ด์ง€ ๋ Œ๋”๋ง

B) POST ๋ฐฉ์‹ ์š”์ฒญ
-->  ์‚ฌ์šฉ์ž ์ธ์ฆ ์ฒ˜๋ฆฌ
login() ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜
(๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ ์„ธ์…˜ ์ƒ์„ฑ ๋ฐ index ํŽ˜์ด์ง€๋กœ ์žฌ์—ฐ๊ฒฐ)

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

 

์ฝ”๋“œ ์„ค๋ช…
if __name__ == '__main__': ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ง์ ‘ ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ, ํ•˜๋‹จ์˜ ์ฝ”๋“œ ์‹คํ–‰
session_storage[os.urandom(1).hex()] = 'admin' ์ดˆ๊ธฐ ๊ด€๋ฆฌ์ž ์„ธ์…˜ ์„ค์ •์„ ์œ„ํ•ด
Session ์ €์žฅ์†Œ์— admin ์‚ฌ์šฉ์ž ์ถ”๊ฐ€
app.run(host='0.0.0.0', port=8000) ์›น ์„œ๋ฒ„ ์‹คํ–‰

3. ๋‹ค์‹œ ๋ฌธ์ œ ํŽ˜์ด์ง€๋กœ ๋Œ์•„์™€ ์„œ๋ฒ„ ์ƒ์„ฑ

4. ๋ฒ„ํ”„ ์Šค์œ„ํŠธ(Burp Suite)๋ฅผ ์‹คํ–‰ํ•œ ํ›„, ์ƒ์„ฑ๋œ ๋งํฌ๋ฅผ ๋ธŒ๋ผ์šฐ์ €๋กœ ์—ด๊ธฐ

 

++ Burp Suite ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•œ ์›น ํŽ˜์ด์ง€ ์ ‘์† 

-->  https://alim11.tistory.com/203

 

[Burp Suite] ๋ฒ„ํ”„์Šค์œ„ํŠธ์—์„œ ํ”„๋ก์‹œ ์„œ๋ฒ„ ์“ฐ์ง€ ์•Š๊ณ  ์›น ํŽ˜์ด์ง€ ์—ฌ๋Š” ๋ฒ•

# ๋ฒ„ํ”„ ์Šค์œ„ํŠธ์—์„œ ํ”„๋ก์‹œ ์„œ๋ฒ„๋ฅผ ์กฐ์ž‘ํ•˜์ง€ ์•Š๊ณ  ์›น ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋Š” ๋ฒ• 1. ๋ฒ„ํ”„ ์Šค์œ„ํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค. 2. Proxy ํƒญ ํด๋ฆญ ํ›„ Options๋ž€์œผ๋กœ ์ด๋™ 3. Options๋ž€์—์„œ ์˜ต์…˜ ์„ค์ •์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•œ๋‹ค. (

alim11.tistory.com

5. Login ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•œ ํ›„, ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜์—ฌ guest๋กœ ๋กœ๊ทธ์ธ

6. ๋‹ค์‹œ ๋ฒ„ํ”„ ์Šค์œ„ํŠธ(Burp Suite)๋กœ ๋Œ์•„์™€, ์ตœ์ƒ๋‹จ์˜ History ํด๋ฆญ

 

7. ๋งˆ์šฐ์Šค ์šฐ์ธก ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ , Send to Intruder ํด๋ฆญ 

 

8. ์ƒ๋‹จ์˜ Intruder ํƒญ์œผ๋กœ ์ด๋™ ํ›„, Positions & Payloads ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •

๊ณต๊ฒฉ ํ˜•์‹์€ Sniper

 

๊ณต๊ฒฉํ•  Target ํ™•์ธ

(ํƒ€๊นƒ์€ ์ด์ „์— ์ƒ์„ฑํ•œ ๋งํฌ์™€ ๋™์ผํ•ด์•ผ ํ•จ)

 

Payload set๋Š” 1, ํƒ€์ž…์€ Brute forcer

 

Character set ์€ a ~ e & 1 ~ 9

+

Min & Max length๋Š” ๋ชจ๋‘ 2 ๋กœ ์„ค์ •

9. Start Attack ํด๋ฆญ ํ›„, ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ํŒจํ‚ท์ด ๋‚˜ํƒ€๋‚  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ

 

10. ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ 97๋ฒˆ ํŒจํ‚ท์˜ Sesson ID ํ™•์ธ

 

97๋ฒˆ ํŒจํ‚ท์˜ Sesson ID๋Š” a0

11. ๋‹ค์‹œ ์ƒ์„ฑํ•œ ๋งํฌ๋กœ ๋Œ์•„์™€ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ ์˜คํ”ˆ

12. ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์˜ Applicaton ํƒญ์œผ๋กœ ์ด๋™ ํ›„ Sesson Storage ํด๋ฆญ

13. guest ์˜ Session ID๋ฅผ 10๋ฒˆ์—์„œ ์ฐพ์€ ID๋กœ ๋ณ€๊ฒฝ

 

14. ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ๋‹ซ๊ณ  ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ  -->  FLAG๊ฐ€ ์ถœ๋ ฅ๋จ์„ ํ™•์ธ