写一个工具可以在服务端执行临时代码
来源:互联网 发布:电脑录像软件排名 编辑:程序博客网 时间: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
- 写一个工具可以在服务端执行临时代码
- java写的Socket客户端和服务端通信,经验证可以执行的代码,并添加了很多注释
- 在C#中写什么语句可以让它执行一个可执行文件呢
- 实现在执行完服务端代码后弹出提醒对话框
- 谈谈如何自己写一个CAB生成工具 对OEM转CAB感兴趣的可以看看 更新部分代码
- 编写可以在Qt4、Qt5中同时执行的代码
- 写一个程序,既能作为Application执行也可以作为Applet程序执行。
- 在xib上拖进一个scrollView,设置contentsize之后可以滑动,再往其上拖上控件之后就不能滑动,控件用代码写的就可以
- 写一个类读配置文件可以在网上搜一下
- 写一个浮框固定在页面,单击可以消失
- 代码统计工具?不会要自己写一个吧
- [ASP.NET]实现在执行完服务端代码后弹出提醒对话框
- python 写的一个Ice服务端在linux下面的守护进程程序
- dbus参考代码--在一个dbus方法中调用另外一个dbus服务端提供的方法
- ajax接收服务端并执行js代码
- 如何实现在任意一个目录下都可以执行想要执行的文件
- 写代码工具
- 让代码在SharePoint页面执行&&如何在aspx页面中写代码
- Java配置环境变量的原因
- 用PHPExcel导出的个人总结
- 集合框架
- Android后台杀死系列之三:LowMemoryKiller原理(4.3-6.0)
- Linux学习之第五章
- 写一个工具可以在服务端执行临时代码
- 21.线程的实现方式三 ---实现Callable接口
- 【ac梦工厂】拓扑排序 hdu2647
- Pairs Forming LCM LightOJ
- 使用ssm框架实现用户账户邮箱激活功能(二)
- 剑指Offer22:栈的压入、弹出序列
- BIM今年那么火,到底学不学(深度解析)
- springmvc拦截器Interceptor实例
- cuda 学习 | GPU的归约、扫描、直方图算法