발생원인
외부 입력값을 적절한 검사과정 없이 XPath 쿼리문 생성에 이용할 경우 발생
영향
데이터 변조, 조회, 인증 우회
코드예
안전하기 않은 코드의 예 - JAVA |
…… // 외부로 부터 입력을 받음 String name = props.getProperty("name"); String passwd = props.getProperty("password"); …… XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); …… // 외부 입력이 xpath의 인자로 사용 XPathExpression expr = xpath.compile("//users/user[login/text()='" +name + "' and password/text() = '" + passwd + "']/home_dir/text()"); Object result = xpr.evaluate(doc,XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++){ String value = nodes.item(i).getNodeValue(); if (value.indexOf(">") < 0) { System.out.println(value); } …… |
Name과 passwd에 대한 입력값 검증을 수행하지 않음 |
안전한 코드의 예 - JAVA |
dologin.xp 파일 declare variable $loginID as xs:string external; declare variable $password as xs:string external; //users/user[@loginID=$loginID //and @password=$password] //XQuery를 이용한 XPath Injection 방지 //외부로 부터 입력을 받음 String name = props.getProperty("name"); String passwd = props.getProperty("password"); Document doc = new Builder().build("users.xml"); // XQuery를 위한 정보 로딩 XQuery xquery = new XQueryFactory().createXQuery(new File("dologin.xq")); Map vars = new HashMap(); vars.put("loginID", name); vars.put("password", passwd); Nodes results = xquery.execute(doc, null, vars).toNodes(); for (int i=0; i < results.size(); i++){ System.out.println(results.get(i).toXML()); } |
쿼리 구조가 변경되는 것을 막는 형태로 구성 |
<참고> XPath 란?
XML문서의 part를 정의하기 위한 문법이다. XML내부를 검색하기 위한 path expression을 사용한다. XPath는 표준함수의 라이브러리를 갖고 있다. W3C표준이다.
XPath 예제
|
|
---|---|
./author |
|
author |
|
first.name |
|
/bookstore |
|
//author |
|
book[/bookstore/@specialty=@style] |
|
author/first-name |
|
bookstore//title |
|
bookstore/*/title |
|
bookstore//book/excerpt//emph |
|
.//title |
|
author/* |
|
book/*/last-name |
|
*/* |
|
*[@specialty] |
|
@style |
|
price/@exchange |
|
price/@exchange/total |
|
book[@style] |
|
book/@style |
|
@* |
|
./first-name |
|
first-name |
|
author[1] |
|
author[first-name][3] |
|
my:book |
|
my:* |
|
@my:* |
|
<x> <y/> <y/> </x> <x> <y/> <y/> </x>
|
|
---|---|
x/y[1] |
|
x/y[position() = 1] |
|
(x/y)[1] |
|
x[1]/y[2] |
|
|
|
---|---|
book[last()] |
|
book/author[last()] |
|
(book/author)[last()] |
|
book[excerpt] |
|
book[excerpt]/title |
|
book[excerpt]/author[degree] |
|
book[author/degree] |
|
author[degree][award] |
|
author[degree and award] |
|
author[(degree or award) and publication] |
|
author[degree and not(publication)] |
|
author[not(degree or award) and publication] |
|
author[last-name = "Bob"] |
|
author[last-name[1] = "Bob"] |
|
author[last-name [position()=1]= "Bob"] |
|
degree[@from != "Harvard"] |
|
author[.= "Matthew Bob"] |
|
author[last-name = "Bob" and ../price > 50] |
|
book[position() <= 3] |
|
author[not(last-name = "Bob")] |
|
author[first-name = "Bob"] |
|
author[* = "Bob"] |
|
author[last-name = "Bob" and first-name = "Joe"] |
|
price[@intl = "Canada"] |
|
degree[position() < 3] |
|
p/text()[2] |
|
ancestor::book[1] |
|
ancestor::book[author][1] |
|
ancestor::author[parent::book][1] |
|
통합예제
통합예제
x | y/x
XML 파일(data1.xml)
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="union.xsl"?>
<root>
<x>green</x>
<y>
<x>blue</x>
<x>blue</x>
</y>
<z>
<x>red</x>
<x>red</x>
</z>
<x>green</x>
</root>
XSLT 파일(union.xsl)
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<xsl:for-each select="x | y/x">
<xsl:value-of select="."/>,
<xsl:if test="not(position()=last())">,</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
서식이 지정된 출력
프로세서 출력
<?xml version="1.0" encoding="UTF-16"?>green,blue,blue,green
'보안 > 시큐어코딩' 카테고리의 다른 글
[시큐어코딩-10] 입력데이터 검증및 표현(10) 크로스사이트 요청위조 (0) | 2012.10.28 |
---|---|
[시큐어코딩-9] 입력데이터 검증및 표현(9) LDAP삽입 (0) | 2012.10.28 |
[시큐어코딩-7] 입력데이터 검증및 표현(7) XQuery삽입 (0) | 2012.10.28 |
[시큐어코딩-6] 입력데이터 검증및 표현(6) 신뢰되지않은 URL주소로 자동접속연결 (0) | 2012.10.28 |
[시큐어코딩-5] 입력데이터 검증및 표현(5) 위험한 형식 파일 업로드 (0) | 2012.10.28 |