๋ฐ์ํ
https://dreamhack.io/wargame/challenges/24/
๋ฌธ์ ์ ๋ณด
๋ก๊ทธ์ธ ์๋น์ค์
๋๋ค.
SQL INJECTION ์ทจ์ฝ์ ์ ํตํด ํ๋๊ทธ๋ฅผ ํ๋ํ์ธ์. ํ๋๊ทธ๋ flag.txt, FLAG ๋ณ์์ ์์ต๋๋ค.
๋๋ณด๊ธฐ
#!/usr/bin/python3
from flask import Flask, request, render_template, g
import sqlite3
import os
import binascii
app = Flask(__name__)
app.secret_key = os.urandom(32)
try:
FLAG = open('./flag.txt', 'r').read()
except:
FLAG = '[**FLAG**]'
DATABASE = "database.db"
if os.path.exists(DATABASE) == False:
db = sqlite3.connect(DATABASE)
db.execute('create table users(userid char(100), userpassword char(100));')
db.execute(f'insert into users(userid, userpassword) values ("guest", "guest"), ("admin", "{binascii.hexlify(os.urandom(16)).decode("utf8")}");')
db.commit()
db.close()
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
return db
def query_db(query, one=True):
cur = get_db().execute(query)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
userid = request.form.get('userid')
userpassword = request.form.get('userpassword')
res = query_db(f'select * from users where userid="{userid}" and userpassword="{userpassword}"')
if res:
userid = res[0]
if userid == 'admin':
return f'hello {userid} flag is {FLAG}'
return f'<script>alert("hello {userid}");history.go(-1);</script>'
return '<script>alert("wrong");history.go(-1);</script>'
app.run(host='0.0.0.0', port=8000)
ํ์ด
else:
userid = request.form.get('userid')
userpassword = request.form.get('userpassword')
res = query_db(f'select * from users where userid="{userid}" and userpassword="{userpassword}"')
if res:
userid = res[0]
if userid == 'admin':
return f'hello {userid} flag is {FLAG}'
return f'<script>alert("hello {userid}");history.go(-1);</script>'
return '<script>alert("wrong");history.go(-1);</script>'
๋ก๊ทธ์ธ ํ์ด์ง๋ก ์ด๋ํ๋ฉด /login ๊ฒฝ๋ก๋ก ๋ค์ด๊ฐ๋ค.
์ด๊ณณ์์ admin ์์ด๋๋ก ๋ก๊ทธ์ธ์ ์ฑ๊ณตํ๋ฉด ํ๋๊ทธ๋ฅผ ํ๋ ํ ์ ์๋ค.
select * from users where userid="{userid}" and userpassword="{userpassword}"
userid์ ์ ๋ ฅํ ๊ฐ์ด {userid} ๊ฐ์ ๋ค์ด๊ฐ๋ฉฐ sql ๋ฌธ์ด ์คํ๋๋ค.
admin"/* ์ userid ์๋ฆฌ์ ๋ฃ์ผ๋ฉด
select * from users where userid="admin" /*" and userpassword="{userpassword}"
๋ค์๊ณผ ๊ฐ์ด admin ๋ท๋ถ๋ถ์ด /* ๋ก ์ธํด ์ฃผ์์ฒ๋ฆฌ๊ฐ ๋๋ฉฐ
๋ด๋ถ์ ์ฝ์ ๋ sql ๊ตฌ๋ฌธ์ด ์คํ๋๋ค.
ํจ์ค์๋์ ์ผ์น ์กฐ๊ฑด์์ด ์ ์ ๋ฅผ ๋ถ๋ฌ์ค๊ฒ ๋๋ฏ๋ก ๋ฐ๋ก admin ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ ์ฑ๊ณต.
๋ฐ์ํ
'๐ Cyber Security > Web Hacking (์นํดํน)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DreamHack] ๋๋ฆผํต ์นํดํน: command-injection-1 (0) | 2023.07.05 |
---|---|
[DreamHack] ๋๋ฆผํต ์นํดํน: csrf-2 (0) | 2022.11.09 |
[DreamHack] ๋๋ฆผํต ์นํดํน: csrf-1 (0) | 2022.11.08 |
[DreamHack] ๋๋ฆผํต ์นํดํน: Session-basic (0) | 2022.09.21 |
[DreamHack] ๋๋ฆผํต ์นํดํน: Cookie (0) | 2022.09.21 |