[λλ¦Όν΅(Dreamhack)] file-download-1
μ€ν¬λ¦½νΈ νμΌμ μ λ ₯μ νλλ μ€νμ΄ λμ§ μμλ€. μ΄μ κ°μ νκ·Έλ μ°ννκ³ μλ보λ€
μ΄λ°μμΌλ‘ path traversalμ μλν΄λ³΄μλλ° μλμλ€
μ½λκ° μ΄λ»κ² μ΄λ£¨μ΄μ Έμλμ§ μ΄μ λ―μ΄λ³΄λλ‘ νκ² λ€
#!/usr/bin/env python3
import os
import shutil
from flask import Flask, request, render_template, redirect
from flag import FLAG
APP = Flask(__name__)
UPLOAD_DIR = 'uploads'
@APP.route('/')
def index():
files = os.listdir(UPLOAD_DIR)
return render_template('index.html', files=files)
@APP.route('/upload', methods=['GET', 'POST'])
def upload_memo():
if request.method == 'POST':
filename = request.form.get('filename')
content = request.form.get('content').encode('utf-8')
if filename.find('..') != -1:
return render_template('upload_result.html', data='bad characters,,')
with open(f'{UPLOAD_DIR}/{filename}', 'wb') as f:
f.write(content)
return redirect('/')
return render_template('upload.html')
@APP.route('/read')
def read_memo():
error = False
data = b''
filename = request.args.get('name', '')
try:
with open(f'{UPLOAD_DIR}/{filename}', 'rb') as f:
data = f.read()
except (IsADirectoryError, FileNotFoundError):
error = True
return render_template('read.html',
filename=filename,
content=data.decode('utf-8'),
error=error)
if __name__ == '__main__':
if os.path.exists(UPLOAD_DIR):
shutil.rmtree(UPLOAD_DIR)
os.mkdir(UPLOAD_DIR)
APP.run(host='0.0.0.0', port=8000)
νμΌμ μ κ·Όν λ render_template('read.html',filename=filename, content=data.decode('utf-8'), error=error) λ₯Ό μ€ννκ³ μλ€.
μλλ read.htmlμ΄λ€
{% extends "base.html" %}
{% block title %}socket{% endblock %}
{% block head %}
{{ super() }}
{% endblock %}
{% block content %}
{% if error %}
<h1>{{ filename }} does not exist. :(</h1>
{% else %}
<h1>{{ filename }} Memo</h1><br/>
<div class="row">
<div class="col-md-12 form-group">
<label for="FileData">Content</label>
<textarea id="FileData" class="form-control" rows="5" name="content" readonly>{{ content }}</textarea>
</div>
</div>
{% endif %}
{% endblock %}
with open(f'{UPLOAD_DIR}/{filename}', 'wb') as f:
f.write(content)
λΌκ³ μ¨μμΌλκΉ ../~ μ΄λ°μμΌλ‘ path traversalμ μ΄μ©νλ©΄ λ λ―νλ€
μκΉλ μ μλμ§γ £..?
λ°λ³΄μλ€. flag.txtκ° μλλΌ flag.pyμλ° γ γ γ γ ν
κ·Έλ°λ° μ΄λ κ² νλ©΄ λ μ λλλΌ...