I. Objectives of the work
Execute temporary code on the server side.
Problems to be solved in the process of implementation:
(1) How do I compile the Java code submitted to the server?
Ans: Commit bytecode, in fact, can also submit. java files.
(2) How do I execute the compiled Java code?
Ans: Let the loader load the class object generated by the classes, and then reflect the method that called the class.
(3) How do I collect Java execution results?
Ans: Here at the time of the output, the symbol reference of the System.out is modified, regardless of the current first.
Second, the main realization of the class
Package com.company.cp9;/** * Created by LSJ on 2015/9/7. * Only externally explicitly calling loadbyte when using DefineClass * virtual machine calls or using LoadClass for class loading */public class Hotswapclassloader extends ClassLoader {public Hotswapclassloader () { super (HotSwapClassLoader.class.getClassLoader ()); } Public Class Loadbyte (Byte [] classbyte) { return defineclass (null, classbyte,0,classbyte.length);} }
- Javaclassexecuter implementation
Package Com.company.cp9;import java.lang.reflect.method;/** * javaclass Execute bytecode Tool */public class Javaclassexecuter {/** * Perform an externally transmitted representation of a byte array of a Java class <br> * Modify the Constant_utf8_info constant in the byte array of the input class to represent java.lang.System to the hijacked Hacksystem class * The execution method is the static main (string[] args) method of the class that outputs the information that the class outputs to the System.out/err * @param classbyte represents a byte array of a Java class * @return execute the knot Fruit */public static String execute (byte[] classbyte) {hacksystem.clearbuffer (); ClassModifier cm = new ClassModifier (classbyte); byte[] Modibytes = cm.modifyutf8constant ("Java/lang/system", "Com/company/cp9/hacksystem"); Hotswapclassloader loader = new Hotswapclassloader (); The above is only modified in the class file in the constant pool section, you can not modify class Clazz = Loader.loadbyte (modibytes); Do not modify constant pool//class Clazz = Loader.loadbyte (classbyte); try {//load directly with LoadClass, load. java file//class clazz = Loader.loadclass ("Com.company.cp9.TestClass"); Object object = Clazz.newinstaNCE (); /** *param 1: Method name param 2: The parameter type of this method */methods = Clazz.getmethod ("main", New Cla Ss[]{string[].class}); /** *param1:the Object Where the method comes from *param2:the args used for the method */Method.invoke (object, new string[] {null}); } catch (Throwable e) {e.printstacktrace (hacksystem.out); } return hacksystem.getbufferstring (); }}
Package Com.company.cp9;import java.io.fileinputstream;import java.io.inputstream;/** * Created by LSJ on 2015/9/9. * Load Testclass.class bytecode into Javaclassexecuter */public class Testmain {public static void Main (String [] args) { SYSTEM.OUT.PRINTLN ("Begin"); try { //Note If the class byte code inputstream is = new FileInputStream ("E:/sourcecode/idea/jvmstudy/testclass.class"); byte [] b = new byte[is.available ()]; Is.read (b); Is.close (); System.out.println (Javaclassexecuter.execute (b)); catch (Exception e) { e.printstacktrace ();}} }
Here we put the Testclass.class file under the corresponding directory.
The corresponding Testclass.java is
Package com.company.cp9;/** * Created by LSJ on 2015/9/7. */public class TestClass {public void main (String [] args) { System.out.println ("Electronics");} }
Third, modify the class file related classes
Package Com.company.cp9;import Java.io.inputstream;import java.io.*/** * provides support for Javaclass hijacking Java.lang.System * In addition to out and err, the rest are forwarded directly to the System for processing * * @author ZZM */public class Hacksystem {public final static inputstream in = System.in ; private static Bytearrayoutputstream buffer = new Bytearrayoutputstream (); Public final static PrintStream out = new PrintStream (buffer); Public final static PrintStream err = out; public static String getbufferstring () {return buffer.tostring (); } public static void Clearbuffer () {buffer.reset (); } public static void Setsecuritymanager (final SecurityManager s) {System.setsecuritymanager (s); } public static SecurityManager getSecurityManager () {return System.getsecuritymanager (); } public static Long Currenttimemillis () {return system.currenttimemillis (); } public static void Arraycopy (object src, int srcpos, object dest, int destpos, int length) {system.arraycopy ( Src, Srcpos, dest, destpos, length); public static int Identityhashcode (Object x) {return system.identityhashcode (x); }//All of the following methods are the same as the name of the Java.lang.System//implementation is the corresponding method of the byte transpose system//The other method is omitted because of the layout reason}
Tool class
Package com.company.cp9;/** * Bytes Array processing tool * @author */public class Byteutils {public static int bytes2int (byte[] B, in t start, int len) {int sum = 0; int end = start + len; for (int i = start; i < end; i++) {int n = ((int) b[i]) & 0xFF; n <<= (--len) * 8; sum = n + sum; } return sum; } public static byte[] Int2bytes (int value, int len) {byte[] b = new Byte[len]; for (int i = 0; i < len; i++) {b[len-i-1] = (byte) ((Value >> 8 * i) & 0xff); } return B; } public static string bytes2string (byte[] B, int start, int len) {return new string (b, start, Len); } public static byte[] String2bytes (String str) {return str.getbytes (); } public static byte[] Bytesreplace (byte[] originalbytes, int offset, int len, byte[] replacebytes) {byte[] New Bytes = new Byte[originalbytes.length + (Replacebytes.length-len)]; System.arraycopy (originalbytes, 0, newbytes, 0, offset); System.arraycopy (replacebytes, 0, newbytes, offset, replacebytes.length); System.arraycopy (originalbytes, offset + len, newbytes, offset + replacebytes.length, Originalbytes.length-offset-len) ; return newbytes; }}
Another
Package com.company.cp9;/** * Modifies class files, temporarily providing only the ability to modify constant pool constants * @author Zzm */public class ClassModifier {/** * class file The starting offset of the constant pool */private static final int constant_pool_count_index = 8; /** * Constant_utf8_info Constant's tag flag */private static final int constant_utf8_info = 1; /** * The length of the 11 constants in the constant pool, except for the Constant_utf8_info type constants, because it is not a fixed length */private static final int[] Constant_item_length = {1 ,-1,-1, 5, 5, 9, 9, 3, 3, 5, 5, 5, 5}; private static final int u1 = 1; private static final int u2 = 2; Private byte[] Classbyte; Public ClassModifier (byte[] classbyte) {this.classbyte = Classbyte; /** * Modify the contents of the Constant_utf8_info constant in the constant pool * @param oldstr the modified string * @param newstr Modified String * @return Modify the result */Public byte[] Modifyutf8constant (String oldstr, String newstr) {int CPC = Getconstantpoolcount (); int offset = Constant_pool_count_index + U2; for (int i = 0; i < CPC; i++) {int tag = BytEutils.bytes2int (Classbyte, offset, U1); if (tag = = constant_utf8_info) {int len = byteutils.bytes2int (classbyte, offset + u1, U2); Offset + = (u1 + U2); String str = byteutils.bytes2string (classbyte, offset, Len); if (Str.equalsignorecase (OLDSTR)) {byte[] strbytes = byteutils.string2bytes (NEWSTR); byte[] StrLen = Byteutils.int2bytes (Newstr.length (), U2); Classbyte = Byteutils.bytesreplace (Classbyte, Offset-u2, U2, StrLen); Classbyte = Byteutils.bytesreplace (classbyte, offset, Len, strbytes); return classbyte; } else {offset + = Len; }} else {offset + = Constant_item_length[tag]; }} return classbyte; }/** * Gets the number of constants in the constant pool * @return Number of constant pools */public int getconstantpoolcount () {return byteutils.bYtes2int (Classbyte, Constant_pool_count_index, U2); }}
Java ClassLoader-(2)