DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Understanding Java Signals
  • A Systematic Approach for Java Software Upgrades
  • Thread-Safety Pitfalls in XML Processing

Trending

  • Testing SingleStore's MCP Server
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  1. DZone
  2. Coding
  3. Languages
  4. Preventing XXE in Java Applications

Preventing XXE in Java Applications

Impact, exploitation, and prevention of XML External Entity Vulnerabilities.

By 
Vickie Li user avatar
Vickie Li
·
Feb. 16, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
2.9K Views

Join the DZone community and get the full member experience.

Join For Free

Welcome back to AppSec simplified! In this tutorial, we are going to talk about how you can prevent XXEs in Java applications. If you are not already familiar with XXEs, please read my previous post first: https://blog.shiftleft.io/intro-to-xxe-vulnerabilities-appsec-simplified-80be40102815.

Why XXEs Happen

DTDs are used to define the structure of an XML document. Within DTDs, you can declare “XML entities”. There is a special type of XML entities called “external entities”, which are used to access local or remote content with a URL.

For example, this DTD declares an external entity named “file” that points to file:///secrets.txton the local file system. The XML parser will replace any &file reference in the document with the contents of file:///secrets.txt.

XML
 




xxxxxxxxxx
1


 
1
<?xml version=”1.0" encoding=”UTF-8"?>
2
<!DOCTYPE example [
3
  <!ENTITY file SYSTEM "file:///secrets.txt" >
4
]>
5
<example>&file;</example>



If users can declare arbitrary XML entities in their uploads, they can declare an external entity to any location on your machine. For example, this XML file contains an external entity that points to file:////etc/shadow on your server.

XML
 




xxxxxxxxxx
1


 
1
<?xml version=”1.0" encoding=”UTF-8"?>
2
<!DOCTYPE example [
3
  <!ENTITY file SYSTEM "file:////etc/shadow" >
4
]>
5
<example>&file;</example>



The “/etc/shadow” file stores usernames and their encrypted passwords on Unix systems. When the parsed XML document is displayed back to the user, the contents of file:////etc/shadow will also be included.

Impact of XXEs

By exploiting the XML parser, a malicious user can now read arbitrary files on your server. They can access and exfiltrate files such as system files, source code, directory listings on the local machine.

Network Exploration

Besides retrieving system files, attackers can use XXE vulnerabilities to launch SSRF attacks against the local network. For example, they can launch a port scan by switching out the external entity’s URL with different ports on the server. Using this technique, they can explore the local network to find other vulnerable machines or services to target.

XML
 




xxxxxxxxxx
1


 
1
<?xml version=”1.0" encoding=”UTF-8"?>
2
<!DOCTYPE example [
3
  <!ENTITY file SYSTEM "http://10.0.0.1:80" >
4
]>
5
<example>&file;</example>



XXEs can also be used to launch an SSRF to read AWS cloud services instance metadata. By accessing the address 169.254.169.254, attackers might be able to retrieve access tokens, secrets, and session token keys of the hosting cloud provider.

XML
 




xxxxxxxxxx
1


 
1
<?xml version=”1.0" encoding=”UTF-8"?>
2
<!DOCTYPE example [
3
  <!ENTITY file SYSTEM "http://169.254.169.254/latest/meta-data/iam/security-credentials/" >
4
]>
5
<example>&file;</example>



Denial of Service

Another potential way that attackers can exploit XML vulnerabilities is to launch Denial of Service attacks, which is when an attacker disrupts the machine so that legitimate users cannot access its services.

Take a look at this XML file for example. This DTD embeds entities within entities, causing the XML parser to recursively dereference to get to the root entity value “lol”.

XML
 




xxxxxxxxxx
1
14


 
1
<?xml version=”1.0" encoding=”UTF-8"?>
2
<!DOCTYPE example [
3
<!ELEMENT example ANY >
4
<!ENTITY lol “lol”>
5
<!ENTITY lol1 “&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;”>
6
<!ENTITY lol2 “&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;”>
7
<!ENTITY lol3 “&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;”>
8
<!ENTITY lol4 “&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;”>
9
<!ENTITY lol5 “&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;”>
10
<!ENTITY lol6 “&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;”>
11
<!ENTITY lol7 “&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;”>
12
<!ENTITY lol8 “&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;”>
13
<!ENTITY lol9 “&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;”>]>
14
<example>&lol9;</example>



Each “lol9” entity would be expanded into ten “lol8”, and each of those would become ten “lol7”, and so on. Eventually, one “lol9” will be expanded into one billion “lol”s. This will overload the memory of the XML parser, potentially causing it to crash.

This attack method is called a “Billion laughs attack” or an “XML bomb”. Interestingly, although this attack is often classified as an XXE attack, it does not involve the use of any external entities! It uses the recursive processing of internal entities instead.

Preventing XXEs in Java

So how do you prevent XXEs from happening? The best way to prevent XXEs 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. If it is impossible to disable DTDs completely, then external entities, parameter entities, and inline DTDs should be disabled. You can also disable the expansion of XML entities entirely. How you can configure the behavior of an XML parser will depend on the XML parser you use.

Java applications are particularly prone to XXEs because most Java XML parsers have the requirements for XXEs enabled by default. To prevent XXE attacks in a Java application, you need to explicitly disable these functionalities.

DocumentBuilderFactory

For instance, for the DocumentBuilderFactory library, you can disallow DTDs with this line.

Java
xxxxxxxxxx
1
 
1
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);


If completely disabling DTDs is not possible, you can disallow XML external entities and parameter entities. Parameter entities are XML entities that can only be referenced elsewhere within the DTD. They can also allow attackers to launch XXEs.

Java
xxxxxxxxxx
1
 
1
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
2
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);


You should also disable external DTDs to prevent attackers from hosting an external DTD and referencing it in an XML document.

Java
 




xxxxxxxxxx
1


 
1
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);



XInclude is a special XML feature that builds a separate XML document from a tag. They also allow attackers to trigger XXEs. So set “setXIncludeAware” to false to disallow XInclude processing. Finally, set “setExpandEntityReferences” to prevent parsers from recursively expanding XML entities into XML bombs.

dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);

XMLInputFactory

For the XMLInputFactory, these lines disable DTDs and external entities.

Java
 




xxxxxxxxxx
1


 
1
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
2
xmlInputFactory.setProperty(“javax.xml.stream.isSupportingExternalEntities”, false);



XMLReader

And to protect XMLReader from XXEs you can disallow DTDs and external DTDs:

Java
 




xxxxxxxxxx
1


 
1
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
2
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);



Or disable the use of external and parameter entities.

Java
 




x


 
1
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
2
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);



The code needed to protect parsers from XXEs varies for different Java XML parsers. For more information about how to secure various parsers visit the OWASP cheat sheet here.

Keeping Libraries Up to Date

It's not just your XML parsers that you should look out for. Many third-party libraries deal with XMLs and thus are susceptible to XXE attacks. Verify that your dependencies are secure from XXE attacks, and update libraries to secure versions.

Hope you had fun with this tutorial! I sure had fun writing it. Static analysis is the most efficient way of uncovering most vulnerabilities in your applications. If you’re interested in learning more about ShiftLeft’s static analysis tool NG-SAST, visit us here.

Thanks for reading! What is the most challenging part of developing secure software for you? I’d love to know. Feel free to connect on Twitter @vickieli7.

Java (programming language) XML application entity Parser (programming language)

Published at DZone with permission of Vickie Li. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Understanding Java Signals
  • A Systematic Approach for Java Software Upgrades
  • Thread-Safety Pitfalls in XML Processing

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!