๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Web Hacking/Dreamhack

[Dreamhack] Simple SQLI ChatGPT ๋“œ๋ฆผํ•ต ์›Œ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์ด

by A Lim Han 2023. 8. 11.

๐Ÿˆ‍โฌ› Simple SQLI ChatGPT ๋“œ๋ฆผํ•ต ์›Œ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์ด

1. ๋ฌธ์ œ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ฝ์€ ํ›„ ๋ฌธ์ œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ

2. ๋‹ค์šด๋ฐ›์€ 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), 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='{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)

 

โ€ป ์ฝ”๋“œ ์ฃผ์š” ๊ธฐ๋Šฅ & Flow

โ‘  ํ•„์š”ํ•œ ๋ชจ๋“ˆ๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Import

โ‘ก Application ์„ ์ƒ์„ฑํ•˜๊ณ , ์‹œํฌ๋ฆฟ ํ‚ค๋ฅผ ๋žœ๋ค ์ƒ์„ฑํ•˜์—ฌ ์„ค์ •

โ‘ข Database ์ดˆ๊ธฐํ™”
Database ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ SQLite Database ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ดˆ๊ธฐ ์‚ฌ์šฉ์ž ์ •๋ณด ์ถ”๊ฐ€

โ‘ฃ Database ์—ฐ๊ฒฐ ๊ด€๋ฆฌ
:  get_db() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Database ์—ฐ๊ฒฐ์„ ์–ป๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ์—์„œ ์—ฐ๊ฒฐ์„ ๋‹ซ์Œ

โ‘ค ํ™ˆ ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๊ธฐ๋ณธ ๊ฒฝ๋กœ('/') ์ •์˜

โ‘ฅ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ
:  '/login' ๊ฒฝ๋กœ๋ฅผ ์ •์˜ ๋กœ๊ทธ์ธ ํผ์—์„œ ์ œ์ถœ๋œ ์‚ฌ์šฉ์ž ๋ ˆ๋ฒจ์„ ํ™•์ธ ํ›„ ๋ ˆ๋ฒจ์ด ์ผ์น˜ํ•˜๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ Database ์—์„œ ๋ถˆ๋Ÿฌ์˜ด
-->  ๊ด€๋ฆฌ์ž์ธ ๊ฒฝ์šฐ ํ”Œ๋ž˜๊ทธ๋ฅผ ํ‘œ์‹œ, ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ด์ „ ํŽ˜์ด์ง€๋กœ ์ด๋™

3. ๋‹ค์‹œ ๋ฌธ์ œ ํŽ˜์ด์ง€๋กœ ๋Œ์•„์™€ ์„œ๋ฒ„ ์ƒ์„ฑ

 

-->  ์ƒ์„ฑ๋œ http://host3.dreamhack.games:21374/ ์œผ๋กœ ์ ‘์†

4. Login ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•œ ํ›„ ์ž…๋ ฅ๊ฐ’์— "0" ๋„ฃ๊ธฐ  -->  Admin ์ด ์•„๋‹Œ Guest ๋กœ ๋กœ๊ทธ์ธ๋จ์„ ํ™•์ธ

 

5. ChatGPT์— ๋กœ๊ทธ์ธํ•˜์—ฌ app.py ํŒŒ์ผ์˜ ์ฝ”๋“œ๋ฅผ ์•Œ๋ ค์ฃผ๊ธฐ

6. ChatGPT ์‚ฌ์šฉ์ด ํ—ˆ๊ฐ€๋œ ๋ฌธ์ œ์ด๋‹ˆ, ์•„๋ž˜์™€ ๊ฐ™์ด ์งˆ๋ฌธ

7. ๋‹ค์‹œ ํ™ˆํŽ˜์ด์ง€๋กœ ์ ‘์†ํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅ ํ›„, Login ๋ฒ„ํŠผ ํด๋ฆญ  -->  FLAG ํ™•์ธ ๊ฐ€๋Šฅ

 

8. ์—ฌ๊ธฐ์„œ ๋๋‚ด๋ฒ„๋ฆฌ๋ฉด ๊ณต๋ถ€๊ฐ€ ์•„๋‹ˆ๋‹ˆ, ChatGPTํ•œํ…Œ ์™œ FLAG๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์—ˆ๋Š”์ง€ ์งˆ๋ฌธ

Q) ๊ทธ ์ด์œ ๋„ ์„ค๋ช…ํ•ด์ค˜.

 

 

A) 

 FLAG๊ฐ’์ด ๋„์ถœ๋˜๋Š” ์ด์œ ๋Š” SQL ์‚ฝ์ž…(SQL Injection) ๊ณต๊ฒฉ์— ๊ธฐ์ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ์–ด์ง„ ์ฝ”๋“œ์—์„œ ๋กœ๊ทธ์ธ ๋ž€ ์ฒ˜๋ฆฌ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

res = query_db(f"select * from users where userlevel='{userlevel}'")

 

 ์œ„ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ ์–‘์‹์— ์ž…๋ ฅํ•œ ๋‚ด์šฉ์€ userlevel์— ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ๊ฐ’์€ SQL ์ฟผ๋ฆฌ์— ์ง์ ‘ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ์ด์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ์ ์ ˆํžˆ ๊ฒ€์ฆํ•˜๊ฑฐ๋‚˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐํ™”ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด SQL ์‚ฝ์ž… ์ทจ์•ฝ์ ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ด์–ด "1' or userid = 'admin' --"๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค:

 

1. 1'์€ ํ˜„์žฌ ์ง„ํ–‰์ค‘์ธ ์ฟผ๋ฆฌ๋ฌธ์„ ์ข…๋ฃŒํ•˜๊ณ  ์ƒˆ๋กœ์šด ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

2. or userid = 'admin'์€ "users" ํ…Œ์ด๋ธ”์—์„œ userid๊ฐ€ 'admin'์ธ ๋ ˆ์ฝ”๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

3. --๋Š” SQL ์ฃผ์„ ๊ธฐํ˜ธ๋กœ, ๋’ค์˜ ๋ชจ๋“  ์ฟผ๋ฆฌ๋ฅผ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.


์œ„์˜ ์ž…๋ ฅ์ด userlevel์— ๋“ค์–ด๊ฐˆ ๊ฒฝ์šฐ, SQL ์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด์„๋ฉ๋‹ˆ๋‹ค:

 

select * from users where userlevel='1' or userid = 'admin' --'

 

 ์œ„์˜ ์ฟผ๋ฆฌ๋Š” "users" ํ…Œ์ด๋ธ”์—์„œ userlevel ๊ฐ’์ด '1'์ธ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ๋˜๋Š” userid ๊ฐ’์ด 'admin'์ธ ๋ ˆ์ฝ”๋“œ๋ฅผ ์„ ํƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜๋ฉด, FLAG ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜์–ด FLAG ๊ฐ’์ด ๋„์ถœ๋ฉ๋‹ˆ๋‹ค.

8. ์ •๋ ฌ ์ˆœ์„œ ๋ฐ ์„ ํƒ๊ณผ ๊ด€๋ จํ•˜์—ฌ ์ถ”๊ฐ€์ ์œผ๋กœ ๊ถ๊ธˆํ–ˆ๋˜ ๋ถ€๋ถ„๋„ ์งˆ๋ฌธ

 

 


 

๋ฌธ์ œํ’€์ด ์ฐธ๊ณ : https://jwcs.tistory.com/37 & ChatGPT