본문 바로가기

Write up - WEB

Dream Hack - Webhacking - command-injection-1

Command Injection을 통해 플래그를 획득하는 문제이다.

문제 사이트를 들어갔을 때의 모습이다.

Ping으로 들어갔을 때의 모습이며 Host 창에 command injection을 사용하는 것을 알 수 있다.

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)

문제 파일을 확인해 보자

/ping

ping -c 3 "8.8.8.8" 형태로 실행되는 것을 알 수 있다.

Host창에 ping -c 3"8.8.8.8;cat flag.py"를 통해 flag 파일을 읽어 flag값을 얻을 수 있기에 이를 실행해봤다.

그냥 입력을 할 시 형식이 일치하지 않아 입력을 못한다고 창이 뜬다.

ping.html

ping 파일을 살펴보니 "pattern"을 통해 이를 규제하고 있음을 확인할 수 있다.

개발자 도구를 통해서 pattern을 지우고 입력을 했다.

FLAG 값을 얻을 수 있었다.