Skip to content

Latest commit

ย 

History

History
153 lines (109 loc) ยท 4.4 KB

SQL_Injection_JDO.md

File metadata and controls

153 lines (109 loc) ยท 4.4 KB

SQL Injection : JDO

JDO๋ž€?

Java Data Objects๋กœ, ์ ์ ˆํ•œ ๊ฒ€์‚ฌ ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  JDO API์˜ SQL ๋˜๋Š” JDOQL ์งˆ์˜๋ฌธ ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ฌธ์ž์—ด๋กœ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์˜๋„ํ•˜์ง€ ์•Š์•˜๋˜ ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•ด ์งˆ์˜๋ฌธ ์˜๋ฏธ๋ฅผ ์™œ๊ณก์‹œํ‚ค๊ฑฐ๋‚˜ ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์ž„์˜์˜ ์งˆ์˜ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

โžก๏ธ ์•ˆ์ „ํ•œ ์ฝ”๋”ฉ ๋ฐฉ๋ฒ•

  • JDO ์งˆ์˜๋ฌธ ์ƒ์„ฑ ์‹œ ์ƒ์ˆ˜ ๋ฌธ์ž์—ด๋งŒ์„ ์‚ฌ์šฉํ•˜๊ณ , Query.execute ์‹คํ–‰ ์‹œ ์ธ์ž๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” Parameterize Query๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์˜ˆ์ œ1 - Parameterize Query

SELECT col1 FROM MYTABLE WHERE name=' ์ž…๋ ฅ๋ฌธ ';

์œ„์˜ ๋ช…๋ น๋ฌธ์—์„œ ์ž…๋ ฅ๋ฌธ ์ž๋ฆฌ์— name'; DROP MYTABLE; โ€” ๋ฅผ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช…๋ น๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.

SELECT col1 FROM MYTABLE WHERE name = 'name'; DROP MYTABLE;โ€”'

๋”ฐ๋ผ์„œ ๊ฒ€์‚ฌ๊ณผ์ • ์—†์ด ์ž…๋ ฅ๋ฌธ์„ ๋ฐ›์œผ๋ฉด Mytable ์ด ์‚ญ์ œ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.


์•ˆ์ „ํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ

try 
{
	Properties props = new Properties();
	String fileName = "contacts.txt";
	FileInputStream in = new FileInputStream(fileName);
	if( in != null ) { props.load(in); }
	in.close();

	// ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ ๋ฐ›๊ธฐ
	String name = props.getProperty("name");
	if( name != null ) 
	{
		query += " where name = '" + name + "'";
	}
	} 
catch (IOException e) { โ€ฆโ€ฆ }

// ์™ธ๋ถ€ ์ž…๋ ฅ๊ฐ’์ด JDO ๊ฐ์ฒด์˜ ์ธ์ž๋กœ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
return (List<Contact>) pm.newQuery(query).execute()

์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์™ธ๋ถ€์—์„œ ์ž…๋ ฅํ•œ ๊ฐ’์„ JDO ๊ฐ์ฒด์˜ ์ธ์ž๋กœ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ฟผ๋ฆฌ๋ฌธ์ด ์ˆ˜์ •๋  ์ˆ˜ ์žˆ์–ด ์•ˆ์ „ํ•˜์ง€ ์•Š๋‹ค.

์™ธ๋ถ€ ์ž…๋ ฅ๋ถ€๋ถ„์„ ? ๋กœ ์„ค์ •ํ•˜๊ณ , ์‹คํ–‰ ์‹œ ํ•ด๋‹น ์ธ์ž๊ฐ’์ด ์ „๋‹ฌ๋˜๋„๋ก ์ˆ˜์ •ํ•˜๋ฉด ์™ธ๋ถ€์˜ ์ž…๋ ฅ๊ฐ’์ด ์ฟผ๋ฆฌ๋ฌธ์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ ํ•  ์ˆ˜ ์žˆ๋‹ค.


์•ˆ์ „ํ•œ ์ฝ”๋“œ

try 
{
	Properties props = new Properties();
	String fileName = "contacts.txt";
	FileInputStream in = new FileInputStream(fileName);
	props.load(in);
	// ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๊ฐ’ ๋ฐ›๊ธฐ
	name = props.getProperty("name");
	// ์ž…๋ ฅ๊ฐ’์„ ์ ๊ฒ€ํ•œ๋‹ค
	if (name == null || "".equals(name)) return null;
	query += " where name = ?";
} 
catch (IOException e) { โ€ฆโ€ฆ }

javax.jdo.Query q = pm.newQuery(query);
// Query API์˜ ์ธ์ž๋กœ ์‚ฌ์šฉ
return (List<Contact>) q.execute(name);



์˜ˆ์ œ2 - Parameterize Query

item์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์–ด์˜ฌ ๋•Œ, itemname์˜ ์ž…๋ ฅ ๊ฐ’์„ name' OR 'a' = 'a' ๋กœ ์ฃผ๊ฒŒ๋˜๋ฉด SELECT * FROM items๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•˜๊ฒŒ ๋œ๋‹ค.

์ฆ‰, ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ•˜๊ฒŒ ๋œ๋‹ค.

SELECT * FROM items WHERE itemname='name' OR 'a' = 'a';

๋”ฐ๋ผ์„œ ์›๋ž˜ ์˜๋„์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ชจ๋“  ์‚ฌ์šฉ์ž์˜ ๋ชจ๋“  item์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.


์•ˆ์ „ํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
	String command = request.getParameter(COMMAND_PARAM);
	...
	if (command.equals(GET_USER_INFO_CMD)) 
	{
		String userId = request.getParameter(USER_ID_PARM);
		String itemName = request.getParameter(ITEM_NAME_PARM);
		 
		PersistenceManager manager = factory.getPersistenceManager();
		
  		//query๋ฌธ์— ์ž…๋ ฅ๊ฐ’ ๋ฐ”๋กœ ์‚ฌ์šฉ
		String sql = "SELECT * FROM items WHERE owner = '" + userName + "' AND itemname = '"
		+ itemName + "'";
		Query query = pm.newQuery(Query.SQL, sql);
		query.setClass(Person.class);
		List people = (List)query.execute();
	...
	}
}

์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์™ธ๋ถ€์˜ ์ž…๋ ฅ๊ฐ’ ๋ฌธ์ž์—ด์„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์‹คํ–‰ ์‹œ ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ์ธ์ž๋กœ ์ „๋‹ฌํ•จ์œผ๋กœ์จ ์งˆ์˜๋ฌธ ์˜๋„๋ฅผ ๋ฐ”๊พธ์ง€ ๋ชปํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.


์•ˆ์ „ํ•œ ์ฝ”๋“œ ์˜ˆ์‹œ

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
	String command = request.getParameter(COMMAND_PARAM);
	...
	if (command.equals(GET_USER_INFO_CMD)) 
	{
		String userId = request.getParameter(USER_ID_PARM);
		String itemName = request.getParameter(ITEM_NAME_PARM);
		
		PersistenceManager manager = factory.getPersistenceManager();
		
		String sql = "SELECT * FROM items WHERE owner = ? AND itemname = ?";
		Query query = pm.newQuery(Query.SQL, sql);
		query.setClass(Person.class);
		 
		List people = (List)query.execute(userId, itemName);
		 ...
	}
}