1717import javax .crypto .spec .SecretKeySpec ;
1818import java .io .*;
1919import java .net .URLDecoder ;
20+ import java .nio .ByteBuffer ;
2021import java .nio .charset .StandardCharsets ;
21- import java .util .*;
22+ import java .util .HashMap ;
23+ import java .util .Map ;
2224import java .util .zip .GZIPInputStream ;
2325import java .util .zip .GZIPOutputStream ;
2426
2830@ Getter
2931@ Setter
3032public class GodzillaManager implements Closeable {
31- private static final List <String > CLASS_NAMES ;
3233
33- static {
34- InputStream classNamesStream = Objects .requireNonNull (GodzillaManager .class .getResourceAsStream ("/godzillaShellClassNames.txt" ));
35- CLASS_NAMES = IOUtils .readLines (classNamesStream , "UTF-8" );
36- }
37-
38- private final OkHttpClient client ;
34+ private final OkHttpClient client = new OkHttpClient .Builder ().build ();
3935 private String cookie = "" ;
4036 private String entrypoint ;
4137 private String key ;
4238 private String pass ;
4339 private String md5 ;
4440 private Request request ;
41+ private boolean http ;
42+ private boolean ws ;
4543 private Map <String , String > headers = new HashMap <>();
4644
47- public GodzillaManager () {
48- this .client = new OkHttpClient .Builder ().build ();
49- }
50-
5145 public static Pair <String , String > getKeyMd5 (String key , String pass ) {
5246 String md5Key = DigestUtils .md5Hex (key ).substring (0 , 16 );
5347 String md5 = DigestUtils .md5Hex (pass + md5Key ).toUpperCase ();
@@ -60,7 +54,6 @@ public static GodzillaManagerBuilder builder() {
6054
6155 @ SneakyThrows
6256 public static byte [] generateGodzilla () {
63- Random random = new Random ();
6457 try (DynamicType .Unloaded <?> make = new ByteBuddy ()
6558 .redefine (Payload .class )
6659 .visit (TargetJreVersionVisitorWrapper .DEFAULT )
@@ -103,7 +96,7 @@ public static String getResultFromRes(String responseBody, String key, String md
10396 return responseBody ;
10497 }
10598 int i = responseBody .indexOf (md5 .substring (0 , 16 ));
106- String result = responseBody .substring (i + 16 );
99+ String result = responseBody .substring (i + 16 );
107100 int lastIndex = result .indexOf (md5 .substring (16 ));
108101 result = result .substring (0 , lastIndex );
109102 byte [] bytes = Base64 .decodeBase64 (result );
@@ -187,37 +180,59 @@ private Response post(byte[] bytes) throws IOException {
187180
188181 public boolean start () {
189182 byte [] bytes = generateGodzilla ();
190- try (Response response = post (bytes )) {
191- String setCookie = response .header ("Set-Cookie" );
192- if (setCookie != null && setCookie .contains ("JSESSIONID=" )) {
193- cookie = setCookie .substring (setCookie .indexOf ("JSESSIONID=" ), setCookie .indexOf (";" ));
183+ if (isHttp ()) {
184+ try (Response response = post (bytes )) {
185+ String setCookie = response .header ("Set-Cookie" );
186+ if (setCookie != null && setCookie .contains ("JSESSIONID=" )) {
187+ cookie = setCookie .substring (setCookie .indexOf ("JSESSIONID=" ), setCookie .indexOf (";" ));
188+ }
189+ if (response .isSuccessful ()) {
190+ return true ;
191+ }
192+ System .out .println (response .body ().string ().trim ());
193+ } catch (IOException e ) {
194+ e .printStackTrace ();
194195 }
195- if (response .isSuccessful ()) {
196+ }
197+ if (isWs ()) {
198+ try {
199+ BlockingJavaWebSocketClient .sendRequestWaitResponse (this .entrypoint , ByteBuffer .wrap (bytes ));
196200 return true ;
201+ } catch (Exception e ) {
202+ e .printStackTrace ();
197203 }
198- System .out .println (response .body ().string ().trim ());
199- } catch (IOException e ) {
200- e .printStackTrace ();
201204 }
202205 return false ;
203206 }
204207
208+ @ SneakyThrows
205209 public boolean test () {
206210 byte [] bytes = generateMethodCallBytes ("test" );
207- try (Response response = post (bytes )) {
208- if (response .isSuccessful ()) {
209- ResponseBody body = response .body ();
210- if (body != null ) {
211- String resultFromRes = getResultFromRes (body .string (), this .key , this .md5 );
212- System .out .println (resultFromRes );
213- return "ok" .equals (resultFromRes );
211+ if (isHttp ()) {
212+ try (Response response = post (bytes )) {
213+ if (response .isSuccessful ()) {
214+ ResponseBody body = response .body ();
215+ if (body != null ) {
216+ String resultFromRes = getResultFromRes (body .string (), this .key , this .md5 );
217+ System .out .println (resultFromRes );
218+ return "ok" .equals (resultFromRes );
219+ }
214220 }
221+ return false ;
222+ } catch (IOException e ) {
223+ e .printStackTrace ();
224+ return false ;
215225 }
216- return false ;
217- } catch (IOException e ) {
218- e .printStackTrace ();
219- return false ;
220226 }
227+
228+ if (isWs ()) {
229+ byte [] bytes1 = BlockingJavaWebSocketClient .sendRequestWaitResponse (this .entrypoint , ByteBuffer .wrap (bytes ));
230+ byte [] x = aes (key , bytes1 , false );
231+ GZIPInputStream gzipInputStream = new GZIPInputStream (new ByteArrayInputStream (x ));
232+ return "ok" .equals (IOUtils .toString (gzipInputStream , StandardCharsets .UTF_8 ));
233+ }
234+
235+ return false ;
221236 }
222237
223238 @ Override
@@ -284,6 +299,12 @@ public GodzillaManager build() {
284299 headers .put ("Accept-Language" , "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2" );
285300 headers .putAll (this .headers );
286301 manager .setHeaders (headers );
302+ if (entrypoint .startsWith ("http" )) {
303+ manager .setHttp (true );
304+ }
305+ if (entrypoint .startsWith ("ws" )) {
306+ manager .setWs (true );
307+ }
287308 return manager ;
288309 }
289310 }
0 commit comments