2. About me
ī§ Head of Vulnerability Research at Code White in Ulm, Germany
ī§ Dev for defense company in the past
ī§ Spent a lot of time on (server-side) Java Security
ī§ Found bugs in products of Oracle, VMware, IBM, SAP, Symantec, Apache, Adobe, HP, etc.
ī§ Recently looking more into the Windows world and client-side stuff
@matthias_kaiser
11.11.2016 2
3. Agenda
ī§ Introduction
ī§ Javaâs Object Serialization
ī§ Whatâs the problem with it
ī§ A history of bugs
ī§ Finding and exploiting
ī§ Code Whiteâs bug parade
ī§ A hands-on example
ī§ More to come?
11.11.2016 3
4. Should you care?
ī§ If your client is running server products of
you SHOULD!
11.11.2016 4
5. Some facts
ī§ The bug class exists for more than 10 years
ī§ Most ignored bug class in the server-side Java world until 2015
ī§ A easy way to get reliable RCE on a server
ī§ Architecture independent exploitation
ī§ With Java deserialization vulnerabilities you can pwn a corp easily!
11.11.2016 5
6. Where is it used
ī§ Several J2EE/JEE core technologies rely on serialization
ī§ Remote Method Invocation (RMI)
ī§ Java Management Extension (JMX)
ī§ Java Message Service (JMS)
ī§ Java Server Faces implementations (ViewState)
ī§ Communication between JVMs in general (because devs are lazy :-)
ī§ Custom application protocols running on top of http, etc.
11.11.2016 6
8. Overview of Javaâs Object Serialization Protocol
Magic
class name
field type
class field
Class description info
TC_OBJECT
TC_CLASSDESC
classdata[]
11.11.2016 8
9. There is protocol spec and a grammar
https://docs.oracle.com/javase/8/docs/platform/serialization/spec/protocol.html
11.11.2016 9
11. Whatâs the problem
ī§ ObjectInputStream doesnât include validation features in its API
ī§ All serializable classes that the current classloader can locate and load can get deserialized
ī§ Although a class cast exception might occur in the end, the object will be created!
11.11.2016 11
12. Whatâs the problem #2
ī§ A developer can customize the (de)-serialization of a serializable class
ī§ Implement methods writeObject(), writeReplace(), readObject() and readResolve()
ī§ ObjectInputStream invokes readObject() and readResolve()
Under our control!
11.11.2016 12
13. Whatâs the problem #3
ī§ Further methods can be triggered by using certain classes as a "trampoline"
ī§ Object.toString() using e.g. javax.management.BadAttributeValueExpException
ī§ Object.hashCode() using e.g. java.util.HashMap
ī§ Comparator.compare() using e.g. java.util.PriorityQueue
ī§ etc.
Trampoline
class
Target
class
11.11.2016 13
14. Whatâs the problem #3
javax.management.BadAttributeValueExpException
1. Reading the field "val"
2. Calling "toString()" on "val"
11.11.2016 14
15. History of Java deserialization vulnerabilities
JRE vulnerabilities
(DoS)
Mark SchÃļnefeld
2006
JSF Viewstate
XSS/DoS
Sun Java Web Console
Luca Carretoni
2008
CVE-2011-2894
Spring Framework RCE
Wouter Coekaerts
CVE-2012-4858
IBM Cognos Business
Intelligence RCE
Pierre Ernst
2011 2012
11.11.2016 15
16. History of Java deserialization vulnerabilities
CVE-2013-1768 Apache OpenJPA RCE
CVE-2013-1777 Apache Geronimo 3 RCE
CVE-2013-2186 Apache commons-fileupload RCE
Pierre Ernst
CVE-2015-3253 Groovy RCE
CVE-2015-7501 Commons-Collection RCE
Gabriel Lawrence and Chris Frohoff
CVE-2013-2165 JBoss RichFaces RCE
Takeshi Terada
2013 2015
11.11.2016 16
19. Finding is trivial
ī§ Use an IDE like Intellij or Eclipse and trace the call paths to ObjectInputStream.readObject()
11.11.2016 19
20. Exploitation
ī§ Exploitation requires a chain of serialized objects triggering interesting functionality e.g.
ī§ writing files
ī§ dynamic method calls using Javaâs Reflection API
ī§ etc.
ī§ For such a chain the term "gadget" got established
ī§ Chris Frohoff and others found several gadgets in standard libs
11.11.2016 20
21. Javassist/Weld Gadget
ī§ Gadget utilizes JBossâ Javassist and Weld framework
ī§ Reported to Oracle with the Weblogic T3 vulnerability
ī§ Works in Oracle Weblogic and JBoss EAP
ī§ Allows us to call a method on a deserialized object
11.11.2016 21
22. "Return of the Rhino"-Gadget
ī§ Gadget utilizes Rhino Script Engine of Mozilla
ī§ Works with latest Rhino in the classpath
ī§ Oracle applied some hardening to its Rhino version
ī§ So only works Oracle JRE <= jre7u13 ī
ī§ Works with latest openjdk7-JRE (e.g. on Debian, Ubuntu) ī
ī§ Allows us to call a method on a deserialized object
ī§ JRE Gadget ī
11.11.2016 22
23. What to look for?
ī§ Look for methods in serializable classes
ī§ working on files
ī§ triggering reflection (invoking methods, getting/setting properties on beans)
ī§ doing native calls
ī§ etc.
AND being called from
ī§ readObject()
ī§ readResolve()
ī§ toString()
ī§ hashCode()
ī§ finalize()
ī§ any other method being called from a "Trampoline" class
11.11.2016 23
24. What to look for?
ī§ Look at serializable classes used in Java reflection proxies
ī§ java.lang.reflect.InvocationHandler implementations
ī§ javassist.util.proxy.MethodHandler implementations
InvocationHandlerInterface
Proxy
toString() invoke (âĻ) // do smth
invoke (target, toString, args)
11.11.2016 24
25. What to look for?
Prints out method being called
11.11.2016 25
26. What to look for?
What if InvocationHandler.invoke()
does "insecure stuff" using values from
the serialized object input stream?
Proxy
11.11.2016 26
27. Making gadget search easier
ī§ Chris Frohoff released a tool for finding gadgets using a graph database
ī§ Using object graph queries for gadget search
11.11.2016 27
28. Exploitation tricks
ī§ Adam Gowdiakâs TemplatesImpl
ī§ com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl is serializable
ī§ Allows to define new classes from your byte[ ][ ]
ī§ Calling TemplatesImpl.newTransformer() on deserialized object ī Code Execution
11.11.2016 28
29. Exploitation tricks
ī§ InitialContext.lookup()
ī§ @benmmurphy used it for a sandbox escape (CVE-2013-5830)
ī§ @zerothoughts published a gadget in Springâs JtaTransactionManager recently
ī§ Triggers InitialContext.lookup(jndiName)
ī§ Uses "rmi://yourFakeRmiServer/Object" as jndiName
ī§ Loads classes from your fake RMI server
ī§ Calling JdbcRowSetImpl.execute() on a deserialized object will do the same ī
11.11.2016 29
30. Payload generation
ī§ Chris Frohoff released the great tool "ysoserial"
ī§ Makes creation of payloads easy
ī§ Includes gadgets for
ī§ Commons Collection 3 & 4
ī§ Spring
ī§ Groovy
ī§ JRE7 (<= jre7u21)
ī§ Commons BeanUtils
ī§ and even more!
11.11.2016 30
31. Custom payloads
ī§ I wouldnât go for Runtime.getRuntime().exec(cmd) for several reasons
ī§ Most of the gadgets donât touch the disk ī
ī§ With scripting languages your life gets even easier
ī§ Use whatâs in the classpath
ī§ Javascript (Rhino, Nashorn)
ī§ Groovy
ī§ Beanshell
ī§ etc.
11.11.2016 31
37. Jenkins
ī§ Open Source Automation Server / Continous Integration Server / "Build"-Server
ī§ Created by Kohsuke Kawaguchi (Ex-Oracle, now CTO of CloudBees)
ī§ Fork of Oracleâs Hudson CI server
ī§ Supports Subversion, Git, Mecurial, etc.
ī§ Runs Maven, Ant, etc.
ī§ More than 1200 plugins! (see https://updates.jenkins-ci.org/download/plugins/)
11.11.2016 37
38. Jenkins
ī§ Nice target because Jenkins
ī§ has access to Source Code repositories
ī§ creates deployment artefacts (Jar, War, Ear, etc.)
ī§ can deploy artefacts on target servers
ī§ stores credentials (user/password, SSH keys)
11.11.2016 38
39. Jenkins Internals
ī§ Jenkins uses an extra port for the Command Line Interface (CLI)
ī§ Can be configured to a fixed or random port
11.11.2016 39
40. Jenkins Internals
ī§ Jenkins uses an own RMI protocol for itâs Command Line Interface (CLI)
ī§ Base64-encoded serialized objects (rO0 ī 0xac,0xed)
11.11.2016 40
41. Jenkins under Attack
ī§ Jenkins CLI endpoint suffered from several vulnerabilities
ī§ CVE-2015-8103 of Steven Breen using Commons Collections gadget
ī§ Jenkins introduced a blacklist to filter gadget classes
ī§ CVE-2016-0788 of Moritz Bechler bypassing the blacklist (see ERNW blog post
https://insinuator.net/2016/07/jenkins-remoting-rce-ii-the-return-of-the-ysoserial/)
ī§ As we all know blacklisting is hard because you never know âĻ
11.11.2016 41
43. Finding a blacklist filter bypass
ī§ How to bypass a gadget blacklist filter?
a) Find a new gadget
b) Find a bypass gadget (see Alvaroâs and Christianâs Research)
c) Look for partially fixed gadget
ī§ After looking at all gadgets of ysoserial and matching them with Jenkinâs third-party libs and
the blacklist I found one interesting gadget discovered by Moritz Bechler:
11.11.2016 43
44. The JSON1 gadget
ī§ "Code execution step"
ī§ filtered by blacklist ī
ī§ "Trigger step"
ī§ invokes all "getter" methods on
a serialized object
ī§ Not filtered by blacklist ī
ī§ "Init step"
11.11.2016 44
45. Finding a blacklist filter bypass #1
ī§ Initial idea was to use the JDBCRowSetImpl trick as code execution step
ī§ "Getter"-methods trigger JNDI call:
ī§ But net.sf.json.JSONObject.containsValue(JDBCRowSetImpl-instance) fails because several
"Getter"-methods trigger Exceptions ī
11.11.2016 45
46. Finding a blacklist filter bypass #2
ī§ Next idea was to look for other serializable classes with "Getter"-Methods leading to code
execution
ī§ Recent research FTW:
11.11.2016 46
47. Finding a blacklist filter bypass #2
ī§ JNDI lookups can lead to RCE (see JDBCRowSetImpl)
ī§ Exploitation using RMI, LDAP and CORBA
ī§ LDAP queries can lead to RCE
ī§ LDAP server needs to be under your control
ī§ data from LDAPresponse is deserialized using ObjectInputStream
ī§ data (URLs) from LDAP response is used to load classes using URLClassLoader -> RCE
ī§ I found some nice classes in package âcom.sun.jndi.ldap"
ī§ One of it is the serializable class "com.sun.jndi.ldap.LdapAttribute"
11.11.2016 47
49. Putting all together for the new JSON2 gadget
11.11.2016 49
ī§ With LdapAttribute.getAttributeDefinition() we get Code Execution using a custom LDAP server
ī§ The "Init-Step" shown before doesnât work, so we need something else ī
ī§ By using Eclipse an alternative code path can be easily found ī
50. Some "updates" with regards to exploitation âĻ
ī§ Previous research only mentioned the CLI port!
ī§ If you have Jenkins running on the internet with firewall / reverse proxy, you canât connect
ī§ But the Jenkins Wiki has some hidden gems for us:
Connection mechanism
1. Jenkins CLI clients and Jenkins server establishes the communication in the following fashion.
Jenkins listens on a TCP/IP port configured under "TCP port for JNLP agents" in the system
configuration page. This single port is used for both agents and CLI.
âĻ
5. If that fails (for example, if there's a reverse proxy and Jenkins runs on a different host, or if a
firewall blocks access to this TCP/IP port), or if the header is not found, it will fall back to the
communication mechanism that uses two simultaenous HTTP connections.
11.11.2016 50
51. Jenkins CLI HTTP "fallback"
11.11.2016 51
ī§ HTTP Connection #1
ī§ Server ī Client channel
ī§ Client reads from InputStream
ī§ UUID as identifier
ī§ Blocks until #2 connects
ī§ HTTP Connection #2
ī§ Client ī Server channel
ī§ Client writes to OutputStream
ī§ UUID as identifier
55. Conclusion
ī§ Java Deserialization is no rocket science
ī§ Finding bugs is trivial, exploitation takes more
ī§ So many products affected by it
ī§ Research has started, again âĻ
ī§ This will never end!
11.11.2016 55