๋ฐ์ํ
https://dreamhack.io/wargame/challenges/269/
๋ฌธ์ ์ ๋ณด
์ฌ๋ฌ ๊ธฐ๋ฅ๊ณผ ์ ๋ ฅ๋ฐ์ URL์ ํ์ธํ๋ ๋ด์ด ๊ตฌํ๋ ์๋น์ค์ ๋๋ค.
CSRF ์ทจ์ฝ์ ์ ์ด์ฉํด ํ๋๊ทธ๋ฅผ ํ๋ํ์ธ์.
๋๋ณด๊ธฐ
#!/usr/bin/python3
from flask import Flask, request, render_template, make_response, redirect, url_for
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**]"
users = {
'guest': 'guest',
'admin': FLAG
}
session_storage = {}
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():
session_id = request.cookies.get('sessionid', None)
try:
username = session_storage[session_id]
except KeyError:
return render_template('index.html', text='please login')
return render_template('index.html', text=f'Hello {username}, {"flag is " + FLAG if username == "admin" else "you are not an admin"}')
@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", "")
session_id = os.urandom(16).hex()
session_storage[session_id] = 'admin'
if not check_csrf(param, {"name":"sessionid", "value": session_id}):
return '<script>alert("wrong??");history.go(-1);</script>'
return '<script>alert("good");history.go(-1);</script>'
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
elif request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
try:
pw = users[username]
except:
return '<script>alert("not found user");history.go(-1);</script>'
if pw == password:
resp = make_response(redirect(url_for('index')) )
session_id = os.urandom(8).hex()
session_storage[session_id] = username
resp.set_cookie('sessionid', session_id)
return resp
return '<script>alert("wrong password");history.go(-1);</script>'
@app.route("/change_password")
def change_password():
pw = request.args.get("pw", "")
session_id = request.cookies.get('sessionid', None)
try:
username = session_storage[session_id]
except KeyError:
return render_template('index.html', text='please login')
users[username] = pw
return 'Done'
app.run(host="0.0.0.0", port=8000)
ํ์ด
login ๊ฒฝ๋ก๋ก ๋ค์ด๊ฐ์ admin ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํด์ผํ๋ ๋ฌธ์
ํ์ง๋ง ๋น๋ฐ๋ฒํธ๋ฅผ ๋ชจ๋ฅด๋ ๋ก๊ทธ์ธ์ ํ ์ ์๋ค
@app.route("/change_password")
def change_password():
pw = request.args.get("pw", "")
session_id = request.cookies.get('sessionid', None)
try:
username = session_storage[session_id]
except KeyError:
return render_template('index.html', text='please login')
users[username] = pw
return 'Done'
์ฝ๋๋ฅผ ์ดํด๋ดค๋๋ /change_password ๊ฒฝ๋ก๋ฅผ ํตํด ์ํ๋ ๊ณ์ ์ ๋น๋ฐ๋ฒํธ๋ฅผ ๋ฐ๊ฟ ์ ์๋ค๊ณ ํ๋ค.
<img src="/change_password?pw=123" />
์ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํด์ pw๋ฅผ args๋ก ๋ฐ์ 123(์์์ ์ซ์)๋ก ์ค์ ํ ์ ์๋ค.
vulm(csrf) page ์์ ์ ๋ ฅํด์ฃผ์
login ๊ฒฝ๋ก์์ admin ์์ด๋์ ๋ฐฉ๊ธ ์ค์ ํ ์์์ ๋น๋ฒ์ผ๋ก ๋ก๊ทธ์ธ์ด ๋๊ณ FLAG ๊ฐ ๋ฌ๋ค
๋ฐ์ํ
'๐ Cyber Security > Web Hacking (์นํดํน)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DreamHack] ๋๋ฆผํต ์นํดํน : file-download-1 (0) | 2023.07.14 |
---|---|
[DreamHack] ๋๋ฆผํต ์นํดํน: command-injection-1 (0) | 2023.07.05 |
[DreamHack] ๋๋ฆผํต ์นํดํน: csrf-1 (0) | 2022.11.08 |
[DreamHack] ๋๋ฆผํต ์นํดํน: simple-sqli (0) | 2022.09.22 |
[DreamHack] ๋๋ฆผํต ์นํดํน: Session-basic (0) | 2022.09.21 |