디렉터리 조작 취약점이 있다고 한다.
#!/usr/bin/python3
from flask import Flask, request, render_template, abort
from functools import wraps
import requests
import os, json
users = {
'0': {
'userid': 'guest',
'level': 1,
'password': 'guest'
},
'1': {
'userid': 'admin',
'level': 9999,
'password': 'admin'
}
}
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
app = Flask(__name__)
app.secret_key = os.urandom(32)
API_HOST = 'http://127.0.0.1:8000'
try:
FLAG = open('./flag.txt', 'r').read() # Flag is here!!
except:
FLAG = '[**FLAG**]'
@app.route('/')
def index():
return render_template('index.html')
@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 #../../flag
return render_template('get_info.html', info=info)
@app.route('/api')
@internal_api
def api():
return '/user/<uid>, /flag'
@app.route('/api/user/<uid>')
@internal_api
def get_flag(uid):
try:
info = users[uid]
except:
info = {}
return json.dumps(info)
@app.route('/api/flag')
@internal_api
def flag():
return FLAG
application = app # app.run(host='0.0.0.0', port=8000)
# Dockerfile
# ENTRYPOINT ["uwsgi", "--socket", "0.0.0.0:8000", "--protocol=http", "--threads", "4", "--wsgi-file", "app.py"]
전체 코드이다.
먼저 flag 위치를 파악해 보면 /api 디렉터리 안에 담겨 있는 걸 볼 수 있다.
좀 더 중요한 코드를 보자면 ...
@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 #../../flag
return render_template('get_info.html', info=info)
이 부분인데
request.method == 'POST'
> 사용자가 폼에 입력을 하면 POST 요청이 발생한다.
userid = request.form.get('userid', '')
> userid 값에 아무것도 없을 경우에는 빈 문자열로 반환시킨다.
info = request.get(f'{API_HOST}/api/user/{userid}'). text [View button]
> requests.get을 이용하여 API로 GET 요청을 보낸다 응답은 text 속성으로 온다.
여기서 보면 'view'를 눌렀을 때 GET 요청으로 인해 api를 호출한다
그렇다면 view를 이용해서 경로를 조작하여 플래그를 출력시킬 수 있을 거 같다.
플래그에 위치는 이미 알고 있으니...
'DreamHack > WEB' 카테고리의 다른 글
proxy-1 (0) | 2024.10.16 |
---|---|
web-misconf-1 (1) | 2024.10.10 |
blind-command (0) | 2024.10.10 |
session (1) | 2024.10.08 |
web-ssrf (2) | 2024.09.26 |