CTF/Web
[AmateuresCTF 2023] Factorial Calculator
youung
2023. 7. 24. 20:50
728x90
반응형
Docker 파일과 app.py 파일이 주어졌다.
FROM python:3.10-slim-buster
RUN pip3 install flask
COPY flag.txt /
WORKDIR /app
COPY app/* /app/
copy app/templates/* /app/templates/
copy app/themes/* /app/themes/
EXPOSE 5000
ENTRYPOINT ["python3", "app.py"]
flag.txt는 / 디렉토리에 있다.
from flask import Flask, render_template, request
import sys
app = Flask(__name__)
def factorial(n):
if n == 0:
return 1
else:
try:
return n * factorial(n - 1)
except RecursionError:
return 1
def filter_path(path):
# print(path)
path = path.replace("../", "")
try:
return filter_path(path)
except RecursionError:
# remove root / from path if it exists
if path[0] == "/":
path = path[1:]
print(path)
return path
@app.route('/')
def index():
safe_theme = filter_path(request.args.get("theme", "themes/theme1.css"))
f = open(safe_theme, "r")
theme = f.read()
f.close()
return render_template('index.html', css=theme)
@app.route('/', methods=['POST'])
def calculate_factorial():
safe_theme = filter_path(request.args.get("theme", "themes/theme1.css"))
f = open(safe_theme, "r")
theme = f.read()
f.close()
try:
num = int(request.form['number'])
if num < 0:
error = "Invalid input: Please enter a non-negative integer."
return render_template('index.html', error=error, css=theme)
result = factorial(num)
return render_template('index.html', result=result, css=theme)
except ValueError:
error = "Invalid input: Please enter a non-negative integer."
return render_template('index.html', error=error, css=theme)
if __name__ == '__main__':
sys.setrecursionlimit(100)
app.run(host='0.0.0.0')
filter_path에서 "../"가 필터링 되고 있다.
"../"가 연속되어 있으면 filter_path 함수는 계속 호출되어 "../"를 필터링할 것이다.
즉, "../"가 없어질 때까지 계속 호출된다는 말이다.
(../../../../../flag.txt를 입력한다면, flag.txt로 반환된다)
그리고 if path[0] == "/" path = path[1:] 에서
"/"가 제거(필터링) 된다.
이를 우회하기 위해서 theme 인자에 "//"를 입력해 주면 된다!
/?theme=//flag.txt 로 이동해보자!
기존 페이지에서 css가 바뀐 것을 확인할 수 있다!
그러나 flag는 나오지 않았다....
헤매던 중에 flag를 <style> 태그에서 찾을 수 있었다!!!
FLAG
amateursCTF{h1tt1ng_th3_r3curs10n_l1mt_1s_1mp0ssibl3}
728x90
반응형