Web Hacking/SWLUG 내부 CTF

[SWLUG] Sqli4 - write up

hanbunny 2025. 4. 14. 11:25

🐰시작!

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', '').lower()
    upw = request.args.get('upw', '').lower()

    uid = uid.replace('union', '')
    upw = upw.replace('union', '')

    query = f"SELECT * FROM sqli4 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 4</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')
 uid = uid.replace('union', '')
    upw = upw.replace('union', '')

Sqli3 에서는 ‘union' 문자 자체가 입력되어 있을 경우 'No hack’을 반환하며 차단을 했는데
이번엔 'union'을 공백으로 대체하며 차단함.

시도1) 대소문자 섞어서 우회

그래서 한 번 넣어봤다.
union이 들어갔더니 공백으로 대체되며 uid값에 아무것도 뜨지않음.

대소문자를 섞어 시도해봤는데

소문자로 변환되며 공백으로 대체됨.
대소문자로 우회하는건 어려울듯하다.

시도2) union사이에 union넣기
Ex) ununionion

?uid=' ununionion select 1, 2, 3,(select upw from sqli4 where uid='admin') #

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

[SWLUG] Sqli7 - write up  (0) 2025.04.14
[SWLUG] Sqli5 - write up  (0) 2025.04.14
[SWLUG] Sqli3 - write up  (0) 2025.04.14
[SWLUG] Sqli2 - write up  (0) 2025.04.14
[SWLUG] Sqli1 - write up  (0) 2025.04.14