Web Hacking/SWLUG 내부 CTF

[SWLUG] Sqli1 - write up

hanbunny 2025. 4. 14. 11:21

🐰시작!

이 창이 뜨고 시작한다.
uid는 'guest'로 들어가 있어서 upw에만 값을 넣어봤다.

-> Invalid credentials 가 뜬다.

그래서 소스코드를 봤음.

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

app = Flask(__name__)

with open('FLAG.txt', 'r') as file:
    FLAG = file.read().strip()

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')

    query = f"SELECT * FROM sqli1 WHERE uid='guest' AND upw='{upw}'"
    print(query)  
    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:
        if result[1] == 'admin': 
            output = f"<h3>Hello! {result[3]}</h3><p>FLAG: {FLAG}</p>"
        else:
            output = f"<h3>Hello! {result[3]}</h3>"
    else:
        output = "<h3>Invalid credentials</h3>"

    return f"""
        <h1>level 1</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문을 보면 'admin'일때 FLAG값을 얻을 수 있다.

upw에 값을 넣은 후 '로 닫아주고 OR '1'='1을 입력해 전체 조건을 참으로 만들어 줘봤음.

' 로 닫아주고 '1'='1 는 뒤에 닫아주지 않는 이유? ----- 넣어보면 앎.

<upw= abc OR '1'='1'로 넣었을 때>
SELECT * FROM sqli1 WHERE uid='guest' AND upw='abc' OR '1'='1''
OR부터 구문으로 인식하지 못 할수도 있고 불필요한 따옴표로 오류 발생.

즉, SQL 구문을 '로 닫아 종료시키고 새로운 구문을 넣어 조작하기 위한거고,
마지막에 '를 안 닫는 이유는 앞에서 추가한 ' 때문에 마지막에'하나가 남아있어 닫힘.

그랬더니 비밀번호를 몰라도 Hello! Guest User가 뜨며 Guest 계정으로 로그인됐다.

FLAG값을 얻기 위해선 Admin계정에 있는 값이 나와야하기 때문에
OR '1'='1대신 OR uid='admin으로 넣어봤다.

FLAG 획득

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

[SWLUG] Sqli5 - write up  (0) 2025.04.14
[SWLUG] Sqli4 - write up  (0) 2025.04.14
[SWLUG] Sqli3 - write up  (0) 2025.04.14
[SWLUG] Sqli2 - write up  (0) 2025.04.14
[SWLUG] Sqli0 - write up  (0) 2025.04.14