Dreamhack - command-injection-chatgpt

·2025년 7월 30일

Dreamhack-Writeups

목록 보기
29/52

command-injection-chatgpt

문제 링크

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

문제 설명

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

chatGPT와 함께 풀도록 제안된 문제입니다. (하지만 문제 난이도를 보니 chatGPT 없어도 충분히 해결 가능한 수준입니다.)

풀이과정

  1. Host에 8.8.8.8을 입력하면 ping이 제대로 넘어감을 확인했고, 이 입력란에 Command Injection을 사용해야 함을 유추해볼 수 있습니다.

  2. app.py를 확인해 보았습니다.

    def ping():
     if request.method == 'POST':
         host = request.form.get('host')
         cmd = f'ping -c 3 {host}'

    이때, cmd = f'ping -c 3 {host}'{host} 는 사용자가 Host에 입력한 값이고, 그 값이 문자열 포매팅을 통해 cmd 문자열 안에 아무런 보안 없이 직접 삽입되고 있는걸 확인할 수 있습니다.

  3. Command Injection 공격을 시도합니다. Host8.8.8.8; cat flag.py 를 입력해줍니다.

    • 8.8.8.8 으로 정상적인 ping값을 보냅니다.
    • ;쉘 명령어 구분자 입니다. 첫 번째 명령어 뒤에 두 번째 명령어를 이어붙이기 위한 역할입니다.
    • cat flag.py 는 공격을 위한 명령어입니다. flag.py에 알고싶은 플래그가 있으므로 텍스트 파일의 내용을 출력하는 명령인 cat 명령어를 사용합니다.

  1. Ping!을 보내고, 최종적으로 플래그를 획득할 수 있었습니다.


배운점

  • Command Injection의 명령어 등, 기본 공격을 학습할 수 있었습니다.
  • cat 명령어 등, 공격에 필요한 필수적인 명령어를 알고 직접 실습해볼 수 있었습니다.
  • 사용자 입력을 검증하지 않으면, 명령어 삽입과 같은 심각한 보안 취약점이 발생할 수 있다는 점을 배웠습니다.
  • 문제를 풀다 어려움이 생길 때, 챗지피티를 통해 해결이 가능함을 학습할 수 있었습니다.

Summary (English)

  • The challenge is about exploiting a Command Injection vulnerability in a web service that pings a given host.
  • The user's input is injected directly into the command string without sanitization: cmd = f'ping -c 3 {host}'.
  • By inputting 8.8.8.8; cat flag.py, two commands are executed:
    • ping -c 3 8.8.8.8 to perform a normal ping
    • cat flag.py to read and display the contents of the flag file
  • The semicolon (;) acts as a shell command separator, allowing the attacker to chain multiple commands.
  • The cat command is used to read the contents of the flag.py file and leak it through the web response.
  • Successfully demonstrated a basic Command Injection attack and retrieved the flag.
  • Learned how such vulnerabilities arise and how tools like ChatGPT can support solving security challenges.
profile
CTF 풀이 및 실습 중심 학습을 기록합니다.

0개의 댓글