【问题标题】:How to prevent XML Injection like XML Bomb and XXE attack如何防止 XML 注入如 XML Bomb 和 XXE 攻击
【发布时间】:2014-10-21 13:47:58
【问题描述】:

我正在开发一个 android 应用程序

android:minSdkVersion="14"

在这个应用程序中需要解析一个 xml。为此,我正在使用这样的 DOM 解析器

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
Document doc = null;
try {      
    dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    e.printStackTrace();
}

但是当检查代码的安全性时,我遇到了两个在线安​​全问题

dBuilder = dbFactory.newDocumentBuilder();,分别是

1.XML实体扩展注入(XML炸弹)

2.XML外部实体注入(XXE攻击)

经过一番研究,我添加了这一行 dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

但现在执行此行时出现异常

javax.xml.parsers.ParserConfigurationException: http://javax.xml.XMLConstants/feature/secure-processing

谁能帮帮我?

【问题讨论】:

  • 我遇到了同样的问题。你有没有找到解决方案?
  • @Elliot Chance - 不
  • 你们中有人找到解决方案了吗?

标签: java android parsing xml-parsing


【解决方案1】:

您是否尝试过来自OWASP page 的以下sn-p?

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features
...

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
  // This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all XML entity attacks are prevented
  // Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
  String FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
  dbf.setFeature(FEATURE, true);

  // If you can't completely disable DTDs, then at least do the following:
  // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities
  // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities
  FEATURE = "http://xml.org/sax/features/external-general-entities";
  dbf.setFeature(FEATURE, false);

  // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-parameter-entities
  // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities
  FEATURE = "http://xml.org/sax/features/external-parameter-entities";
  dbf.setFeature(FEATURE, false);

  // and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks" (see reference below)
  dbf.setXIncludeAware(false);
  dbf.setExpandEntityReferences(false);

  // And, per Timothy Morgan: "If for some reason support for inline DOCTYPEs are a requirement, then 
  // ensure the entity settings are disabled (as shown above) and beware that SSRF attacks
  // (http://cwe.mitre.org/data/definitions/918.html) and denial 
  // of service attacks (such as billion laughs or decompression bombs via "jar:") are a risk."

  // remaining parser logic
  ...

    catch (ParserConfigurationException e) {
        // This should catch a failed setFeature feature
        logger.info("ParserConfigurationException was thrown. The feature '" +
                    FEATURE +
                    "' is probably not supported by your XML processor.");
        ...
    }
    catch (SAXException e) {
        // On Apache, this should be thrown when disallowing DOCTYPE
        logger.warning("A DOCTYPE was passed into the XML document");
        ...
    }
    catch (IOException e) {
        // XXE that points to a file that doesn't exist
        logger.error("IOException occurred, XXE may still possible: " + e.getMessage());
        ...
    }

【讨论】:

  • 这也行不通。我找不到任何关于此的文档。所有解析器都需要支持 FEATURE_SECURE_PROCESSING...但没有关于为什么 android 行为不同的信息。
【解决方案2】:

字符串 jaxbContext = "com.fnf.dfbatch.jaxb";

    JAXBContext jc = null;
    Unmarshaller u = null;
    String FEATURE_GENERAL_ENTITIES = "http://xml.org/sax/features/external-general-entities";
    String FEATURE_PARAMETER_ENTITIES = "http://xml.org/sax/features/external-parameter-entities";
    try {
        jc = JAXBContext.newInstance(jaxbContext);
        u = jc.createUnmarshaller();
        /*jobsDef = (BatchJobs) u.unmarshal(DfBatchDriver.class
                .getClassLoader().getResourceAsStream(
                        DfJobManager.configFile));*/

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();          
        dbf.setFeature(FEATURE_GENERAL_ENTITIES, false);            
        dbf.setFeature(FEATURE_PARAMETER_ENTITIES, false);      
        dbf.setXIncludeAware(false);
        dbf.setExpandEntityReferences(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(DfBatchDriver.class
                .getClassLoader().getResourceAsStream(
                        DfJobManager.configFile));
        jobsDef = (BatchJobs) u.unmarshal(document);

【讨论】:

  • 想解释一下你的答案吗?
猜你喜欢
  • 2017-03-31
  • 2012-12-23
  • 2012-10-10
  • 1970-01-01
  • 2016-05-01
  • 2015-10-10
  • 1970-01-01
  • 2020-12-15
  • 2020-02-24
相关资源
最近更新 更多