πŸ” Security/Web

[λ“œλ¦Όν•΅(Dreamhack)] file-download-1

Rosieblue 2023. 9. 26. 17:34
728x90

 

슀크립트 νŒŒμΌμ„ μž…λ ₯을 ν–ˆλ”λ‹ˆ 싀행이 λ˜μ§€ μ•Šμ•˜λ‹€. 이와 같은 νƒœκ·ΈλŠ” μš°νšŒν•˜κ³  μžˆλ‚˜λ³΄λ‹€

 

 

 

μ΄λŸ°μ‹μœΌλ‘œ 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μ˜€λ”° γ…‹γ…‹γ…‹ γ…‡ν•˜

 

 

그런데 μ΄λ ‡κ²Œ ν•˜λ©΄ 또 μ•ˆ λ˜λ”λΌ...