Billion Laughs Attack是否应该在C#中运行?
我试图从MSDN杂志页面测试XML代码,它说下面的代码行将导致处理时内存使用量增加到3GB。
<!DOCTYPE lolz [ ]> &lol9;
当我尝试将该文本粘贴到Visual Studio中的xml文件中时,确实显示内存和CPU使用率都有所增加。 但是,当我尝试将其放在文本文件中而不是XML文件并使用c#加载它时,它没有任何影响。
更新:我认为LoadXml方法应该有影响,但我想这不是处理部分。 当我试图得到第一个孩子时,它(即c#)抛出一个exception,告诉MaxCharactersFromEntities
超出了MaxCharactersFromEntities
。
更新:这里也是我的代码:
using System; using System.Xml; namespace BillionLaughsAttack { class Program { //The file containing the billion laughs mentioned previously //a txt file: Since an xml file causes visual studio to parse static String xmlFileLocation = "./MyData/DeepXML.txt"; static void Main(string[] args) { String xmlContent = null; System.IO.StreamReader sr; System.Xml.XmlDocument document = new XmlDocument(); try { sr = new System.IO.StreamReader(xmlFileLocation); xmlContent = sr.ReadToEnd(); //Load xml containing Billion Laughs Attack (this won't do anything!) document.LoadXml(xmlContent); //Proces xml by getting first child (this will cause an exception!) String val = document.FirstChild.Value; } catch (Exception e) { Console.WriteLine(e.Message); } } } }
此攻击利用易受攻击的XMLfunction。
通过XML解析器运行它将递归地扩展实体并占用大量内存。
将其作为纯文本阅读根本不会做任何事情。