CTF & Wargame(WEB)

pathtraversal beginner

KWAKBUMJUN 2025. 4. 14. 18:10

웹 서비스 분석

사용자에게 userid를 입력 받고, 입력받은 유저의 정보를 표시해주는 기능이 있는 웹 사이트이다.

 

엔드포인트 분석

  • /get_info
  • /api
  • /api/flag
  • /api/user/<uid>

/get_info

@app.route('/get_info', methods=['GET', 'POST'])
def get_info():
    if request.method == 'GET':
        return render_template('get_info.html')
    elif request.method == 'POST':
        userid = request.form.get('userid', '')
        info = requests.get(f'{API_HOST}/api/user/{userid}').text
        return render_template('get_info.html', info=info)

GET 요청으로 get_info.html을 표시해준다.

POST 요청으로 사용자에게 userid를 입력 받고, API_HOST/api/user/usrid라는 url에 HTTP get 요청을 보낸다. 그 후에 응답을 get_info.html에 표시

 

/api/flag

@app.route('/api/flag')
@internal_api
def flag():
    return FLAG

/api/flag 라는 내부 api 경로에 접속하면 FLAG를 반환해줌.

 

함수 internal_api()

def internal_api(func):
    @wraps(func)
    def decorated_view(*args, **kwargs):
        if request.remote_addr == '127.0.0.1':
            return func(*args, **kwargs)
        else:
            abort(401)
    return decorated_view

internal_api 함수는 접속자의 requests.remote_addr이 127.0.0.1이라면 접속 가능하고 아니라면 접속하지 못하게 막는 기능이다.

 

취약점 분석

@app.route('/get_info', methods=['GET', 'POST'])
def get_info():
    if request.method == 'GET':
        return render_template('get_info.html')
    elif request.method == 'POST':
        userid = request.form.get('userid', '')
        info = requests.get(f'{API_HOST}/api/user/{userid}').text
        return render_template('get_info.html', info=info)

취약점은 get_info 페이지에서 발생한다. 해당 문제의 코드에서는 API_HOST로 접근하지 않으면 api 내부 페이지로 접근을 못하도록 막고 있는데, /get_info 페이지에서 사용자가 입력한 info를 가져올때 API_HOST로써 api 페이지에 접근하여 userinfo를 가져오도록 코딩되어 있다.  또한 userid를 입력받을때 해당 값에 대한 검증을 진행하지 않고 있기 때문에 path traversal 취약점이 발생한다.

 

여기서 userid에 path_traversal 기법을 활용한 payload를 삽입해주면 flag를 획득할 수 있을 것이다.

익스플로잇

http://host8.dreamhack.games:20566/api/flag

api_host가 아니면 api/flag 페이지에 접근하지 못한다. 하지만 burp_suit를 활용하면 쉽게 로컬 호스트로 접속하여 flag를 출력하도록 할 수 있다.

 

기존의 url은 /api/user/{userid} 이런 경로로 표시되어 있다. 하지만 flag를 출력해주는 페이지의 경로는 /api/flag이다. 따라서 

api/user/../flag --> api/flag로 변조 가능

 

이렇게 flag가 출력되었다.

 

'CTF & Wargame(WEB)' 카테고리의 다른 글

session beginner  (0) 2025.04.14
web-misconf-1 beginner  (0) 2025.04.14
blind-command level 2  (0) 2025.04.14
Carve Party beginner  (0) 2025.04.14
web-ssrf level 2  (0) 2025.04.14