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

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

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

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

  • Go Application Vulnerability Cheatsheet
  • How To Create a Homescreen Widget in Android
  • Application Security in Technical Product Management
  • Combatting the OpenSSH Vulnerability

Trending

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Concourse CI/CD Pipeline: Webhook Triggers
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  1. DZone
  2. Coding
  3. Languages
  4. Intro to XXE Vulnerabilities: AppSec Simplified

Intro to XXE Vulnerabilities: AppSec Simplified

Protect your XML parsers against malicious XML documents!

By 
Vickie Li user avatar
Vickie Li
·
Feb. 03, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.5K Views

Join the DZone community and get the full member experience.

Join For Free

Hey! And welcome to the first installment of AppSec Simplified. Today, we are going to explore a fascinating vulnerability called XML External Entity vulnerabilities, or XXEs!

What Are XXEs?

To understand XXEs, we need to first talk about “DTDs” in XML documents.

XML documents can contain a Document Type Definition, or a DTD. DTDs are used to define the structure of an XML document and the data it contains. They are declared within the document using a DOCTYPE tag, like this:

XML
 




x


 
1
<?xml version=”1.0" encoding=”UTF-8"?>
2
<!DOCTYPE ...INSERT DTD HERE...>


Within DTDs, you can declare XML entities. XML entities work a lot like variables in programming languages.

For example, this DTD declares an XML entity called greeting with the value of ”Hello World!”. In the XML document, you can reference this entity using &greeting, and the XML document will load “Hello World!” in its place.

XML
 




xxxxxxxxxx
1


 
1
<?xml version=”1.0" encoding=”UTF-8"?>
2
<!DOCTYPE example [
3
  <!ENTITY greeting "Hello World!" >
4
]>
5
<example>&greeting;</example>


XML External Entities

There is a special type of XML entities called external entities. They are used to access local or remote content with a URL. XML external entities can be declared using the SYSTEM keyword. 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
<example>&file;</example>


What Is the Problem?

So, what is the problem with this functionality? Imagine if your application parses user-supplied XML documents and displays the results on your site.

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
<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.

By exploiting the XML parser, a malicious user can now read arbitrary files on your server. They might be able to retrieve user information, configuration files, or other sensitive information like AWS credentials. Attackers can also launch a denial of service attack by making the XML parser dereference entities recursively. This is called a billion laughs attack. Talk about a catastrophic vulnerability!

Preventing XXEs

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. For example, if you are using the default PHP XML parser, libxml_disable_entity_loader needs to be set to TRUE to disable the use of external entities. For more information on how to do it for your parser, consult the OWASP Cheat Sheet.

Finally, you should routinely audit your applications to catch XXEs that might already be written into your code.

How Do You Detect XXEs?

How would you go about detecting XXEs in your application? One approach you can take is to go through your application’s functionalities that process XML documents and test them with malicious XML input. For example, you can submit this XML document and see if the file file:///etc/hostname gets sent back to you.

XML
 




xxxxxxxxxx
1


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


But obviously, using a black-box approach is risky because it does not guarantee that you will find all instances of XXE in your system. Since XXE is a vulnerability with a clear and definable signature, analyzing your source code is a much better approach.

An XML parser is vulnerable to XXEs when they process user-supplied XML files or XML files whose DTD is polluted by user input. At the same time, the parser needs to be configured to evaluate DTDs and external entities. We are essentially looking for two things:

  1. First, we are looking for XML parsers that receive user-supplied XML files or DTDs.
  2. Then, we are checking if that XML parser evaluates DTDs or external entities.

You can manually audit your source code to look for these signatures or employ a static analysis security testing tool to stare at the code for you. Most static analysis tools can detect if your XML parsers are evaluating DTDs and external entities. But only static analysis tools that can work with data flows like ShiftLeft’s NG-SAST can detect if that parser is reachable by user input and thus automatically detect both of these conditions.

Finally, application dependencies cause many XXEs, so you should also monitor and upgrade all XML processors and libraries in use by your application or the underlying operating system.

Later in AppSec Simplified, we’ll target an open-source application and see how we can find XXE vulnerabilities using code analysis. Stay tuned!

If you’re interested in learning about ShiftLeft and how we’re approaching DevSecOps with a fresh take, visit us here: https://www.shiftleft.io/.

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.

XML Vulnerability entity Parser (programming language) Document application

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

Opinions expressed by DZone contributors are their own.

Related

  • Go Application Vulnerability Cheatsheet
  • How To Create a Homescreen Widget in Android
  • Application Security in Technical Product Management
  • Combatting the OpenSSH Vulnerability

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!