[Dreamhack] command-injection-1

#코딩노예#·2022년 9월 28일
0
post-thumbnail

문제 코드

@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')
  • 사용자로 부터 host에 데이터를 받아옵니다.
  • ping -c 3 뒤에 host 값을 붙입니다.
  • /bin/sh ping -c 3 [host]를 실행하고 출력 문자열을 리턴합니다.

⇾ 명령어를 실행하고 실행 결과를 리턴하는 함수에 사용자가 인자를 전달할 수 있기 때문에 commnad injection이 발생합니다.



익스플로잇

  • Host 값을 입력하는 입력창에 8.8.8.8을 입력하고 결과를 확인해보면

코드에서 확인한 것 처럼 ping -c 3 8.8.8.8 명령을 실행한 결과를 보여줍니다.


이번에는 8.8.8.8";"cat" "flag.py 를 입력값으로 줘서 command injection을 시도해보면

요청한 형식과 일치시키세요. 라는 메시지가 출력되고 전송이 되지 않습니다. 입력값에 필터링이 걸려 있는거 같습니다.


처음에는 host에 8.8.8.8 을 써서 보내고 프록시를 이용해서 중간에서 host 값을 변조해야 할거 같습니다.

*프록시*

서버와 클라이언트 사이에 중계기로써 대리로 통신을 수행하는 것을 말합니다.


프록시로 Burp Suit 를 사용해서 host 값을 8.8.8.8에서 8.8.8.8";"cat" "flag.py로 변조해서 보내보겠습니다.

결과를 확인해보면 ping -c 3 8.8.8.8;cat flag.py가 실행되어서 FLAG가 출력되었습니다.

0개의 댓글