写一个工具可以在服务端执行临时代码

来源:互联网 发布:电脑录像软件排名 编辑:程序博客网 时间:2024/06/02 13:14

字节工具类

作用转化int和字符串相互转换

package com.kailong.classloader;/** * Created by Administrator on 2017/4/4. */public class ByteUtils {    public static int bytes2Int(byte[] classByte, int start, int len) {       int sum=0;        int end=start+len;        for(int i=start;i<end;i++){            int n=((int)classByte[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[] str,int start,int len){        return new String(str,start,len);    }    public static byte[] string2Bytes(String str){        return str.getBytes();    }    public static byte[] bytesReplace(byte[] originalBytes,int offset,int len,byte[] replaceBytes){        byte[] newBytes=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;    }}
package com.kailong.classloader;import javax.print.DocFlavor;/** * Created by Administrator on 2017/4/4. * 修改class文件 修改常量池常量的功能 可以拓展 */public class ClassModifier {    private static final int CONSTANT_POOL_INDEX=8;    private static final int CONSTANT_Utf8_info=1;    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;    }    public byte[] modifyUTF8Constant(String oldStr,String newStr){        int cpc=getConstantPollCount();        int offset=CONSTANT_POOL_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;    }    private int getConstantPollCount() {        return ByteUtils.bytes2Int(classByte,CONSTANT_POOL_INDEX,u2);    }}
package com.kailong.classloader;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.PrintStream;/** * Created by Administrator on 2017/4/4. */public class HackSystem {    public final  static InputStream in=System.in;    private static ByteArrayOutputStream buff=new ByteArrayOutputStream();    public final static PrintStream out=new PrintStream(buff);    public final static PrintStream err=out;    public static String getBufferString(){        return buff.toString();    }    public static void clearBuffer(){        buff.reset();    }    public static void setSecurityManager(final SecurityManager s){        System.setSecurityManager(s);    }    public static SecurityManager getSecurityManager(){        return System.getSecurityManager();    }    public static long currrentTimeMillis(){        return System.currentTimeMillis();    }    public static int identitiHashcode(Object o){        return System.identityHashCode(o);    }    public static void  arraycopy(Object src,int srcPos,Object dest,int destPos,int length){        System.arraycopy(src,srcPos,dest,destPos,length);    }}
package com.kailong.classloader;/** * Created by Administrator on 2017/4/4. * 为了多次执行而加入加载器 */public class HotSwapClassLoader extends ClassLoader{    public HotSwapClassLoader(){        super(HotSwapClassLoader.class.getClassLoader());    }    public Class loadByte(byte[] classByte){        return defineClass(null,classByte,0,classByte.length);    }}

package com.kailong.classloader;import java.lang.reflect.Method;/** * Created by Administrator on 2017/4/4. */public class JavaClassExecuter {    public static String execute(byte[] classByte){        HackSystem.clearBuffer();        ClassModifier classModifier=new ClassModifier(classByte);        byte[] modiBytes= classModifier.modifyUTF8Constant("java/lang/System","com/kailong/classloader/HackSystem");        HotSwapClassLoader loader=new HotSwapClassLoader();        Class clazz = loader.loadByte(modiBytes);        try{         Method method=clazz.getMethod("main",new Class[]{String[].class});         method.invoke(null,new String[]{null});         }catch (Throwable e){            e.printStackTrace();        }        return HackSystem.getBufferString();    }}

效果


测试class 代码

public class Test{public static void main(String [] args){System.out.println("11111111111111111111111111111");for(int i=0;i<=100;i++){System.out.println("likailong"+i);}}}

拓展可以从文本框读取类。远程执行功能。


0 0