본문 바로가기

드림핵

[드림핵 워게임] command-injection-chatgpt

<문제>

 

 

<풀이>

1. 사이트 및 코드 확인

문제 사이트에 접속하면 처음으로 보이는 페이지이다.

Ping을 클릭해보았다.

 

Host 를 입력하는 폼이 나온다.

 

@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')

코드를 확인해보면 20행에서 사용자가 입력한 Host값을 ping -c 3 {host}로 구성해서

완성된 명령어를 셸에서 실행하고 그 결과를 ping_result.html 페이지에 출력하는 것을 알 수 있다.

 

2. 익스플로잇

host 값으로 127.0.0.1; cat flag.py를 입력해서

최종적으로 완성되는 명령어가 ping -c 3 127.0.0.1; cat flag.py 가 되도록 했다.

이렇게 페이로드를 보내면 ping -c 3 127.0.0.1이 먼저 실행되고 이어서 cat flag.py 가 실행된다.

Ping!을 클릭한다.

 

 

3. FLAG 획득

플래그가 출력되었다.