About the Unsuccessful Quest for a Deserialization Gadget (or: How I found CVE-2021-21481)
2021-06-11 19:05:00 Author: www.blogger.com(查看原文) 阅读量:161 收藏

tag:blogger.com,1999:blog-718041348982184937.post-25959845013811064732021-06-11T12:05:00.000+02:002021-06-11T12:05:33.768+02:00About the Unsuccessful Quest for a Deserialization Gadget (or: How I found CVE-2021-21481)This blog post describes the research on SAP J2EE Engine 7.50 I did between October 2020 and January 2021. The first part describes how I set off to find a pure SAP deserialization gadget, which would allow to leverage SAP's P4 protocol for exploitation, and how that led me, by sheer coincidence, to an entirely unrelated, yet critical vulnerability, which is outlined in part two. <p>The reader is assumed to be familiar with Java Deserialization and should have a basic understanding of Remote Method Invocation (RMI) in Java. <a name='more'></a></p><h2>Prologue</h2>It was in 2016 when I first started to look into the topic of Java Exploitation, or, more precisely: into exploitation of unsafe deserialization of Java objects. Because of my professional history, it made sense to have a look at an SAP product that was written in Java. Naturally, the P4 protocol of SAP NetWeaver Java caught my attention since it is an RMI-like protocol for remote administration, similar to Oracle WebLogic's T3. In May 2017, <a href="/2017/05/sap-customers-make-sure-your-sapjvm-is.html">I published a blog post</a> about an exploit that was getting RCE by using the Jdk7u21 gadget. At that point, SAP had already provided a fix long ago. Since then, the subject has not left me alone. While there were new deserialization gadgets for Oracle's Java server product almost every month, it surprised me no one ever heard of an SAP deserialization gadget with comparable impact. Even more so, since everybody who knows SAP software knows the vast amount of code they ship with each of their products. It seemed very improbable to me that they would be absolutely immune against the most prominent bug class in the Java world of the past six years. In October 2020 I finally found the time and energy to set off for a new hunt. To my great disappointment, the search was in the end not successful. A gadget that yields RCE similar to the ones from the famous ysoserial project is still not in sight. However in January, I found a completely unprotected RMI call that in the end yielded administrative access to the J2EE Engine. Besides the fact that it can be invoked through P4 it has nothing in common with the deserialization topic. Even though a mere chance find, it is still highly critical and allows to compromise the security of the underlying J2EE server. <p>The bug was filed as CVE-2021-21481. On march 9th 2021, SAP provided a fix. SAP note 3224022 describes the details. </p><h2>P4 and JNDI</h2> Listing 1 shows a small program that connects to a SAP J2EE server using P4: <script src="https://gist.github.com/supersache/774068d31be4be1bf62dd33659deb5a5.js"></script> The only hint that this code has something to do with a proprietary protocol called <i>P4</i> is the URL that starts with <code>P4://</code>. Other than that, everything is encapsulated by P4 RMI calls (<a href="https://docs.oracle.com/javase/tutorial/jndi/overview/index.html">for those who want to refresh their memory about JNDI</a>). Furthermore, it is not obvious that what is going on behind the scenes has something to do with RMI. However, if you inspect more closely the types of the involved Java objects, you'll find that <code>keysMngr</code> is of type <code>com.sun.proxy.$Proxy</code> (implementing interface <code>KeystoreManagerWrapper</code>) and <code>keysMngr.getKeystore()</code> is a plain vanilla RMI-call. The argument (the name of the keystore to be instantiated) will be serialized and sent to the server which will return a serialized keystore object (in this case it won't because there is no keystore "whatever"). Also not obvious is that the instantiation of the <code>InitialContext</code> requires various RMI calls in the background, for example the instantiation of a <code>RemoteLoginContext</code> object that will allow to process the login with the provided credentials. <p>Each of these RMI calls would in theory be a sink to send a deserialization gadget to. In the exploit I mentioned above, one of the first calls inside <code>new InitialContext()</code> was used to send the Jdk7u21 gadget (instead of a <code>java.lang.String</code> object, by the way). </p><p>Now, since the Jdk7u21 gadget is not available anymore and I was looking for a gadget consisting merely of SAP classes, I had to struggle with a very annoying limitation: The classloader segmentation. SAP J2EE knows various types of software components: interfaces, services, libraries and applications (which can consist of web applications and EJBs). When you deploy a component, you have to declare the dependencies to other components your component relies upon. Usually, web applications depend on 2-3 services and libraries which will have a couple of dependencies to other services and libraries, as well. At the bottom of this dependency chain are the core components. </p><p>Now, the limitation I was talking about is the fact that the dependency management greatly affects which classes a component can see: It can precisely see all classes of all components it relies upon (plus of course JDK classes) but not more. If your class ships as part of the keystore service above, it will only be able to resolve classes from components the keystore service declares as dependencies. </p><div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-i5HbsVcMzQs/YMIAkz2jtEI/AAAAAAAACvw/GnDwRKpF-q8XVxRlgtZz0QkoP_ntHQk5wCLcBGAsYHQ/s920/SCREENSHOT%2B1.png" style="display: block; padding: 1em 0px; text-align: center;"><img alt="" border="0" data-original-height="535" data-original-width="920" src="https://1.bp.blogspot.com/-i5HbsVcMzQs/YMIAkz2jtEI/AAAAAAAACvw/GnDwRKpF-q8XVxRlgtZz0QkoP_ntHQk5wCLcBGAsYHQ/s16000/SCREENSHOT%2B1.png" /></a></div><p></p><p></p><p></p><p></p><p></p>Figure 1: dependencies of the keystore service with all child and parent classloaders<p> This has dramatic consequences for gadget development. Suppose you found a gadget whose classes come from components X, Y and Z but there are no dependencies between these components and in addition, there is no component which depends on all of them. Then, no matter in which classloader context your gadget will be deserialized, at least one of X, Y or Z will be missing in the classpath and the deserialization will end up in a <code>ClassNotFoundException</code>. By using a similar approach to the one described in the <a href="https://github.com/BishopFox/GadgetProbe">GadgetProbe project</a> I found out that at the point the Jdk7u21 gadget was deserialized in the above mentioned exploit, there were only about 160 non-JDK classes visible that implement <code>java.io.Serializable</code>. Not ideal for building an exploit. Going back to listing 1, in case we send a gadget instead of the string "whatever", we can tell from figure 1 that classes from ten components (the ones listed beneath "Direct parent loaders") will be in the class path. Code that sends an arbitrary serializable object instead of the string "whatever" could e.g. look like this (instead of <code>keysMgr.getKeystore()</code>): <script src="https://gist.github.com/supersache/6d8f592cf719bb3f9d0bb104246dc0ab.js"></script> If there was a gadget, one could send it with <code>out.writeObject()</code>. <p>With this approach, the critical mass of accessible serializable classes can be significantly increased. The telnet interface of SAP J2EE provides useful information about the services and their dependencies. <p>Regardless of the classloader challenge, I was eager to get an overview of how many serializable classes existed in the server. The number of classes in the core layer, services and libraries amounts to roughly 100,000, and this does not even count application code. I quickly realized that I needed something smarter than the analysis features of Eclipse to handle such volumes. So I developed my own tool which analyses Java bytecode using the <a href="https://asm.ow2.io/">OW2 ASM Framwork</a>. It writes object and interface inheritance dependencies, methods, method calls and attributes to a SQLite DB. It turned out that out of the 100,000 classes, about 16,000 implemented <code>java.io.Serializable</code>. The RDBMS approach was pretty handy since it allowed build complex queries like <p> <i>Give classes which are Serializable and Cloneable which implement <code>private void readObject(java.io.ObjectInputStream)</code> and whose <code>toString()</code> method exists and has more than five calls to distinct other methods</i> <p> This question translates to <script src="https://gist.github.com/supersache/b83fa4c8c14710a4b59af6040a30df49.js"></script> <div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-E9ZtfLy_Jm4/YMIDbUBOzEI/AAAAAAAACv4/ixo8O3RTQ54DhkP_JG9DdKjCG0upgR-mQCLcBGAsYHQ/s0/SCREENSHOT%2B3.png" style="display: block; padding: 1em 0; text-align: center; "><img alt="" border="0" data-original-height="558" data-original-width="1015" src="https://1.bp.blogspot.com/-E9ZtfLy_Jm4/YMIDbUBOzEI/AAAAAAAACv4/ixo8O3RTQ54DhkP_JG9DdKjCG0upgR-mQCLcBGAsYHQ/s0/SCREENSHOT%2B3.png"/></a></div><p> The work on this tool and also the process of constantly inventing new and original queries to find potentially interesting classes was great fun. Unfortunately, it was also in vain. There is a library, which <i>almost</i> allowed to build a wonderful chain from a <code>toString()</code> call to the ubiquitous <code>TemplatesImpl.getOutputProperties()</code>, but the API provided by the library is so very complex and undocumented that, after two months, I gave up in total frustration. There were some more small findings which don't really deserve to be mentioned. However, I'd like to elaborate on one more thing before I'll start part two of the blog post, that covers the real vulnerability. <p>One of the first interesting classes I discovered performs a JNDI lookup with an attacker controlled URL in <code>private void readObject(java.io.ObjectInputStream)</code>. What would have been a direct hit four years ago could at least have been a respectable success in 2020. Remember: Oracle JRE finally switched off remote classloading when resolving LDAP references in 2019 in version JRE 1.8.0_191. Had this been exploitable, it would have opened up an attack avenue at least for systems with outdated JRE. My SAP J2EE was running on top of a JRE version 1.8.0_51 from 2015, so the JNDI injection should have worked, but, to my great surprise, it didn't. The reason can be found in the method <code>getObjectInstance</code> of <code>javax.naming.spi.DirectoryManager</code>: <script src="https://gist.github.com/supersache/96de0b10c5812d655a57d9f764ca2f50.js"></script> The hightlighted call to <code>getObjectFactoryFromReference</code> is where an attacker needs to get to. The method resolves the JNDI reference using an <code>URLClassLoader</code> and an attacker-supplied codebase. However, as one can easily see, if <code>getObjectFactoryBuilder()</code> returns a non-null object the code returns in either of the two branches of the following if-clause and the call to <code>getObjectFactoryFromReference</code> below is never reached. And that is exactly what happens. SAP J2EE registers an <code>ObjectFactoryBuilder</code> of type <code>com.sap.engine.system.naming.provider.ObjectFactoryBuilderImpl</code>. This class will try to find a factory class based on the <code>factoryName</code>-attribute and completely ignore the <code>codebase</code>-attribute of the JNDI reference. Bottom line is that JNDI injection might never have worked in SAP J2EE, which would eliminate one of the most important attack primitives in the context of Java Deserialization attacks. <h2>CVE-2021-21481</h2> After digressing about how I searched for deserialization gadgets, I'd like to cover the real vulnerability now, which has absolutely nothing to do with Java Deserialization. It is a plain vanilla instance of CWE-749: Exposed Dangerous Method or Function. Let's go back to Listing 1. We can see that the JNDI context allows to query interfaces by name, in our example we were querying the <code>KeyStoreManager</code> interface by the name "keystore". On several occasions, I had already tried to find an available rich client for SAP J2EE Engine administration that uses P4. Every time I was unsuccessful, I believed such a client did not officially exist, or at least was not at everyone's disposal. <p>However, whenever you install a SAP J2EE Engine, the P4 port is enabled by default and listening on the same network interface as the HTTP(s) services. Because I was totally focussing on Deserialization, for a long time I was oblivious how much information one can glean through the JNDI context. E.g. it is trivial to get all bindings: <script src="https://gist.github.com/supersache/a5ec450ed91a182067302c76a219cbab.js"></script> The <code>list()</code> call allows to simply iterate through all bindings: <div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-PomEEctPRL8/YMIOKRvbwKI/AAAAAAAACwA/qjBQvR6nwVw1BYtaFMyoBMVrkON8dS1vwCLcBGAsYHQ/s0/SCREENSHOT%2B2.png" style="display: block; padding: 1em 0; text-align: center; "><img alt="" border="0" data-original-height="148" data-original-width="1003" src="https://1.bp.blogspot.com/-PomEEctPRL8/YMIOKRvbwKI/AAAAAAAACwA/qjBQvR6nwVw1BYtaFMyoBMVrkON8dS1vwCLcBGAsYHQ/s0/SCREENSHOT%2B2.png"/></a></div> Interesting items are proxy objects and the <code>_Stub</code> objects. E.g. the proxy for <code>messaging.system.MonitorBean</code> can be cast to <code>com.sap.engine.messaging.app.MonitorHI</code>. <p>During debugging of the server, I had already encountered the class <code>JUpgradeIF_Stub</code>, long before I executed the call from Listing 5. The class has a method <code>openCfg(String path)</code> and it was not difficult to establish that the server version of the call didn't perform any authorization check. This one definitively looked fishy to me, but since I wasn't looking for unprotected RMI calls I put the finding into the box with the label "check on a rainy sunday afternoon when the kids are busy with someone else". But then, eventually, I <i>did</i> check it. It didn't take long to realize that I had found a huge problem. Compare Listing 6. <script src="https://gist.github.com/supersache/209a4c8516900e166e71516b9cd4b959.js"></script> The configuration settings of SAP J2EE Engine are organized in a hierarchical structure. The location of an object can be specified by a path, pretty much like a path of a file in the file system. The above code gets a reference to the <code>JUpgradeIF_Stub</code> by querying the JNDI context with name "MigrationService", gets an instance of a <code>Configuration</code> object by a call to <code>openCfg()</code> and then walks down the path to the leaf node. The element found there can be exported to an archive that is stored in the file system of the server (call to <code>export(String path)</code>). If carefully chosen, the local path on the server will point to a root folder of a web application. There, <code>download.zip</code>can simply be downloaded through HTTP. If you want to check for yourself, the UME configuration is stored at <code>cluster_config/system/custom_global/cfg/services/com.sap.security.core.ume.service/properties</code>. <p>You'd probably say "hey! I need to be Administrator to do that! Where's the harm?". Right, I thought so, too. But neither do you need to be Administrator, nor do you even have to be authenticated. The following code works perfectly fine: <script src="https://gist.github.com/supersache/efce1f9a3de80f0680319d7215034878.js"></script> So does the enumeration using <code>ctxt.list()</code> from Listing 5. The fact that authentication is not needed at this point is not new at all by the way, compare <a href="https://erpscan.io/advisories/erpscan-16-037-sap-java-p4-mspruntimeinterface-information-disclosure/">CVE-2017-5372</a>. <p> However, you <i>will</i> get a permission exception when calling <code>keysMngr.getKeystore()</code> (because <code>getKeystore()</code> does have a permission check). But <code>JUpgradeIF.openCfg()</code> was missing the check until SAP fixed it. <p> At this point, even without SAP specific knowledge an attacker can cause significant harm. E.g. flood the server's file system with archives causing a resource exhaustion DoS condition. <p>With a little insider knowledge one can get admin access. In the configuration tree, there is a keystore called <i>TicketKeystore</i>. Its cryptographic key pair is used to sign SAP Logon Tickets. If you steal the keystore, you can issue a ticket for the <i>Administrator</i> user and log on with full admin rights. There are also various other keystores, e.g. for XML signatures and the like (let alone the fact that there is tons of stuff in this store. No one probably knows all the security sensitive things you can get access to ...) <p>This information should be sufficient to the understanding of CVE-2021-21481. The exact location of the keystores in the configuration and the relative local path in order to download the archive by HTTP are left as an exercise to the reader.Kai Ullrichhttps://www.blogger.com/profile/12998022707605713014[email protected]

文章来源: https://www.blogger.com/feeds/718041348982184937/posts/default/2595984501381106473
如有侵权请联系:admin#unsafe.sh