https://blogs.msdn.microsoft.com/tomholl/2009/05/21/protecting-against-xml-entity-expansion-attacks/

Protecting against XML Entity Expansion attacks

Tom Hollander May 21, 2009

One of the critical responsibilities of every developer and architect is to understand, and know how to prevent, as many kinds of security attacks as possible. While there are many types of attacks and many weapons at our disposal to thwart them, the most basic defence we have is input validation. The rule of thumb really needs to be to assume all input from uncontrolled sources is malicious, unless you can prove otherwise. This includes input from end users, as well as input from other systems.

Recently I worked on an application that received XML files from that most untrustworthy of sources, the Internet. Knowing the kind of people who lurk there, we took what seemed like a responsibly paranoid approach involving validating each parsed document against an XML schema, checking a digital signature to ensure it came from a known sender, and cherry-picking the values we needed out of the document.

So I was quite surprised to learn that there were was a class of attack which we had not mitigated. It turns out that you should never load untrusted XML content into a .NET XmlDocument class as a first step, even if you plan to do all sorts of checks on it afterwards. This is because there is a class of attack which can bring your server to meltdown just by getting it to parse some XML.

Consider this piece of XML:

<!DOCTYPE foo [

<!ENTITY a “1234567890” >

<!ENTITY b “&a;&a;&a;&a;&a;&a;&a;&a;” >

<!ENTITY c “&b;&b;&b;&b;&b;&b;&b;&b;” >

<!ENTITY d “&c;&c;&c;&c;&c;&c;&c;&c;” >

<!ENTITY e “&d;&d;&d;&d;&d;&d;&d;&d;” >

<!ENTITY f “&e;&e;&e;&e;&e;&e;&e;&e;” >

<!ENTITY g “&f;&f;&f;&f;&f;&f;&f;&f;” >

<!ENTITY h “&g;&g;&g;&g;&g;&g;&g;&g;” >

<!ENTITY i “&h;&h;&h;&h;&h;&h;&h;&h;” >

<!ENTITY j “&i;&i;&i;&i;&i;&i;&i;&i;” >

<!ENTITY k “&j;&j;&j;&j;&j;&j;&j;&j;” >

<!ENTITY l “&k;&k;&k;&k;&k;&k;&k;&k;” >

<!ENTITY m “&l;&l;&l;&l;&l;&l;&l;&l;” >

]>

<foo>&m;</foo>

This certainly looks like an odd bit of XML, but at first glance it doesn’t appear overly scary. It’s compact, well-formed and actually only contains one element: <foo>. But what’s in that element? It’s a single custom-defined entity, &m;. And how is that defined? Well, it’s 8 other custom &l; entities. So what’s an &l; then? Hmm, it’s 8 &k;s. You can see where this is going. The document will end up with 812 &a;s, where each &a; has 10 characters, so that innocent looking &m; will blow out to 10×812 or 687,194,767,360 characters. And on my reasonably well spec’ed developer machine, expanding that number of characters consumed all of my CPU for longer than I was prepared to put up with. A bad guy armed with this attack isn’t going to steal any data, but they could still cause a lot of damage through denial of service.

The good news is that it’s actually very easy to stop this entity expansion in its tracks. The key is to use an XmlReader before parsing the document into an XmlDocument (or instead of, if you can live without a fully-parsed document). It’s possible to validate against an XSD or other schema type using an XmlReader too, but here’s a minimalist example showing how you can check that a document is well-formed, contains no DTDs (and hence no entity definitions) and is less than 10K in size:

// Prepare text reader and settings for Xml Validation

StringReader textReader = new StringReader(unparsedXml);

XmlReaderSettings settings = new XmlReaderSettings();

settings.XmlResolver = null;

settings.MaxCharactersInDocument = 10000;

// Successfully parse the file, otherwise an XmlException is to be thrown

XmlReader reader = XmlReader.Create(textReader, settings);

while (reader.Read()) ;

If you get to this point without an XmlException being thrown, the document should be safe to parse. Of course, there could be all sorts of evil things lurking within the elements of the document, so you need to continue to use appropriate validation and encoding as you would for any untrusted input.

