๐Ÿ”’ Cyber Security/Web Hacking (์›นํ•ดํ‚น)

[DreamHack] ๋“œ๋ฆผํ•ต ์›นํ•ดํ‚น: command-injection-1

์„ ๋‹ฌ 2023. 7. 5. 16:55
๋ฐ˜์‘ํ˜•

 

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

 

command-injection-1

ํŠน์ • Host์— ping ํŒจํ‚ท์„ ๋ณด๋‚ด๋Š” ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. Command Injection์„ ํ†ตํ•ด ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•˜์„ธ์š”. ํ”Œ๋ž˜๊ทธ๋Š” flag.py์— ์žˆ์Šต๋‹ˆ๋‹ค. Reference Introduction of Webhacking

dreamhack.io

 

๋ฌธ์ œ

ํŠน์ • Host์— ping ํŒจํ‚ท์„ ๋ณด๋‚ด๋Š” ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.
Command Injection์„ ํ†ตํ•ด ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•˜์„ธ์š”. ํ”Œ๋ž˜๊ทธ๋Š” flag.py์— ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ฌธ์ œํŒŒ์ผ

๋”๋ณด๊ธฐ
#!/usr/bin/env python3
import subprocess

from flask import Flask, request, render_template, redirect

from flag import FLAG

APP = Flask(__name__)


@APP.route('/')
def index():
    return render_template('index.html')


@APP.route('/ping', methods=['GET', 'POST'])
def ping():
    if request.method == 'POST':
        host = request.form.get('host')
        cmd = f'ping -c 3 ""; cat "flag.py"'
        try:
            output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
            return render_template('ping_result.html', data=output.decode('utf-8'))
        except subprocess.TimeoutExpired:
            return render_template('ping_result.html', data='Timeout !')
        except subprocess.CalledProcessError:
            return render_template('ping_result.html', data=f'an error occurred while executing the command. -> {cmd}')

    return render_template('ping.html')


if __name__ == '__main__':
    APP.run(host='0.0.0.0', port=8000)

 

ํ’€์ด

์ ‘์† ํ›„ ping ํƒญ์— ๋“ค์–ด๊ฐ€๋ฉด ์ธํ’‹์— ํ˜ธ์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•ด์„œ ping์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

๋‹น์—ฐํžˆ ping์„ ๋ณด๋‚ผ๋•Œ๋Š” ์‹œ์Šคํ…œ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์šฐ์„  app.py ํŒŒ์ผ์„ ํ•œ๋ฒˆ ๋œฏ์–ด๋ณด์ž

 

@APP.route('/ping', methods=['GET', 'POST'])
def ping():
    if request.method == 'POST':
        host = request.form.get('host')
        cmd = f'ping -c 3 "{host}"'
        try:
            output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
            return render_template('ping_result.html', data=output.decode('utf-8'))
        except subprocess.TimeoutExpired:
            return render_template('ping_result.html', data='Timeout !')
        except subprocess.CalledProcessError:
            return render_template('ping_result.html', data=f'an error occurred while executing the command. -> {cmd}')

    return render_template('ping.html')

 

์œ ์ €๊ฐ€ ์ธํ’‹์— ๋„ฃ์€ ๊ฐ’์„ host๋กœ ๋ฐ›์•„์„œ ํฌํ•จ์‹œํ‚จ ์‹œ์Šคํ…œ ๋ช…๋ น์–ด ping์„ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋กœ์ง์ด๋‹ค

 

์šฐ๋ฆฌ๊ฐ€ flag.py์— ์žˆ๋Š” ํ”Œ๋ž˜๊ทธ๋ฅผ ๊ฒฐ๊ณผ๋กœ์„œ ์ถœ๋ ฅ์‹œํ‚ค๋ ค๋ฉด

cat flag.py

์š” ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

 

"; cat "flag.py"
    if request.method == 'POST':
        host = request.form.get('host')
        cmd = f'ping -c 3 ""; cat "flag.py"'
        try:

