Resolve classes correctly
jd9999 Jan 9, 2016 8:10 PMam having trouble at the moment with imports in javassist. I had a look at the Javassist Tutorial, where it said I should use the importPackages(String packagename) method, but when I tried that I am still getting the same error. However, it also says it doesn't affect the get(String classname) method, as I have been using.
My error is this:
[source error] no such class: String
javassist.CtNewMethod.make(CtNewMethod.java:79)
javassist.CtNewMethod.make(CtNewMethod.java:45)
RainbowBansTransAgent.Transformationer.transform(Transformationer.java:30)
RainbowBansTransAgent.Transformationer.transform(Transformationer.java:105)
sun.instrument.TransformerManager.transform(Unknown Source)
sun.instrument.InstrumentationImpl.transform(Unknown Source)
sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
sun.instrument.InstrumentationImpl.retransformClasses(Unknown Source)
RainbowBansTransAgent.TransAgent.premain(TransAgent.java:106)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
I heard that javassist automatically imported the java.lang package, but wouldn't that mean that java.lang.String would automatically be imported?
I then tried to insert the code using fully qualified class names such as:
java.io.BufferedReader reader = new java.io.BufferedReader (new java.io.FileReader(new java.io.File("file.txt")); java.lang.String a = reader.readline();
But that just changes from:
[source error] no such class: String
to:
[source error] no such class: java.lang.String
Here is my source code:
package RainbowBansTransAgent;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.NotFoundException;
public class Transformationer implements ClassFileTransformer {
public byte[] transform(String arg1){
try {
ClassLoader.getSystemClassLoader().loadClass("java.net.SocketAddress");
this.getClass().getClassLoader().loadClass("java.net.SocketAddress");
CtClass cc = TransAgent.coldwater.get(arg1);
if(cc.isFrozen()){
BooleanKeys.frozen = true;
return null;
}else{
cc.getClassFile().setMajorVersion(javassist.bytecode.ClassFile.JAVA_4);
CtMethod m = cc.getDeclaredMethod("checkIfShouldDisconnect");
String doublequote = String.valueOf('"');
CtMethod method = CtNewMethod.make("public static java.lang.String getCauseOfBan(java.lang.String playername){"
+ "java.lang.String cause = " + doublequote + "Server" + doublequote + ";"
+ "try {"
+ "java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(new java.io.File(getRainbowPath()" + doublequote + "banned-players.json" + doublequote + ")));"
+ "java.nio.file.Path path = java.nio.file.Paths.get(getRainbowPath(), " + doublequote + "banned-players.json" + doublequote + ");"
+ "byte[] filebytes = java.nio.file.Files.readAllBytes(path);"
+ "java.lang.String ln = new java.lang.String(filebytes);"
+ "java.lang.System.out.println(ln);"
+ "int beginIndex = ln.indexOf(playername);"
+ "if(beginIndex < 0){"
+ "System.out.println(" + doublequote + "could not find" + doublequote + "playername" + ");"
+ "reader.close();"
+ "return " + doublequote + "Unknown" + doublequote + ";"
+ "}"
+ "java.lang.String plrln = ln.substring(beginIndex, ln.length());"
+ "java.lang.String searchline = plrln.substring(0, plrln.indexOf(" + doublequote + "expires" + doublequote + "));"
+ "cause = plrln.substring((searchline.indexOf(" + doublequote + "source" + doublequote + ") + 10), (searchline.length() - 8));"
+ "reader.close();"
+"} catch (java.io.IOException e) {"
+ "e.printStackTrace();"
+ "}"
+ "return cause;"
+ "}", cc);
CtField f = CtField.make("java.io.File banmessage = new java.io.File(" + doublequote + "RainbowBans + File.separatorChar" + doublequote + " banmessage.txt);", cc);
CtField g = CtField.make("java.io.File adminplace = new java.io.File(" + doublequote + "banned-players.json" + doublequote + ");", cc); cc.addField(g); cc.addField(f);
cc.addMethod(method);
int n = m.insertAt(409, true, "{ java.io.BufferedReader reader = null;"
+ " reader = new java.io.BufferedReader(new java.io.FileReader(banmessage));"
+ " java.lang.String message = reader.readLine();"
+ " reader.close();"
+ " java.lang.String admin = getCauseOfBan(var2.getName());"
+ " java.lang.String var4 = message.replaceAll(" + doublequote + "%REASON%" + doublequote + ", var5.d()).replaceAll(" + doublequote + "%CURRENTTIME%" + doublequote + ", new java.util.Date().toString()).replaceAll(" + doublequote + "%ADMIN%" + doublequote + ", admin);}");
if(n == 409){
BooleanKeys.returns_line = true;
NumberKeys.line_Inserted_At = 409;
}else{
BooleanKeys.returns_line = false;
NumberKeys.line_Inserted_At = n;
}
System.out.println("Code inserted at line " + n);
return cc.toBytecode();
}
} catch (IOException e) {
BooleanKeys.error = true;
TransAgent.ln.add(69125L);
} catch (CannotCompileException e) {
BooleanKeys.error = true;
TransAgent.ln.add(31513169125L);
TransAgent.randomexception = e;
} catch (ClassNotFoundException e) {
BooleanKeys.error = true;
TransAgent.ln.add(14152061521144L);
} catch (NotFoundException e) {
BooleanKeys.error = true;
TransAgent.ln.add(14152061521144L);
}
return null;
}
@Override
public byte[] transform(ClassLoader arg0, String className, Class<?> arg2, ProtectionDomain arg3, byte[] arg4) throws IllegalClassFormatException {
BooleanKeys.transformer_loaded = true;
byte[] b = null;
String realName = className.replaceAll("/", ".");
if(realName.equals("joebkt.PlayerList")){
if(BooleanKeys.returned_bytes){
return null;
}else{
BooleanKeys.found_class = true;
b = transform(realName);
if(b !=null){
BooleanKeys.returned_bytes = true;
}
}
} else System.out.println("Class name " + realName + " is not what we're looking for!");
return b;
}
}
EDIT: After some more research, I saw that quotes are usually defined as:
System.out.println(/"hello"/)
Do I need to do that?