๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[Dreamhack]WebHacking/๋กœ๋“œ๋งต_Basic

[Dreamhack] Level1: file-download-1

by Yun2๐Ÿ‘ 2023. 9. 3.
๋ฐ˜์‘ํ˜•

๐Ÿ›Ž๏ธAccess

File Download ์ทจ์•ฝ์ ์ด ์กด์žฌํ•˜๋Š” ์›น ์„œ๋น„์Šค์ด๋‹ค.

flag.py๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ๋ฉด ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๐Ÿ‘พExploit Algorithm & Payload

๋”๋ณด๊ธฐ
#!/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)

 

 

#1


: '/upload' ํŽ˜์ด์ง€์—์„œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ ๋ฉ”๋ชจ๋ฅผ ์—…๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€์ด๋‹ค.

 

: '/read' ํŽ˜์ด์ง€์—์„œ๋Š” uploadํ•œ ํŒŒ์ผ๋“ค์ด Filename(test1)์˜ ์ด๋ฆ„ ํ˜•ํƒœ๋กœ ๋งํฌ๋˜์–ด ์žˆ๋‹ค.

: ํ•ด๋‹น ๋งํฌ๋ฅผ ํ™•์ธํ•˜๋ฉด ๋ฐฉ๊ธˆ ์ž‘์„ฑํ•œ ๋ฉ”๋ชจ๊ฐ€ ์—ฐ๊ฒฐ๋œ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , GET์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•ํƒœ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
(read?name=[ํŒŒ์ผ๋ช…]์˜ ๊ตฌ์กฐ)

 

: ์ฆ‰, ํ•ด๋‹น ํŒŒ์ผ ์—…๋กœ๋“œ ๋ฐ ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ๋ฅผ ์กฐ์ž‘ํ•˜๋ฉด(file vulnerability) FLAG๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

#2


: burp suite ๋„๊ตฌ๋ฅผ ์ด์šฉํ•˜์—ฌ uploadํ–ˆ์„ ๊ฒฝ์šฐ์˜ ํŒจํ‚ท์„ ๋ถ„์„ํ–ˆ๋‹ค.

: POST๋ฐฉ์‹์œผ๋กœ filename, content๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค.

 

: ์—…๋กœ๋“œ ๊ฒฐ๊ณผ๋ฅผ Proxyํ•˜์—ฌ ํŒจํ‚ท์„ ๋ถ„์„ํ•  ๊ฒฝ์šฐ ๋ฐฉ๊ธˆ ์ „์— ์ž…๋ ฅํ–ˆ๋˜ ํ•ด๋‹น filename๊ณผ content๋ฅผ ์‘๋‹ต๊ฐ’์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

: ์ด์— read?name=test1์˜ ํŒŒ์ผ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ , ํ•ด๋‹น ์„œ๋ฒ„์˜ ์œ„์น˜์— ํŒŒ์ผ์ด ์กด์žฌํ•˜๋ฉด ํ™•์ธ ๊ฐ€๋Šฅํ•  ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ–ˆ๋‹ค.

('../',  './' , '~', ... )

 

 

#3


: ์„œ๋ฒ„์— ํŒŒ์ผ ์žˆ์„๊ฒƒ์ด๋ผ ํŒ๋‹จ๋˜๋Š” ๊ฒƒ์„ ์ž…๋ ฅํ•˜์—ฌ ์ƒ์œ„ ํด๋”๋กœ ์ด๋™ํ•˜๋ฉฐ ๋™์ž‘ ์‹œ์ผฐ๋”๋‹ˆ ๊ฐ’์„ ํ™•์ธํ•   ์ˆ˜ ์žˆ์—ˆ๋‹ค.

: ์ด์ œ FLAG๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ flag.py๋ฅผ ์ฐพ์œผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

(์ฃผ์š” ํŒŒ์ผ ๊ฒฝ๋กœ ์ค‘ ํ•˜๋‚˜์ธ /etc/passwd๋ฅผ ์—ด๋žŒ)

(์›น ์„œ๋ฒ„๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋ฒ—์–ด๋‚˜ ์ž„์˜์˜ ์œ„์น˜์— ์žˆ๋Š” ํŒŒ์ผ๊นŒ์ง€ ์—ด๋žŒ๋˜์–ด ์„œ๋ฒ„ ์ฃผ์š” ํŒŒ์ผ์ด ๋…ธ์ถœ๋˜๋Š” ์œ„ํ—˜)

 

 

๐Ÿ”‘Analysis and results for obtaining the Flag DH{…}


: '../flag.py' ๊ฒฝ๋กœ์—์„œ FLAG๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

๐Ÿ“ŒSummary


File Vulnerability(File Download)

-๋ฐœ์ƒ ์›์ธ-
1) ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ์‹œ ํŒŒ์ผ์˜ ์ ˆ๋Œ€๊ฒฝ๋กœ ๋˜๋Š” ์ƒ๋Œ€ ๊ฒฝ๋กœ๊ฐ€ ๋…ธ์ถœ
2) ๋‹ค์šด๋กœ๋“œ ๋ชจ๋“ˆ์ด ํŒŒ์ผ์˜ ๊ฒฝ๋กœ, ์ด๋ฆ„, ํŒŒ๋ผ๋ฏธํ„ฐ, …๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ
3) ํŒŒ์ผ ๊ฒฝ๋กœ์™€ ํŒŒ์ผ๋ช… ํ•„ํ„ฐ๋ง ๋ฏธํก( ‘.’ , ‘..’ , ‘~’ ,…)
4) ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ๊ฐ€ ๋…ธ์ถœ๋˜์ง€ ์•Š๋”๋ผ๋„ ๊ตฌ์กฐ๊ฐ€ ๋‹จ์ˆœํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€์กฐ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š์€ ํŒŒ์ผ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ

...

-๋Œ€์‘ ๋ฐฉ์•ˆ-
1) ํŒŒ์ผ๋ช…๊ณผ ๊ฒฝ๋กœ๋ช…์„ DB์—์„œ ๊ด€๋ฆฌ
2) ํ—ˆ์šฉ๋˜์ง€ ์•Š์€ ํŒŒ์ผ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋„๋ก ๋‚ฎ์€ ๊ถŒํ•œ์œผ๋กœ ์„œ๋น„์Šค ์šด์˜
3) ๊ฒฝ๋กœ ์ด๋™ ๋ฌธ์ž์—ด์„ ํ•„ํ„ฐ๋งํ•˜์—ฌ ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ฐจ๋‹จ
4) ์„œ๋น„์Šค๊ฐ€ ํŠน์ • ํ™•์žฅ์ž์˜ ํŒŒ์ผ๋งŒ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๋Š”๋‹ค๋ฉด ๋‹ค์šด๋กœ๋“œ ํŒŒ์ผ์— ๋Œ€ํ•œ ํ™•์žฅ์ž ๊ฒ€์ฆ ์ˆ˜ํ–‰
5) ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ๋ฅผ staticํ•˜๊ฒŒ ์„ค์ •ํ•˜์—ฌ, ๋‹ค๋ฅธ ๋””๋ ‰ํ† ๋ฆฌ์— ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ฐจ๋‹จ

...

๋ฐ˜์‘ํ˜•

'[Dreamhack]WebHacking > ๋กœ๋“œ๋งต_Basic' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Dreamhack] Level2: blind-command  (0) 2024.01.05
[Dreamhack] Level2: web-ssrf  (0) 2023.09.09
[Dreamhack] Level1: image-storage  (0) 2023.08.26
[Dreamhack] Level1: command-injection-1  (0) 2023.08.24
[Dreamhack] Level2: Mango  (0) 2023.08.23