Handcrafted Gadgets
The author found a Java deserialization vulnerability in a B2B portal and developed a handcrafted gadget for Groovy 2.4.5 that bypasses a patch by suppressing exceptions during deserialization, allowing remote code execution.
In Q4 2017 I was pentesting a customer. Shortly before, I had studied json attacks when I stumbled over an internet-facing B2B-portal-type-of-product written in Java they were using (I cannot disclose more details due to responsible disclosure).
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the application deserialized untrusted input without proper validation, and the patch for the known gadget was incomplete because it only threw an exception in readObject without preventing the object from being used later via references.
Impact
An attacker could achieve remote code execution on the server by sending a malicious serialized object, provided the application deserializes untrusted input and the vulnerable Groovy version is present.
What the attacker needed
Application deserializes untrusted input; vulnerable Groovy 2.4.5 library present; attacker can send serialized data
How it was found
- Identify that the server sends a serialized Java object and find a way to make it deserialize untrusted input.
- Study the existing Jdk7u21 gadget and its patch in AnnotationInvocationHandler.
- Learn the bypass technique using BeanContextSupport to suppress exceptions and references to access the cached object.
- Adapt the technique to Groovy 2.4.5 by using KRBError to convert the UnsupportedOperationException from MethodClosure.readResolve into an IOException caught by BeanContextSupport.
- Construct the final serialized stream with a HashMap containing BeanContextSupport as key and the Groovy gadget as value.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Java can turn objects into a stream of bytes and back again, a process called serialization and deserialization. If an application deserializes data from an untrusted source, an attacker can craft a malicious byte stream that, when deserialized, causes the application to run arbitrary code. This report describes how to build such a malicious stream by hand to bypass a security fix.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.