Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Montgomery Reduction
- XSS
- 웹해킹
- Cube Root Attack
- Franklin-Reiter Related Message Attack
- 드림핵
- pycrpytodome
- OverTheWire Bandit Level 1 → Level 2
- Bandit Level 1 → Level 2
- arp
- Crypto
- Hastad
- redirect
- 암호학
- weak key
- AES
- dreamhack
- cryptography
- picoCTF
- RSA
- return address overflow
- shellcode
- 시스템해킹
- rao
- RSA Common Modulas Attack
- dns
- spoofing
- bandit
- CSRF
- overthewire
Archives
- Today
- Total
암호(수학) 등.. 공부한 거 잊을거 같아서 만든 블로그
[Dreamhack] simple_sqli 본문
문제
문제 파일 : 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'<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:
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 페이지 부분의 코드를 보면 select query 를 통해 입력한 userid, userpassword 에 해당하는 값을 db에 불러와서 userid 가 admin 이면 flag를 출력해주는 것을 알 수 있다.
db에 query를 보내주는 함수로 보이는 query_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
execute 메서드를 통하여 query를 db에 보내는 함수인 것을 알 수 있다.
execute 메서드는 사용자가 입력한 값을 그대로 db에 query로 보내므로 입력을 조작하여 비정상적인 요청을 보낼 수 있다.
따라서 userid에 admin"-- 를 입력하고 비밀번호를 아무 값이나 입력하면 다음과 같은 query를 db에 보내게 된다.
select * from users where userid="admin"--" and userpassword="{userpassword}"
-- 는 주석표시로 뒷부분이 무시되므로 select * from users where userid="admin" 가 된다.
userid 가 admin인 값을 가져오므로 if문을 만족하여 flag를 알게 되었다.
'Dreamhack > Web' 카테고리의 다른 글
[Dreamhack] command-injection-1 (0) | 2023.05.11 |
---|---|
[Dreamhack] Mango (0) | 2023.05.08 |
[Dreamhack] csrf-2 (0) | 2023.04.16 |
[Dreamhack] csrf-1 (0) | 2023.04.14 |
[Dreamhack] xss-2 (0) | 2023.04.08 |