Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

CIDY

[Web_Hacking] stage7_문제풀이(command-injection-1) 본문

Hack/DreamHack(로드맵)

[Web_Hacking] stage7_문제풀이(command-injection-1)

CIDY 2022. 7. 30. 05:50
#!/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)

 

host를 입력하면서 메타문자로 뭘 더 하면 될 것 같은데, 입력 형식을 지키라는 알림이 계속 떠서 html코드를 보니 아래와 같았다.

 

 

뭘 해야할지 진짜 고민했는데 그냥 조건을 바꾸자~

 

처음에는 아스키 상으로 마지막이 ~니까

 

 -~로 바꿔줄려고 했는데 생각해보면 그냥 통째로 없애면 더 편하다.

 

처음에는 127.0.0.1; cat flag.py했는데 위와 같은 에러메시지가 출력된다. 내 입력이 ""로 묶이면서 전체가 호스트로 인식되는 것 -> 메타문자 이전에 따옴표로 한 번 끊어주고, 입력 가장 뒤쪽에도 "를 한 번 써 줘야 할 것 같다.

 

127.0.0.1"; cat flag.py"

 

flag