Skip to content

Latest commit

ย 

History

History
170 lines (100 loc) ยท 4.08 KB

SQL_Injection_mybatis_Data_Map.md

File metadata and controls

170 lines (100 loc) ยท 4.08 KB

4. SQL ์‚ฝ์ž… ๊ณต๊ฒฉ: mybatis Data Map

1) ์ •์˜

์™ธ๋ถ€์—์„œ ์ž…๋ ฅ๋œ ๊ฐ’์ด ์ฟผ๋ฆฌ๋ฌธ์˜ ์ธ์ž๊ฐ’๊ณผ ์ฟผ๋ฆฌ ๋ช…๋ น์–ด์— ์—ฐ๊ฒฐ๋˜๋Š” ๋ฌธ์ž์—ด๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๊ณต๊ฒฉ์ž ์˜๋„์—์„œ ๋ฒ—์–ด๋‚œ ๋ฌธ์ž์—ด ์ „๋‹ฌํ•ด ์ฟผ๋ฆฌ๋ฌธ ์˜๋ฏธ ์™œ๊ณก ๋˜๋Š” ๊ตฌ์กฐ ๋ณ€๊ฒฝํ•ด ์ž„์˜์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช…๋ น์–ด ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ

2) ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

  • ์™ธ๋ถ€ ์ž…๋ ฅ์—์„œ ์œ„ํ—˜ํ•œ ๋ฌธ์ž๋‚˜ ์˜๋„ ๋ฒ—์–ด๋‚œ ์ž…๋ ฅ ์ œ๊ฑฐํ•˜๋Š” ์ฝ”๋“œ ํ”„๋กœ๊ทธ๋žจ ๋‚ด์— ์ถ”๊ฐ€

  • mybatis Data Map ํŒŒ์ผ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š” ์ฟผ๋ฆฌ ๋ช…๋ น์–ด ์ •์˜ํ•  ๋•Œ ๋ฌธ์ž์—ด ์‚ฝ์ธ ์ธ์ž($...$) ์‚ฌ์šฉ X
    -> #<์ธ์ž์ด๋ฆ„># ํ˜•ํƒœ์˜ ์ฟผ๋ฆฌ๋ฌธ ์‚ฌ์šฉ

3) ์˜ˆ์ œ[1]

mybatis Data Map์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์งˆ์˜๋ฌธ ์„ค์ •ํŒŒ์ผ(XML)


์•ˆ์ „ํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ(java) :

์ •์˜๋œ ์ฟผ๋ฆฌ๋ฌธ ์ค‘ delStudent ๋ช…๋ น์–ด ์„ ์–ธ์—์„œ ์ฟผ๋ฆฌ๋ฌธ ์‚ฝ์ž… ์ธ์ž ์ค‘ $name$๋กœ ์ „๋‹ฌ๋˜๋Š” ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ ์—ฐ๊ฒฐํ•ด ์งˆ์˜๋ฌธ ์ƒ์„ฑ
name์˜ ๊ฐ’์œผ๋กœ ' OR 'x'='x' ์ „๋‹ฌ์‹œ (DELETE STUDENTS WHERE NUM = #num# and Name = '' OR 'x'='x') ํ€ด๋ฆฌ๋ฌธ ์ˆ˜ํ–‰๋˜์–ด ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์›์†Œ ์‚ญ์ œํ•จ

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="Student">

	<resultMap id="StudentResult" class="Student">

		<result column="ID" property="id" />

		<result column="NAME" property="name" />

	</resultMap>

	<select id="listStudents" resultMap="StudentResult">

		SELECT NUM, NAME

		FROM STUDENTS

		ORDER BY NUM

	</select>

	<select id="nameStudent" parameterClass="Integer" resultClass="Student">

		SELECT NUM, NAME

		FROM STUDENTS

		WHERE NUM = #num#

	</select>

	<!-- dynamic SQL ์‚ฌ์šฉ -->

	<delete id="delStudent" parameterClass="Student">

		DELETE STUDENTS

		WHERE NUM = #num# AND Name = '$name$'

	</delete>

</sqlMap>

์•ˆ์ „ํ•œ ์ฝ”๋“œ(java) :

Name ์ธ์ž๋ฅผ #name# ํ˜•ํƒœ๋กœ ๋ฐ›๋„๋ก ์ˆ˜์ •

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="Student">

	<resultMap id="StudentResult" class="Student">

		<result column="ID" property="id" />

		<result column="NAME" property="name" />

	</resultMap>

	<select id="listStudents" resultMap="StudentResult">

		SELECT NUM, NAME

		FROM STUDENTS

		ORDER BY NUM

	</select>

	<select id="nameStudent" parameterClass="Integer" resultClass="Student">

		SELECT NUM, NAME

		FROM STUDENTS

		WHERE NUM = #num#

	</select>



	<!-- static SQL ์‚ฌ์šฉ -->

	<delete id="delStudent" parameterClass="Student">

		DELETE STUDENTS

		WHERE NUM = #num# AND Name = '#name#'

	</delete>

</sqlMap>

4) ์˜ˆ์ œ[2]

์‚ฌ์šฉ์ž๊ฐ€ ์†Œ์œ ํ•˜๊ณ  ์žˆ๋Š” item์— ๋Œ€ํ•œ ์ •๋ณด ์–ป์–ด ์˜ค๊ณ  ์žˆ์Œ


์•ˆ์ „ํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ(java) :
$itemName$ ์‚ฌ์šฉํ•ด ๋™์  ์งˆ์˜๋ฌธ ์ƒ์„ฑ
itemname์„ name'; DROP items; --๋กœ ์ฃผ๋ฉด ์ฟผ๋ฆฌ๋ฌธ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋Š” SELECT * FROM items WHERE owner=``user name`` AND itemname= ``item name``;๊ณผ DROP items; ์‹คํ–‰ ๊ฒฐ๊ณผ ๋™์ผ
-> DB์˜ items ํ…Œ์ด๋ธ” ์ •๋ณด ์‚ญ์ œ๋จ

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
 <sqlMap namespace="UserItem">
 ...
 <select id="getItems" parameterClass="MyClass" resultClass="items">
 SELECT * FROM items WHERE owner = #userName# AND itemname = '$itemName$'
 </select>
 ...
 </sqlMap>

์•ˆ์ „ํ•œ ์ฝ”๋“œ(java) :
$itemName$ ๋Œ€์‹  #itemName#์‚ฌ์šฉํ•ด ์ •์  ์งˆ์˜๋ฌธ ์ƒ์„ฑ
itemname์„ name'; DROP items; -ํ•˜๋ฉด itemname์˜ ์ด๋ฆ„์ด name'; DROP items; -์ธ ํ•ญ๋ชฉ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ๋ฌธ ์˜๋„์—์„œ ๋ฒ—์–ด๋‚œ ๋™์ž‘ ๋ฐฉ์ง€ ๊ฐ€๋Šฅ

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
 <sqlMap namespace="UserItem">
 ...
 <select id="getItems" parameterClass="MyClass" resultClass="items">
 SELECT * FROM items WHERE owner = #userName# AND itemname = '#itemName#'
 </select>
 ...
 </sqlMap>