forked from c0ny1/java-memshell-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomcat-memshell-scanner.jsp
345 lines (344 loc) · 20 KB
/
tomcat-memshell-scanner.jsp
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
<%@ page import="java.net.URL" %>
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.sun.org.apache.bcel.internal.Repository" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.util.Map" %>
<%@ page import="org.apache.catalina.core.StandardWrapper" %>
<%@ page import="java.lang.reflect.Method" %>
<%@ page import="org.apache.catalina.Container" %>
<%@ page import="org.apache.tomcat.websocket.server.WsServerContainer" %>
<%@ page import="javax.websocket.server.ServerContainer" %>
<%@ page import="javax.websocket.server.ServerEndpointConfig" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>tomcat-memshell-killer</title>
</head>
<body>
<center>
<div>
<%!
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 deleteEndpint(HttpServletRequest request,String servletName) throws Exception{
Map<String,Object> ExactMatch = getExactMatch(request);
ExactMatch.remove(servletName);
}
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 Map<String,Object> getExactMatch(HttpServletRequest request) throws Exception{
WsServerContainer wsServerContainer = (WsServerContainer) request.getSession().getServletContext().getAttribute(ServerContainer.class.getName());
Class<?> obj = Class.forName("org.apache.tomcat.websocket.server.WsServerContainer");
Field field = obj.getDeclaredField("configExactMatchMap");
field.setAccessible(true);
Map<String, Object> configExactMatchMap = (Map<String, Object>) field.get(wsServerContainer);
return configExactMatchMap;
}
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 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 EndpointName1 = request.getParameter("EndpointName");
String className = request.getParameter("className");
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") && EndpointName1 != null){
deleteEndpint(request,EndpointName1);
}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 Endpoint
out.write("<h4>Endpoint 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%\">Patern</th>\n" +
" <th width=\"20%\">Endpoint class</th>\n" +
" <th width=\"20%\">Endpoint classLoader</th>\n" +
" <th width=\"25%\">Endpoint class file path</th>\n" +
" <th width=\"5%\">dump class</th>\n" +
" <th width=\"5%\">kill</th>\n" +
" </thead>\n" +
" <tbody>");
Map<String,Object> ExactMatch = null;
ExactMatch = getExactMatch(request);
// Map<String,String> servletMappings = getServletMaps(request);
int Endpointid = 0;
for(Map.Entry<String,Object> map:ExactMatch.entrySet()){
String EndpointPath = map.getKey();
Class EndpointName = ((ServerEndpointConfig) map.getValue()).getEndpointClass();
ClassLoader EndpointClassLoaderName = ((ServerEndpointConfig) map.getValue()).getEndpointClass().getClassLoader();
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&EndpointName=%s\">kill</a></td>"
, Endpointid + 1
, EndpointPath
, EndpointName.getName()
, EndpointClassLoaderName
, classFileIsExists(EndpointName)
, EndpointName.getName()
, EndpointPath));
out.write("</tr>");
servletId++;
}
out.write("</tbody></table>");
}
%>
</div>
<br/>
code by c0ny1
</center>
</body>
</html>