Why nested deserialization is harmful: Magento XXE (CVE-2024-34102)
Magento is one of the most popular e-commerce solutions in use on the internet. It's estimated that there are over 140,000 instances of Magento running as of late 2023. Adobe's most recent advisory for Adobe Commerce / Magento, published on June 11th, 2024 highlighted a critical, pre-authentication XML entity injection issue (CVE-2024-34102) which Adobe rated as CVSS 9.8.
It was quite surprising to us that no public proof-of-concept existed at the time of us reading the advisory. Given the criticality of this issue and in order to provide customers of our Attack Surface Management Platform certainty around the exploitability of this issue, our security research team developed a proof-of-concept, well before our customers could be exploited by malicious actors.
We believe that the vulnerability is severe is due to the following reasons:
- It is possible to exfiltrate the <span class="code_single-line">app/etc/env.php</span> file from Magento, which contains a cryptographic key used to sign JWTs used for authentication. An attacker can craft an administrator JWT and abuse Magento's APIs as an admin user on affected installations.
- The vulnerability can be chained with recent research in PHP filter chains leading to RCE through the CVE-2024-2961 exploit, credit to Charles Fol.
- The broader impacts of XXE (any local file or remote URL's contents can be exfiltrated).
We want to acknowledge the original author for his excellent work on discovering this vulnerability, Sergey Temnikov. Shortly after this vulnerability was dubbed "CosmicString" by SanSec, he released a limited write-up of the issue, which discusses his methodology in discovering this issue but does not reveal the proof of concept. We highly recommend reading this write-up as he explains Magento's internal deserialization process and its inherent dangers.
As we tracked the public knowledge of this vulnerability, we found that SanSec's original emergency mitigation could be bypassed, and Sergey's first iteration of the "fixed" mitigation could also be bypassed. This led to both SanSec and Sergey updating their emergency hotfix mitigations over time.
This was interesting to observe as it highlighted the importance and effectiveness of peer review when it comes to emergency hot fixes and an argument for why disclosing the technical details of a vulnerability is important for the broader security industry.
To understand the key differences between an unpatched version of Magento and a patched one, we downloaded the packages <span class="code_single-line">magento2-2.4.7.zip</span> (unpatched) and <span class="code_single-line">magento2-2.4.7-p1.zip</span> (patched) from the Magneto GitHub repository. Extracting these and then running DiffMerge on these two directories revealed a very important clue to discovering this vulnerability:
With the information that was publicly available, i.e., SanSec's first patch (blocking <span class="code_single-line">dataIsURL</span> inside the POST body) as well as the diff we can see in the image above, it was clear to us that this vulnerability was to do with instantiating a <span class="code_single-line">SimpleXMLElement</span>. PHP's documentation for this class revealed that <span class="code_single-line">dataIsURL</span> is an argument that can be passed to the <span class="code_single-line">SimpleXMLElement</span> constructor, which allows for loading XML from external sources.
The additional updates to the hotfix from Sergey revealed that you could not rely on blocking <span class="code_single-line">dataIsURL</span> as the vulnerability was exploitable without this, and his mitigation focused on blocking the keyword <span class="code_single-line">sourceData</span>.
With all of this information, we spent most of our time setting up a development environment for Magento and then searching for a deserialization gadget that would lead us to the instantiation of a <span class="code_single-line">SimpleXMLElement</span> with controllable arguments.
When it comes to complex deserialization issues, we highly suggest setting up a development environment with the ability to debug the code by setting breakpoints. For Magento 2, we utilized the following repo to bootstrap our development efforts. This docker image includes XDebug and is already configured for PhpStorm. After spinning up this docker image, we were able to install & seed Magento with sample data using the following commands:
When searching through the Magento 2 code base for <span class="code_single-line">Simplexml\\Element.*sourceData</span>, we identified the following locations that could be viable targets:
From this list, we believed the most likely candidate that could be reached without authentication would be <span class="code_single-line">Magento/Quote/Model/Quote/Address/Total/Collector.php</span>. We found that reading through the code itself for how the nesting worked and allowed for the instantiation of <span class="code_single-line">sourceData</span> was not obvious.
To make further headway, it was necessary for us to understand at a high level how the input deserialization works. For that, we looked at <span class="code_single-line">magento2-2.4.7/lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php</span> and its <span class="code_single-line">_createFromArray</span> method:
At a high level, if Magento is parsing some input data and expects a field <span class="code_single-line">address</span> that contains an <span class="code_single-line">\Magento\Quote\Api\Data\Address</span>, what it will do is the following:
- First, if the fields of the JSON match any of the names of the variables in the constructor of the class, pass that field as an argument;
- Second, if the name doesn't match, instead look for a method on the class named <span class="code_single-line">set</span> plus the field.
For example, if you passed the following JSON to the <span class="code_single-line">/rest/all/V1/guest-carts/test/estimate-shipping-methods</span> endpoint:
- The field <span class="code_single-line">data</span> is in the constructor of the <span class="code_single-line">Address</span> class as <span class="code_single-line">array $data = []</span>, so it will be passed there.
- The <span class="code_single-line">Address</span> class has a method <span class="code_single-line">setBaseShippingAmount</span>, so after the class is instantiated it will call <span class="code_single-line">->setBaseShippingAmount(123)</span>.
The danger comes from the fact that this is done recursively: if either the constructor or the setter takes a non-primitive type, such as another class, then the deserialization process is done recursively on that field. Looking at the constructor for the <span class="code_single-line">Address</span> class, is has 37 parameters, and it's clear the Magento developers did not intend for you to be able to instantiate all of these:
This provides a huge surface for bugs. By traversing chains of constructors and setters, it is possible to instantiate a wide variety of internal classes that were never meant to be user-facing. And if any of those constructors or setters do dangerous things, such as in the case of <span class="code_single-line">SimpleXMLElement</span>, this could lead to a security vulnerability. Further details on how to map out the pre-authentication endpoints and corresponding models can be found in Sergey's write up.
The goal is now to find a chain of types in constructors that allow us to reach one of the <span class="code_single-line">Simplexml</span> sinks identified earlier. Rather than trace the constructor manually for each class, we added the following line to <span class="code_single-line">magento2-2.4.7/lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php</span>:
This simple <span class="code_single-line">var_dump</span> helped us to quickly understand all of the different parameters we could provide when calling the unauthenticated REST APIs based on the magic deserialisation logic that Magento had built.
We found that the pre-authentication endpoint <span class="code_single-line">/rest/all/V1/guest-carts/test/estimate-shipping-methods</span> mentioned earlier was likely the best candidate to reach <span class="code_single-line">sourceData</span> through reading the names of the constructor elements.
Debugging the available parameters was made easier with our <span class="code_single-line">var_dump</span> call, allowing us to quickly iterate on our payload with output as seen below:
With further experimentation, we were able to develop the following payload, which instantiated a <span class="code_single-line">SimpleXMLElement</span> with controllable arguments via the <span class="code_single-line">sourceData</span> parameter:
With our DTD containing:
This resulted in the following:
More Like This
Ready to get started?
Get on a call with our team and learn how Assetnote can change the way you secure your attack surface. We'll set you up with a trial instance so you can see the impact for yourself.