Protecting against XML Entity Expansion attacks的更多相关文章

  1. ibatis提示Unable to load embedded resource from assembly "Entity.Ce_SQL.xml,Entity".

    原本以为是xml文件配置错误,尝试无果,最终原因未将xml文件的生成操作选择为嵌入的资源.很无语!

  2. XML 实体扩展攻击

    XMl Entity Expansion(攻击)某种程度上类似于 XML Entity Expansion,但是它主要试图通过消耗目标程序的服务器环境来进行DOS攻击的.这种攻击基于XML Entit ...

  3. XEE介绍

    摘要: XMl Entity Expansion(攻击)某种程度上类似于 XML Entity Expansion,但是它主要试图通过消耗目标程序的服务器环境来进行DOS攻击的.这种攻击基于XML E ...

  4. List of XML and HTML character entity references

    A character entity reference refers to the content of a named entity. An entity declaration is creat ...

  5. XML External Entity attack/XXE攻击

    XML External Entity attack/XXE攻击   1.相关背景介绍 可扩展标记语言(eXtensible Markup Language,XML)是一种标记语言,被设计用来传输和存 ...

  6. XXE (XML External Entity Injection) 外部实体注入漏洞案例分析

    ENTITY 实体 在一个甚至多个XML文档中频繁使用某一条数据,我们可以预先定义一个这条数据的“别名”,即一个ENTITY,然后在这些文档中需要该数据的地方调用它. XML定义了两种类型的ENTIT ...

  7. 【译】Attacking XML with XML External Entity Injection (XXE)

    原文链接:Attacking XML with XML External Entity Injection (XXE) XXE:使用XML外部实体注入攻击XML 在XML中,有一种注入外部文件的方式. ...

  8. Play XML Entities

    链接:https://pentesterlab.com/exercises/play_xxe/course Introduction This course details the exploitat ...

  9. XML文件解析之SAX解析

    使用DOM解析的时候是需要把文档的所有内容读入内存然后建立一个DOM树结构,然后通过DOM提供的接口来实现XML文件的解析,如果文件比较小的时候肯定是很方便的.但是如果是XML文件很大的话,那么这种方 ...

随机推荐

  1. 推荐一个不错的css3网站 可以直接调用的

    animate.css 一搜就能出来  我用着还不错

  2. 【java开发】数据类型

    ok,为期两天的ubuntu常用命令学习结束,现在开始java语言的学习. 上篇结尾说了ubuntu下的jdk文件安装,现在顺便说一下win下的jdk环境变量配置 在官网下载符合系统的jdk文件,可以 ...

  3. github的使用(概要版)

    Github的世界 什么是github Github除提供Git仓库托管服务外,还为开发者或团队提供了一系列功能,帮助其高效率,高品质地进行代码编写. 使用github带来哪些变化 写作形式的变化 在 ...

  4. [转]说说JSON和JSONP,也许你会豁然开朗,含jQuery用例

    本文转自:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 说到AJAX就会不可避免的面临两 ...

  5. Manacher's algorithm

    Manacher's algorithm 以\(O(n)\)的线性时间求一个字符串的最大回文子串. 1. 预处理 一个最棘手的问题是需要考虑最长回文子串的长度为奇数和偶数的情况.我们通过在任意两个字符 ...

  6. Libevent初探

    Libevent 是一个用C语言编写的.轻量级的开源高性能网络库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大:源代码相当精炼.易 ...

  7. OpenFlow:Enabling Innovation in Campus Networks

    SDN领域,OpenFLow现在已经成为了广泛使用的南向接口协议.若想好好学习SDN,在这个领域有所进步,需要熟悉OpenFlow协议.我最近找了篇有关OpenFLow的论文,发现最早该协议是在Sig ...

  8. 高阶Laplace曲面形变算法(Polyharmonic Deformation)

    数学上曲面的连续光滑形变可以通过最小化能量函数来建模得到,其中能量函数用来调节曲面的拉伸或弯曲程度,那么能量函数最小化同时满足所有边界条件的最优解就是待求曲面. 能量函数通常是二次函数形式: 其中S* ...

  9. 爬虫(Java实现)

    说明: 使用了htmlparser库. 运行过程: 从某个网址开始,摘取网页中的链接,并通过广度搜索,对这些链接递归执行上述操作. 在以上过程中把网址存入数据库中.以防止搜索中出现环路. 但是,程序经 ...

  10. 移位操作(>>、<<)

    public static void main(String[] args) { /** 移位就是二进制的数往左或右移动,因为接近计算机底层,所以速度比较快 * 8 4 2 1 * * 7 化为二进制 ...