ํ˜น์‹œ๋ชฐ๋ผ "๋กœ ํฐ๋”ฐ์˜ดํ‘œ๋ฅผ ๋‹ซ์•„์ฃผ๊ณ 

; ๋กœ ์•ž ๋ช…๋ น์–ด์™€ ๊ตฌ๋ถ„ํ•ด์ฃผ๊ณ 

๋ช…๋ น์–ด๋ฅผ ๋„ฃ์–ด์ฃผ๋Š”๋ฐ, ์ด๋•Œ ๋ฌด์กฐ๊ฑด ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์˜ ๋งจ ๋’ค์— "๊ฐ€ ํฌํ•จ๋˜์–ด ์‹คํ–‰๋˜๊ธฐ ๋–„๋ฌธ์— flag.py ์•ž์— ํฐ ๋”ฐ์˜ดํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•ด์คฌ๋‹ค.

์•ˆ๊ทธ๋Ÿฌ๋ฉด ping -c 3 ""; cat flag.py" ๊ฐ€ ๋˜์–ด๋ฒ„๋ ค์„œ ์˜ค๋ฅ˜ ๋œธ

 

์ด์ œ ์‹คํ–‰ํ•ด์ฃผ์ž

 

 

Oh.

์š”์ฒญํ•œ ํ˜•์‹๊ณผ ์ผ์น˜์‹œํ‚ค๋ผ๋Š” ๊ฒฝ๊ณ ์™€ ํ•จ๊ป˜ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ž…๋ ฅ๊ฐ’์ด ํผ์— ์ œ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.

ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋‹ˆ ์ € ๊ฒฝ๊ณ ๋ฌธ์˜ ๋””์ž์ธ์ด ๋งค์šฐ ์ต์ˆ™ํ–ˆ๋Š”๋ฐ, html์—์„œ input ํƒœ๊ทธ์— ์†์„ฑ์œผ๋กœ ์กฐ๊ฑด์„ ๊ฑธ๋ฉด ๋‚˜์˜ค๋Š” ๊ฒฝ๊ณ ๋ฌธ์ด๋‹ค

 

๊ฐœ๋ฐœ์ž๋„๊ตฌ(Command + F12)๋ฅผ ์ผœ์„œ ํŒŒ์ผ์„ ์‚ดํŽด๋ณด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์กฐ์ž‘ํ•ด๋ณด์ž

 

์˜ˆ์ƒ๋Œ€๋กœ <input/> ํƒœ๊ทธ์— patter ์†์„ฑ์ด ๊ฑธ๋ ค์žˆ๋‹ค

์ € ๋ถ€๋ถ„๋งŒ ์‚ญ์ œํ•ด๋ฒ„๋ฆฌ๋ฉด ํŒจํ„ด ์กฐ๊ฑด ๊ฒ€์‚ฌ ์—†์ด ์ œ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค

 

์ด์ œ ๊ฒฝ๊ณ  ์•Œ๋ฆผ๋„ ์•ˆ๋œจ๊ณ  ์ œ์ถœ์ด ๋œ๋‹ค

 

์„ฑ๊ณต!

 

ํ›„์ผ๋‹ด

๋‚˜์ค‘์— ๋‹ค๋ฅธ ์ •๋‹ต ๋ธ”๋กœ๊ทธ๋ฅผ ์‚ดํŽด๋ณด๋‹ˆ ์ € ํŒจํ„ด๊ฒ€์‚ฌ ์šฐํšŒ๋ฅผ ์œ„ํ•ด burpsuit ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์•˜๋Š”๋ฐ,,

๊ฐ„๋‹จํ•œ ๋ณ€๊ฒฝ์ •๋„๋ผ ๊ฐœ๋ฐœ์ž ๋ชจ๋“œ์—์„œ ํ•œ๋ฒˆ์— ๋š๋”ฑ ํ•˜๊ธด ํ–ˆ์ง€๋งŒ ๋ณต์žกํ•œ ๋ณ€๊ฒฝ์ผ ๊ฒฝ์šฐ ํ•ด๋‹น ํˆด์„ ์‚ฌ์šฉํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

๋ฐ˜์‘ํ˜•