|
| 1 | +package com.reajason.javaweb.probe.payload.filter; |
| 2 | + |
| 3 | +import javax.management.MBeanServer; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.PrintStream; |
| 6 | +import java.lang.management.ManagementFactory; |
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author ReaJason |
| 13 | + */ |
| 14 | +public class WebLogicFilterProbe { |
| 15 | + |
| 16 | + @Override |
| 17 | + public String toString() { |
| 18 | + String msg = ""; |
| 19 | + Map<String, List<Map<String, String>>> allFiltersData = new LinkedHashMap<String, List<Map<String, String>>>(); |
| 20 | + Set<Object> contexts = null; |
| 21 | + try { |
| 22 | + contexts = getContext(); |
| 23 | + } catch (Throwable throwable) { |
| 24 | + msg += "context error: " + getErrorMessage(throwable); |
| 25 | + } |
| 26 | + if (contexts == null || contexts.isEmpty()) { |
| 27 | + msg += "context not found\n"; |
| 28 | + } else { |
| 29 | + for (Object context : contexts) { |
| 30 | + String contextRoot = getContextRoot(context); |
| 31 | + List<Map<String, String>> filters = collectFiltersData(context); |
| 32 | + allFiltersData.put(contextRoot, filters); |
| 33 | + } |
| 34 | + msg += formatFiltersData(allFiltersData); |
| 35 | + } |
| 36 | + return msg; |
| 37 | + } |
| 38 | + |
| 39 | + private List<Map<String, String>> collectFiltersData(Object context) { |
| 40 | + List<Map<String, String>> result = new ArrayList<>(); |
| 41 | + try { |
| 42 | + Object filterManager = getFieldValue(context, "filterManager"); |
| 43 | + Map<String, Object> filters = (Map<String, Object>) getFieldValue(filterManager, "filters"); |
| 44 | + List<Object> filterPatternList = (ArrayList<Object>) getFieldValue(filterManager, "filterPatternList"); |
| 45 | + if (filterPatternList == null || filterPatternList.isEmpty()) { |
| 46 | + return Collections.emptyList(); |
| 47 | + } |
| 48 | + for (Object filterInfo : filterPatternList) { |
| 49 | + Map<String, String> info = new HashMap<>(); |
| 50 | + Object urlMap = getFieldValue(filterInfo, "map"); |
| 51 | + String filterName = (String) getFieldValue(filterInfo, "filterName"); |
| 52 | + if (filterName == null) { |
| 53 | + // WebLogic 10.3.6 |
| 54 | + Object[] mapValues = (Object[]) invokeMethod(urlMap, "values", null, null); |
| 55 | + filterName = ((String) mapValues[0]); |
| 56 | + } |
| 57 | + Object filterWrapper = filters.get(filterName); |
| 58 | + String filterClassName = null; |
| 59 | + try { |
| 60 | + filterClassName = (String) getFieldValue(filterWrapper, "filterClassName"); |
| 61 | + } catch (NoSuchFieldException e) { |
| 62 | + // WebLogic 10.3.6 |
| 63 | + filterClassName = (String) getFieldValue(filterWrapper, "filterclass"); |
| 64 | + } |
| 65 | + if (filterClassName == null) { |
| 66 | + Object filter = getFieldValue(filterWrapper, "filter"); |
| 67 | + if (filter != null) { |
| 68 | + filterClassName = filter.getClass().getName(); |
| 69 | + } |
| 70 | + } |
| 71 | + info.put("filterName", filterName); |
| 72 | + info.put("filterClass", filterClassName); |
| 73 | + String[] urlPatterns = (String[]) invokeMethod(urlMap, "keys", null, null); |
| 74 | + info.put("urlPatterns", Arrays.toString(urlPatterns)); |
| 75 | + result.add(info); |
| 76 | + } |
| 77 | + } catch (Exception e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + @SuppressWarnings("all") |
| 84 | + private String formatFiltersData(Map<String, List<Map<String, String>>> allFiltersData) { |
| 85 | + StringBuilder output = new StringBuilder(); |
| 86 | + for (Map.Entry<String, List<Map<String, String>>> entry : allFiltersData.entrySet()) { |
| 87 | + String context = entry.getKey(); |
| 88 | + List<Map<String, String>> filters = entry.getValue(); |
| 89 | + output.append("Context: ").append(context).append("\n"); |
| 90 | + if (filters.isEmpty()) { |
| 91 | + output.append("No filters found\n"); |
| 92 | + } else if (filters.size() == 1 && filters.get(0).containsKey("error")) { |
| 93 | + output.append(filters.get(0).get("error")).append("\n"); |
| 94 | + } else { |
| 95 | + for (Map<String, String> info : filters) { |
| 96 | + appendIfPresent(output, "", info.get("filterName"), ""); |
| 97 | + appendIfPresent(output, " -> ", info.get("filterClass"), ""); |
| 98 | + appendIfPresent(output, " -> URL:", info.get("urlPatterns"), ""); |
| 99 | + output.append("\n"); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + return output.toString(); |
| 104 | + } |
| 105 | + |
| 106 | + private void appendIfPresent(StringBuilder sb, String prefix, String value, String suffix) { |
| 107 | + if (value != null && !value.isEmpty()) { |
| 108 | + sb.append(prefix).append(value).append(suffix); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @SuppressWarnings("all") |
| 113 | + private String getContextRoot(Object context) { |
| 114 | + String r = null; |
| 115 | + try { |
| 116 | + r = (String) invokeMethod(context, "getContextPath", null, null); |
| 117 | + } catch (Exception ignored) { |
| 118 | + } |
| 119 | + String c = context.getClass().getName(); |
| 120 | + if (r == null) { |
| 121 | + return c; |
| 122 | + } |
| 123 | + if (r.isEmpty()) { |
| 124 | + return c + "(/)"; |
| 125 | + } |
| 126 | + return c + "(" + r + ")"; |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + /** |
| 131 | + * weblogic.servlet.internal.WebAppServletContext |
| 132 | + * /opt/oracle/wls1036/server/lib/weblogic.jar |
| 133 | + * /u01/oracle/wlserver/modules/com.oracle.weblogic.servlet.jar |
| 134 | + */ |
| 135 | + public static Set<Object> getContext() throws Exception { |
| 136 | + Set<Object> webappContexts = new HashSet<Object>(); |
| 137 | + MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); |
| 138 | + Map<String, Object> objectsByObjectName = (Map<String, Object>) getFieldValue(platformMBeanServer, "objectsByObjectName"); |
| 139 | + for (Map.Entry<String, Object> entry : objectsByObjectName.entrySet()) { |
| 140 | + String key = entry.getKey(); |
| 141 | + if (key.contains("Type=WebAppComponentRuntime")) { |
| 142 | + Object value = entry.getValue(); |
| 143 | + Object managedResource = getFieldValue(value, "managedResource"); |
| 144 | + if (managedResource != null && managedResource.getClass().getSimpleName().equals("WebAppRuntimeMBeanImpl")) { |
| 145 | + webappContexts.add(getFieldValue(managedResource, "context")); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + try { |
| 150 | + Object workEntry = getFieldValue(Thread.currentThread(), "workEntry"); |
| 151 | + Object request = null; |
| 152 | + try { |
| 153 | + Object connectionHandler = getFieldValue(workEntry, "connectionHandler"); |
| 154 | + request = getFieldValue(connectionHandler, "request"); |
| 155 | + } catch (Exception x) { |
| 156 | + // WebLogic 10.3.6 |
| 157 | + request = workEntry; |
| 158 | + } |
| 159 | + if (request != null) { |
| 160 | + webappContexts.add(getFieldValue(request, "context")); |
| 161 | + } |
| 162 | + } catch (Throwable ignored) { |
| 163 | + } |
| 164 | + return webappContexts; |
| 165 | + } |
| 166 | + |
| 167 | + @SuppressWarnings("all") |
| 168 | + public static Object invokeMethod(Object obj, String methodName, Class<?>[] paramClazz, Object[] param) { |
| 169 | + try { |
| 170 | + Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); |
| 171 | + Method method = null; |
| 172 | + while (clazz != null && method == null) { |
| 173 | + try { |
| 174 | + if (paramClazz == null) { |
| 175 | + method = clazz.getDeclaredMethod(methodName); |
| 176 | + } else { |
| 177 | + method = clazz.getDeclaredMethod(methodName, paramClazz); |
| 178 | + } |
| 179 | + } catch (NoSuchMethodException e) { |
| 180 | + clazz = clazz.getSuperclass(); |
| 181 | + } |
| 182 | + } |
| 183 | + if (method == null) { |
| 184 | + throw new NoSuchMethodException("Method not found: " + methodName); |
| 185 | + } |
| 186 | + |
| 187 | + method.setAccessible(true); |
| 188 | + return method.invoke(obj instanceof Class ? null : obj, param); |
| 189 | + } catch (Exception e) { |
| 190 | + throw new RuntimeException("Error invoking method: " + methodName, e); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + @SuppressWarnings("all") |
| 195 | + public static Object getFieldValue(Object obj, String name) throws Exception { |
| 196 | + Class<?> clazz = obj.getClass(); |
| 197 | + while (clazz != Object.class) { |
| 198 | + try { |
| 199 | + Field field = clazz.getDeclaredField(name); |
| 200 | + field.setAccessible(true); |
| 201 | + return field.get(obj); |
| 202 | + } catch (NoSuchFieldException var5) { |
| 203 | + clazz = clazz.getSuperclass(); |
| 204 | + } |
| 205 | + } |
| 206 | + throw new NoSuchFieldException(obj.getClass().getName() + " Field not found: " + name); |
| 207 | + } |
| 208 | + |
| 209 | + @SuppressWarnings("all") |
| 210 | + private String getErrorMessage(Throwable throwable) { |
| 211 | + PrintStream printStream = null; |
| 212 | + try { |
| 213 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 214 | + printStream = new PrintStream(outputStream); |
| 215 | + throwable.printStackTrace(printStream); |
| 216 | + return outputStream.toString(); |
| 217 | + } finally { |
| 218 | + if (printStream != null) { |
| 219 | + printStream.close(); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments