문제파일을 다운받고, 익스플로잇 홈페이지로 들어가 보았다.
참고로, app.py의 코드는 이렇다.
#!/usr/bin/python3
from flask import Flask, request, render_template
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import urllib
import os
app = Flask(__name__)
app.secret_key = os.urandom(32)
try:
FLAG = open("./flag.txt", "r").read()
except:
FLAG = "[**FLAG**]"
def read_url(url, cookie={"name": "name", "value": "value"}):
cookie.update({"domain": "127.0.0.1"})
try:
service = Service(executable_path="/chromedriver")
options = webdriver.ChromeOptions()
for _ in [
"headless",
"window-size=1920x1080",
"disable-gpu",
"no-sandbox",
"disable-dev-shm-usage",
]:
options.add_argument(_)
driver = webdriver.Chrome(service=service, options=options)
driver.implicitly_wait(3)
driver.set_page_load_timeout(3)
driver.get("<http://127.0.0.1:8000/>")
driver.add_cookie(cookie)
driver.get(url)
except Exception as e:
driver.quit()
# return str(e)
return False
driver.quit()
return True
def check_xss(param, cookie={"name": "name", "value": "value"}):
url = f"<http://127.0.0.1:8000/vuln?param={urllib.parse.quote(param)}>"
return read_url(url, cookie)
def xss_filter(text):
_filter = ["script", "on", "javascript:"]
for f in _filter:
if f in text.lower():
text = text.replace(f, "")
return text
@app.route("/")
def index():
return render_template("index.html")
@app.route("/vuln")
def vuln():
param = request.args.get("param", "")
param = xss_filter(param)
return param
@app.route("/flag", methods=["GET", "POST"])
def flag():
if request.method == "GET":
return render_template("flag.html")
elif request.method == "POST":
param = request.form.get("param")
if not check_xss(param, {"name": "flag", "value": FLAG.strip()}):
return '<script>alert("wrong??");history.go(-1);</script>'
return '<script>alert("good");history.go(-1);</script>'
memo_text = ""
@app.route("/memo")
def memo():
global memo_text
text = request.args.get("memo", "")
memo_text += text + "\\n"
return render_template("memo.html", memo=memo_text)
app.run(host="0.0.0.0", port=8000)
참고로, 필터링되는 문자열은 "script", "on", "javascript:” 가 있다.
flag 페이지로 들어가 보니, url의 param값을 xss filtering을 우회한 값으로 입력해서 flag 값을 얻어내는 것으로 보인다.
def check_xss(param, cookie={"name": "name", "value": "value"}):
url = f"<http://127.0.0.1:8000/vuln?param={urllib.parse.quote(param)}>"
return read_url(url, cookie)
@app.route("/flag", methods=["GET", "POST"])
def flag():
if request.method == "GET":
return render_template("flag.html")
elif request.method == "POST":
param = request.form.get("param")
if not check_xss(param, {"name": "flag", "value": FLAG.strip()}):
return '<script>alert("wrong??");history.go(-1);</script>'
return '<script>alert("good");history.go(-1);</script>'
document.cookie로 flag 값을 얻어낼 수 있는 것으로 보인다….보면 cookie에 플래그 값이 들어가 있는 것을 확인할 수 있다.
xss filtering bypass로 뭐가 많이 쓰이는지 구글링을 좀 해 보았다.
일단 xss 공격 코드로 자주 사용되는것
<img src="<https://dreamhack.io/valid.jpg>" onload="alert(document.domain)">
<!--유효한 이미지 로드 후 onload 핸들러 실행 -->
<img src="about:invalid" onload="alert(document.domain)">
<!--이미지 로드 실패, onload 핸들러 실행하지 않음 -->
<img src="valid.jpg" onerror="alert(document.domain)">
<!--유효한 이미지 로드 성공, onerror 핸들러 실행하지 않음 -->
<img src="about:invalid" onload="alert(document.domain)">
<!--이미지 로드 실패, onerror 핸들러 실행 -->