首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JAXB防止XXE攻击

用JAXB防止XXE攻击
EN

Stack Overflow用户
提问于 2012-10-19 15:14:45
回答 1查看 55.2K关注 0票数 46

最近,我们对代码进行了安全审计,问题之一是我们的应用程序受到Xml eXternal Entity (XXE)攻击。

基本上,应用程序是通过Web接收XML输入的计算器.

下面是对我们的应用程序进行这种XXE攻击的一个例子:

代码语言:javascript
复制
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <foo:calculateStuff>
         <!--Optional:-->
         <xmlInput><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE currency [  
   <!ENTITY include SYSTEM "file:///d:/" >]>
<calcinput>...</calcinput>
]]></xmlInput>
      </foo:calculateStuff>
   </soapenv:Body>
</soapenv:Envelope>

如您所见,我们可以引用指向外部文件("file:///d:/")的实体。

关于XML本身( <calcinput>...</calcinput>部分)是用JAXB (v2.1)进行解组的。web服务部分基于jaxws-rt (2.1)。

我需要做些什么来保护我的网络服务?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-19 16:48:40

JAXB

您可以通过将eXternal和/或XMLInputFactory.SUPPORT_DTD属性设置为falseXMLStreamReader解压缩来防止Xml IS_SUPPORTING_EXTERNAL_ENTITIES实体(XXE)攻击。

JAX-WS

JAX实现应该为您处理这个问题。如果没有,我建议针对特定的植入打开一个bug。

示例

Demo

代码语言:javascript
复制
package xxe;

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("src/xxe/input.xml"));

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(xsr);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }

}

input.xml

此XML文档包含一个实体,该实体已被设置以获取用于创建此示例的文件列表。

代码语言:javascript
复制
<?xml version="1.0"?>
<!DOCTYPE customer
[
<!ENTITY name SYSTEM "/Users/bdoughan/Examples/src/xxe/">
]
>
<customer>
  <name>&name;</name>
</customer>

客户

代码语言:javascript
复制
package xxe;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

输出-默认配置

默认情况下,实体将被解析。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
    <name>Customer.java
Demo.java
input.xml
</name>
</customer>

XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES false属性设置为属性时的输出

设置此属性时,该实体不会被解析。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
    <name></name>
</customer>

XMLInputFactory.SUPPORT_DTD false属性设置为属性时的输出

设置此属性时,将引发试图解析实体的异常。

代码语言:javascript
复制
Exception in thread "main" javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.stream.XMLStreamException: ParseError at [row,col]:[8,15]
Message: The entity "name" was referenced, but not declared.]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:436)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:372)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:342)
    at xxe.Demo.main(Demo.java:18)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[8,15]
Message: The entity "name" was referenced, but not declared.
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:598)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:196)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:370)
    ... 2 more
票数 67
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12977299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档