Tiny Bunny

Wargame/dreamhack

file-download-1

bento 2023. 10. 3. 17:15

드림핵 

Dreamhack file-download-1

 

File Download Vulnerability

보통 Path Traversal을 이용한 파일 다운로드 취약점이 많음

- 파일 이름을 직접 입력 받아 임의 디렉터리에 있는 파일을 다운로드 받을 수 있는 취약점

ex.


file-download-1

https://dreamhack.io/wargame/challenges/37

 

file-download-1

File Download 취약점이 존재하는 웹 서비스입니다. flag.py를 다운로드 받으면 플래그를 획득할 수 있습니다. Reference Introduction of Webhacking

dreamhack.io

💡 File Download 취약점이 존재하는 웹 서비스입니다.

flag.py를 다운로드 받으면 플래그를 획득할 수 있습니다.

 

초기 페이지

 

그대로 입력해봤다

 

이때 보면 경로가 http://host3.dreamhack.games:10339/read?name=my-first-memo

filename에서 그대로 받아오는 구나 → name 파라미터를 flag.py로 바꿔보기로 시도

 

전체코드

더보기
#!/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)

 

  • /uploads/{filename} 등록되는 경로 확인
`UPLOAD_DIR = 'uploads'`

`@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)`

 

상위 디렉토리로 접근 시도

실패!!

 

  • upload memo 에서 filename 필터링 하는 부분을 발견할 수 있음
if filename.find('..') != -1: return render_template('upload_result.html', data='bad characters,,')

 

name파라미터를 ../flag.py로 바꿔 접근

 

728x90

'Wargame > dreamhack' 카테고리의 다른 글

image-storage  (0) 2023.10.03
command-injection-1  (0) 2023.10.03
MANGO  (0) 2023.10.03
NoSQL  (0) 2023.10.03
simple_sqli  (0) 2023.10.03