fix: keyword is not allowed for classname

This commit is contained in:
ReaJason
2025-02-19 00:04:29 +08:00
parent 21f9af2f70
commit a8d1b5e29a
7 changed files with 64 additions and 6 deletions
@@ -31,7 +31,7 @@ public class AntSwordManager {
@SneakyThrows
public String getInfo() {
byte[] bytes = new ByteBuddy(ClassFileVersion.JAVA_V6).redefine(Info.class)
.name(Utils.getRandomClassName(Info.class.getName()))
.name(Utils.getRandomClassName())
.visit(new TargetJreVersionVisitorWrapper(Opcodes.V1_6))
.make().getBytes();
String base64String = Base64.getEncoder().encodeToString(bytes);
@@ -20,7 +20,7 @@ public class Utils {
return sb.toString();
}
public static String getRandomClassName(String sourceName) {
public static String getRandomClassName() {
String[] domainAs = new String[]{"com", "net", "org", "sun"};
String domainB = getRandomAlpha((new Random()).nextInt(5) + 3).toLowerCase();
String domainC = getRandomAlpha((new Random()).nextInt(5) + 3).toLowerCase();
@@ -44,7 +44,7 @@ public class BehinderManager {
@SneakyThrows
public boolean test() {
byte[] bytes = new ByteBuddy(ClassFileVersion.JAVA_V6).redefine(Test.class)
.name(Utils.getRandomClassName(Test.class.getName()))
.name(Utils.getRandomClassName())
.visit(new TargetJreVersionVisitorWrapper(Opcodes.V1_6))
.make().getBytes();
String param = "xixi";
@@ -20,7 +20,7 @@ public class Utils {
return sb.toString();
}
public static String getRandomClassName(String sourceName) {
public static String getRandomClassName() {
String[] domainAs = new String[]{"com", "net", "org", "sun"};
String domainB = getRandomAlpha((new Random()).nextInt(5) + 3).toLowerCase();
String domainC = getRandomAlpha((new Random()).nextInt(5) + 3).toLowerCase();
@@ -61,11 +61,10 @@ public class GodzillaManager implements Closeable {
@SneakyThrows
public static byte[] generateGodzilla() {
Random random = new Random();
String className = CLASS_NAMES.get(random.nextInt(CLASS_NAMES.size()));
try (DynamicType.Unloaded<?> make = new ByteBuddy()
.redefine(Payload.class)
.visit(TargetJreVersionVisitorWrapper.DEFAULT)
.name(className)
.name(Utils.getRandomClassName())
.make()) {
return make.getBytes();
}
@@ -0,0 +1,41 @@
package com.reajason.javaweb.godzilla;
import java.util.Random;
/**
* @author ReaJason
* @since 2024/12/21
*/
public class Utils {
public static String getRandomAlpha(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; ++i) {
int number = random.nextInt(52);
sb.append(str.charAt(number));
}
return sb.toString();
}
public static String getRandomClassName() {
String[] domainAs = new String[]{"com", "net", "org", "sun"};
String domainB = getRandomAlpha((new Random()).nextInt(5) + 3).toLowerCase();
String domainC = getRandomAlpha((new Random()).nextInt(5) + 3).toLowerCase();
String domainD = getRandomAlpha((new Random()).nextInt(5) + 3).toLowerCase();
String className = getRandomAlpha((new Random()).nextInt(7) + 4);
className = className.substring(0, 1).toUpperCase() + className.substring(1).toLowerCase();
int domainAIndex = (new Random()).nextInt(4);
String domainA = domainAs[domainAIndex];
int randomSegments = (new Random()).nextInt(3) + 3;
if (randomSegments == 3) {
return domainA + "." + domainB + "." + className;
} else if (randomSegments == 4) {
return domainA + "." + domainB + "." + domainC + "." + className;
} else {
return domainA + "." + domainB + "." + domainC + "." + className;
}
}
}