김민주

[web] command-injection-1 본문

SWUFORCE/[공통] 드림핵 스터디

[web] command-injection-1

7alswn 2023. 10. 3. 19:36

문제는 다음과 같다.

 

문제에서 주어진 페이지에 접속해 보았다.

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)

ping 페이지에서 host ip를 입력하고 버튼을 클릭하면 시스템에서 수행되는 명령어를 발견했다.

사용자 입력이 시스템 명령에 반영되는 부분은 "{host}" 부분이기 때문에 앞에서 ping 명령을 수행하고 또 다른 명령어를 실행하게끔 해야한다.

프록시 툴을 이용해서 풀어보겠다.

 

시스템 명령이 수행된 현재 디렉토리에 flag.py 파일이 보인다. 

위에서 명령어를 삽입한 방식으로 cat 명령어를 통해 flag.py 파일을 읽어주겠다.

 

flag 발견

문제풀이 성공

'SWUFORCE > [공통] 드림핵 스터디' 카테고리의 다른 글

[pwnabel] shell_basic  (0) 2023.11.05
[pwnable] welcome  (0) 2023.11.05
[web] pathtraversal  (0) 2023.10.10
[web] Carve Party  (0) 2023.10.10
[web] file-download-1  (0) 2023.10.03