728x90

[๋ฌธ์ œ]

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

 

command-injection-chatgpt

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

dreamhack.io


[๋ฌธ์ œ ํ’€์ด]

  • ๋จผ์ € ํŽ˜์ด์ง€๋ฅผ ๋ถ„์„ํ•ด ๋ณด๋‹ˆ ํ˜ธ์ŠคํŠธ ์ž…๋ ฅ์„ ํ†ตํ•ด ping ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋˜์–ด์žˆ๋‹ค.
#!/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 {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')


if __name__ == '__main__':
    APP.run(host='0.0.0.0', port=8000)
  • ๊ทธ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด๋ณด๋ฉด,  @APP.route('/ping', methods=['GET', 'POST']): ๋ถ€๋ถ„์—์„œ /ping ๊ฒฝ๋กœ์— ๋Œ€ํ•œ GET ๋ฐ POST ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  • ping ํ•จ์ˆ˜๋Š” POST ์š”์ฒญ์—์„œ host ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ping ๋ช…๋ น์„ ์‹คํ–‰ํ•œ๋‹ค. subprocess.check_output์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…๋ น์–ด ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์บก์ฒ˜ํ•œ ๋’ค ๋ช…๋ น์–ด๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉด ๊ฒฐ๊ณผ๋ฅผ ping_result.html ํ…œํ”Œ๋ฆฟ์— ์ „๋‹ฌํ•œ๋‹ค.
  • ping ๋ช…๋ น์–ด๋Š” ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋ช…๋ น์–ด ์ฃผ์ž… ๊ณต๊ฒฉ์— ์ทจ์•ฝํ•˜์—ฌ ์ด๋ฅผ ์ด์šฉํ•ด ;, & ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋ฌธ์ œ ์„ค๋ช…์—์„œ ํ”Œ๋ž˜๊ทธ๋Š” flag.py์— ์žˆ๋‹ค๊ณ  ํ–ˆ์œผ๋‹ˆ ;๋ฅผ ์ด์šฉํ•ด ์ถ”๊ฐ€ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด๋ณด์ž.

  • ๊ทธ ๊ฒฐ๊ณผ, flag๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค!

+ Recent posts