개발 블로그

[DreamHeck] web-HTTP-CLI 본문

Study/Web Hacking

[DreamHeck] web-HTTP-CLI

얀 짱 2026. 8. 5. 22:24

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

 

로그인 | Dreamhack

 

dreamhack.io

 

포함되어 있는 파일이 app.py 밖에 없었음.

#!/usr/bin/python3
import urllib.request
import socket

try:
    FLAG = open('./flag.txt', 'r').read()
except:
    FLAG = '[**FLAG**]'

def get_host_port(url):
    return url.split('://')[1].split('/')[0].lower().split(':')     # https://dreamhack.io:443 느면 host='dreamhack.io', port='443'으로 쪼개져


# 소켓 서버 생성 
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind(('', 8000))      # 바인딩 
    s.listen()

    while True:
        try:
            # 접속 대기 및 접속 수락 
            cs, ca = s.accept()
            cs.sendall('[Input Example]\n'.encode())
            cs.sendall('> https://dreamhack.io:443/\n'.encode())
        except:
            continue
        while True:
            cs.sendall('> '.encode())
            url = cs.recv(1024).decode().strip()
            print(url)
            if len(url) == 0:
                break
            try:
                (host, port) = get_host_port(url)
                if 'localhost' == host:     # 사용 불가 
                    cs.sendall('cant use localhost\n'.encode())
                    continue
                if 'dreamhack.io' != host:      # 사용 불가 
                    if '.' in host:
                        cs.sendall('cant use .\n'.encode())     # host 안에 .이 들어가면 사용 불가
                        continue
                cs.sendall('result: '.encode() + urllib.request.urlopen(url).read())
            except:
                cs.sendall('error\n'.encode())
        cs.close()

# localhost 안되고, 127.0.0.0도 안됨
# -> https://stackoverflow.com/questions/2241229/going-from-127-0-0-1-to-2130706433-and-back-again : 2130706433으로 표현 가능하대 10진수 정수 (Integer)

 

https://hacktricks.wiki/en/pentesting-web/ssrf-server-side-request-forgery/index.html

 

SSRF (Server Side Request Forgery) - HackTricks

A Server-side Request Forgery (SSRF) vulnerability occurs when an attacker manipulates a server-side application into making HTTP requests to a domain of...

hacktricks.wiki

 

SSRF 취약점이라는 것은 알았음. 그래서 localhost, 127.0.0.1 로 접속을 해야 하는데

if 'localhost' == host:     # 사용 불가 
    cs.sendall('cant use localhost\n'.encode())
    continue
if 'dreamhack.io' != host:      # 사용 불가 
    if '.' in host:
        cs.sendall('cant use .\n'.encode())     # host 안에 .이 들어가면 사용 불가
        continue

 

이것 때문에 둘 다 사용 불가함. 어떡하지 어떡하지 하며 구글링을 함.

 

맨 마지막에 적어준 것처럼

https://stackoverflow.com/questions/2241229/going-from-127-0-0-1-to-2130706433-and-back-again

 

Going from 127.0.0.1 to 2130706433, and back again

Using the standard Java libraries, what is the quickest way to get from the dotted string representation of an IPV4-address ("127.0.0.1") to the equivalent integer representation (2130706433). And

stackoverflow.com

 

이걸 찾아서 127.0.0.1이 10진수 정수로 2130706433 변경이 가능함.

 

그래서 계속 https://2130706433/을 입력했는데 에러가 뜸.

 

2130706433만 넣었을 때는 필터의 get_host_port 함수 자체가 :로 나눌 게 없어서 error가 났음. 그래서 host:port 형태를 억지로 맞추기 위한 더미 값을 여러 개 시도해봤지만 계속 error가 떴음.

이 error가 필터 우회 실패인지 urlopen 단계 실패인지 파일 경로가 틀린 건지 구분이 안 됐기 때문에 로컬 환경에서 서버 로직을 그대로 재현해 자동으로 여러 후보를 테스트해보기로 했음.

 

1. http:// 말고 file://을 쓰게 된 이유

https://en.wikipedia.org/wiki/File_URI_scheme

 

  • localhost, 점(.) 포함 IP는 필터에 막힘 -> 2130706433로 우회는 성공했지만 정작 내부에 어떤 포트가 열려있는지, 그 포트에서 뭘 내주는지 전혀 모르는 상태임.
  • 포트를 하나하나 브루트포스해야 하는데 시간도 오래 걸리고 싫음.

찾아보니 urllib.request.urlopen()은 http://뿐 아니라 file://도 지원함. file://은 네트워크 요청이 아니라 파이썬 인터프리터가 로컬 디스크의 파일을 직접 열어서 읽는 동작이라고 함.

즉 이 SSRF 취약점의 본질은 서버가 내 URL을 대신 열어준다는 거니까 그 URL의 스킴을 file://로 바꾸면 네트워크 요청 없이 서버 파일시스템에 있는 파일을 바로 읽을 수 있겠다고 생각함.

 

2. /flag.txt 넣은 이유

FLAG = open('./flag.txt', 'r').read()

 

이 코드는 서버가 시작될 때 자기 자신의 파일시스템에서 flag.txt를 읽어들이고 있음.

 

3. /proc/self/cwd를 쓰게 된 이유

FLAG = open('./flag.txt', 'r').read()

 

./flag.txt는 상대 경로임. 그래서 경로를 몰라도 되는 방법을 찾다가 해당 방법을 찾음.

https://aceatom.tistory.com/398

 

/proc/self/cwd

'/proc/self/cwd'는 현재 실행중인 프로세스의 디렉토리 표시하는 명령어다. ctf때 쓸 수 있을 것 같다.

aceatom.tistory.com

 

  • 리눅스에서 실행 중인 모든 프로세스는 /proc/[PID]/cwd라는 심볼릭 링크를 가지고 있고 이건 그 프로세스가 지금 어느 디렉토리에서 실행되고 있는지를 가리킴.
file://2130706433:/proc/self/cwd/flag.txt

 

이것으로 플래그를 얻음.

'Study > Web Hacking' 카테고리의 다른 글

[DreamHeck] Padding Oracle  (0) 2026.08.13
[DreamHeck] XSS Filtering Bypass Advanced  (0) 2026.08.06
[HackTheBox] Spookifier  (0) 2026.08.04
[DreamHeck] DreamDocs  (0) 2026.07.28
[DreamHeck] Are you admin?  (0) 2026.07.24