Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent XXE attack (XmlDocument in .NET)

We had a security audit on our code, and they mentioned that our code is vulnerable to EXternal Entity (XXE) attack. I am using following code -

string OurOutputXMLString= "<ce><input><transaction><length>00000</length><tran_type>Login</tran_type></transaction><user><user_id>ce_userid</user_id><subscriber_name>ce_subscribername</subscriber_name><subscriber_id>ce_subscriberid</subscriber_id><group_id>ce_groupid</group_id><permissions></permissions></user><consumer><login_details><username>UnitTester9</username><password>pDhE5AsKBHw85Sqgg6qdKQ==</password><pin>tOlkiae9epM=</pin></login_details></consumer></input></ce>"   XmlDocument xmlDoc = new XmlDocument();  xmlDoc.LoadXml(OurOutputXMLString); 

In the audit report they say that it's failing because an XML entity can contain URLs that can resolve outside of intended control. XML entity resolver will attempt to resolve and retrieve external references. If attacker-controlled XML can be submitted to one of these functions, then the attacker could gain access to information about an internal network, local filesystem, or other sensitive data. To avoid this I wrote the following code but it doesn't work.

MemoryStream stream =     new MemoryStream(System.Text.Encoding.Default.GetBytes(OurOutputXMLString));  XmlReaderSettings settings = new XmlReaderSettings();  settings.DtdProcessing = DtdProcessing.Prohibit; settings.MaxCharactersFromEntities = 6000; XmlReader reader = XmlReader.Create(stream, settings); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); 

But I can see here that reader does not have any value to load into xmlDoc(XmlDocument). Can anyone help where I am missing things?

like image 592
junni lomo Avatar asked Jan 09 '13 08:01

junni lomo


People also ask

How can XXE be prevented?

The safest way to prevent XXE is always to disable DTDs (External Entities) completely. Depending on the parser, the method should be similar to the following: factory. setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

What is one way to prevent XML injection attacks?

The best way to avoid XML Bombs is for the application to configure the XML parser to disable inline expansion of entities.

What XML parser features you should disable to minimize the risk of an XXE vulnerability?

The best way to prevent XXE is to limit the capabilities of your XML parsers. Since DTD processing is a requirement for XXE attacks, developers should disable DTD processing on their XML parsers.

What type of applications might be vulnerable to XXE attacks?

Applications and in particular XML-based web services or downstream integrations might be vulnerable to attack if: * The application accepts XML directly or XML uploads, especially from untrusted sources, or inserts untrusted data into XML documents, which is then parsed by an XML processor.


2 Answers

External resources are resolved using the XmlResolver provided via XmlDocument.XmlResolver property. If your XML documents **should not contain any external resource **(for example DTDs or schemas) simply set this property to null:

XmlDocument xmlDoc = new XmlDocument(); xmlDoc.XmlResolver = null; xmlDoc.LoadXml(OurOutputXMLString); 

If you want to filter where these URLs come from (for example to allow only certain domains) just derive your own class from XmlUrlResolver and override the ResolveUri() method. There you can check what the URL is and sanitize it (for example you can allow only URLs within your local network or from trusted sources).

For example:

class CustomUrlResovler : XmlUrlResolver {     public override Uri ResolveUri(Uri baseUri, string relativeUri)     {         Uri uri = new Uri(baseUri, relativeUri);         if (IsUnsafeHost(uri.Host))             return null;          return base.ResolveUri(baseUri, relativeUri);     }      private bool IsUnsafeHost(string host)     {         return false;      } } 

Where IsUnsafeHost() is a custom function that check if the given host is allowed or not. See this post here on SO for few ideas. Just return null from ResolveUri() to save your code from this kind of attacks. In case the URI is allowed you can simply return the default XmlUrlResolver.ResolveUri() implementation.

To use it:

XmlDocument xmlDoc = new XmlDocument(); xmlDoc.XmlResolver = new CustomUrlResolver(); xmlDoc.LoadXml(OurOutputXMLString); 

For more details about how XML external resources are resolved just read Resolving External Resources on MS Docs. If your code is more complex than this example then you should definitely read Remarks section for XmlDocument.XmlResolver property.

like image 133
Adriano Repetti Avatar answered Sep 20 '22 02:09

Adriano Repetti


So its better to use

new XmlDocument { XmlResolver = null }; 

Interestingly from .net 4.5.2 and 4.6, the default resolver behaves differently and does not use an XmlUrlResolver upfront implicitly to resolve any urls or locations as i seen.

//In pre 4.5.2 it is a security issue. //In 4.5.2 it will not resolve any more the url references in dtd and such,  //Still better to avoid the below since it will trigger security warnings. new XmlDocument();  
like image 27
mousetwentytwo Avatar answered Sep 22 '22 02:09

mousetwentytwo