-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathtomcat-memshell-scanner.jsp
More file actions
499 lines (453 loc) · 26.4 KB
/
tomcat-memshell-scanner.jsp
File metadata and controls
499 lines (453 loc) · 26.4 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<%@ page import="java.net.URL" %>
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="com.sun.org.apache.bcel.internal.Repository" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="org.apache.catalina.core.StandardWrapper" %>
<%@ page import="java.lang.reflect.Method" %>
<%@ page import="java.util.concurrent.CopyOnWriteArrayList" %>
<%@ page import="org.apache.catalina.Valve" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.catalina.core.StandardContext" %>
<%@ page import="org.apache.catalina.connector.Request" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>tomcat-memshell-killer</title>
</head>
<body>
<center>
<div>
<%!
public Object getRequest(HttpServletRequest request) throws NoSuchFieldException, IllegalAccessException {
Field _request = request.getClass().getDeclaredField("request");
_request.setAccessible(true);
Object __request = _request.get(request);
return __request;
}
public Object getStandardContext(HttpServletRequest request) throws NoSuchFieldException, IllegalAccessException {
Object context = request.getSession().getServletContext();
Field _context = context.getClass().getDeclaredField("context");
_context.setAccessible(true);
Object appContext = _context.get(context);
Field __context = appContext.getClass().getDeclaredField("context");
__context.setAccessible(true);
Object standardContext = __context.get(appContext);
return standardContext;
}
public HashMap<String, Object> getFilterConfig(HttpServletRequest request) throws Exception {
Object standardContext = getStandardContext(request);
Field _filterConfigs = standardContext.getClass().getDeclaredField("filterConfigs");
_filterConfigs.setAccessible(true);
HashMap<String, Object> filterConfigs = (HashMap<String, Object>) _filterConfigs.get(standardContext);
return filterConfigs;
}
// FilterMap[]
public Object[] getFilterMaps(HttpServletRequest request) throws Exception {
Object standardContext = getStandardContext(request);
Field _filterMaps = standardContext.getClass().getDeclaredField("filterMaps");
_filterMaps.setAccessible(true);
Object filterMaps = _filterMaps.get(standardContext);
Object[] filterArray = null;
try { // tomcat 789
Field _array = filterMaps.getClass().getDeclaredField("array");
_array.setAccessible(true);
filterArray = (Object[]) _array.get(filterMaps);
} catch (Exception e) { // tomcat 6
filterArray = (Object[]) filterMaps;
}
return filterArray;
}
/**
* 遗留问题,getFilterConfig()依然存在2个
* @param request
* @param filterName
* @throws Exception
*/
public synchronized void deleteFilter(HttpServletRequest request, String filterName) throws Exception {
Object standardContext = getStandardContext(request);
// org.apache.catalina.core.StandardContext#removeFilterDef
HashMap<String, Object> filterConfig = getFilterConfig(request);
Object appFilterConfig = filterConfig.get(filterName);
Field _filterDef = appFilterConfig.getClass().getDeclaredField("filterDef");
_filterDef.setAccessible(true);
Object filterDef = _filterDef.get(appFilterConfig);
Class clsFilterDef = null;
try {
// Tomcat 8
clsFilterDef = Class.forName("org.apache.tomcat.util.descriptor.web.FilterDef");
} catch (Exception e) {
// Tomcat 7
clsFilterDef = Class.forName("org.apache.catalina.deploy.FilterDef");
}
Method removeFilterDef = standardContext.getClass().getDeclaredMethod("removeFilterDef", new Class[]{clsFilterDef});
removeFilterDef.setAccessible(true);
removeFilterDef.invoke(standardContext, filterDef);
// org.apache.catalina.core.StandardContext#removeFilterMap
Class clsFilterMap = null;
try {
// Tomcat 8
clsFilterMap = Class.forName("org.apache.tomcat.util.descriptor.web.FilterMap");
} catch (Exception e) {
// Tomcat 7
clsFilterMap = Class.forName("org.apache.catalina.deploy.FilterMap");
}
Object[] filterMaps = getFilterMaps(request);
for (Object filterMap : filterMaps) {
Field _filterName = filterMap.getClass().getDeclaredField("filterName");
_filterName.setAccessible(true);
String filterName0 = (String) _filterName.get(filterMap);
if (filterName0.equals(filterName)) {
Method removeFilterMap = standardContext.getClass().getDeclaredMethod("removeFilterMap", new Class[]{clsFilterMap});
removeFilterDef.setAccessible(true);
removeFilterMap.invoke(standardContext, filterMap);
}
}
}
public synchronized void deleteServlet(HttpServletRequest request, String servletName) throws Exception {
HashMap<String, Object> childs = getChildren(request);
Object objChild = childs.get(servletName);
String urlPattern = null;
HashMap<String, String> servletMaps = getServletMaps(request);
for (Map.Entry<String, String> servletMap : servletMaps.entrySet()) {
if (servletMap.getValue().equals(servletName)) {
urlPattern = servletMap.getKey();
break;
}
}
if (urlPattern != null) {
// 反射调用 org.apache.catalina.core.StandardContext#removeServletMapping
Object standardContext = getStandardContext(request);
Method removeServletMapping = standardContext.getClass().getDeclaredMethod("removeServletMapping", new Class[]{String.class});
removeServletMapping.setAccessible(true);
removeServletMapping.invoke(standardContext, urlPattern);
// Tomcat 6必须removeChild 789可以不用
// 反射调用 org.apache.catalina.core.StandardContext#removeChild
Method removeChild = standardContext.getClass().getDeclaredMethod("removeChild", new Class[]{org.apache.catalina.Container.class});
removeChild.setAccessible(true);
removeChild.invoke(standardContext, objChild);
}
}
public synchronized void deleteValve(HttpServletRequest request, String containerName, String valveName)
throws Exception {
StandardContext standardContext = (StandardContext) getStandardContext(request);
Request _request = (Request) getRequest(request);
Map<String, List<Valve>> valveMap = getValveMaps(request);
Object pipeline;
// Object container;
// 使用if兼容tomcat7
if (containerName.equals("Engine")) {
pipeline = standardContext.getParent().getParent().getPipeline();
} else if (containerName.equals("Host")) {
pipeline = standardContext.getParent().getPipeline();
} else if (containerName.equals("Context")) {
pipeline = standardContext.getPipeline();
} else if (containerName.equals("Wrapper")) {
// container = (_request.getClass().getDeclaredMethod("getWrapper").invoke(_request));
pipeline = _request.getWrapper().getPipeline();
} else {
return;
}
for (Valve valve : valveMap.get(containerName)) {
if (valve.getClass().getName().equals(valveName)) {
pipeline.getClass().getDeclaredMethod("removeValve", Valve.class).invoke(pipeline, valve);
break;
}
}
}
public synchronized void deleteListener(HttpServletRequest request, String listenerName) throws Exception {
Object standardContext = getStandardContext(request);
List<Object> listeners = getListenerList(request);
List<Object> newListeners = new ArrayList<Object>();
for (Object listener : listeners) {
if (listener.getClass().getName().equals(listenerName)) {
continue;
}
newListeners.add(listener);
}
try {
Method setListenerList = standardContext.getClass().getMethod("setApplicationEventListeners",
Object[].class);
setListenerList.invoke(standardContext, new Object[]{newListeners.toArray()});
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized HashMap<String, Object> getChildren(HttpServletRequest request) throws Exception {
Object standardContext = getStandardContext(request);
Field _children = standardContext.getClass().getSuperclass().getDeclaredField("children");
_children.setAccessible(true);
HashMap<String, Object> children = (HashMap<String, Object>) _children.get(standardContext);
return children;
}
public synchronized HashMap<String, String> getServletMaps(HttpServletRequest request) throws Exception {
Object standardContext = getStandardContext(request);
Field _servletMappings = standardContext.getClass().getDeclaredField("servletMappings");
_servletMappings.setAccessible(true);
HashMap<String, String> servletMappings = (HashMap<String, String>) _servletMappings.get(standardContext);
return servletMappings;
}
public synchronized Map<String, List<Valve>> getValveMaps(HttpServletRequest request) throws Exception {
Map<String, List<Valve>> valveMap = new HashMap<String, List<Valve>>();
StandardContext standardContext = (StandardContext) getStandardContext(request);
Request req = (Request) getRequest(request);
// StandardEngine
valveMap.put("Engine", Arrays.asList(standardContext.getParent().getParent().getPipeline().getValves()));
// StandardHost
valveMap.put("Host", Arrays.asList(standardContext.getParent().getPipeline().getValves()));
// StandardContext
valveMap.put("Context", Arrays.asList(standardContext.getPipeline().getValves()));
// StandardWrapper
valveMap.put("Wrapper", Arrays.asList((req.getWrapper()).getPipeline().getValves()));
return valveMap;
}
public synchronized List<Object> getListenerList(HttpServletRequest request) throws Exception {
List<Object> listenerList = new ArrayList<Object>();
try { // tomcat 8、9
Object standardContext = getStandardContext(request);
Field _listenersList = standardContext.getClass().getDeclaredField("applicationEventListenersList");
_listenersList.setAccessible(true);
listenerList.addAll((CopyOnWriteArrayList) _listenersList.get(standardContext));
} catch (Exception e) { // tomcat 7
Object standardContext = getStandardContext(request);
Field _listenersList = standardContext.getClass().getDeclaredField("applicationEventListenersObjects");
_listenersList.setAccessible(true);
listenerList.addAll(Arrays.asList((Object[]) _listenersList.get(standardContext)));
}
return listenerList;
}
public String getFilterName(Object filterMap) throws Exception {
Method getFilterName = filterMap.getClass().getDeclaredMethod("getFilterName");
getFilterName.setAccessible(true);
return (String) getFilterName.invoke(filterMap, null);
}
public String[] getURLPatterns(Object filterMap) throws Exception {
Method getFilterName = filterMap.getClass().getDeclaredMethod("getURLPatterns");
getFilterName.setAccessible(true);
return (String[]) getFilterName.invoke(filterMap, null);
}
String classFileIsExists(Class clazz) {
if (clazz == null) {
return "class is null";
}
String className = clazz.getName();
String classNamePath = className.replace(".", "/") + ".class";
URL is = clazz.getClassLoader().getResource(classNamePath);
if (is == null) {
return "在磁盘上没有对应class文件,可能是内存马";
} else {
return is.getPath();
}
}
String arrayToString(String[] str) {
String res = "[";
for (String s : str) {
res += String.format("%s,", s);
}
res = res.substring(0, res.length() - 1);
res += "]";
return res;
}
%>
<%
out.write("<h2>Tomcat memshell scanner 0.1.0</h2>");
String action = request.getParameter("action");
String filterName = request.getParameter("filterName");
String servletName = request.getParameter("servletName");
String valveName = request.getParameter("valveName");
String listenerName = request.getParameter("listenerName");
String className = request.getParameter("className");
String containerName = request.getParameter("containerName");
if (action != null && action.equals("kill") && filterName != null) {
deleteFilter(request, filterName);
} else if (action != null && action.equals("kill") && servletName != null) {
deleteServlet(request, servletName);
} else if (action != null && action.equals("kill") && containerName != null && valveName != null) {
deleteValve(request, containerName, valveName);
} else if (action != null && action.equals("kill") && listenerName != null) {
deleteListener(request, listenerName);
} else if (action != null && action.equals("dump") && className != null) {
byte[] classBytes = Repository.lookupClass(Class.forName(className)).getBytes();
response.addHeader("content-Type", "application/octet-stream");
String filename = Class.forName(className).getSimpleName() + ".class";
String agent = request.getHeader("User-Agent");
if (agent.toLowerCase().indexOf("chrome") > 0) {
response.addHeader("content-Disposition", "attachment;filename=" + new String(filename.getBytes("UTF-8"), "ISO8859-1"));
} else {
response.addHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
}
ServletOutputStream outDumper = response.getOutputStream();
outDumper.write(classBytes, 0, classBytes.length);
outDumper.close();
} else {
// Scan filter
out.write("<h4>Filter scan result</h4>");
out.write("<table border=\"1\" cellspacing=\"0\" width=\"95%\" style=\"table-layout:fixed;word-break:break-all;background:#f2f2f2\">\n" +
" <thead>\n" +
" <th width=\"5%\">ID</th>\n" +
" <th width=\"10%\">Filter name</th>\n" +
" <th width=\"10%\">Patern</th>\n" +
" <th width=\"20%\">Filter class</th>\n" +
" <th width=\"20%\">Filter classLoader</th>\n" +
" <th width=\"25%\">Filter class file path</th>\n" +
" <th width=\"5%\">dump class</th>\n" +
" <th width=\"5%\">kill</th>\n" +
" </thead>\n" +
" <tbody>");
HashMap<String, Object> filterConfigs = getFilterConfig(request);
Object[] filterMaps1 = getFilterMaps(request);
for (int i = 0; i < filterMaps1.length; i++) {
out.write("<tr>");
Object fm = filterMaps1[i];
Object appFilterConfig = filterConfigs.get(getFilterName(fm));
if (appFilterConfig == null) {
continue;
}
Field _filter = appFilterConfig.getClass().getDeclaredField("filter");
_filter.setAccessible(true);
Object filter = _filter.get(appFilterConfig);
String filterClassName = filter.getClass().getName();
String filterClassLoaderName = filter.getClass().getClassLoader().getClass().getName();
// ID Filtername 匹配路径 className classLoader 是否存在file dump kill
out.write(String.format("<td style=\"text-align:center\">%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td style=\"text-align:center\"><a href=\"?action=dump&className=%s\">dump</a></td><td style=\"text-align:center\"><a href=\"?action=kill&filterName=%s\">kill</a></td>"
, i + 1
, getFilterName(fm)
, arrayToString(getURLPatterns(fm))
, filterClassName
, filterClassLoaderName
, classFileIsExists(filter.getClass())
, filterClassName
, getFilterName(fm)));
out.write("</tr>");
}
out.write("</tbody></table>");
// Scan servlet
out.write("<h4>Servlet scan result</h4>");
out.write("<table border=\"1\" cellspacing=\"0\" width=\"95%\" style=\"table-layout:fixed;word-break:break-all;background:#f2f2f2\">\n" +
" <thead>\n" +
" <th width=\"5%\">ID</th>\n" +
" <th width=\"10%\">Servlet name</th>\n" +
" <th width=\"10%\">Patern</th>\n" +
" <th width=\"20%\">Servlet class</th>\n" +
" <th width=\"20%\">Servlet classLoader</th>\n" +
" <th width=\"25%\">Servlet class file path</th>\n" +
" <th width=\"5%\">dump class</th>\n" +
" <th width=\"5%\">kill</th>\n" +
" </thead>\n" +
" <tbody>");
HashMap<String, Object> children = getChildren(request);
Map<String, String> servletMappings = getServletMaps(request);
int servletId = 0;
for (Map.Entry<String, String> map : servletMappings.entrySet()) {
String servletMapPath = map.getKey();
String servletName1 = map.getValue();
StandardWrapper wrapper = (StandardWrapper) children.get(servletName1);
Class servletClass = null;
try {
servletClass = Class.forName(wrapper.getServletClass());
} catch (Exception e) {
Object servlet = wrapper.getServlet();
if (servlet != null) {
servletClass = servlet.getClass();
}
}
if (servletClass != null) {
out.write("<tr>");
String servletClassName = servletClass.getName();
String servletClassLoaderName = null;
try {
servletClassLoaderName = servletClass.getClassLoader().getClass().getName();
} catch (Exception e) {
}
out.write(String.format("<td style=\"text-align:center\">%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td style=\"text-align:center\"><a href=\"?action=dump&className=%s\">dump</a></td><td style=\"text-align:center\"><a href=\"?action=kill&servletName=%s\">kill</a></td>"
, servletId + 1
, servletName1
, servletMapPath
, servletClassName
, servletClassLoaderName
, classFileIsExists(servletClass)
, servletClassName
, servletName1));
out.write("</tr>");
}
servletId++;
}
out.write("</tbody></table>");
// Scan Valve
out.write("<h4>Valve scan result</h4>");
out.write("<table border=\"1\" cellspacing=\"0\" width=\"95%\" style=\"table-layout:fixed;word-break:break-all;background:#f2f2f2\">\n" +
" <thead>\n" +
" <th width=\"5%\">ID</th>\n" +
" <th width=\"7%\">Container</th>\n" +
" <th width=\"20%\">Valve class</th>\n" +
" <th width=\"30%\">Valve classLoader</th>\n" +
" <th width=\"35%\">Valve class file path</th>\n" +
" <th width=\"5%\">dump class</th>\n" +
" <th width=\"5%\">kill</th>\n" +
" </thead>\n" +
" <tbody>");
Map<String, List<Valve>> valveMap = getValveMaps(request);
int valveId = 0;
String key = "";
for (Map.Entry<String, List<Valve>> valvemap : valveMap.entrySet()) {
key = valvemap.getKey();
for (Valve valve : valvemap.getValue()) {
out.write("<tr>");
out.write(String.format("<td style=\"text-align:center\">%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td style=\"text-align:center\"><a href=\"?action=dump&className=%s\">dump</a></td><td style=\"text-align:center\"><a href=\"?action=kill&containerName=%s&valveName=%s\">kill</a></td>"
, valveId + 1
, key
, valve.getClass().getName()
, valve.getClass().getClassLoader()
, classFileIsExists(valve.getClass())
, valve.getClass().getName()
, key
, valve.getClass().getName()));
out.write("</tr>");
valveId++;
}
}
out.write("</tbody></table>");
List<Object> listeners = getListenerList(request);
// if (listeners == null || listeners.size() == 0) {
// return;
// }
out.write("<tbody>");
List<ServletRequestListener> newListeners = new ArrayList<ServletRequestListener>();
for (Object o : listeners) {
if (o instanceof ServletRequestListener) {
newListeners.add((ServletRequestListener) o);
}
}
// Scan listener
out.write("<h4>Listener scan result</h4>");
out.write("<table border=\"1\" cellspacing=\"0\" width=\"95%\" style=\"table-layout:fixed;word-break:break-all;background:#f2f2f2\">\n" +
" <thead>\n" +
" <th width=\"5%\">ID</th>\n" +
" <th width=\"20%\">Listener class</th>\n" +
" <th width=\"30%\">Listener classLoader</th>\n" +
" <th width=\"35%\">Listener class file path</th>\n" +
" <th width=\"5%\">dump class</th>\n" +
" <th width=\"5%\">kill</th>\n" +
" </thead>\n" +
" <tbody>");
int index = 0;
for (ServletRequestListener listener : newListeners) {
out.write("<tr>");
out.write(String.format("<td style=\"text-align:center\">%d</td><td>%s</td><td>%s</td><td>%s</td><td style=\"text-align:center\"><a href=\"?action=dump&className=%s\">dump</a></td><td style=\"text-align:center\"><a href=\"?action=kill&listenerName=%s\">kill</a></td>"
, index + 1
, listener.getClass().getName()
, listener.getClass().getClassLoader()
, classFileIsExists(listener.getClass())
, listener.getClass().getName()
, listener.getClass().getName()));
out.write("</tr>");
index++;
}
out.write("</tbody></table>");
}
%>
</div>
<br/>
code by c0ny1
</center>
</body>
</html>