|
| 1 | +package com.reajason.javaweb.probe.payload.filter; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.PrintStream; |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.*; |
| 8 | + |
| 9 | +/** |
| 10 | + * @author ReaJason |
| 11 | + */ |
| 12 | +public class ApusicFilterProbe { |
| 13 | + |
| 14 | + @Override |
| 15 | + public String toString() { |
| 16 | + String msg = ""; |
| 17 | + Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>(); |
| 18 | + Set<Object> contexts = null; |
| 19 | + try { |
| 20 | + contexts = getContext(); |
| 21 | + } catch (Throwable throwable) { |
| 22 | + msg += "context error: " + getErrorMessage(throwable); |
| 23 | + } |
| 24 | + if (contexts == null || contexts.isEmpty()) { |
| 25 | + msg += "context not found\n"; |
| 26 | + } else { |
| 27 | + for (Object context : contexts) { |
| 28 | + String contextRoot = getContextRoot(context); |
| 29 | + List<Map<String, String>> filters = collectFiltersData(context); |
| 30 | + allFiltersData.put(contextRoot, filters); |
| 31 | + } |
| 32 | + msg += formatFiltersData(allFiltersData); |
| 33 | + } |
| 34 | + return msg; |
| 35 | + } |
| 36 | + |
| 37 | + @SuppressWarnings("all") |
| 38 | + private List<Map<String, String>> collectFiltersData(Object context) { |
| 39 | + Map<String, Map<String, Object>> aggregatedData = new LinkedHashMap<>(); |
| 40 | + |
| 41 | + try { |
| 42 | + Object webModule = getFieldValue(context, "webapp"); |
| 43 | + if (webModule == null) { |
| 44 | + return Collections.emptyList(); |
| 45 | + } |
| 46 | + |
| 47 | + Object[] filterMappings = (Object[]) invokeMethod(webModule, "getAllFilterMappings"); |
| 48 | + if (filterMappings == null || filterMappings.length == 0) { |
| 49 | + return Collections.emptyList(); |
| 50 | + } |
| 51 | + |
| 52 | + Object[] filters = (Object[]) invokeMethod(webModule, "getFilterList"); |
| 53 | + Map<String, String> filterClassMap = new HashMap<>(); |
| 54 | + if (filters != null) { |
| 55 | + for (Object filter : filters) { |
| 56 | + String name = (String) invokeMethod(filter, "getName"); |
| 57 | + String className = (String) invokeMethod(filter, "getFilterClass"); |
| 58 | + if (name != null && className != null) { |
| 59 | + filterClassMap.put(name, className); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + for (Object fm : filterMappings) { |
| 65 | + String name = (String) invokeMethod(fm, "getFilterName"); |
| 66 | + if (name == null) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + if (!aggregatedData.containsKey(name)) { |
| 70 | + Map<String, Object> info = new HashMap<>(); |
| 71 | + info.put("filterName", name); |
| 72 | + info.put("filterClass", filterClassMap.getOrDefault(name, "N/A")); |
| 73 | + info.put("urlPatterns", new LinkedHashSet<String>()); |
| 74 | + aggregatedData.put(name, info); |
| 75 | + } |
| 76 | + Map<String, Object> info = aggregatedData.get(name); |
| 77 | + String urlPattern = (String) invokeMethod(fm, "getUrlPattern"); |
| 78 | + if (urlPattern != null) { |
| 79 | + ((Set<String>) info.get("urlPatterns")).add(urlPattern); |
| 80 | + } |
| 81 | + } |
| 82 | + } catch (Exception ignored) { |
| 83 | + } |
| 84 | + |
| 85 | + List<Map<String, String>> result = new ArrayList<>(); |
| 86 | + for (Map<String, Object> entry : aggregatedData.values()) { |
| 87 | + Map<String, String> finalInfo = new HashMap<>(); |
| 88 | + finalInfo.put("filterName", (String) entry.get("filterName")); |
| 89 | + finalInfo.put("filterClass", (String) entry.get("filterClass")); |
| 90 | + Set<?> urls = (Set<?>) entry.get("urlPatterns"); |
| 91 | + finalInfo.put("urlPatterns", urls.isEmpty() ? "" : urls.toString()); |
| 92 | + result.add(finalInfo); |
| 93 | + } |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + @SuppressWarnings("all") |
| 98 | + private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) { |
| 99 | + StringBuilder output = new StringBuilder(); |
| 100 | + for (Map.Entry<String, List<Map<String, String>>> entry : allFiltersData.entrySet()) { |
| 101 | + String context = entry.getKey(); |
| 102 | + List<Map<String, String>> filters = entry.getValue(); |
| 103 | + output.append("Context: ").append(context).append("\n"); |
| 104 | + if (filters.isEmpty()) { |
| 105 | + output.append("No filters found\n"); |
| 106 | + } else { |
| 107 | + for (Map<String, String> info : filters) { |
| 108 | + appendIfPresent(output, "", info.get("filterName"), ""); |
| 109 | + appendIfPresent(output, " -> ", info.get("filterClass"), ""); |
| 110 | + appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); |
| 111 | + output.append("\n"); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return output.toString(); |
| 116 | + } |
| 117 | + |
| 118 | + private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { |
| 119 | + if (value != null && !value.isEmpty()) { |
| 120 | + sb.append(prefix).append(value).append(suffix); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @SuppressWarnings("all") |
| 125 | + private String getContextRoot(Object context) { |
| 126 | + String r = null; |
| 127 | + try { |
| 128 | + r = (String) invokeMethod(context, "getContextPath"); |
| 129 | + } catch (Exception ignored) { |
| 130 | + } |
| 131 | + String c = context.getClass().getName(); |
| 132 | + if (r == null) { |
| 133 | + return c; |
| 134 | + } |
| 135 | + if (r.isEmpty()) { |
| 136 | + return c + "(/)"; |
| 137 | + } |
| 138 | + return c + "(" + r + ")"; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * context: com.apusic.web.container.WebContainer |
| 143 | + * context - webapp: com.apusic.deploy.runtime.WebModule |
| 144 | + * /usr/local/ass/lib/apusic.jar |
| 145 | + */ |
| 146 | + public Set<Object> getContext() throws Exception { |
| 147 | + Set<Object> contexts = new HashSet<Object>(); |
| 148 | + Set<Thread> threads = Thread.getAllStackTraces().keySet(); |
| 149 | + for (Thread thread : threads) { |
| 150 | + if (thread.getName().contains("HouseKeeper")) { |
| 151 | + // Apusic 9.0 SPX |
| 152 | + Object sessionManager = getFieldValue(thread, "this$0"); |
| 153 | + contexts.add(getFieldValue(sessionManager, "container")); |
| 154 | + } else if (thread.getName().contains("HTTPSession")) { |
| 155 | + // Apusic 9.0.1 |
| 156 | + Object sessionManager = getFieldValue(thread, "this$0"); |
| 157 | + Map<?, ?> contextMap = ((Map<?, ?>) getFieldValue(getFieldValue(sessionManager, "vhost"), "contexts")); |
| 158 | + contexts.addAll(contextMap.values()); |
| 159 | + } |
| 160 | + } |
| 161 | + return contexts; |
| 162 | + } |
| 163 | + |
| 164 | + @SuppressWarnings("all") |
| 165 | + public static Object getFieldValue(Object obj, String fieldName) throws Exception { |
| 166 | + Class<?> clazz = obj.getClass(); |
| 167 | + while (clazz != null) { |
| 168 | + try { |
| 169 | + Field field = clazz.getDeclaredField(fieldName); |
| 170 | + field.setAccessible(true); |
| 171 | + return field.get(obj); |
| 172 | + } catch (NoSuchFieldException e) { |
| 173 | + clazz = clazz.getSuperclass(); |
| 174 | + } |
| 175 | + } |
| 176 | + throw new NoSuchFieldException(fieldName + " for " + obj.getClass().getName()); |
| 177 | + } |
| 178 | + |
| 179 | + @SuppressWarnings("all") |
| 180 | + public static Object invokeMethod(Object obj, String methodName) { |
| 181 | + try { |
| 182 | + Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); |
| 183 | + Method method = null; |
| 184 | + while (clazz != null && method == null) { |
| 185 | + try { |
| 186 | + method = clazz.getDeclaredMethod(methodName); |
| 187 | + } catch (NoSuchMethodException e) { |
| 188 | + clazz = clazz.getSuperclass(); |
| 189 | + } |
| 190 | + } |
| 191 | + if (method == null) { |
| 192 | + throw new NoSuchMethodException("Method not found: " + methodName); |
| 193 | + } |
| 194 | + |
| 195 | + method.setAccessible(true); |
| 196 | + return method.invoke(obj instanceof Class ? null : obj); |
| 197 | + } catch (Exception e) { |
| 198 | + throw new RuntimeException("Error invoking method: " + methodName, e); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + @SuppressWarnings("all") |
| 203 | + private String getErrorMessage(Throwable throwable) { |
| 204 | + PrintStream printStream = null; |
| 205 | + try { |
| 206 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 207 | + printStream = new PrintStream(outputStream); |
| 208 | + throwable.printStackTrace(printStream); |
| 209 | + return outputStream.toString(); |
| 210 | + } finally { |
| 211 | + if (printStream != null) { |
| 212 | + printStream.close(); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments